safetoken
Advanced tools
Comparing version 1.3.3 to 1.3.4
@@ -19,3 +19,3 @@ export declare class SafeToken { | ||
newRefreshToken(data?: string, _r?: true): string; | ||
verifyAccessToken(hashString: string, _r?: true): string | boolean; | ||
verifyAccessToken(hashString: string, _r?: boolean): string | boolean; | ||
verifyRefreshToken(hashString: string): string | boolean; | ||
@@ -22,0 +22,0 @@ resetAccessToken(): void; |
@@ -31,2 +31,8 @@ // src/index.ts | ||
} | ||
if (!_r) { | ||
const diff = SafeToken.timeDiff(this.lastAccessTime); | ||
if (diff.diffSeconds > this.tokenT) { | ||
this.resetAccessToken(); | ||
} | ||
} | ||
let si = Math.floor(Math.random() * ((_r ? this.refreshtoken.length : this.token.length) - 10 + 1)); | ||
@@ -39,8 +45,2 @@ if (String(si).length < 2) { | ||
} | ||
setTimeout(() => { | ||
const diff = SafeToken.timeDiff(this.lastAccessTime); | ||
if (diff.diffSeconds > this.tokenT) { | ||
this.resetAccessToken(); | ||
} | ||
}); | ||
return si + ":" + (data + (_r ? this.refreshtoken : this.token).slice(si - 10, si)); | ||
@@ -55,3 +55,3 @@ } | ||
} | ||
verifyAccessToken(hashString, _r) { | ||
verifyAccessToken(hashString, _r = false) { | ||
let data = true; | ||
@@ -62,4 +62,6 @@ let [si, hash] = (hashString || "").split(":"); | ||
if (hash.length !== 10) { | ||
data = this.dec(hash.slice(0, hash.length - 10)); | ||
hash = hash.slice(hash.length - 10, hash.length); | ||
[hash, data] = [ | ||
hash.slice(hash.length - 10, hash.length), | ||
this.dec(hash.slice(0, hash.length - 10)) | ||
]; | ||
} | ||
@@ -109,7 +111,8 @@ const key = (_r ? this.refreshtoken : this.token).slice(Number(si) - 10, Number(si)); | ||
throw new Error("Encryption key must be 32 charaters"); | ||
text = Buffer.from(text, "hex").toString("binary"); | ||
const decipher = createDecipheriv("aes-256-cbc", this.key, this.iv); | ||
let decoded = decipher.update(text, "binary", "utf8"); | ||
decoded += decipher.final("utf8"); | ||
return decoded; | ||
const decipher = createDecipheriv("aes-256-cbc", Buffer.from(this.key), this.iv); | ||
const decrypted = Buffer.concat([ | ||
decipher.update(Buffer.from(text, "hex")), | ||
decipher.final() | ||
]); | ||
return decrypted.toString(); | ||
} | ||
@@ -119,6 +122,5 @@ enc(text) { | ||
throw new Error("Encryption key must be 32 charaters"); | ||
const encipher = createCipheriv("aes-256-cbc", this.key, this.iv); | ||
let encryptdata = encipher.update(text, "utf8", "binary"); | ||
encryptdata += encipher.final("binary"); | ||
return Buffer.from(encryptdata, "binary").toString("hex"); | ||
const cipher = createCipheriv("aes-256-cbc", Buffer.from(this.key), this.iv); | ||
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]); | ||
return encrypted.toString("hex"); | ||
} | ||
@@ -125,0 +127,0 @@ } |
{ | ||
"name": "safetoken", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "SafeToken - Generate/Validate secured tokens for authentication seamlessly", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
154
10747