@geekmidas/auth 0.1.0 → 1.0.0

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.
Files changed (117) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{cacheTokenStorage-Ca05vf3T.mjs → cacheTokenStorage-16NE8Cd9.mjs} +3 -3
  3. package/dist/cacheTokenStorage-16NE8Cd9.mjs.map +1 -0
  4. package/dist/{cacheTokenStorage-BzBRJfd-.d.mts → cacheTokenStorage-B7sOoysu.d.cts} +8 -6
  5. package/dist/cacheTokenStorage-B7sOoysu.d.cts.map +1 -0
  6. package/dist/{cacheTokenStorage-CvptztkW.cjs → cacheTokenStorage-CEpMniTi.cjs} +3 -3
  7. package/dist/cacheTokenStorage-CEpMniTi.cjs.map +1 -0
  8. package/dist/{cacheTokenStorage-yNUpDD8W.d.cts → cacheTokenStorage-_tq4Kmhc.d.mts} +8 -6
  9. package/dist/cacheTokenStorage-_tq4Kmhc.d.mts.map +1 -0
  10. package/dist/cacheTokenStorage.cjs +1 -1
  11. package/dist/cacheTokenStorage.d.cts +1 -1
  12. package/dist/cacheTokenStorage.d.mts +1 -1
  13. package/dist/cacheTokenStorage.mjs +1 -1
  14. package/dist/{client-DaiUm0UZ.cjs → client-BPl_mMvb.cjs} +14 -8
  15. package/dist/client-BPl_mMvb.cjs.map +1 -0
  16. package/dist/{client-DjNZS6vZ.mjs → client-eidYNJSH.mjs} +14 -8
  17. package/dist/client-eidYNJSH.mjs.map +1 -0
  18. package/dist/client.cjs +2 -2
  19. package/dist/client.d.cts +1 -1
  20. package/dist/client.d.mts +1 -1
  21. package/dist/client.mjs +2 -2
  22. package/dist/hono/jwt.cjs +1 -1
  23. package/dist/hono/jwt.cjs.map +1 -1
  24. package/dist/hono/jwt.d.cts +2 -1
  25. package/dist/hono/jwt.d.cts.map +1 -0
  26. package/dist/hono/jwt.d.mts +2 -1
  27. package/dist/hono/jwt.d.mts.map +1 -0
  28. package/dist/hono/jwt.mjs +1 -1
  29. package/dist/hono/jwt.mjs.map +1 -1
  30. package/dist/hono/oidc.cjs +1 -1
  31. package/dist/hono/oidc.cjs.map +1 -1
  32. package/dist/hono/oidc.d.cts +3 -2
  33. package/dist/hono/oidc.d.cts.map +1 -0
  34. package/dist/hono/oidc.d.mts +3 -2
  35. package/dist/hono/oidc.d.mts.map +1 -0
  36. package/dist/hono/oidc.mjs +1 -1
  37. package/dist/hono/oidc.mjs.map +1 -1
  38. package/dist/index.cjs +3 -3
  39. package/dist/index.d.cts +2 -2
  40. package/dist/index.d.mts +2 -2
  41. package/dist/index.mjs +3 -3
  42. package/dist/{jwt-DF9kFbmh.d.cts → jwt-3eIuY2dg.d.cts} +2 -1
  43. package/dist/jwt-3eIuY2dg.d.cts.map +1 -0
  44. package/dist/jwt-Bo_eACEi.cjs.map +1 -1
  45. package/dist/{jwt-DH8CJ2xC.d.mts → jwt-BqLlRkw-.d.mts} +2 -1
  46. package/dist/jwt-BqLlRkw-.d.mts.map +1 -0
  47. package/dist/jwt-D88cPJyT.mjs.map +1 -1
  48. package/dist/jwt.d.cts +1 -1
  49. package/dist/jwt.d.mts +1 -1
  50. package/dist/lambda/jwt.cjs +2 -2
  51. package/dist/lambda/jwt.cjs.map +1 -1
  52. package/dist/lambda/jwt.d.cts +2 -1
  53. package/dist/lambda/jwt.d.cts.map +1 -0
  54. package/dist/lambda/jwt.d.mts +2 -1
  55. package/dist/lambda/jwt.d.mts.map +1 -0
  56. package/dist/lambda/jwt.mjs +2 -2
  57. package/dist/lambda/jwt.mjs.map +1 -1
  58. package/dist/lambda/oidc.cjs +2 -2
  59. package/dist/lambda/oidc.cjs.map +1 -1
  60. package/dist/lambda/oidc.d.cts +3 -2
  61. package/dist/lambda/oidc.d.cts.map +1 -0
  62. package/dist/lambda/oidc.d.mts +3 -2
  63. package/dist/lambda/oidc.d.mts.map +1 -0
  64. package/dist/lambda/oidc.mjs +2 -2
  65. package/dist/lambda/oidc.mjs.map +1 -1
  66. package/dist/oidc-C0KyYaAK.cjs.map +1 -1
  67. package/dist/{oidc-CKOGLxjV.d.mts → oidc-ChA17wLB.d.cts} +3 -2
  68. package/dist/oidc-ChA17wLB.d.cts.map +1 -0
  69. package/dist/{oidc-DDLIB5gb.d.cts → oidc-DTRgBBl6.d.mts} +3 -2
  70. package/dist/oidc-DTRgBBl6.d.mts.map +1 -0
  71. package/dist/oidc-KA30gMlx.mjs.map +1 -1
  72. package/dist/oidc.d.cts +2 -2
  73. package/dist/oidc.d.mts +2 -2
  74. package/dist/server.cjs +2 -2
  75. package/dist/server.d.cts +2 -2
  76. package/dist/server.d.mts +2 -2
  77. package/dist/server.mjs +2 -2
  78. package/dist/{tokenManager--Q93eNYr.d.mts → tokenManager-CL3yE06D.d.cts} +2 -1
  79. package/dist/tokenManager-CL3yE06D.d.cts.map +1 -0
  80. package/dist/{tokenManager-BVXxfHRa.mjs → tokenManager-Ci2Tgxp8.mjs} +5 -3
  81. package/dist/tokenManager-Ci2Tgxp8.mjs.map +1 -0
  82. package/dist/{tokenManager-EPmwgai2.cjs → tokenManager-CvEMgxOp.cjs} +5 -3
  83. package/dist/tokenManager-CvEMgxOp.cjs.map +1 -0
  84. package/dist/{tokenManager-H6-lWEah.d.cts → tokenManager-RrwjL0y-.d.mts} +2 -1
  85. package/dist/tokenManager-RrwjL0y-.d.mts.map +1 -0
  86. package/dist/tokenManager.cjs +1 -1
  87. package/dist/tokenManager.d.cts +1 -1
  88. package/dist/tokenManager.d.mts +1 -1
  89. package/dist/tokenManager.mjs +1 -1
  90. package/package.json +3 -3
  91. package/src/__tests__/cacheTokenStorage.spec.ts +148 -148
  92. package/src/__tests__/client.spec.ts +339 -339
  93. package/src/__tests__/hono-jwt.spec.ts +219 -219
  94. package/src/__tests__/hono-oidc.spec.ts +560 -560
  95. package/src/__tests__/jwt.spec.ts +150 -127
  96. package/src/__tests__/lambda-jwt.spec.ts +205 -205
  97. package/src/__tests__/lambda-oidc.spec.ts +580 -579
  98. package/src/__tests__/oidc.spec.ts +365 -365
  99. package/src/__tests__/tokenManager.spec.ts +254 -254
  100. package/src/cacheTokenStorage.ts +32 -32
  101. package/src/client.ts +199 -190
  102. package/src/hono/jwt.ts +111 -111
  103. package/src/hono/oidc.ts +131 -131
  104. package/src/index.ts +2 -2
  105. package/src/jwt.ts +57 -57
  106. package/src/lambda/jwt.ts +170 -170
  107. package/src/lambda/oidc.ts +165 -165
  108. package/src/oidc.ts +121 -121
  109. package/src/server.ts +6 -7
  110. package/src/tokenManager.ts +89 -87
  111. package/tsconfig.json +10 -0
  112. package/dist/cacheTokenStorage-Ca05vf3T.mjs.map +0 -1
  113. package/dist/cacheTokenStorage-CvptztkW.cjs.map +0 -1
  114. package/dist/client-DaiUm0UZ.cjs.map +0 -1
  115. package/dist/client-DjNZS6vZ.mjs.map +0 -1
  116. package/dist/tokenManager-BVXxfHRa.mjs.map +0 -1
  117. package/dist/tokenManager-EPmwgai2.cjs.map +0 -1
