@geekmidas/auth 0.0.5 → 0.2.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/README.md +343 -214
  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 +10 -5
  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/lambda/jwt.ts CHANGED
@@ -1,62 +1,62 @@
1
1
  import type {
2
- APIGatewayAuthorizerResult,
3
- APIGatewayRequestAuthorizerEvent,
4
- APIGatewayTokenAuthorizerEvent,
5
- Context as LambdaContext,
6
- PolicyDocument,
2
+ APIGatewayAuthorizerResult,
3
+ APIGatewayRequestAuthorizerEvent,
4
+ APIGatewayTokenAuthorizerEvent,
5
+ Context as LambdaContext,
6
+ PolicyDocument,
7
7
  } from 'aws-lambda';
8
8
  import {
9
- type JwtClaims,
10
- type JwtConfig,
11
- JwtVerifier,
12
- type TokenExtractionOptions,
9
+ type JwtClaims,
10
+ type JwtConfig,
11
+ JwtVerifier,
12
+ type TokenExtractionOptions,
13
13
  } from '../jwt';
14
14
 
15
15
  export {
16
- JwtVerifier,
17
- type JwtClaims,
18
- type JwtConfig,
19
- type TokenExtractionOptions,
16
+ JwtVerifier,
17
+ type JwtClaims,
18
+ type JwtConfig,
19
+ type TokenExtractionOptions,
20
20
  };
21
21
 
22
22
  function generatePolicy(
23
- principalId: string,
24
- effect: 'Allow' | 'Deny',
25
- resource: string,
26
- context?: Record<string, string | number | boolean>,
23
+ principalId: string,
24
+ effect: 'Allow' | 'Deny',
25
+ resource: string,
26
+ context?: Record<string, string | number | boolean>,
27
27
  ): APIGatewayAuthorizerResult {
28
- const policyDocument: PolicyDocument = {
29
- Version: '2012-10-17',
30
- Statement: [
31
- {
32
- Action: 'execute-api:Invoke',
33
- Effect: effect,
34
- Resource: resource,
35
- },
36
- ],
37
- };
38
-
39
- return {
40
- principalId,
41
- policyDocument,
42
- context,
43
- };
28
+ const policyDocument: PolicyDocument = {
29
+ Version: '2012-10-17',
30
+ Statement: [
31
+ {
32
+ Action: 'execute-api:Invoke',
33
+ Effect: effect,
34
+ Resource: resource,
35
+ },
36
+ ],
37
+ };
38
+
39
+ return {
40
+ principalId,
41
+ policyDocument,
42
+ context,
43
+ };
44
44
  }
45
45
 
46
46
  function getWildcardResource(methodArn: string): string {
47
- const parts = methodArn.split('/');
48
- return `${parts[0]}/${parts[1]}/*`;
47
+ const parts = methodArn.split('/');
48
+ return `${parts[0]}/${parts[1]}/*`;
49
49
  }
50
50
 
51
51
  export interface JwtAuthorizerOptions<TClaims extends JwtClaims = JwtClaims> {
52
- config: JwtConfig;
53
- extraction?: TokenExtractionOptions;
54
- wildcardResource?: boolean;
55
- getPrincipalId?: (claims: TClaims) => string;
56
- getContext?: (
57
- claims: TClaims,
58
- ) => Record<string, string | number | boolean> | undefined;
59
- authorize?: (claims: TClaims) => boolean | Promise<boolean>;
52
+ config: JwtConfig;
53
+ extraction?: TokenExtractionOptions;
54
+ wildcardResource?: boolean;
55
+ getPrincipalId?: (claims: TClaims) => string;
56
+ getContext?: (
57
+ claims: TClaims,
58
+ ) => Record<string, string | number | boolean> | undefined;
59
+ authorize?: (claims: TClaims) => boolean | Promise<boolean>;
60
60
  }
61
61
 
62
62
  /**
@@ -80,131 +80,131 @@ export interface JwtAuthorizerOptions<TClaims extends JwtClaims = JwtClaims> {
80
80
  * ```
81
81
  */
