unjwt 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.mjs +31 -12
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -44,7 +44,10 @@ interface JWTRegisteredClaims {
|
|
|
44
44
|
iat?: number;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
declare function
|
|
47
|
+
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
|
|
48
|
+
declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
49
|
+
|
|
50
|
+
declare function signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, signatureMethod, hashMethod, }: {
|
|
48
51
|
payload?: T;
|
|
49
52
|
secret: string;
|
|
50
53
|
issuer: string;
|
|
@@ -54,13 +57,12 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
|
|
|
54
57
|
* @default 30
|
|
55
58
|
*/
|
|
56
59
|
expires?: number;
|
|
57
|
-
/** @default { typ: 'JWT', alg: 'HS256' } */
|
|
58
|
-
header?: Record<string, any>;
|
|
59
60
|
/** @default 'HMAC' */
|
|
60
61
|
signatureMethod?: string;
|
|
61
62
|
/** @default 'SHA-256' */
|
|
62
63
|
hashMethod?: string;
|
|
63
64
|
}): Promise<string>;
|
|
65
|
+
|
|
64
66
|
declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureMethod, hashMethod, }: {
|
|
65
67
|
token: string;
|
|
66
68
|
secret: string;
|
|
@@ -73,6 +75,5 @@ declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureM
|
|
|
73
75
|
/** @default 'SHA-256' */
|
|
74
76
|
hashMethod?: string;
|
|
75
77
|
}): Promise<void>;
|
|
76
|
-
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
77
78
|
|
|
78
|
-
export { JWTRegisteredClaims, decodeJWT, signJWT, verifyJWT };
|
|
79
|
+
export { JWTRegisteredClaims, _decodeJWT, decodeJWT, signJWT, verifyJWT };
|
package/dist/index.d.ts
CHANGED
|
@@ -44,7 +44,10 @@ interface JWTRegisteredClaims {
|
|
|
44
44
|
iat?: number;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
declare function
|
|
47
|
+
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
|
|
48
|
+
declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
49
|
+
|
|
50
|
+
declare function signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, signatureMethod, hashMethod, }: {
|
|
48
51
|
payload?: T;
|
|
49
52
|
secret: string;
|
|
50
53
|
issuer: string;
|
|
@@ -54,13 +57,12 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
|
|
|
54
57
|
* @default 30
|
|
55
58
|
*/
|
|
56
59
|
expires?: number;
|
|
57
|
-
/** @default { typ: 'JWT', alg: 'HS256' } */
|
|
58
|
-
header?: Record<string, any>;
|
|
59
60
|
/** @default 'HMAC' */
|
|
60
61
|
signatureMethod?: string;
|
|
61
62
|
/** @default 'SHA-256' */
|
|
62
63
|
hashMethod?: string;
|
|
63
64
|
}): Promise<string>;
|
|
65
|
+
|
|
64
66
|
declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureMethod, hashMethod, }: {
|
|
65
67
|
token: string;
|
|
66
68
|
secret: string;
|
|
@@ -73,6 +75,5 @@ declare function verifyJWT({ token, secret, issuer, audience, leeway, signatureM
|
|
|
73
75
|
/** @default 'SHA-256' */
|
|
74
76
|
hashMethod?: string;
|
|
75
77
|
}): Promise<void>;
|
|
76
|
-
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
77
78
|
|
|
78
|
-
export { JWTRegisteredClaims, decodeJWT, signJWT, verifyJWT };
|
|
79
|
+
export { JWTRegisteredClaims, _decodeJWT, decodeJWT, signJWT, verifyJWT };
|
package/dist/index.mjs
CHANGED
|
@@ -42,13 +42,31 @@ function decodeFromBase64UrlToBuffer(base64Url) {
|
|
|
42
42
|
return buffer.buffer;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
function decodeJWT(token) {
|
|
46
|
+
try {
|
|
47
|
+
return _decodeJWT(token);
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function _decodeJWT(token) {
|
|
52
|
+
const parts = String(token).split(".");
|
|
53
|
+
if (parts.length !== 3)
|
|
54
|
+
throw new Error("Invalid token format");
|
|
55
|
+
const [, payload] = parts;
|
|
56
|
+
return JSON.parse(decodeFromBase64Url(payload));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const headerAlgMap = {
|
|
60
|
+
HMAC: "HS",
|
|
61
|
+
RSA: "RS",
|
|
62
|
+
ECDSA: "ES"
|
|
63
|
+
};
|
|
45
64
|
async function signJWT({
|
|
46
65
|
payload = {},
|
|
47
66
|
secret,
|
|
48
67
|
issuer,
|
|
49
68
|
audience,
|
|
50
69
|
expires = 30,
|
|
51
|
-
header = { typ: "JWT", alg: "HS256" },
|
|
52
70
|
signatureMethod = DEFAULT_SIGNATURE_METHOD,
|
|
53
71
|
hashMethod = DEFAULT_HASH_METHODS
|
|
54
72
|
}) {
|
|
@@ -57,6 +75,10 @@ async function signJWT({
|
|
|
57
75
|
name: signatureMethod,
|
|
58
76
|
hash: hashMethod
|
|
59
77
|
});
|
|
78
|
+
const header = {
|
|
79
|
+
typ: "JWT",
|
|
80
|
+
alg: `${headerAlgMap[signatureMethod]}${hashMethod.split("-")[1]}`
|
|
81
|
+
};
|
|
60
82
|
const iat = Math.floor(Date.now() / 1e3);
|
|
61
83
|
const exp = iat + expires * 24 * 60 * 60;
|
|
62
84
|
const jwtPayload = {
|
|
@@ -75,6 +97,7 @@ async function signJWT({
|
|
|
75
97
|
jwtPayload
|
|
76
98
|
)}.${signature}`;
|
|
77
99
|
}
|
|
100
|
+
|
|
78
101
|
async function verifyJWT({
|
|
79
102
|
token,
|
|
80
103
|
secret,
|
|
@@ -99,19 +122,15 @@ async function verifyJWT({
|
|
|
99
122
|
);
|
|
100
123
|
if (!isValid)
|
|
101
124
|
throw new Error("Invalid JWT signature");
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
125
|
+
let decodedPayload;
|
|
126
|
+
try {
|
|
127
|
+
decodedPayload = JSON.parse(decodeFromBase64Url(payload));
|
|
128
|
+
} catch {
|
|
129
|
+
throw new Error("Invalid JWT payload");
|
|
130
|
+
}
|
|
105
131
|
const now = Math.floor(Date.now() / 1e3);
|
|
106
132
|
if (issuer !== decodedPayload.iss || (Array.isArray(decodedPayload.aud) ? !decodedPayload.aud.includes(audience) : audience !== decodedPayload.aud) || decodedPayload.exp && now > decodedPayload.exp + leeway || decodedPayload.nbf && now < decodedPayload.nbf - leeway)
|
|
107
133
|
throw new Error("Invalid JWT claims");
|
|
108
134
|
}
|
|
109
|
-
function decodeJWT(token) {
|
|
110
|
-
const parts = token.split(".");
|
|
111
|
-
if (parts.length !== 3)
|
|
112
|
-
throw new Error("Invalid token format");
|
|
113
|
-
const [, payload] = parts;
|
|
114
|
-
return JSON.parse(decodeFromBase64Url(payload));
|
|
115
|
-
}
|
|
116
135
|
|
|
117
|
-
export { decodeJWT, signJWT, verifyJWT };
|
|
136
|
+
export { _decodeJWT, decodeJWT, signJWT, verifyJWT };
|
package/package.json
CHANGED