@authrim/server 0.1.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 (60) hide show
  1. package/README.md +610 -0
  2. package/dist/adapters/express.cjs +3 -0
  3. package/dist/adapters/express.cjs.map +1 -0
  4. package/dist/adapters/express.d.cts +75 -0
  5. package/dist/adapters/express.d.ts +75 -0
  6. package/dist/adapters/express.js +3 -0
  7. package/dist/adapters/express.js.map +1 -0
  8. package/dist/adapters/fastify.cjs +3 -0
  9. package/dist/adapters/fastify.cjs.map +1 -0
  10. package/dist/adapters/fastify.d.cts +101 -0
  11. package/dist/adapters/fastify.d.ts +101 -0
  12. package/dist/adapters/fastify.js +3 -0
  13. package/dist/adapters/fastify.js.map +1 -0
  14. package/dist/adapters/hono.cjs +2 -0
  15. package/dist/adapters/hono.cjs.map +1 -0
  16. package/dist/adapters/hono.d.cts +85 -0
  17. package/dist/adapters/hono.d.ts +85 -0
  18. package/dist/adapters/hono.js +2 -0
  19. package/dist/adapters/hono.js.map +1 -0
  20. package/dist/adapters/koa.cjs +3 -0
  21. package/dist/adapters/koa.cjs.map +1 -0
  22. package/dist/adapters/koa.d.cts +75 -0
  23. package/dist/adapters/koa.d.ts +75 -0
  24. package/dist/adapters/koa.js +3 -0
  25. package/dist/adapters/koa.js.map +1 -0
  26. package/dist/adapters/nestjs.cjs +3 -0
  27. package/dist/adapters/nestjs.cjs.map +1 -0
  28. package/dist/adapters/nestjs.d.cts +126 -0
  29. package/dist/adapters/nestjs.d.ts +126 -0
  30. package/dist/adapters/nestjs.js +3 -0
  31. package/dist/adapters/nestjs.js.map +1 -0
  32. package/dist/chunk-7POGA5LZ.cjs +3 -0
  33. package/dist/chunk-7POGA5LZ.cjs.map +1 -0
  34. package/dist/chunk-N3ONRO35.js +2 -0
  35. package/dist/chunk-N3ONRO35.js.map +1 -0
  36. package/dist/chunk-O2ALCNXB.cjs +2 -0
  37. package/dist/chunk-O2ALCNXB.cjs.map +1 -0
  38. package/dist/chunk-OS567YCE.js +3 -0
  39. package/dist/chunk-OS567YCE.js.map +1 -0
  40. package/dist/chunk-TPROSFE7.cjs +2 -0
  41. package/dist/chunk-TPROSFE7.cjs.map +1 -0
  42. package/dist/chunk-XOFM2JHF.js +2 -0
  43. package/dist/chunk-XOFM2JHF.js.map +1 -0
  44. package/dist/config-I0GIVJA_.d.cts +364 -0
  45. package/dist/config-I0GIVJA_.d.ts +364 -0
  46. package/dist/index.cjs +3 -0
  47. package/dist/index.cjs.map +1 -0
  48. package/dist/index.d.cts +791 -0
  49. package/dist/index.d.ts +791 -0
  50. package/dist/index.js +3 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/providers/index.cjs +2 -0
  53. package/dist/providers/index.cjs.map +1 -0
  54. package/dist/providers/index.d.cts +79 -0
  55. package/dist/providers/index.d.ts +79 -0
  56. package/dist/providers/index.js +2 -0
  57. package/dist/providers/index.js.map +1 -0
  58. package/dist/types-CzpMdWFR.d.cts +435 -0
  59. package/dist/types-D7gjcvs9.d.ts +435 -0
  60. package/package.json +119 -0