package/src/hono/oidc.ts CHANGED
@@ -1,61 +1,61 @@
1
1
  import type { Context, MiddlewareHandler, Next } from 'hono';
2
2
  import {
3
- type OidcClaims,
4
- type OidcConfig,
5
- type OidcUserInfo,
6
- OidcVerifier,
7
- type TokenExtractionOptions,
3
+ type OidcClaims,
4
+ type OidcConfig,
5
+ type OidcUserInfo,
6
+ OidcVerifier,
7
+ type TokenExtractionOptions,
8
8
  } from '../oidc';
9
9
 
10
10
  export {
11
- OidcVerifier,
12
- type OidcClaims,
13
- type OidcConfig,
14
- type OidcUserInfo,
15
- type TokenExtractionOptions,
11
+ OidcVerifier,
12
+ type OidcClaims,
13
+ type OidcConfig,
14
+ type OidcUserInfo,
15
+ type TokenExtractionOptions,
16
16
  };
17
17
 
18
18
  function extractToken(
19
- c: Context,
20
- options: TokenExtractionOptions = {},
19
+ c: Context,
20
+ options: TokenExtractionOptions = {},
21
21
  ): string | null {
22
- const {
23
- headerName = 'authorization',
24
- cookieName,
25
- tokenPrefix = 'Bearer ',
26
- } = options;
27
-
28
- const headerValue = c.req.header(headerName);
29
- if (headerValue) {
30
- if (tokenPrefix && headerValue.startsWith(tokenPrefix)) {
31
- return headerValue.slice(tokenPrefix.length);
32
- }
33
- return headerValue;
34
- }
35
-
36
- if (cookieName) {
37
- const cookieHeader = c.req.header('cookie');
38
- if (cookieHeader) {
39
- const match = cookieHeader.match(new RegExp(`${cookieName}=([^;]+)`));
40
- if (match) {
41
- return match[1];
42
- }
43
- }
44
- }
45
-
46
- return null;
22
+ const {
23
+ headerName = 'authorization',
24
+ cookieName,
25
+ tokenPrefix = 'Bearer ',
26
+ } = options;
27
+
28
+ const headerValue = c.req.header(headerName);
29
+ if (headerValue) {
30
+ if (tokenPrefix && headerValue.startsWith(tokenPrefix)) {
31
+ return headerValue.slice(tokenPrefix.length);
32
+ }
33
+ return headerValue;
34
+ }
35
+
36
+ if (cookieName) {
37
+ const cookieHeader = c.req.header('cookie');
38
+ if (cookieHeader) {
39
+ const match = cookieHeader.match(new RegExp(`${cookieName}=([^;]+)`));
40
+ if (match?.[1]) {
41
+ return match[1];
42
+ }
43
+ }
44
+ }
45
+
46
+ return null;
47
47
  }
