Comparing version 1.0.11 to 1.0.12
{ | ||
"name": "kscryp", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Cryptography package with support for: JWT, RSA, MD5, SHA, Base64, HEX, JSON, Basic", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,9 +6,10 @@ const KsDriver = require("../KsDriver"); | ||
encode(data, options) { | ||
try{ | ||
try { | ||
const jwt = require('jsonwebtoken'); | ||
return jwt.sign(data, options?.privateKey || "!ksike!", { | ||
expiresIn: options?.expiresIn || "30m" | ||
expiresIn: options?.expiresIn || 60 * 60 | ||
}); | ||
} | ||
catch(error){ | ||
catch (error) { | ||
this.lib?.log && this.lib.log({ src: "kscryp:JWT:encode", data, error }); | ||
@@ -21,6 +22,10 @@ return null; | ||
try { | ||
if (options?.verify === false) { | ||
let tmp = this.unpack(data); | ||
return options?.full ? tmp : tmp?.data; | ||
} | ||
const jwt = require('jsonwebtoken'); | ||
return jwt.verify(data, options?.privateKey || "!ksike!", options?.callback); | ||
} | ||
catch(error) { | ||
catch (error) { | ||
this.lib?.log && this.lib.log({ src: "kscryp:JWT:encode", data, error }); | ||
@@ -34,3 +39,21 @@ return null; | ||
} | ||
unpack(data) { | ||
try { | ||
if (!data) { | ||
return null; | ||
} | ||
const lst = data.split('.'); | ||
return { | ||
head: this.lib.decode(Buffer.from(lst[0], 'base64').toString(), "json"), | ||
data: this.lib.decode(Buffer.from(lst[1], 'base64').toString(), "json"), | ||
code: lst[2] | ||
}; | ||
} | ||
catch (error) { | ||
this.lib?.log && this.lib.log({ src: "kscryp:JWT:encode", data, error }); | ||
return null; | ||
} | ||
} | ||
} | ||
module.exports = KsJWT; |
@@ -40,2 +40,14 @@ const KsCryp = require('../'); | ||
}); | ||
it("unpack", (done) => { | ||
const jwtStr = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdDEiLCJpYXQiOjE2OTIyOTg2OTYsImV4cCI6MTY5MjMwMDQ5Nn0.XACLhB1ggc1wvEQxt6JQBuCaP9djw7OO8e85A7L9TzM"; | ||
const jwtObj = KsCryp.decode(jwtStr, "jwt", { verify: false }); | ||
expect(jwtObj).toBeInstanceOf(Object); | ||
expect(jwtObj.name).toBe("test1"); | ||
expect(jwtObj.exp).toBe(1692300496); | ||
expect(jwtObj.iat).toBe(1692298696); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54151
1000