@@ -0,0 +1,435 @@
1
+ import { P as PublicJwk, A as AuthrimServerConfig, R as ResolvedAuthrimServerConfig } from './config-I0GIVJA_.js';
2
+
3
+ /**
4
+ * JWT Claims Type Definitions
5
+ *
6
+ * Based on RFC 7519 (JSON Web Token) and OIDC Core 1.0
7
+ */
8
+ /**
9
+ * Standard JWT claims (RFC 7519 Section 4.1)
10
+ */
11
+ interface StandardClaims {
12
+ /** Issuer */
13
+ iss?: string;
14
+ /** Subject */
15
+ sub?: string;
16
+ /** Audience (string or array) */
17
+ aud?: string | string[];
18
+ /** Expiration Time (Unix timestamp) */
19
+ exp?: number;
20
+ /** Not Before (Unix timestamp) */
21
+ nbf?: number;
22
+ /** Issued At (Unix timestamp) */
23
+ iat?: number;
24
+ /** JWT ID */
25
+ jti?: string;
26
+ }
27
+ /**
28
+ * DPoP confirmation claim (RFC 9449)
29
+ */
30
+ interface ConfirmationClaim {
31
+ /** JWK Thumbprint (RFC 7638) */
32
+ jkt?: string;
33
+ }
34
+ /**
35
+ * Access token claims
36
+ */
37
+ interface AccessTokenClaims extends StandardClaims {
38
+ /** Client ID */
39
+ client_id?: string;
40
+ /** Scope (space-separated string) */
41
+ scope?: string;
42
+ /** Token ID (for introspection reference) */
43
+ token_id?: string;
44
+ /** Confirmation claim (for DPoP-bound tokens) */
45
+ cnf?: ConfirmationClaim;
46
+ /** Allow additional custom claims */
47
+ [key: string]: unknown;
48
+ }
49
+ /**
50
+ * ID token claims (OIDC Core 1.0)
51
+ */
52
+ interface IdTokenClaims extends StandardClaims {
53
+ /** Nonce */
54
+ nonce?: string;
55
+ /** Authentication time */
56
+ auth_time?: number;
57
+ /** Access token hash */
58
+ at_hash?: string;
59
+ /** Code hash */
60
+ c_hash?: string;
61
+ /** ACR (Authentication Context Class Reference) */
62
+ acr?: string;
63
+ /** AMR (Authentication Methods References) */
64
+ amr?: string[];
65
+ /** Authorized party */
66
+ azp?: string;
67
+ /** Allow additional custom claims */
68
+ [key: string]: unknown;
69
+ }
70
+ /**
71
+ * Validated token result
72
+ */
73
+ interface ValidatedToken {
74
+ /** Parsed and validated claims */
75
+ claims: AccessTokenClaims;
76
+ /** Raw token string */
77
+ token: string;
78
+ /** Token type */
79
+ tokenType: 'Bearer' | 'DPoP';
80
+ /** Time remaining until expiration (seconds) */
81
+ expiresIn?: number;
82
+ }
83
+ /**
84
+ * JWT header (RFC 7519 Section 5)
85
+ */
86
+ interface JwtHeader {
87
+ /** Algorithm */
88
+ alg: string;
89
+ /** Type (should be 'JWT') */
90
+ typ?: string;
91
+ /** Key ID */
92
+ kid?: string;
93
+ /** JWK (for DPoP proofs) */
94
+ jwk?: Record<string, unknown>;
95
+ }
96
+ /**
97
+ * Parsed JWT structure
98
+ */
99
+ interface ParsedJwt<T = Record<string, unknown>> {
100
+ header: JwtHeader;
101
+ payload: T;
102
+ signature: string;
103
+ }
104
+
105
+ /**
106
+ * Token-related Type Definitions
107
+ */
108
+
109
+ /**
110
+ * Token validation options
111
+ */
112
+ interface TokenValidationOptions {
113
+ /** Expected issuer(s) */
114
+ issuer: string | string[];
115
+ /** Expected audience(s) */
116
+ audience: string | string[];
117
+ /** Clock tolerance in seconds (default: 60) */
118
+ clockToleranceSeconds?: number;
119
+ /** Required scopes (if any) */
120
+ requiredScopes?: string[];
121
+ /** Whether to validate DPoP binding if cnf claim is present */
122
+ validateDPoP?: boolean;
123
+ }
124
+ /**
125
+ * Claims validation options
126
+ */
127
+ interface ClaimsValidationOptions {
128
+ /** Expected issuer(s) */
129
+ issuer: string | string[];
130
+ /** Expected audience(s) */
131
+ audience: string | string[];
132
+ /** Clock tolerance in seconds */
133
+ clockToleranceSeconds: number;
134
+ /** Current timestamp (Unix seconds) */
135
+ now: number;
136
+ /**
137
+ * Require exp claim to be present
138
+ * Per OIDC Core 1.0 Section 3.1.3.7, ID Tokens MUST have exp claim
139
+ * Default: false (for generic JWT validation)
140
+ */
141
+ requireExp?: boolean;
142
+ /**
143
+ * Require iat claim to be present
144
+ * Per OIDC Core 1.0 Section 3.1.3.7, ID Tokens MUST have iat claim
145
+ * Default: false (for generic JWT validation)
146
+ */
147
+ requireIat?: boolean;
148
+ }
149
+ /**
150
+ * Claims validation result
151
+ */
152
+ interface ClaimsValidationResult {
153
+ valid: boolean;
154
+ error?: {
155
+ code: string;
156
+ message: string;
157
+ };
158
+ }
159
+ /**
160
+ * Token validation result (success case)
161
+ */
162
+ interface TokenValidationSuccess {
163
+ data: ValidatedToken;
164
+ error: null;
165
+ }
166
+ /**
167
+ * Token validation result (error case)
168
+ */
169
+ interface TokenValidationError {
170
+ data: null;
171
+ error: {
172
+ code: string;
173
+ message: string;
174
+ };
175
+ }
176
+ /**
177
+ * Token validation result (discriminated union)
178
+ */
179
+ type TokenValidationResult = TokenValidationSuccess | TokenValidationError;
180
+ /**
181
+ * Token introspection request (RFC 7662)
182
+ */
183
+ interface IntrospectionRequest {
184
+ /** Token to introspect */
185
+ token: string;
186
+ /** Token type hint */
187
+ token_type_hint?: 'access_token' | 'refresh_token';
188
+ }
189
+ /**
190
+ * Token introspection response (RFC 7662)
191
+ */
192
+ interface IntrospectionResponse {
193
+ /** Whether the token is active */
194
+ active: boolean;
195
+ /** Scope */
196
+ scope?: string;
197
+ /** Client ID */
198
+ client_id?: string;
199
+ /** Username */
200
+ username?: string;
201
+ /** Token type */
202
+ token_type?: string;
203
+ /** Expiration time */
204
+ exp?: number;
205
+ /** Issued at */
206
+ iat?: number;
207
+ /** Not before */
208
+ nbf?: number;
209
+ /** Subject */
210
+ sub?: string;
211
+ /** Audience */
212
+ aud?: string | string[];
213
+ /** Issuer */
214
+ iss?: string;
215
+ /** JWT ID */
216
+ jti?: string;
217
+ /** Confirmation (DPoP binding) */
218
+ cnf?: {
219
+ jkt?: string;
220
+ };
221
+ /** Additional claims */
222
+ [key: string]: unknown;
223
+ }
224
+ /**
225
+ * Token revocation request (RFC 7009)
226
+ */
227
+ interface RevocationRequest {
228
+ /** Token to revoke */
229
+ token: string;
230
+ /** Token type hint */
231
+ token_type_hint?: 'access_token' | 'refresh_token';
232
+ }
233
+
234
+ /**
235
+ * DPoP Type Definitions (RFC 9449)
236
+ *
237
+ * Demonstrating Proof of Possession at the Application Layer
238
+ */
239
+
240
+ /**
241
+ * DPoP proof header (RFC 9449 Section 4.2)
242
+ */
243
+ interface DPoPProofHeader {
244
+ /** Type (must be 'dpop+jwt') */
245
+ typ: 'dpop+jwt';
246
+ /** Algorithm */
247
+ alg: string;
248
+ /** Public key (required in header) */
249
+ jwk: PublicJwk;
250
+ }
251
+ /**
252
+ * DPoP proof payload (RFC 9449 Section 4.2)
253
+ */
254
+ interface DPoPProofPayload {
255
+ /** Unique identifier (for replay prevention) */
256
+ jti: string;
257
+ /** HTTP method (uppercase) */
258
+ htm: string;
259
+ /** HTTP URI (scheme + authority + path, no query/fragment) */
260
+ htu: string;
261
+ /** Issued at (Unix timestamp) */
262
+ iat: number;
263
+ /** Server-provided nonce (optional) */
264
+ nonce?: string;
265
+ /** Access token hash (optional, for resource requests) */
266
+ ath?: string;
267
+ }
268
+ /**
269
+ * DPoP validation options
270
+ */
271
+ interface DPoPValidationOptions {
272
+ /** Expected HTTP method */
273
+ method: string;
274
+ /** Expected HTTP URI */
275
+ uri: string;
276
+ /** Access token (for ath validation) */
277
+ accessToken?: string;
278
+ /** Expected JWK thumbprint (from token's cnf.jkt) */
279
+ expectedThumbprint?: string;
280
+ /** Server-provided nonce to validate */
281
+ expectedNonce?: string;
282
+ /** Maximum age for iat claim (seconds, default: 60) */
283
+ maxAge?: number;
284
+ /** Clock tolerance (seconds, default: 60) */
285
+ clockTolerance?: number;
286
+ }
287
+ /**
288
+ * DPoP validation result
289
+ */
290
+ interface DPoPValidationResult {
291
+ /** Whether validation succeeded */
292
+ valid: boolean;
293
+ /** JWK thumbprint of the proof key */
294
+ thumbprint?: string;
295
+ /** Error code if validation failed */
296
+ errorCode?: string;
297
+ /** Error message if validation failed */
298
+ errorMessage?: string;
299
+ }
300
+
301
+ /**
302
+ * AuthrimServer - Main entry point for the server SDK
303
+ */
304
+
305
+ /**
306
+ * AuthrimServer
307
+ *
308
+ * Main class for server-side token validation and DPoP handling.
309
+ */
310
+ declare class AuthrimServer {
311
+ private readonly config;
312
+ private jwksManager;
313
+ private tokenValidator;
314
+ private dpopValidator;
315
+ private introspectionClient;
316
+ private revocationClient;
317
+ private initPromise;
318
+ private initialized;
319
+ constructor(config: AuthrimServerConfig);
320
+ /**
321
+ * Initialize the server (discovers JWKS endpoint if needed)
322
+ *
323
+ * This method is idempotent and thread-safe. Multiple concurrent calls
324
+ * will wait for the same initialization to complete.
325
+ */
326
+ init(): Promise<void>;
327
+ private doInit;
328
+ /**
329
+ * Discover JWKS URI from OpenID Configuration
330
+ */
331
+ private discoverJwksUri;
332
+ /**
333
+ * Validate a JWT access token
334
+ *
335
+ * @param token - JWT string
336
+ * @returns Validation result
337
+ */
338
+ validateToken(token: string): Promise<TokenValidationResult>;
339
+ /**
340
+ * Validate a DPoP proof
341
+ *
342
+ * @param proof - DPoP proof JWT
343
+ * @param options - Validation options
344
+ * @returns Validation result
345
+ */
346
+ validateDPoP(proof: string, options: DPoPValidationOptions): Promise<DPoPValidationResult>;
347
+ /**
348
+ * Introspect a token
349
+ *
350
+ * @param token - Token to introspect
351
+ * @param tokenTypeHint - Optional token type hint
352
+ * @returns Introspection response
353
+ */
354
+ introspect(token: string, tokenTypeHint?: 'access_token' | 'refresh_token'): Promise<IntrospectionResponse>;
355
+ /**
356
+ * Revoke a token
357
+ *
358
+ * @param token - Token to revoke
359
+ * @param tokenTypeHint - Optional token type hint
360
+ */
361
+ revoke(token: string, tokenTypeHint?: 'access_token' | 'refresh_token'): Promise<void>;
362
+ /**
363
+ * Get the resolved configuration
364
+ */
365
+ getConfig(): ResolvedAuthrimServerConfig;
366
+ /**
367
+ * Invalidate JWKS cache
368
+ */
369
+ invalidateJwksCache(): void;
370
+ }
371
+ /**
372
+ * Create an AuthrimServer instance
373
+ *
374
+ * @param config - Server configuration
375
+ * @returns AuthrimServer instance
376
+ */
377
+ declare function createAuthrimServer(config: AuthrimServerConfig): AuthrimServer;
378
+
379
+ /**
380
+ * Middleware Type Definitions
381
+ */
382
+
383
+ /**
384
+ * Framework-agnostic request representation
385
+ */
386
+ interface AuthenticateRequest {
387
+ /** HTTP headers (keys should be lowercase) */
388
+ headers: Record<string, string | string[] | undefined>;
389
+ /** HTTP method */
390
+ method: string;
391
+ /** Full URL (scheme://host:port/path) */
392
+ url: string;
393
+ }
394
+ /**
395
+ * Authentication result (success case)
396
+ */
397
+ interface AuthenticateSuccess {
398
+ data: {
399
+ /** Validated token claims */
400
+ claims: ValidatedToken;
401
+ /** Token type */
402
+ tokenType: 'Bearer' | 'DPoP';
403
+ };
404
+ error: null;
405
+ }
406
+ /**
407
+ * Authentication result (error case)
408
+ */
409
+ interface AuthenticateError {
410
+ data: null;
411
+ error: {
412
+ code: string;
413
+ message: string;
414
+ httpStatus: number;
415
+ };
416
+ }
417
+ /**
418
+ * Authentication result (discriminated union)
419
+ */
420
+ type AuthenticateResult = AuthenticateSuccess | AuthenticateError;
421
+ /**
422
+ * Middleware options
423
+ */
424
+ interface MiddlewareOptions {
425
+ /** Optional realm for WWW-Authenticate header */
426
+ realm?: string;
427
+ /** Required scopes (optional) */
428
+ requiredScopes?: string[];
429
+ /** Whether to validate DPoP binding */
430
+ validateDPoP?: boolean;
431
+ /** Custom error handler */
432
+ onError?: (error: AuthenticateError['error']) => void;
433
+ }
434
+
435
+ export { AuthrimServer as A, type ClaimsValidationOptions as C, type DPoPValidationOptions as D, type IntrospectionRequest as I, type JwtHeader as J, type MiddlewareOptions as M, type ParsedJwt as P, type RevocationRequest as R, type StandardClaims as S, type TokenValidationOptions as T, type ValidatedToken as V, type AuthenticateRequest as a, type AuthenticateResult as b, type ClaimsValidationResult as c, type TokenValidationResult as d, type IntrospectionResponse as e, type DPoPValidationResult as f, type AccessTokenClaims as g, type AuthenticateError as h, type AuthenticateSuccess as i, type ConfirmationClaim as j, type DPoPProofHeader as k, type DPoPProofPayload as l, type IdTokenClaims as m, createAuthrimServer as n };
package/package.json ADDED
@@ -0,0 +1,119 @@
1
+ {
2
+ "name": "@authrim/server",
3
+ "packageManager": "pnpm@9.15.0",
4
+ "version": "0.1.0",
5
+ "description": "Authrim Server SDK - Token validation, DPoP, and middleware for server-side applications",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
12
+ },
13
+ "./providers": {
14
+ "types": "./dist/providers/index.d.ts",
15
+ "import": "./dist/providers/index.js",
16
+ "require": "./dist/providers/index.cjs"
17
+ },
18
+ "./adapters/express": {
19
+ "types": "./dist/adapters/express.d.ts",
20
+ "import": "./dist/adapters/express.js",
21
+ "require": "./dist/adapters/express.cjs"
22
+ },
23
+ "./adapters/fastify": {
24
+ "types": "./dist/adapters/fastify.d.ts",
25
+ "import": "./dist/adapters/fastify.js",
26
+ "require": "./dist/adapters/fastify.cjs"
27
+ },
28
+ "./adapters/hono": {
29
+ "types": "./dist/adapters/hono.d.ts",
30
+ "import": "./dist/adapters/hono.js",
31
+ "require": "./dist/adapters/hono.cjs"
32
+ },
33
+ "./adapters/koa": {
34
+ "types": "./dist/adapters/koa.d.ts",
35
+ "import": "./dist/adapters/koa.js",
36
+ "require": "./dist/adapters/koa.cjs"
37
+ },
38
+ "./adapters/nestjs": {
39
+ "types": "./dist/adapters/nestjs.d.ts",
40
+ "import": "./dist/adapters/nestjs.js",
41
+ "require": "./dist/adapters/nestjs.cjs"
42
+ }
43
+ },
44
+ "main": "./dist/index.cjs",
45
+ "module": "./dist/index.js",
46
+ "types": "./dist/index.d.ts",
47
+ "files": [
48
+ "dist",
49
+ "README.md"
50
+ ],
51
+ "sideEffects": false,
52
+ "scripts": {
53
+ "build": "tsup",
54
+ "dev": "tsup --watch",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest",
57
+ "typecheck": "tsc --noEmit",
58
+ "lint": "eslint src --ext .ts",
59
+ "clean": "rm -rf dist coverage"
60
+ },
61
+ "keywords": [
62
+ "auth",
63
+ "authentication",
64
+ "oidc",
65
+ "oauth2",
66
+ "jwt",
67
+ "dpop",
68
+ "token-validation",
69
+ "middleware",
70
+ "authrim"
71
+ ],
72
+ "author": "Authrim",
73
+ "license": "Apache-2.0",
74
+ "repository": {
75
+ "type": "git",
76
+ "url": "https://github.com/authrim/js-server.git"
77
+ },
78
+ "homepage": "https://github.com/authrim/js-server#readme",
79
+ "bugs": {
80
+ "url": "https://github.com/authrim/js-server/issues"
81
+ },
82
+ "dependencies": {
83
+ "jose": "^5.2.0"
84
+ },
85
+ "devDependencies": {
86
+ "@types/node": "^20.10.0",
87
+ "eslint": "^8.56.0",
88
+ "tsup": "^8.0.0",
89
+ "typescript": "^5.3.0",
90
+ "vitest": "^4.0.17"
91
+ },
92
+ "peerDependencies": {
93
+ "express": "^4.18.0 || ^5.0.0",
94
+ "fastify": "^4.0.0 || ^5.0.0",
95
+ "hono": "^4.0.0",
96
+ "koa": "^2.14.0",
97
+ "@nestjs/common": "^10.0.0"
98
+ },
99
+ "peerDependenciesMeta": {
100
+ "express": {
101
+ "optional": true
102
+ },
103
+ "fastify": {
104
+ "optional": true
105
+ },
106
+ "hono": {
107
+ "optional": true
108
+ },
109
+ "koa": {
110
+ "optional": true
111
+ },
112
+ "@nestjs/common": {
113
+ "optional": true
114
+ }
115
+ },
116
+ "engines": {
117
+ "node": ">=18"
118
+ }
119
+ }