@yuva-devlab/auth-server 0.2.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.
package/dist/index.d.cts CHANGED
@@ -11,6 +11,8 @@ declare const DEFAULT_API_KEY_HEADER = "x-api-key";
11
11
  declare const DEFAULT_BEARER_PREFIX = "Bearer ";
12
12
  /** Default cache time-to-live for fetched RS256 JWKS public keys (1 hour) */
13
13
  declare const DEFAULT_JWKS_CACHE_TTL_MS = 3600000;
14
+ /** Default cache time-to-live for validated M2M API keys (5 minutes) */
15
+ declare const DEFAULT_API_KEY_CACHE_TTL_MS = 300000;
14
16
  /** Default public endpoints bypassing mandatory authentication */
15
17
  declare const DEFAULT_PUBLIC_PATHS: readonly string[];
16
18
  /** HTTP Status Code for unauthenticated requests */
@@ -105,6 +107,44 @@ interface JwksConfig {
105
107
  /**
106
108
  * Decoded JWT claims payload.
107
109
  */
110
+ /**
111
+ * Result of validating a Machine-to-Machine API key against the DevLab Portal.
112
+ */
113
+ interface ApiKeyValidationResult {
114
+ /** Whether the API key is active, valid, and unexpired */
115
+ readonly valid: boolean;
116
+ /** Primary API key UUID */
117
+ readonly keyId?: string;
118
+ /** Name assigned to this machine key */
119
+ readonly name?: string;
120
+ /** Target application slug this key belongs to */
121
+ readonly appId?: string;
122
+ /** Target application display name */
123
+ readonly appName?: string;
124
+ /** Scopes and permissions granted to this key */
125
+ readonly scopes?: readonly string[];
126
+ /** Allowed requests per minute limit */
127
+ readonly rateLimitRpm?: number;
128
+ /** Human-readable rejection reason if invalid */
129
+ readonly reason?: string;
130
+ }
131
+ /**
132
+ * Configuration options for Machine-to-Machine API key validation.
133
+ */
134
+ interface M2mConfig {
135
+ /** Base URL of the authentication server / identity provider */
136
+ readonly authUrl?: string;
137
+ /** Legacy alias for authUrl */
138
+ readonly portalUrl?: string;
139
+ /** In-memory cache duration for validated API keys in milliseconds */
140
+ readonly cacheTtlMs?: number;
141
+ /** Expected client or application identifier */
142
+ readonly requiredClientId?: string;
143
+ /** Legacy alias for requiredClientId */
144
+ readonly requiredAppId?: string;
145
+ /** Required operational scopes that must be granted to the API key */
146
+ readonly requiredScopes?: readonly string[];
147
+ }
108
148
  interface JwtPayload {
109
149
  readonly sub: string;
110
150
  readonly iss?: string;
@@ -113,32 +153,26 @@ interface JwtPayload {
113
153
  readonly nbf?: number;
114
154
  readonly iat?: number;
115
155
  readonly email?: string;
156
+ readonly name?: string;
157
+ readonly role?: string;
116
158
  readonly roles?: readonly string[];
117
159
  readonly tenantId?: string;
160
+ readonly tenant_id?: string;
161
+ readonly tenant_slug?: string;
162
+ readonly tenant_name?: string;
163
+ readonly apps?: readonly string[];
164
+ readonly appId?: string;
118
165
  readonly [key: string]: unknown;
119
166
  }
120
167
 
121
168
  /**
122
169
  * @file packages/auth-server/src/token/jwt-validator.ts
123
- * @description In-memory cached RS256 JWKS validator for authenticating JWT session tokens.
170
+ * @description RS256 JWKS validator providing cryptographic signature verification and claims checking.
124
171
  * @module @yuva-devlab/auth-server
125
172
  */
126
173
 
127
174
  /**
128
175
  * Validates JWT access tokens against an in-memory cached JWKS public key set.
129
- *
130
- * @example
131
- * ```typescript
132
- * import { JwksValidator } from "@yuva-devlab/auth-server";
133
- *
134
- * const validator = new JwksValidator({
135
- * jwksUri: "https://auth.devlab.io/.well-known/jwks.json",
136
- * issuer: "https://auth.devlab.io",
137
- * audience: "api.devlab.io",
138
- * });
139
- *
140
- * const claims = await validator.verifyToken(token);
141
- * ```
142
176
  */
143
177
  declare class JwksValidator {
144
178
  private readonly jwksUri;
@@ -146,35 +180,42 @@ declare class JwksValidator {
146
180
  private readonly issuer?;
147
181
  private readonly cacheTtlMs;
148
182
  private keyCache;
149
- /**
150
- * Initializes the JWKS validator.
151
- *
152
- * @param config - JWKS configuration parameters
153
- */
154
- constructor(config: JwksConfig);
155
- /**
156
- * Decodes a JWT token without verifying cryptographic signature.
157
- *
158
- * @param token - Base64Url encoded JWT string
159
- * @returns Header and payload objects
160
- */
183
+ private jwksSetCache;
184
+ constructor(config?: Partial<JwksConfig>);
161
185
  parseUnverified(token: string): {
162
186
  header: Record<string, unknown>;
163
187
  payload: JwtPayload;
188
+ rawHeader: string;
189
+ rawPayload: string;
190
+ signature: string;
164
191
  };
165
- /**
166
- * Verifies standard claims (exp, nbf, iss, aud) on a JWT token.
167
- *
168
- * @param token - Bearer access token
169
- * @returns Validated JwtPayload claims
170
- */
192
+ private fetchJwks;
193
+ private resolveKeyObject;
171
194
  verifyToken(token: string): Promise<JwtPayload>;
172
- /**
173
- * Clears cached public keys from memory.
174
- */
175
195
  clearKeyCache(): void;
176
196
  }
177
197
 
198
+ /**
199
+ * @file packages/auth-server/src/token/m2m-validator.ts
200
+ * @description In-memory cached Machine-to-Machine API key validator introspecting the auth provider.
201
+ * @module @yuva-devlab/auth-server
202
+ */
203
+
204
+ /**
205
+ * Validates machine-to-machine API keys against central authentication server with in-memory caching.
206
+ */
207
+ declare class M2mApiKeyValidator {
208
+ private readonly authUrl?;
209
+ private readonly cacheTtlMs;
210
+ private readonly requiredClientId?;
211
+ private readonly requiredScopes?;
212
+ private readonly keyCache;
213
+ constructor(config?: Partial<M2mConfig>);
214
+ validateKey(rawKey: string): Promise<ApiKeyValidationResult>;
215
+ private enforceKeyConstraints;
216
+ clearCache(): void;
217
+ }
218
+
178
219
  /**
179
220
  * @file packages/auth-server/src/guards/auth-guards.ts
180
221
  * @description Authorization assertions and scope/role enforcement guards.
@@ -266,4 +307,4 @@ interface CreateAuthMiddlewareOptions {
266
307
  */
267
308
  declare function createAuthMiddleware(options: CreateAuthMiddlewareOptions): (req: GenericHttpRequest, res: GenericHttpResponse) => Promise<boolean>;
268
309
 
269
- export { type ApiKey, AppStatus, type AuthContext, type AuthUser, type CreateAuthMiddlewareOptions, DEFAULT_API_KEY_HEADER, DEFAULT_AUTH_HEADER, DEFAULT_BEARER_PREFIX, DEFAULT_JWKS_CACHE_TTL_MS, DEFAULT_PUBLIC_PATHS, type GenericHttpRequest, type GenericHttpResponse, HTTP_STATUS_FORBIDDEN, HTTP_STATUS_SERVICE_UNAVAILABLE, HTTP_STATUS_UNAUTHORIZED, type JwksConfig, JwksValidator, type JwtPayload, createAppKillSwitchMiddleware, createAuthMiddleware, requireApiKey, requireScope, requireUser };
310
+ export { type ApiKey, type ApiKeyValidationResult, AppStatus, type AuthContext, type AuthUser, type CreateAuthMiddlewareOptions, DEFAULT_API_KEY_CACHE_TTL_MS, DEFAULT_API_KEY_HEADER, DEFAULT_AUTH_HEADER, DEFAULT_BEARER_PREFIX, DEFAULT_JWKS_CACHE_TTL_MS, DEFAULT_PUBLIC_PATHS, type GenericHttpRequest, type GenericHttpResponse, HTTP_STATUS_FORBIDDEN, HTTP_STATUS_SERVICE_UNAVAILABLE, HTTP_STATUS_UNAUTHORIZED, type JwksConfig, JwksValidator, type JwtPayload, M2mApiKeyValidator, type M2mConfig, createAppKillSwitchMiddleware, createAuthMiddleware, requireApiKey, requireScope, requireUser };
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ declare const DEFAULT_API_KEY_HEADER = "x-api-key";
11
11
  declare const DEFAULT_BEARER_PREFIX = "Bearer ";
12
12
  /** Default cache time-to-live for fetched RS256 JWKS public keys (1 hour) */
13
13
  declare const DEFAULT_JWKS_CACHE_TTL_MS = 3600000;
14
+ /** Default cache time-to-live for validated M2M API keys (5 minutes) */
15
+ declare const DEFAULT_API_KEY_CACHE_TTL_MS = 300000;
14
16
  /** Default public endpoints bypassing mandatory authentication */
15
17
  declare const DEFAULT_PUBLIC_PATHS: readonly string[];
16
18
  /** HTTP Status Code for unauthenticated requests */
@@ -105,6 +107,44 @@ interface JwksConfig {
105
107
  /**
106
108
  * Decoded JWT claims payload.
107
109
  */
110
+ /**
111
+ * Result of validating a Machine-to-Machine API key against the DevLab Portal.
112
+ */
113
+ interface ApiKeyValidationResult {
114
+ /** Whether the API key is active, valid, and unexpired */
115
+ readonly valid: boolean;
116
+ /** Primary API key UUID */
117
+ readonly keyId?: string;
118
+ /** Name assigned to this machine key */
119
+ readonly name?: string;
120
+ /** Target application slug this key belongs to */
121
+ readonly appId?: string;
122
+ /** Target application display name */
123
+ readonly appName?: string;
124
+ /** Scopes and permissions granted to this key */
125
+ readonly scopes?: readonly string[];
126
+ /** Allowed requests per minute limit */
127
+ readonly rateLimitRpm?: number;
128
+ /** Human-readable rejection reason if invalid */
129
+ readonly reason?: string;
130
+ }
131
+ /**
132
+ * Configuration options for Machine-to-Machine API key validation.
133
+ */
134
+ interface M2mConfig {
135
+ /** Base URL of the authentication server / identity provider */
136
+ readonly authUrl?: string;
137
+ /** Legacy alias for authUrl */
138
+ readonly portalUrl?: string;
139
+ /** In-memory cache duration for validated API keys in milliseconds */
140
+ readonly cacheTtlMs?: number;
141
+ /** Expected client or application identifier */
142
+ readonly requiredClientId?: string;
143
+ /** Legacy alias for requiredClientId */
144
+ readonly requiredAppId?: string;
145
+ /** Required operational scopes that must be granted to the API key */
146
+ readonly requiredScopes?: readonly string[];
147
+ }
108
148
  interface JwtPayload {
109
149
  readonly sub: string;
110
150
  readonly iss?: string;
@@ -113,32 +153,26 @@ interface JwtPayload {
113
153
  readonly nbf?: number;
114
154
  readonly iat?: number;
115
155
  readonly email?: string;
156
+ readonly name?: string;
157
+ readonly role?: string;
116
158
  readonly roles?: readonly string[];
117
159
  readonly tenantId?: string;
160
+ readonly tenant_id?: string;
161
+ readonly tenant_slug?: string;
162
+ readonly tenant_name?: string;
163
+ readonly apps?: readonly string[];
164
+ readonly appId?: string;
118
165
  readonly [key: string]: unknown;
119
166
  }
120
167
 
121
168
  /**
122
169
  * @file packages/auth-server/src/token/jwt-validator.ts
123
- * @description In-memory cached RS256 JWKS validator for authenticating JWT session tokens.
170
+ * @description RS256 JWKS validator providing cryptographic signature verification and claims checking.
124
171
  * @module @yuva-devlab/auth-server
125
172
  */
126
173
 
127
174
  /**
128
175
  * Validates JWT access tokens against an in-memory cached JWKS public key set.
129
- *
130
- * @example
131
- * ```typescript
132
- * import { JwksValidator } from "@yuva-devlab/auth-server";
133
- *
134
- * const validator = new JwksValidator({
135
- * jwksUri: "https://auth.devlab.io/.well-known/jwks.json",
136
- * issuer: "https://auth.devlab.io",
137
- * audience: "api.devlab.io",
138
- * });
139
- *
140
- * const claims = await validator.verifyToken(token);
141
- * ```
142
176
  */
143
177
  declare class JwksValidator {
144
178
  private readonly jwksUri;
@@ -146,35 +180,42 @@ declare class JwksValidator {
146
180
  private readonly issuer?;
147
181
  private readonly cacheTtlMs;
148
182
  private keyCache;
149
- /**
150
- * Initializes the JWKS validator.
151
- *
152
- * @param config - JWKS configuration parameters
153
- */
154
- constructor(config: JwksConfig);
155
- /**
156
- * Decodes a JWT token without verifying cryptographic signature.
157
- *
158
- * @param token - Base64Url encoded JWT string
159
- * @returns Header and payload objects
160
- */
183
+ private jwksSetCache;
184
+ constructor(config?: Partial<JwksConfig>);
161
185
  parseUnverified(token: string): {
162
186
  header: Record<string, unknown>;
163
187
  payload: JwtPayload;
188
+ rawHeader: string;
189
+ rawPayload: string;
190
+ signature: string;
164
191
  };
165
- /**
166
- * Verifies standard claims (exp, nbf, iss, aud) on a JWT token.
167
- *
168
- * @param token - Bearer access token
169
- * @returns Validated JwtPayload claims
170
- */
192
+ private fetchJwks;
193
+ private resolveKeyObject;
171
194
  verifyToken(token: string): Promise<JwtPayload>;
172
- /**
173
- * Clears cached public keys from memory.
174
- */
175
195
  clearKeyCache(): void;
176
196
  }
177
197
 
198
+ /**
199
+ * @file packages/auth-server/src/token/m2m-validator.ts
200
+ * @description In-memory cached Machine-to-Machine API key validator introspecting the auth provider.
201
+ * @module @yuva-devlab/auth-server
202
+ */
203
+
204
+ /**
205
+ * Validates machine-to-machine API keys against central authentication server with in-memory caching.
206
+ */
207
+ declare class M2mApiKeyValidator {
208
+ private readonly authUrl?;
209
+ private readonly cacheTtlMs;
210
+ private readonly requiredClientId?;
211
+ private readonly requiredScopes?;
212
+ private readonly keyCache;
213
+ constructor(config?: Partial<M2mConfig>);
214
+ validateKey(rawKey: string): Promise<ApiKeyValidationResult>;
215
+ private enforceKeyConstraints;
216
+ clearCache(): void;
217
+ }
218
+
178
219
  /**
179
220
  * @file packages/auth-server/src/guards/auth-guards.ts
180
221
  * @description Authorization assertions and scope/role enforcement guards.
@@ -266,4 +307,4 @@ interface CreateAuthMiddlewareOptions {
266
307
  */
267
308
  declare function createAuthMiddleware(options: CreateAuthMiddlewareOptions): (req: GenericHttpRequest, res: GenericHttpResponse) => Promise<boolean>;
268
309
 
269
- export { type ApiKey, AppStatus, type AuthContext, type AuthUser, type CreateAuthMiddlewareOptions, DEFAULT_API_KEY_HEADER, DEFAULT_AUTH_HEADER, DEFAULT_BEARER_PREFIX, DEFAULT_JWKS_CACHE_TTL_MS, DEFAULT_PUBLIC_PATHS, type GenericHttpRequest, type GenericHttpResponse, HTTP_STATUS_FORBIDDEN, HTTP_STATUS_SERVICE_UNAVAILABLE, HTTP_STATUS_UNAUTHORIZED, type JwksConfig, JwksValidator, type JwtPayload, createAppKillSwitchMiddleware, createAuthMiddleware, requireApiKey, requireScope, requireUser };
310
+ export { type ApiKey, type ApiKeyValidationResult, AppStatus, type AuthContext, type AuthUser, type CreateAuthMiddlewareOptions, DEFAULT_API_KEY_CACHE_TTL_MS, DEFAULT_API_KEY_HEADER, DEFAULT_AUTH_HEADER, DEFAULT_BEARER_PREFIX, DEFAULT_JWKS_CACHE_TTL_MS, DEFAULT_PUBLIC_PATHS, type GenericHttpRequest, type GenericHttpResponse, HTTP_STATUS_FORBIDDEN, HTTP_STATUS_SERVICE_UNAVAILABLE, HTTP_STATUS_UNAUTHORIZED, type JwksConfig, JwksValidator, type JwtPayload, M2mApiKeyValidator, type M2mConfig, createAppKillSwitchMiddleware, createAuthMiddleware, requireApiKey, requireScope, requireUser };