48
48
 
49
49
  export interface OidcMiddlewareOptions<
50
- TClaims extends OidcClaims = OidcClaims,
51
- TUserInfo extends OidcUserInfo = OidcUserInfo,
50
+ TClaims extends OidcClaims = OidcClaims,
51
+ TUserInfo extends OidcUserInfo = OidcUserInfo,
52
52
  > {
53
- config: OidcConfig;
54
- extraction?: TokenExtractionOptions;
55
- contextKey?: string;
56
- fetchUserInfo?: boolean;
57
- onError?: (c: Context, error: Error) => Response | Promise<Response>;
58
- transformClaims?: (claims: OidcClaims, userInfo?: TUserInfo) => TClaims;
53
+ config: OidcConfig;
54
+ extraction?: TokenExtractionOptions;
55
+ contextKey?: string;
56
+ fetchUserInfo?: boolean;
57
+ onError?: (c: Context, error: Error) => Response | Promise<Response>;
58
+ transformClaims?: (claims: OidcClaims, userInfo?: TUserInfo) => TClaims;
59
59
  }
60
60
 
61
61
  /**
@@ -80,91 +80,91 @@ export interface OidcMiddlewareOptions<
80
80
  * ```
81
81
  */
82
82
  export class OidcMiddleware<
83
- TClaims extends OidcClaims = OidcClaims,
84
- TUserInfo extends OidcUserInfo = OidcUserInfo,
83
+ TClaims extends OidcClaims = OidcClaims,
84
+ TUserInfo extends OidcUserInfo = OidcUserInfo,
85
85
  > {
86
- private readonly verifier: OidcVerifier<TClaims, TUserInfo>;
87
- private readonly extraction: TokenExtractionOptions;
88
- private readonly contextKey: string;
89
- private readonly fetchUserInfo: boolean;
90
- private readonly onError?: (
91
- c: Context,
92
- error: Error,
93
- ) => Response | Promise<Response>;
94
- private readonly transformClaims?: (
95
- claims: OidcClaims,
96
- userInfo?: TUserInfo,
97
- ) => TClaims;
98
-
99
- constructor(options: OidcMiddlewareOptions<TClaims, TUserInfo>) {
100
- this.verifier = new OidcVerifier(options.config);
101
- this.extraction = options.extraction ?? {};
102
- this.contextKey = options.contextKey ?? 'oidcClaims';
103
- this.fetchUserInfo = options.fetchUserInfo ?? false;
104
- this.onError = options.onError;
105
- this.transformClaims = options.transformClaims;
106
- }
107
-
108
- handler(): MiddlewareHandler {
109
- return async (c: Context, next: Next) => {
110
- const token = extractToken(c, this.extraction);
111
-
112
- if (!token) {
113
- if (this.onError) {
114
- return this.onError(c, new Error('No token provided'));
115
- }
116
- return c.json({ error: 'Unauthorized' }, 401);
117
- }
118
-
119
- try {
120
- const payload = await this.verifier.verify(token);
121
-
122
- let userInfo: TUserInfo | undefined;
123
- if (this.fetchUserInfo) {
124
- userInfo = (await this.verifier.fetchUserInfo(token)) ?? undefined;
125
- c.set('oidcUserInfo', userInfo);
126
- }
127
-
128
- const claims = this.transformClaims
129
- ? this.transformClaims(payload, userInfo)
130
- : payload;
131
-
132
- c.set(this.contextKey, claims);
133
- c.set('oidcToken', token);
134
-
135
- await next();
136
- } catch (error) {
137
- if (this.onError) {
138
- return this.onError(c, error as Error);
139
- }
140
- return c.json({ error: 'Invalid token' }, 401);
141
- }
142
- };
143
- }
144
-
145
- optional(): MiddlewareHandler {
146
- return async (c: Context, next: Next) => {
147
- const token = extractToken(c, this.extraction);
148
-
149
- if (token) {
150
- const payload = await this.verifier.verifyOrNull(token);
151
- if (payload) {
152
- let userInfo: TUserInfo | undefined;
153
- if (this.fetchUserInfo) {
154
- userInfo = (await this.verifier.fetchUserInfo(token)) ?? undefined;
155
- c.set('oidcUserInfo', userInfo);
156
- }
157
-
158
- const claims = this.transformClaims
159
- ? this.transformClaims(payload, userInfo)
160
- : payload;
161
-
162
- c.set(this.contextKey, claims);
163
- c.set('oidcToken', token);
164
- }
165
- }
166
-
167
- await next();
168
- };
169
- }
86
+ private readonly verifier: OidcVerifier<TClaims, TUserInfo>;
87
+ private readonly extraction: TokenExtractionOptions;
88
+ private readonly contextKey: string;
89
+ private readonly fetchUserInfo: boolean;
90
+ private readonly onError?: (
91
+ c: Context,
92
+ error: Error,
93
+ ) => Response | Promise<Response>;
94
+ private readonly transformClaims?: (
95
+ claims: OidcClaims,
96
+ userInfo?: TUserInfo,
97
+ ) => TClaims;
98
+
99
+ constructor(options: OidcMiddlewareOptions<TClaims, TUserInfo>) {
100
+ this.verifier = new OidcVerifier(options.config);
101
+ this.extraction = options.extraction ?? {};
102
+ this.contextKey = options.contextKey ?? 'oidcClaims';
103
+ this.fetchUserInfo = options.fetchUserInfo ?? false;
104
+ this.onError = options.onError;
105
+ this.transformClaims = options.transformClaims;
106
+ }
107
+
108
+ handler(): MiddlewareHandler {
109
+ return async (c: Context, next: Next) => {
110
+ const token = extractToken(c, this.extraction);
111
+
112
+ if (!token) {
113
+ if (this.onError) {
114
+ return this.onError(c, new Error('No token provided'));
115
+ }
116
+ return c.json({ error: 'Unauthorized' }, 401);
117
+ }
118
+
119
+ try {
120
+ const payload = await this.verifier.verify(token);
121
+
122
+ let userInfo: TUserInfo | undefined;
123
+ if (this.fetchUserInfo) {
124
+ userInfo = (await this.verifier.fetchUserInfo(token)) ?? undefined;
125
+ c.set('oidcUserInfo', userInfo);
126
+ }
127
+
128
+ const claims = this.transformClaims
129
+ ? this.transformClaims(payload, userInfo)
130
+ : payload;
131
+
132
+ c.set(this.contextKey, claims);
133
+ c.set('oidcToken', token);
134
+
135
+ await next();
136
+ } catch (error) {
137
+ if (this.onError) {
138
+ return this.onError(c, error as Error);
139
+ }
140
+ return c.json({ error: 'Invalid token' }, 401);
141
+ }
142
+ };
143
+ }
144
+
145
+ optional(): MiddlewareHandler {
146
+ return async (c: Context, next: Next) => {
147
+ const token = extractToken(c, this.extraction);
148
+
149
+ if (token) {
150
+ const payload = await this.verifier.verifyOrNull(token);
151
+ if (payload) {
152
+ let userInfo: TUserInfo | undefined;
153
+ if (this.fetchUserInfo) {
154
+ userInfo = (await this.verifier.fetchUserInfo(token)) ?? undefined;
155
+ c.set('oidcUserInfo', userInfo);
156
+ }
157
+
158
+ const claims = this.transformClaims
159
+ ? this.transformClaims(payload, userInfo)
160
+ : payload;
161
+
162
+ c.set(this.contextKey, claims);
163
+ c.set('oidcToken', token);
164
+ }
165
+ }
166
+
167
+ await next();
168
+ };
169
+ }
170
170
  }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './tokenManager.js';
