@elysiajs/jwt 1.3.1 → 1.3.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.
@@ -1,17 +1,86 @@
1
- import { Elysia } from 'elysia';
2
- import { type JWTPayload, type JWSHeaderParameters, type CryptoKey, type JWK, type KeyObject } from 'jose';
3
- import type { Static, TSchema } from '@sinclair/typebox';
1
+ import { Elysia, type TSchema, type UnwrapSchema as Static } from 'elysia';
2
+ import { type CryptoKey, type JWK, type KeyObject, type JoseHeaderParameters } from 'jose';
4
3
  type UnwrapSchema<Schema extends TSchema | undefined, Fallback = unknown> = Schema extends TSchema ? Static<NonNullable<Schema>> : Fallback;
4
+ /**
5
+ * This interface is a specific, strongly-typed representation of the
6
+ * standard claims found in a JWT payload.
7
+ *
8
+ * It is re-declared here to override potentially generic definitions from
9
+ * third-party libraries, ensuring the compiler knows every expected field.
10
+ *
11
+ * This interface can be modified as needed within the plugin to easily
12
+ * accommodate custom claims for specific use cases.
13
+ */
5
14
  export interface JWTPayloadSpec {
15
+ /**
16
+ * JWT Issuer
17
+ *
18
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.1 RFC7519#section-4.1.1}
19
+ */
6
20
  iss?: string;
21
+ /**
22
+ * JWT Subject
23
+ *
24
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.2 RFC7519#section-4.1.2}
25
+ */
7
26
  sub?: string;
27
+ /**
28
+ * JWT Audience
29
+ *
30
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.3 RFC7519#section-4.1.3}
31
+ */
8
32
  aud?: string | string[];
33
+ /**
34
+ * JWT ID
35
+ *
36
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.7 RFC7519#section-4.1.7}
37
+ */
9
38
  jti?: string;
10
- nbf?: number;
11
- exp?: number;
12
- iat?: number;
39
+ /**
40
+ * JWT Not Before
41
+ *
42
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5 RFC7519#section-4.1.5}
43
+ */
44
+ nbf?: string | number;
45
+ /**
46
+ * JWT Expiration Time
47
+ *
48
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 RFC7519#section-4.1.4}
49
+ */
50
+ exp?: string | number;
51
+ /**
52
+ * JWT Issued At
53
+ *
54
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6 RFC7519#section-4.1.6}
55
+ */
56
+ iat?: boolean;
57
+ }
58
+ /**
59
+ * Defines the types for the header parameters of a JWS.
60
+ *
61
+ * Much like `JWTPayloadSpec`, this interface is declared to provide strong,
62
+ * explicit typing, allowing TypeScript to validate the header's structure
63
+ * and provide accurate autocompletion.
64
+ *
65
+ * It can also be modified within the plugin to handle custom header
66
+ * parameters required for specific development scenarios.
67
+ */
68
+ export interface JWTHeaderParameters extends JoseHeaderParameters {
69
+ /**
70
+ * JWS "alg" (Algorithm) Header Parameter
71
+ *
72
+ * @see {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}
73
+ */
74
+ alg?: string;
75
+ /**
76
+ * This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
77
+ * Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
78
+ */
79
+ b64?: true;
80
+ /** JWS "crit" (Critical) Header Parameter */
81
+ crit?: string[];
13
82
  }
