Sfoglia il codice sorgente

Création class token

Loquicom 5 anni fa
parent
commit
d767086e3c
1 ha cambiato i file con 46 aggiunte e 0 eliminazioni
  1. 46 0
      node/src/token.js

+ 46 - 0
node/src/token.js

@@ -0,0 +1,46 @@
+const crypto = require('crypto');
+
+class Token {
+
+    constructor (secret, time) {
+        if(typeof secret != 'string' || typeof time != 'number') {
+            throw new Error('Bad type: secret = string and time = number');
+        }
+        this.secret = secret;
+        this.time = time;
+    }
+
+    generate(data) {
+        data = JSON.stringify(data) || '';
+        // Calcul timestamp actuel + timestamp expiration token
+        const timestamp = Math.floor(new Date().getTime() / 1000);
+        const endtime = timestamp + this.time;
+        // Création d'un hash
+        let hash = data + this.secret + endtime;
+        hash = crypto.createHash('sha512').update(hash).digest('base64');
+        hash = hash.replace(/=/g, '');
+        // Le token correspond au timestamp de fin + le hash
+        return endtime + '.' + hash;
+    }
+
+    validate(token, data) {
+        // Verification que le token correspond au format attendu
+        if(typeof token != 'string') {
+            return false;
+        }
+        const split = token.split('.');
+        if(split.length != 2) {
+            return false;
+        }
+        // Recreation du hash supposé du token
+        data = JSON.stringify(data) || '';
+        let hash = data + this.secret + split[0];
+        hash = crypto.createHash('sha512').update(hash).digest('base64');
+        hash = hash.replace(/=/g, '');
+        // Verification que la hash est le même que celui du token passé en parametre
+        return hash == split[1];
+    }
+
+}
+
+module.exports = Token;