2
- export * from './client.js';
3
1
  export * from './cacheTokenStorage.js';
2
+ export * from './client.js';
3
+ export * from './tokenManager.js';
package/src/jwt.ts CHANGED
@@ -4,33 +4,33 @@ import * as jose from 'jose';
4
4
  * Standard JWT claims
5
5
  */
6
6
  export interface JwtClaims {
7
- sub?: string;
8
- iss?: string;
9
- aud?: string | string[];
10
- exp?: number;
11
- iat?: number;
12
- nbf?: number;
13
- jti?: string;
7
+ sub?: string;
8
+ iss?: string;
9
+ aud?: string | string[];
10
+ exp?: number;
11
+ iat?: number;
12
+ nbf?: number;
13
+ jti?: string;
14
14
  }
15
15
 
16
16
  /**
17
17
  * Configuration for JWT verification with a symmetric secret
18
18
  */
19
19
  export interface JwtSecretConfig {
20
- secret: string;
21
- issuer?: string;
22
- audience?: string | string[];
23
- algorithms?: string[];
20
+ secret: string;
21
+ issuer?: string;
22
+ audience?: string | string[];
23
+ algorithms?: string[];
24
24
  }
25
25
 
26
26
  /**
27
27
  * Configuration for JWT verification with JWKS
28
28
  */