82
82
  export class JwtAuthorizer<TClaims extends JwtClaims = JwtClaims> {
83
- private readonly verifier: JwtVerifier<TClaims>;
84
- private readonly extraction: TokenExtractionOptions;
85
- private readonly wildcardResource: boolean;
86
- private readonly getPrincipalId: (claims: TClaims) => string;
87
- private readonly getContext?: (
88
- claims: TClaims,
89
- ) => Record<string, string | number | boolean> | undefined;
90
- private readonly authorize?: (claims: TClaims) => boolean | Promise<boolean>;
91
-
92
- constructor(options: JwtAuthorizerOptions<TClaims>) {
93
- this.verifier = new JwtVerifier(options.config);
94
- this.extraction = options.extraction ?? {};
95
- this.wildcardResource = options.wildcardResource ?? true;
96
- this.getPrincipalId = options.getPrincipalId ?? ((c) => c.sub ?? 'unknown');
97
- this.getContext = options.getContext;
98
- this.authorize = options.authorize;
99
- }
100
-
101
- /**
102
- * Returns a TOKEN authorizer handler
103
- */
104
- tokenHandler() {
105
- return async (
106
- event: APIGatewayTokenAuthorizerEvent,
107
- _context: LambdaContext,
108
- ): Promise<APIGatewayAuthorizerResult> => {
109
- const { authorizationToken, methodArn } = event;
110
- const resource = this.wildcardResource
111
- ? getWildcardResource(methodArn)
112
- : methodArn;
113
-
114
- const token = authorizationToken.startsWith('Bearer ')
115
- ? authorizationToken.slice(7)
116
- : authorizationToken;
117
-
118
- try {
119
- const claims = await this.verifier.verify(token);
120
-
121
- if (this.authorize) {
122
- const isAuthorized = await this.authorize(claims);
123
- if (!isAuthorized) {
124
- return generatePolicy('unauthorized', 'Deny', resource);
125
- }
126
- }
127
-
128
- const principalId = this.getPrincipalId(claims);
129
- const context = this.getContext?.(claims);
130
-
131
- return generatePolicy(principalId, 'Allow', resource, context);
132
- } catch {
133
- return generatePolicy('unauthorized', 'Deny', resource);
134
- }
135
- };
136
- }
137
-
138
- /**
139
- * Returns a REQUEST authorizer handler
140
- */
141
- requestHandler() {
142
- const {
143
- headerName = 'authorization',
144
- cookieName,
145
- tokenPrefix = 'Bearer ',
146
- } = this.extraction;
147
-
148
- const extractToken = (
149
- event: APIGatewayRequestAuthorizerEvent,
150
- ): string | null => {
151
- const headers = event.headers ?? {};
152
- const headerKey = Object.keys(headers).find(
153
- (k) => k.toLowerCase() === headerName.toLowerCase(),
154
- );
155
- if (headerKey) {
156
- const headerValue = headers[headerKey];
157
- if (headerValue) {
158
- if (tokenPrefix && headerValue.startsWith(tokenPrefix)) {
159
- return headerValue.slice(tokenPrefix.length);
160
- }
161
- return headerValue;
162
- }
163
- }
164
-
165
- if (cookieName) {
166
- const cookieHeader = headers['cookie'] ?? headers['Cookie'] ?? '';
167
- const match = cookieHeader.match(new RegExp(`${cookieName}=([^;]+)`));
168
- if (match) {
169
- return match[1];
170
- }
171
- }
172
-
173
- return null;
174
- };
175
-
176
- return async (
177
- event: APIGatewayRequestAuthorizerEvent,
178
- _context: LambdaContext,
179
- ): Promise<APIGatewayAuthorizerResult> => {
180
- const methodArn = event.methodArn;
181
- const resource = this.wildcardResource
182
- ? getWildcardResource(methodArn)
183
- : methodArn;
184
-
185
- const token = extractToken(event);
186
-
187
- if (!token) {
188
- return generatePolicy('unauthorized', 'Deny', resource);
189
- }
190
-
191
- try {
192
- const claims = await this.verifier.verify(token);
193
-
194
- if (this.authorize) {
195
- const isAuthorized = await this.authorize(claims);
196
- if (!isAuthorized) {
197
- return generatePolicy('unauthorized', 'Deny', resource);
198
- }
199
- }
200
-
201
- const principalId = this.getPrincipalId(claims);
202
- const context = this.getContext?.(claims);
203
-
204
- return generatePolicy(principalId, 'Allow', resource, context);
205
- } catch {
206
- return generatePolicy('unauthorized', 'Deny', resource);
207
- }
208
- };
209
- }
83
+ private readonly verifier: JwtVerifier<TClaims>;
84
+ private readonly extraction: TokenExtractionOptions;
85
+ private readonly wildcardResource: boolean;
86
+ private readonly getPrincipalId: (claims: TClaims) => string;
87
+ private readonly getContext?: (
88
+ claims: TClaims,
89
+ ) => Record<string, string | number | boolean> | undefined;
90
+ private readonly authorize?: (claims: TClaims) => boolean | Promise<boolean>;
91
+
92
+ constructor(options: JwtAuthorizerOptions<TClaims>) {
93
+ this.verifier = new JwtVerifier(options.config);
94
+ this.extraction = options.extraction ?? {};
95
+ this.wildcardResource = options.wildcardResource ?? true;
96
+ this.getPrincipalId = options.getPrincipalId ?? ((c) => c.sub ?? 'unknown');
97
+ this.getContext = options.getContext;
98
+ this.authorize = options.authorize;
99
+ }
100
+
101
+ /**
102
+ * Returns a TOKEN authorizer handler
103
+ */
104
+ tokenHandler() {
105
+ return async (
106
+ event: APIGatewayTokenAuthorizerEvent,
107
+ _context: LambdaContext,
108
+ ): Promise<APIGatewayAuthorizerResult> => {
109
+ const { authorizationToken, methodArn } = event;
110
+ const resource = this.wildcardResource
111
+ ? getWildcardResource(methodArn)
112
+ : methodArn;
113
+
114
+ const token = authorizationToken.startsWith('Bearer ')
115
+ ? authorizationToken.slice(7)
116
+ : authorizationToken;
117
+
118
+ try {
119
+ const claims = await this.verifier.verify(token);
120
+
121
+ if (this.authorize) {
122
+ const isAuthorized = await this.authorize(claims);
123
+ if (!isAuthorized) {
124
+ return generatePolicy('unauthorized', 'Deny', resource);
125
+ }
126
+ }
127
+
128
+ const principalId = this.getPrincipalId(claims);
129
+ const context = this.getContext?.(claims);
130
+
131
+ return generatePolicy(principalId, 'Allow', resource, context);
132
+ } catch {
133
+ return generatePolicy('unauthorized', 'Deny', resource);
134
+ }
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Returns a REQUEST authorizer handler
140
+ */
141
+ requestHandler() {
142
+ const {
143
+ headerName = 'authorization',
144
+ cookieName,
145
+ tokenPrefix = 'Bearer ',
146
+ } = this.extraction;
147
+
148
+ const extractToken = (
149
+ event: APIGatewayRequestAuthorizerEvent,
150
+ ): string | null => {
151
+ const headers = event.headers ?? {};
152
+ const headerKey = Object.keys(headers).find(
153
+ (k) => k.toLowerCase() === headerName.toLowerCase(),
154
+ );
155
+ if (headerKey) {
156
+ const headerValue = headers[headerKey];
157
+ if (headerValue) {
158
+ if (tokenPrefix && headerValue.startsWith(tokenPrefix)) {
159
+ return headerValue.slice(tokenPrefix.length);
160
+ }
161
+ return headerValue;
162
+ }
163
+ }
164
+
165
+ if (cookieName) {
166
+ const cookieHeader = headers.cookie ?? headers.Cookie ?? '';
167
+ const match = cookieHeader.match(new RegExp(`${cookieName}=([^;]+)`));
168
+ if (match?.[1]) {
169
+ return match[1];
170
+ }
171
+ }
172
+
173
+ return null;
174
+ };
175
+
176
+ return async (
177
+ event: APIGatewayRequestAuthorizerEvent,
178
+ _context: LambdaContext,
179
+ ): Promise<APIGatewayAuthorizerResult> => {
180
+ const methodArn = event.methodArn;
181
+ const resource = this.wildcardResource
182
+ ? getWildcardResource(methodArn)
183
+ : methodArn;
184
+
185
+ const token = extractToken(event);
186
+
187
+ if (!token) {
188
+ return generatePolicy('unauthorized', 'Deny', resource);
189
+ }
190
+
191
+ try {
192
+ const claims = await this.verifier.verify(token);
193
+
194
+ if (this.authorize) {
195
+ const isAuthorized = await this.authorize(claims);
196
+ if (!isAuthorized) {
197
+ return generatePolicy('unauthorized', 'Deny', resource);
198
+ }
199
+ }
200
+
201
+ const principalId = this.getPrincipalId(claims);
202
+ const context = this.getContext?.(claims);
203
+
204
+ return generatePolicy(principalId, 'Allow', resource, context);
205
+ } catch {
206
+ return generatePolicy('unauthorized', 'Deny', resource);
207
+ }
208
+ };
209
+ }
210
210
  }