14
- export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWSHeaderParameters, Omit<JWTPayload, 'nbf' | 'exp'> {
83
+ export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWTHeaderParameters, JWTPayloadSpec {
15
84
  /**
16
85
  * Name to decorate method as
17
86
  *
@@ -39,31 +108,13 @@ export interface JWTOption<Name extends string | undefined = 'jwt', Schema exten
39
108
  * Type strict validation for JWT payload
40
109
  */
41
110
  schema?: Schema;
42
- /**
43
- * JWT Not Before
44
- *
45
- * @see [RFC7519#section-4.1.5](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5)
46
- */
47
- nbf?: string | number;
48
- /**
49
- * JWT Expiration Time
50
- *
51
- * @see [RFC7519#section-4.1.4](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4)
52
- */
53
- exp?: string | number;
54
111
  }
55
- export declare const jwt: <const Name extends string = "jwt", const Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => Elysia<"", {
112
+ export declare const jwt: <const Name extends string = "jwt", const Schema extends TSchema | undefined = undefined>({ name, secret, schema, ...defaultValues }: JWTOption<Name, Schema>) => Elysia<"", {
56
113
  decorator: { [name in Name extends string ? Name : "jwt"]: {
57
- sign(morePayload: UnwrapSchema<Schema, Record<string, string | number>> & Omit<JWTPayloadSpec, "exp" | "nbf"> & {
58
- exp?: string | number;
59
- nbf?: string | number;
60
- }): Promise<string>;
114
+ sign(data: UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec): Promise<string>;
61
115
  verify(jwt?: string): Promise<(UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec) | false>;
62
116
  }; } extends infer T ? { [K in keyof T]: { [name in Name extends string ? Name : "jwt"]: {
63
- sign(morePayload: UnwrapSchema<Schema, Record<string, string | number>> & Omit<JWTPayloadSpec, "exp" | "nbf"> & {
64
- exp?: string | number;
65
- nbf?: string | number;
66
- }): Promise<string>;
117
+ sign(data: UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec): Promise<string>;
67
118
  verify(jwt?: string): Promise<(UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec) | false>;
68
119
  }; }[K]; } : never;
69
120
  store: {};
package/dist/cjs/index.js CHANGED
@@ -2642,15 +2642,8 @@ var Type = type_exports2;
2642
2642
  var jwt = ({
2643
2643
  name = "jwt",
2644
2644
  secret,
2645
- // Start JWT Header
2646
- alg = "HS256",
2647
- crit,
2648
2645
  schema,
2649
- // End JWT Header
2650
- // Start JWT Payload
2651
- nbf,
2652
- exp,
2653
- ...payload
2646
+ ...defaultValues
2654
2647
  }) => {
2655
2648
  if (!secret) throw new Error("Secret can't be empty");
2656
2649
  const key = typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
@@ -2666,7 +2659,7 @@ var jwt = ({
2666
2659
  jti: Type.Optional(Type.String()),
2667
2660
  nbf: Type.Optional(Type.Union([Type.String(), Type.Number()])),
2668
2661
  exp: Type.Optional(Type.Union([Type.String(), Type.Number()])),
2669
- iat: Type.Optional(Type.String())
2662
+ iat: Type.Optional(Type.Union([Type.Number(), Type.String()]))
2670
2663
  })
2671
2664
  ]),
2672
2665
  {
@@ -2678,32 +2671,56 @@ var jwt = ({
2678
2671
  seed: {
2679
2672
  name,
2680
2673
  secret,
2681
- alg,
2682
- crit,
2683
2674
  schema,
2684
- nbf,
2685
- exp,
2686
- ...payload
2675
+ ...defaultValues
2687
2676
  }
2688
2677
  }).decorate(name, {
2689
- sign(morePayload) {
2690
- const {
2691
- exp: morePayloadExp,
2692
- nbf: morePayloadNbf,
2693
- ...claimsMorePayload
2694
- } = morePayload;
2695
- let jwt2 = new import_jose.SignJWT({
2696
- ...payload,
2697
- ...claimsMorePayload
2698
- }).setProtectedHeader({
2699
- alg,
2700
- crit
2678
+ sign(data) {
2679
+ const JWTHeader = {
2680
+ alg: defaultValues.alg ?? "HS256",
2681
+ b64: defaultValues.b64,
2682
+ crit: defaultValues.crit,
2683
+ cty: defaultValues.cty,
2684
+ jku: defaultValues.jku,
2685
+ jwk: defaultValues.jwk,
2686
+ kid: defaultValues.kid,
2687
+ typ: defaultValues.typ ?? "JWT",
2688
+ x5c: defaultValues.x5c,
2689
+ x5t: defaultValues.x5t,
2690
+ x5u: defaultValues.x5u
2691
+ };
2692
+ const JWTPayload = {
2693
+ /**
2694
+ * Audience (aud): Identifies the recipients that the JWT is intended for.
2695
+ */
2696
+ aud: data.aud ?? defaultValues.aud,
2697
+ /**
2698
+ * Issuer (iss): Identifies the principal that issued the JWT.
2699
+ */
2700
+ iss: data.iss ?? defaultValues.iss,
2701
+ /**
2702
+ * JWT ID (jti): Provides a unique identifier for the JWT.
2703
+ */
2704
+ jti: data.jti ?? defaultValues.jti,
2705
+ /**
2706
+ * Subject (sub): Identifies the principal that is the subject of the JWT.
2707
+ */
2708
+ sub: data.sub ?? defaultValues.sub,
2709
+ // Includes all other properties from the data source, both standard and custom.
2710
+ ...data
2711
+ };
2712
+ let jwt2 = new import_jose.SignJWT({ ...JWTPayload }).setProtectedHeader({
2713
+ alg: JWTHeader.alg,
2714
+ ...JWTHeader
2701
2715
  });
2702
- if (morePayloadNbf !== void 0) {
2703
- jwt2 = jwt2.setNotBefore(morePayloadNbf);
2716
+ if (data.nbf !== void 0 || defaultValues.nbf !== void 0) {
2717
+ jwt2 = jwt2.setNotBefore(data.nbf ?? defaultValues.nbf);
2704
2718
  }
2705
- if (morePayloadExp !== void 0) {
2706
- jwt2 = jwt2.setExpirationTime(morePayloadExp);
2719
+ if (data.exp !== void 0 || defaultValues.exp !== void 0) {
2720
+ jwt2 = jwt2.setExpirationTime(data.exp ?? defaultValues.exp);
2721
+ }
2722
+ if (defaultValues.iat !== false || data.iat !== false) {
2723
+ jwt2 = jwt2.setIssuedAt(/* @__PURE__ */ new Date());
2707
2724
  }
2708
2725
  return jwt2.sign(key);
2709
2726
  },
package/dist/index.d.ts CHANGED
@@ -1,17 +1,86 @@
1
- import { Elysia } from 'elysia';
2
- import { type JWTPayload, type JWSHeaderParameters, type CryptoKey, type JWK, type KeyObject } from 'jose';
3
- import type { Static, TSchema } from '@sinclair/typebox';
1
+ import { Elysia, type TSchema, type UnwrapSchema as Static } from 'elysia';
2
+ import { type CryptoKey, type JWK, type KeyObject, type JoseHeaderParameters } from 'jose';
4
3
  type UnwrapSchema<Schema extends TSchema | undefined, Fallback = unknown> = Schema extends TSchema ? Static<NonNullable<Schema>> : Fallback;
4
+ /**
5
+ * This interface is a specific, strongly-typed representation of the
6
+ * standard claims found in a JWT payload.
7
+ *
8
+ * It is re-declared here to override potentially generic definitions from
9
+ * third-party libraries, ensuring the compiler knows every expected field.
10
+ *
11
+ * This interface can be modified as needed within the plugin to easily
12
+ * accommodate custom claims for specific use cases.
13
+ */
5
14
  export interface JWTPayloadSpec {
15
+ /**
16
+ * JWT Issuer
17
+ *
18
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.1 RFC7519#section-4.1.1}
19
+ */
6
20
  iss?: string;
21
+ /**
22
+ * JWT Subject
23
+ *
24
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.2 RFC7519#section-4.1.2}
25
+ */
7
26
  sub?: string;
27
+ /**
28
+ * JWT Audience
29
+ *
30
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.3 RFC7519#section-4.1.3}
31
+ */
8
32
  aud?: string | string[];
33
+ /**
34
+ * JWT ID
35
+ *
36
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.7 RFC7519#section-4.1.7}
37
+ */
9
38
  jti?: string;
10
- nbf?: number;
11
- exp?: number;
12
- iat?: number;
39
+ /**
40
+ * JWT Not Before
41
+ *
42
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5 RFC7519#section-4.1.5}
43
+ */
44
+ nbf?: string | number;
45
+ /**
46
+ * JWT Expiration Time
47
+ *
48
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 RFC7519#section-4.1.4}
49
+ */
50
+ exp?: string | number;
51
+ /**
52
+ * JWT Issued At
53
+ *
54
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6 RFC7519#section-4.1.6}
55
+ */
56
+ iat?: boolean;
57
+ }
58
+ /**
59
+ * Defines the types for the header parameters of a JWS.
60
+ *
61
+ * Much like `JWTPayloadSpec`, this interface is declared to provide strong,
62
+ * explicit typing, allowing TypeScript to validate the header's structure
63
+ * and provide accurate autocompletion.
64
+ *
65
+ * It can also be modified within the plugin to handle custom header
66
+ * parameters required for specific development scenarios.
67
+ */
68
+ export interface JWTHeaderParameters extends JoseHeaderParameters {
69
+ /**
70
+ * JWS "alg" (Algorithm) Header Parameter
71
+ *
72
+ * @see {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}
73
+ */
74
+ alg?: string;
75
+ /**
76
+ * This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
77
+ * Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
78
+ */
79
+ b64?: true;
80
+ /** JWS "crit" (Critical) Header Parameter */
81
+ crit?: string[];
13
82
  }
14
- export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWSHeaderParameters, Omit<JWTPayload, 'nbf' | 'exp'> {
83
+ export interface JWTOption<Name extends string | undefined = 'jwt', Schema extends TSchema | undefined = undefined> extends JWTHeaderParameters, JWTPayloadSpec {
15
84
  /**
16
85
  * Name to decorate method as
17
86
  *
@@ -39,31 +108,13 @@ export interface JWTOption<Name extends string | undefined = 'jwt', Schema exten
39
108
  * Type strict validation for JWT payload
40
109
  */
41
110
  schema?: Schema;
42
- /**
43
- * JWT Not Before
44
- *
45
- * @see [RFC7519#section-4.1.5](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5)
46
- */
47
- nbf?: string | number;
48
- /**
49
- * JWT Expiration Time
50
- *
51
- * @see [RFC7519#section-4.1.4](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4)
52
- */
53
- exp?: string | number;
54
111
  }
55
- export declare const jwt: <const Name extends string = "jwt", const Schema extends TSchema | undefined = undefined>({ name, secret, alg, crit, schema, nbf, exp, ...payload }: JWTOption<Name, Schema>) => Elysia<"", {
112
+ export declare const jwt: <const Name extends string = "jwt", const Schema extends TSchema | undefined = undefined>({ name, secret, schema, ...defaultValues }: JWTOption<Name, Schema>) => Elysia<"", {
56
113
  decorator: { [name in Name extends string ? Name : "jwt"]: {
57
- sign(morePayload: UnwrapSchema<Schema, Record<string, string | number>> & Omit<JWTPayloadSpec, "exp" | "nbf"> & {
58
- exp?: string | number;
59
- nbf?: string | number;
60
- }): Promise<string>;
114
+ sign(data: UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec): Promise<string>;
61
115
  verify(jwt?: string): Promise<(UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec) | false>;
62
116
  }; } extends infer T ? { [K in keyof T]: { [name in Name extends string ? Name : "jwt"]: {
63
- sign(morePayload: UnwrapSchema<Schema, Record<string, string | number>> & Omit<JWTPayloadSpec, "exp" | "nbf"> & {
64
- exp?: string | number;
65
- nbf?: string | number;
66
- }): Promise<string>;
117
+ sign(data: UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec): Promise<string>;
67
118
  verify(jwt?: string): Promise<(UnwrapSchema<Schema, Record<string, string | number>> & JWTPayloadSpec) | false>;
68
119
  }; }[K]; } : never;
69
120
  store: {};
package/dist/index.mjs CHANGED
@@ -5,7 +5,11 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/index.ts
8
- import { Elysia, ValidationError, getSchemaValidator } from "elysia";
8
+ import {
9
+ Elysia,
10
+ ValidationError,
11
+ getSchemaValidator
12
+ } from "elysia";
9
13
  import {
10
14
  SignJWT,
11
15
  jwtVerify
@@ -2626,15 +2630,8 @@ var Type = type_exports2;
2626
2630
  var jwt = ({
2627
2631
  name = "jwt",
2628
2632
  secret,
2629
- // Start JWT Header
2630
- alg = "HS256",
2631
- crit,
2632
2633
  schema,
2633
- // End JWT Header
2634
- // Start JWT Payload
2635
- nbf,
2636
- exp,
2637
- ...payload
2634
+ ...defaultValues
2638
2635
  }) => {
2639
2636
  if (!secret) throw new Error("Secret can't be empty");
2640
2637
  const key = typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
@@ -2650,7 +2647,7 @@ var jwt = ({
2650
2647
  jti: Type.Optional(Type.String()),
2651
2648
  nbf: Type.Optional(Type.Union([Type.String(), Type.Number()])),
2652
2649
  exp: Type.Optional(Type.Union([Type.String(), Type.Number()])),
2653
- iat: Type.Optional(Type.String())
2650
+ iat: Type.Optional(Type.Union([Type.Number(), Type.String()]))
2654
2651
  })
2655
2652
  ]),
2656
2653
  {
@@ -2662,32 +2659,56 @@ var jwt = ({
2662
2659
  seed: {
2663
2660
  name,
2664
2661
  secret,
2665
- alg,
2666
- crit,
2667
2662
  schema,
2668
- nbf,
2669
- exp,
2670
- ...payload
2663
+ ...defaultValues
2671
2664
  }
2672
2665
  }).decorate(name, {
2673
- sign(morePayload) {
2674
- const {
2675
- exp: morePayloadExp,
2676
- nbf: morePayloadNbf,
2677
- ...claimsMorePayload
2678
- } = morePayload;
2679
- let jwt2 = new SignJWT({
2680
- ...payload,
2681
- ...claimsMorePayload
2682
- }).setProtectedHeader({
2683
- alg,
2684
- crit
2666
+ sign(data) {
2667
+ const JWTHeader = {
2668
+ alg: defaultValues.alg ?? "HS256",
2669
+ b64: defaultValues.b64,
2670
+ crit: defaultValues.crit,
2671
+ cty: defaultValues.cty,
2672
+ jku: defaultValues.jku,
2673
+ jwk: defaultValues.jwk,
2674
+ kid: defaultValues.kid,
2675
+ typ: defaultValues.typ ?? "JWT",
2676
+ x5c: defaultValues.x5c,
2677
+ x5t: defaultValues.x5t,
2678
+ x5u: defaultValues.x5u
2679
+ };
2680
+ const JWTPayload = {
2681
+ /**
2682
+ * Audience (aud): Identifies the recipients that the JWT is intended for.
2683
+ */
2684
+ aud: data.aud ?? defaultValues.aud,
2685
+ /**
2686
+ * Issuer (iss): Identifies the principal that issued the JWT.
2687
+ */
2688
+ iss: data.iss ?? defaultValues.iss,
2689
+ /**
2690
+ * JWT ID (jti): Provides a unique identifier for the JWT.
2691
+ */
2692
+ jti: data.jti ?? defaultValues.jti,
2693
+ /**
2694
+ * Subject (sub): Identifies the principal that is the subject of the JWT.
2695
+ */
2696
+ sub: data.sub ?? defaultValues.sub,
2697
+ // Includes all other properties from the data source, both standard and custom.
2698
+ ...data
2699
+ };
2700
+ let jwt2 = new SignJWT({ ...JWTPayload }).setProtectedHeader({
2701
+ alg: JWTHeader.alg,
2702
+ ...JWTHeader
2685
2703
  });
2686
- if (morePayloadNbf !== void 0) {
2687
- jwt2 = jwt2.setNotBefore(morePayloadNbf);
2704
+ if (data.nbf !== void 0 || defaultValues.nbf !== void 0) {
2705
+ jwt2 = jwt2.setNotBefore(data.nbf ?? defaultValues.nbf);
2706
+ }
2707
+ if (data.exp !== void 0 || defaultValues.exp !== void 0) {
2708
+ jwt2 = jwt2.setExpirationTime(data.exp ?? defaultValues.exp);
2688
2709
  }
2689
- if (morePayloadExp !== void 0) {
2690
- jwt2 = jwt2.setExpirationTime(morePayloadExp);
2710
+ if (defaultValues.iat !== false || data.iat !== false) {
2711
+ jwt2 = jwt2.setIssuedAt(/* @__PURE__ */ new Date());
2691
2712
  }
2692
2713
  return jwt2.sign(key);
2693
2714
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elysiajs/jwt",
3
3
  "description": "Plugin for Elysia for using JWT Authentication",
4
- "version": "1.3.1",
4
+ "version": "1.3.2",
5
5
  "author": {
6
6
  "name": "saltyAom",
7
7
  "url": "https://github.com/SaltyAom",