29
29
  export interface JwtJwksConfig {
30
- jwksUri: string;
31
- issuer?: string;
32
- audience?: string | string[];
33
- algorithms?: string[];
30
+ jwksUri: string;
31
+ issuer?: string;
32
+ audience?: string | string[];
33
+ algorithms?: string[];
34
34
  }
35
35
 
36
36
  export type JwtConfig = JwtSecretConfig | JwtJwksConfig;
@@ -39,60 +39,60 @@ export type JwtConfig = JwtSecretConfig | JwtJwksConfig;
39
39
  * Options for token extraction from requests
40
40
  */
41
41
  export interface TokenExtractionOptions {
42
- headerName?: string;
43
- cookieName?: string;
44
- tokenPrefix?: string;
42
+ headerName?: string;
43
+ cookieName?: string;
44
+ tokenPrefix?: string;
45
45
  }
46
46
 
47
47
  export function isJwksConfig(config: JwtConfig): config is JwtJwksConfig {
48
- return 'jwksUri' in config;
48
+ return 'jwksUri' in config;
49
49
  }
50
50
 
51
51
  /**
52
52
  * JWT Verifier with JWKS caching
53
53
  */
54
54
  export class JwtVerifier<TClaims extends JwtClaims = JwtClaims> {
55
- private jwks: jose.JWTVerifyGetKey | null = null;
56
- private readonly verifyOptions: jose.JWTVerifyOptions;
55
+ private jwks: jose.JWTVerifyGetKey | null = null;
56
+ private readonly verifyOptions: jose.JWTVerifyOptions;
57
57
 
58
- constructor(private readonly config: JwtConfig) {
59
- this.verifyOptions = {
60
- issuer: config.issuer,
61
- audience: config.audience,
62
- algorithms:
63
- config.algorithms ?? (isJwksConfig(config) ? undefined : ['HS256']),
64
- };
65
- }
58
+ constructor(private readonly config: JwtConfig) {
59
+ this.verifyOptions = {
60
+ issuer: config.issuer,
61
+ audience: config.audience,
62
+ algorithms:
63
+ config.algorithms ?? (isJwksConfig(config) ? undefined : ['HS256']),
64
+ };
65
+ }
66
66
 
67
- async verify(token: string): Promise<TClaims> {
68
- if (isJwksConfig(this.config)) {
69
- if (!this.jwks) {
70
- this.jwks = jose.createRemoteJWKSet(new URL(this.config.jwksUri));
71
- }
72
- const { payload } = await jose.jwtVerify(
73
- token,
74
- this.jwks,
75
- this.verifyOptions,
76
- );
77
- return payload as unknown as TClaims;
78
- }
67
+ async verify(token: string): Promise<TClaims> {
68
+ if (isJwksConfig(this.config)) {
69
+ if (!this.jwks) {
70
+ this.jwks = jose.createRemoteJWKSet(new URL(this.config.jwksUri));
71
+ }
72
+ const { payload } = await jose.jwtVerify(
73
+ token,
74
+ this.jwks,
75
+ this.verifyOptions,
76
+ );
77
+ return payload as unknown as TClaims;
78
+ }
79
79
 
80
- const secret = new TextEncoder().encode(this.config.secret);
81
- const { payload } = await jose.jwtVerify(token, secret, this.verifyOptions);
82
- return payload as unknown as TClaims;
83
- }
80
+ const secret = new TextEncoder().encode(this.config.secret);
81
+ const { payload } = await jose.jwtVerify(token, secret, this.verifyOptions);
82
+ return payload as unknown as TClaims;
83
+ }
84
84
 
85
- async verifyOrNull(token: string): Promise<TClaims | null> {
86
- try {
87
- return await this.verify(token);
88
- } catch {
89
- return null;
90
- }
91
- }
85
+ async verifyOrNull(token: string): Promise<TClaims | null> {
86
+ try {
87
+ return await this.verify(token);
88
+ } catch {
89
+ return null;
90
+ }
91
+ }
92
92
 
93
- clearCache(): void {
94
- this.jwks = null;
95
- }
93
+ clearCache(): void {
94
+ this.jwks = null;
95
+ }
96
96
  }
97
97
 
98
98
  /**
@@ -100,7 +100,7 @@ export class JwtVerifier<TClaims extends JwtClaims = JwtClaims> {
100
100
  * WARNING: This does NOT verify the token signature!
101
101
  */
102
102
  export function decodeJwt<TClaims extends JwtClaims = JwtClaims>(
103
- token: string,
103
+ token: string,
104
104
  ): TClaims {
105
- return jose.decodeJwt(token) as unknown as TClaims;
105
+ return jose.decodeJwt(token) as unknown as TClaims;
106
106
  }