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 CHANGED
@@ -27,7 +27,7 @@ yarn add unjwt
27
27
  ## Usage
28
28
 
29
29
  ```ts
30
- import { signJWT, verifyJWT } from 'unjwt'
30
+ import { decodeJWT, signJWT, verifyJWT } from 'unjwt'
31
31
 
32
32
  interface JWTUserClaims {
33
33
  email: string
package/dist/index.d.mts CHANGED
@@ -44,7 +44,10 @@ interface JWTRegisteredClaims {
44
44
  iat?: number;
45
45
  }
46
46
 
47
- declare function signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, header, signatureMethod, hashMethod, }: {
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 signJWT<T extends Record<string, any> = Record<string, any>>({ payload, secret, issuer, audience, expires, header, signatureMethod, hashMethod, }: {
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
- const decodedPayload = JSON.parse(
103
- decodeFromBase64Url(payload)
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unjwt",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "Easy creation and verification of JSON Web Tokens (JWT)",
7
7
  "author": "Johann Schopplich <pkg@johannschopplich.com>",