@authyon/server 0.2.0-beta.0 → 0.2.0-beta.1
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/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/browser.cjs +21 -0
- package/dist/browser.d.cts +2 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +4 -0
- package/dist/index.cjs +889 -68
- package/dist/index.d.cts +343 -61
- package/dist/index.d.ts +343 -61
- package/dist/index.js +876 -67
- package/package.json +58 -36
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
+
import { JSONWebKeySet, JWTPayload, JWSHeaderParameters } from 'jose';
|
|
2
|
+
|
|
3
|
+
interface ClientRequestContext {
|
|
4
|
+
/** Real end-user IP, resolved only from infrastructure trusted by the application. */
|
|
5
|
+
clientIp?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Rejects header injection and values that are not a single IPv4 or IPv6 address. */
|
|
8
|
+
declare function normalizeClientIp(value: string): string;
|
|
9
|
+
|
|
10
|
+
interface HttpAdapterRequest {
|
|
11
|
+
url: string;
|
|
12
|
+
method: string;
|
|
13
|
+
headers: Record<string, string>;
|
|
14
|
+
body?: BodyInit | null;
|
|
15
|
+
signal: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
/** Transport contract accepted by both Authyon clients. */
|
|
18
|
+
interface HttpAdapter {
|
|
19
|
+
request(request: HttpAdapterRequest): Promise<Response>;
|
|
20
|
+
}
|
|
21
|
+
type HttpLogEvent = {
|
|
22
|
+
type: "request";
|
|
23
|
+
method: string;
|
|
24
|
+
url: string;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
} | {
|
|
27
|
+
type: "response";
|
|
28
|
+
method: string;
|
|
29
|
+
url: string;
|
|
30
|
+
status: number;
|
|
31
|
+
durationMs: number;
|
|
32
|
+
requestId?: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
} | {
|
|
35
|
+
type: "error";
|
|
36
|
+
method: string;
|
|
37
|
+
url: string;
|
|
38
|
+
errorName: string;
|
|
39
|
+
durationMs: number;
|
|
40
|
+
timestamp: number;
|
|
41
|
+
};
|
|
42
|
+
/** Receives sanitized HTTP lifecycle events. Headers, bodies and query values are never exposed. */
|
|
43
|
+
type HttpLogger = (event: HttpLogEvent) => void;
|
|
44
|
+
/**
|
|
45
|
+
* Mutable logging configuration. Change `enabled` at runtime to enable or disable logging
|
|
46
|
+
* without recreating the Authyon client.
|
|
47
|
+
*/
|
|
48
|
+
interface HttpLoggerOptions {
|
|
49
|
+
enabled: boolean;
|
|
50
|
+
logger?: HttpLogger;
|
|
51
|
+
}
|
|
52
|
+
/** Default adapter backed by the platform's Fetch API. */
|
|
53
|
+
declare class FetchHttpAdapter implements HttpAdapter {
|
|
54
|
+
private readonly fetchImpl;
|
|
55
|
+
constructor(fetchImpl?: typeof fetch);
|
|
56
|
+
request(request: HttpAdapterRequest): Promise<Response>;
|
|
57
|
+
}
|
|
58
|
+
/** Adds safe, configurable lifecycle logging to any HTTP adapter. */
|
|
59
|
+
declare class LoggingHttpAdapter implements HttpAdapter {
|
|
60
|
+
private readonly adapter;
|
|
61
|
+
private readonly options;
|
|
62
|
+
constructor(adapter: HttpAdapter, options: HttpLoggerOptions);
|
|
63
|
+
request(request: HttpAdapterRequest): Promise<Response>;
|
|
64
|
+
private log;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface PaginationOptions {
|
|
68
|
+
skip?: number;
|
|
69
|
+
take?: number;
|
|
70
|
+
}
|
|
71
|
+
interface Paged<T> {
|
|
72
|
+
data: T[];
|
|
73
|
+
perPage?: number;
|
|
74
|
+
pageSize: number;
|
|
75
|
+
total: number;
|
|
76
|
+
pages: number;
|
|
77
|
+
hasNext: boolean;
|
|
78
|
+
hasPrev: boolean;
|
|
79
|
+
}
|
|
80
|
+
interface IntrospectResult {
|
|
81
|
+
active: boolean;
|
|
82
|
+
sub?: string;
|
|
83
|
+
username?: string | null;
|
|
84
|
+
email?: string | null;
|
|
85
|
+
roles?: string[] | null;
|
|
86
|
+
permissions?: string[];
|
|
87
|
+
client_id?: string;
|
|
88
|
+
scope?: string;
|
|
89
|
+
exp?: number;
|
|
90
|
+
iat?: number;
|
|
91
|
+
jti?: string;
|
|
92
|
+
token_type?: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
1
95
|
/** GET/POST /env/tenants — confirmed against the live API. */
|
|
2
96
|
interface Organization {
|
|
3
97
|
id: string;
|
|
@@ -74,21 +168,6 @@ interface EnvironmentUser {
|
|
|
74
168
|
/** JSON-encoded string. */
|
|
75
169
|
customFields?: string;
|
|
76
170
|
}
|
|
77
|
-
/** POST /auth/introspect (RFC 7662) — confirmed against the live API. */
|
|
78
|
-
interface IntrospectResult {
|
|
79
|
-
active: boolean;
|
|
80
|
-
sub?: string;
|
|
81
|
-
username?: string | null;
|
|
82
|
-
email?: string | null;
|
|
83
|
-
roles?: string[] | null;
|
|
84
|
-
permissions?: string[];
|
|
85
|
-
client_id?: string;
|
|
86
|
-
scope?: string;
|
|
87
|
-
exp?: number;
|
|
88
|
-
iat?: number;
|
|
89
|
-
jti?: string;
|
|
90
|
-
token_type?: string;
|
|
91
|
-
}
|
|
92
171
|
/**
|
|
93
172
|
* POST /auth/validate — confirmed against the live API. The wire shape is
|
|
94
173
|
* `{ valid, reason, profile }`, not `{ user, organization }` as the
|
|
@@ -202,21 +281,6 @@ type LoginActivity = AuditEvent;
|
|
|
202
281
|
* Endpoints without `skip`/`take` params (e.g. `/env/tenants`) return a
|
|
203
282
|
* bare array instead.
|
|
204
283
|
*/
|
|
205
|
-
interface Page<T> {
|
|
206
|
-
data: T[];
|
|
207
|
-
/** Item count actually returned for this page. */
|
|
208
|
-
perPage?: number;
|
|
209
|
-
pageSize: number;
|
|
210
|
-
total: number;
|
|
211
|
-
pages: number;
|
|
212
|
-
hasNext: boolean;
|
|
213
|
-
hasPrev: boolean;
|
|
214
|
-
}
|
|
215
|
-
/** Pagination options accepted by list endpoints. */
|
|
216
|
-
interface PageParams {
|
|
217
|
-
skip?: number;
|
|
218
|
-
take?: number;
|
|
219
|
-
}
|
|
220
284
|
/** OAuth 2.0 client-credentials pair minted in the Authyon console. */
|
|
221
285
|
interface ClientCredentials {
|
|
222
286
|
clientId: string;
|
|
@@ -257,18 +321,75 @@ interface AuthyonServerClientOptions {
|
|
|
257
321
|
clientSecret?: string;
|
|
258
322
|
/** API origin. Defaults to `https://api.authyon.com`. */
|
|
259
323
|
baseUrl?: string;
|
|
260
|
-
/**
|
|
324
|
+
/** Allow an HTTP `baseUrl`. Intended only for explicitly trusted local development. */
|
|
325
|
+
allowInsecureHttp?: boolean;
|
|
326
|
+
/** Maximum duration of each HTTP request. Defaults to 15 seconds; set to `0` to disable. */
|
|
327
|
+
timeoutMs?: number;
|
|
328
|
+
/** Custom HTTP adapter for tracing, mocks or an alternative HTTP stack. */
|
|
329
|
+
httpAdapter?: HttpAdapter;
|
|
330
|
+
/** Safe HTTP lifecycle logging. Disabled unless this option is provided with `enabled: true`. */
|
|
331
|
+
httpLogger?: HttpLoggerOptions;
|
|
332
|
+
/** @deprecated Prefer `httpAdapter: new FetchHttpAdapter(customFetch)`. */
|
|
261
333
|
fetch?: typeof fetch;
|
|
262
334
|
}
|
|
263
335
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
336
|
+
type SafeJwtAlgorithm = "RS256" | "PS256" | "ES256" | "EdDSA";
|
|
337
|
+
interface AuthyonJwtClaims extends JWTPayload {
|
|
338
|
+
sub: string;
|
|
339
|
+
iss: string;
|
|
340
|
+
aud: string | string[];
|
|
341
|
+
exp: number;
|
|
342
|
+
iat: number;
|
|
343
|
+
email?: string;
|
|
344
|
+
username?: string;
|
|
345
|
+
roles?: string[];
|
|
346
|
+
permissions?: string[];
|
|
347
|
+
scope?: string;
|
|
348
|
+
}
|
|
349
|
+
interface VerifiedAccessToken {
|
|
350
|
+
claims: AuthyonJwtClaims;
|
|
351
|
+
protectedHeader: JWSHeaderParameters;
|
|
352
|
+
}
|
|
353
|
+
interface JwksTokenVerifierOptions {
|
|
354
|
+
/** Exact trusted issuer from Authyon OpenID discovery. */
|
|
355
|
+
issuer: string;
|
|
356
|
+
/** Audience assigned to the API consuming this token. */
|
|
357
|
+
audience: string | string[];
|
|
358
|
+
/** Trusted HTTPS JWKS endpoint. Never derive this value from the token header. */
|
|
359
|
+
jwksUri?: string;
|
|
360
|
+
/** Static trusted keys for pinned or offline verification. */
|
|
361
|
+
jwks?: JSONWebKeySet;
|
|
362
|
+
algorithms?: readonly SafeJwtAlgorithm[];
|
|
363
|
+
acceptedTokenTypes?: readonly string[];
|
|
364
|
+
clockToleranceSeconds?: number;
|
|
365
|
+
maxTokenAgeSeconds?: number;
|
|
366
|
+
jwksTimeoutMs?: number;
|
|
367
|
+
jwksCacheMs?: number;
|
|
368
|
+
jwksCooldownMs?: number;
|
|
369
|
+
}
|
|
370
|
+
interface AuthyonJwksDiscoveryOptions extends Omit<JwksTokenVerifierOptions, "issuer" | "jwksUri" | "jwks"> {
|
|
371
|
+
/** Optional pin; discovery fails if Authyon advertises a different issuer. */
|
|
372
|
+
issuer?: string;
|
|
373
|
+
/** Override the client environment key used by the discovery endpoint. */
|
|
374
|
+
publishableKey?: string;
|
|
375
|
+
}
|
|
376
|
+
type TokenVerificationErrorCode = "token.invalid" | "token.expired" | "token.not_active" | "token.invalid_claims" | "token.invalid_signature" | "token.key_unavailable";
|
|
377
|
+
declare class TokenVerificationError extends Error {
|
|
378
|
+
readonly code: TokenVerificationErrorCode;
|
|
379
|
+
constructor(code: TokenVerificationErrorCode, options?: {
|
|
380
|
+
cause?: unknown;
|
|
381
|
+
});
|
|
271
382
|
}
|
|
383
|
+
/** Secure, cached JWT signature and claims verifier backed by a trusted remote JWKS. */
|
|
384
|
+
declare class JwksTokenVerifier {
|
|
385
|
+
private readonly options;
|
|
386
|
+
private readonly verifyKey;
|
|
387
|
+
private readonly algorithms;
|
|
388
|
+
private readonly acceptedTokenTypes;
|
|
389
|
+
constructor(options: JwksTokenVerifierOptions);
|
|
390
|
+
verifyAccessToken(token: string): Promise<VerifiedAccessToken>;
|
|
391
|
+
}
|
|
392
|
+
|
|
272
393
|
/**
|
|
273
394
|
* Server-side Authyon client: environment/tenant administration (OAuth
|
|
274
395
|
* client-credentials) plus access-token verification (publishable key).
|
|
@@ -279,10 +400,9 @@ declare class AuthyonServerClient {
|
|
|
279
400
|
private readonly envKey?;
|
|
280
401
|
private readonly clientId?;
|
|
281
402
|
private readonly clientSecret?;
|
|
282
|
-
private readonly
|
|
283
|
-
private readonly
|
|
284
|
-
private
|
|
285
|
-
private environmentTokenInFlight;
|
|
403
|
+
private readonly transport;
|
|
404
|
+
private readonly http;
|
|
405
|
+
private readonly environmentTokenProvider;
|
|
286
406
|
constructor(options?: AuthyonServerClientOptions);
|
|
287
407
|
private request;
|
|
288
408
|
/** Mints (and caches) an environment access token from `clientId`/`clientSecret`. */
|
|
@@ -293,10 +413,12 @@ declare class AuthyonServerClient {
|
|
|
293
413
|
/** GET /.well-known/openid-configuration — OIDC discovery document. */
|
|
294
414
|
openidConfiguration: (publishableKey?: string) => Promise<OpenIdConfiguration>;
|
|
295
415
|
};
|
|
416
|
+
/** Discovers Authyon's trusted issuer/JWKS and creates a cached local access-token verifier. */
|
|
417
|
+
createJwksTokenVerifier(options: AuthyonJwksDiscoveryOptions): Promise<JwksTokenVerifier>;
|
|
296
418
|
/** POST /auth/introspect — lightweight token introspection (RFC 7662). */
|
|
297
|
-
introspect(token: string): Promise<IntrospectResult>;
|
|
419
|
+
introspect(token: string, context?: ClientRequestContext): Promise<IntrospectResult>;
|
|
298
420
|
/** POST /auth/validate — recommended: cross-checks DB state, catches revocation immediately. */
|
|
299
|
-
validate(token: string): Promise<ValidateResult>;
|
|
421
|
+
validate(token: string, context?: ClientRequestContext): Promise<ValidateResult>;
|
|
300
422
|
readonly environmentAuth: {
|
|
301
423
|
/** POST /env/oauth/token — exchanges environment client credentials for an access token. */
|
|
302
424
|
token: (credentials: ClientCredentials) => Promise<TokenResult>;
|
|
@@ -317,7 +439,7 @@ declare class AuthyonServerClient {
|
|
|
317
439
|
/** GET /env/users — paginated list of users in the environment. */
|
|
318
440
|
list: (params?: {
|
|
319
441
|
search?: string;
|
|
320
|
-
} &
|
|
442
|
+
} & PaginationOptions) => Promise<Paged<EnvironmentUser>>;
|
|
321
443
|
/** POST /env/users — create a user in the environment; returns only the new id. */
|
|
322
444
|
create: (input: CreateUserInput) => Promise<{
|
|
323
445
|
id: string;
|
|
@@ -372,7 +494,7 @@ declare class AuthyonServerClient {
|
|
|
372
494
|
* accepting `skip`/`take`), with the same shape
|
|
373
495
|
* `environment.users.list()` items have.
|
|
374
496
|
*/
|
|
375
|
-
list: (tenantId: string, params?:
|
|
497
|
+
list: (tenantId: string, params?: PaginationOptions) => Promise<EnvironmentUser[]>;
|
|
376
498
|
/** POST /env/tenants/{tenantId}/members — add a member to a tenant. */
|
|
377
499
|
add: (tenantId: string, userId: string, roles?: string[]) => Promise<void>;
|
|
378
500
|
/** DELETE /env/tenants/{tenantId}/members/{userId} — remove a member from a tenant. */
|
|
@@ -423,9 +545,9 @@ declare class AuthyonServerClient {
|
|
|
423
545
|
};
|
|
424
546
|
audit: {
|
|
425
547
|
/** GET /env/audit — paginated list of the environment's audit events. */
|
|
426
|
-
list: (params?:
|
|
548
|
+
list: (params?: PaginationOptions) => Promise<Paged<AuditEvent>>;
|
|
427
549
|
/** GET /env/audit/login-activity — paginated login activity in the environment. */
|
|
428
|
-
loginActivity: (params?:
|
|
550
|
+
loginActivity: (params?: PaginationOptions) => Promise<Paged<LoginActivity>>;
|
|
429
551
|
};
|
|
430
552
|
};
|
|
431
553
|
readonly permissions: {
|
|
@@ -435,7 +557,12 @@ declare class AuthyonServerClient {
|
|
|
435
557
|
revoke: (userId: string, permission: string) => Promise<void>;
|
|
436
558
|
};
|
|
437
559
|
/** @internal used by {@link TenantScopedClient} to share the base URL/fetch/error handling. */
|
|
438
|
-
_requestAsTenant<T>(accessToken: string, path: string, options?:
|
|
560
|
+
_requestAsTenant<T>(accessToken: string, path: string, options?: {
|
|
561
|
+
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
562
|
+
body?: unknown;
|
|
563
|
+
query?: object;
|
|
564
|
+
headers?: Record<string, string>;
|
|
565
|
+
}): Promise<T>;
|
|
439
566
|
}
|
|
440
567
|
/**
|
|
441
568
|
* Client scoped to a single tenant's OAuth credentials — every call acts on
|
|
@@ -444,9 +571,7 @@ declare class AuthyonServerClient {
|
|
|
444
571
|
*/
|
|
445
572
|
declare class TenantScopedClient {
|
|
446
573
|
private readonly server;
|
|
447
|
-
private readonly
|
|
448
|
-
private token;
|
|
449
|
-
private tokenInFlight;
|
|
574
|
+
private readonly tokenProvider;
|
|
450
575
|
constructor(server: AuthyonServerClient, credentials: ClientCredentials);
|
|
451
576
|
private getAccessToken;
|
|
452
577
|
private request;
|
|
@@ -460,7 +585,7 @@ declare class TenantScopedClient {
|
|
|
460
585
|
* user records), since both are the "admin" member listing as opposed
|
|
461
586
|
* to `/auth/tenants/{id}/members`'s paginated, lightweight one.
|
|
462
587
|
*/
|
|
463
|
-
list: (params?:
|
|
588
|
+
list: (params?: PaginationOptions) => Promise<EnvironmentUser[]>;
|
|
464
589
|
/** POST /tenant/members — add a member to the token's tenant. */
|
|
465
590
|
add: (userId: string, roles?: string[]) => Promise<void>;
|
|
466
591
|
/** DELETE /tenant/members/{userId} — remove a member from the token's tenant. */
|
|
@@ -497,23 +622,180 @@ declare class TenantScopedClient {
|
|
|
497
622
|
/** Convenience factory: `const authyon = createClient({ clientId, clientSecret })`. */
|
|
498
623
|
declare function createClient(options?: AuthyonServerClientOptions): AuthyonServerClient;
|
|
499
624
|
|
|
500
|
-
/**
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
625
|
+
/** Builds a server client while keeping environment credentials grouped. */
|
|
626
|
+
declare class AuthyonServerClientBuilder {
|
|
627
|
+
private readonly options;
|
|
628
|
+
withEnvironmentKey(envKey: string): this;
|
|
629
|
+
withEnvironmentCredentials(clientId: string, clientSecret: string): this;
|
|
630
|
+
withBaseUrl(baseUrl: string, allowInsecureHttp?: boolean): this;
|
|
631
|
+
withTimeout(timeoutMs: number): this;
|
|
632
|
+
withHttpAdapter(httpAdapter: HttpAdapter): this;
|
|
633
|
+
withHttpLogger(httpLogger: HttpLoggerOptions): this;
|
|
634
|
+
build(): AuthyonServerClient;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
declare const ErrorCodes: {
|
|
638
|
+
readonly Unknown: "unknown";
|
|
639
|
+
readonly NetworkError: "request.network_error";
|
|
640
|
+
readonly Timeout: "request.timeout";
|
|
641
|
+
readonly SessionMalformed: "session.malformed";
|
|
642
|
+
readonly NotAuthenticated: "auth.not_authenticated";
|
|
643
|
+
readonly InvalidToken: "auth.invalid_token";
|
|
644
|
+
readonly MissingToken: "auth.missing_token";
|
|
645
|
+
readonly EmailTaken: "user.email_taken";
|
|
646
|
+
readonly PasswordWeak: "user.password_weak";
|
|
647
|
+
readonly PasswordPwned: "user.password_pwned";
|
|
648
|
+
readonly RateLimited: "rate_limited";
|
|
649
|
+
};
|
|
650
|
+
type KnownErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
651
|
+
type AuthyonErrorCategory = "network" | "timeout" | "authentication" | "authorization" | "validation" | "not_found" | "conflict" | "rate_limit" | "server" | "unknown";
|
|
652
|
+
type AuthyonErrorAction = "retry" | "reauthenticate" | "request_access" | "fix_input" | "not_found" | "resolve_conflict" | "contact_support";
|
|
653
|
+
interface AuthyonErrorInterpretation {
|
|
654
|
+
category: AuthyonErrorCategory;
|
|
655
|
+
action: AuthyonErrorAction;
|
|
656
|
+
retryable: boolean;
|
|
657
|
+
retryAfter?: number;
|
|
658
|
+
requestId?: string;
|
|
659
|
+
}
|
|
660
|
+
/** Shared RFC 7807 error implementation used by both public SDKs. */
|
|
506
661
|
declare class AuthyonError extends Error {
|
|
507
662
|
readonly status: number;
|
|
508
663
|
readonly code: string;
|
|
509
664
|
readonly title: string;
|
|
510
665
|
readonly detail?: string;
|
|
666
|
+
readonly requestId?: string;
|
|
667
|
+
readonly retryAfter?: number;
|
|
668
|
+
readonly cause?: unknown;
|
|
511
669
|
constructor(status: number, body: Partial<{
|
|
512
670
|
title: string;
|
|
513
671
|
detail: string;
|
|
514
672
|
code: string;
|
|
515
|
-
}
|
|
673
|
+
}>, options?: {
|
|
674
|
+
requestId?: string;
|
|
675
|
+
retryAfter?: number;
|
|
676
|
+
cause?: unknown;
|
|
677
|
+
});
|
|
516
678
|
is(code: string): boolean;
|
|
679
|
+
isAny(...codes: readonly string[]): boolean;
|
|
680
|
+
hasPrefix(prefix: string): boolean;
|
|
681
|
+
isStatus(...statuses: readonly number[]): boolean;
|
|
682
|
+
/** Stable interpretation for UI decisions, retries, telemetry and support flows. */
|
|
683
|
+
interpret(): AuthyonErrorInterpretation;
|
|
684
|
+
get category(): AuthyonErrorCategory;
|
|
685
|
+
get retryable(): boolean;
|
|
686
|
+
toJSON(): Record<string, unknown>;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
type AbilitySubject = string | Record<string, unknown>;
|
|
690
|
+
type AbilityConditions = Record<string, unknown>;
|
|
691
|
+
interface AbilityRule {
|
|
692
|
+
action: string | string[];
|
|
693
|
+
subject: string | string[];
|
|
694
|
+
inverted?: boolean;
|
|
695
|
+
conditions?: AbilityConditions;
|
|
696
|
+
fields?: string[];
|
|
697
|
+
reason?: string;
|
|
698
|
+
}
|
|
699
|
+
interface AuthyonPermissionSource {
|
|
700
|
+
permissions?: readonly string[] | null;
|
|
701
|
+
roles?: readonly string[] | null;
|
|
702
|
+
/** OAuth scopes are also accepted and interpreted as Authyon permissions. */
|
|
703
|
+
scope?: string | null;
|
|
704
|
+
}
|
|
705
|
+
interface AuthyonAbilityOptions {
|
|
706
|
+
rules?: readonly AbilityRule[];
|
|
707
|
+
/** Rules added when the source contains a matching Authyon role. */
|
|
708
|
+
roleRules?: Readonly<Record<string, readonly AbilityRule[]>>;
|
|
709
|
+
roles?: readonly string[] | null;
|
|
710
|
+
detectSubjectType?: (subject: Record<string, unknown>) => string;
|
|
711
|
+
}
|
|
712
|
+
type AbilityEvent = "updated";
|
|
713
|
+
type AbilityListener = (rules: readonly AbilityRule[]) => void;
|
|
714
|
+
/** Isomorphic, deny-by-default authorization engine backed by Authyon permissions. */
|
|
715
|
+
declare class AuthyonAbility {
|
|
716
|
+
private readonly detectSubjectType;
|
|
717
|
+
private currentRules;
|
|
718
|
+
private readonly listeners;
|
|
719
|
+
constructor(rules?: readonly AbilityRule[], detectSubjectType?: (subject: Record<string, unknown>) => string);
|
|
720
|
+
get rules(): readonly AbilityRule[];
|
|
721
|
+
can(action: string, subject: AbilitySubject, field?: string): boolean;
|
|
722
|
+
cannot(action: string, subject: AbilitySubject, field?: string): boolean;
|
|
723
|
+
rulesFor(action: string, subject: string): readonly AbilityRule[];
|
|
724
|
+
update(rules: readonly AbilityRule[]): void;
|
|
725
|
+
on(event: AbilityEvent, listener: AbilityListener): () => void;
|
|
726
|
+
}
|
|
727
|
+
/** Fluent rule builder with CASL-like `can` and `cannot` methods. */
|
|
728
|
+
declare class AuthyonAbilityBuilder {
|
|
729
|
+
readonly rules: AbilityRule[];
|
|
730
|
+
can(action: string | string[], subject: string | string[], conditions?: AbilityConditions, fields?: string[]): this;
|
|
731
|
+
cannot(action: string | string[], subject: string | string[], conditions?: AbilityConditions, fields?: string[], reason?: string): this;
|
|
732
|
+
build(options?: Pick<AuthyonAbilityOptions, "detectSubjectType">): AuthyonAbility;
|
|
733
|
+
}
|
|
734
|
+
/** Creates an ability from Authyon permissions, OAuth scope and optional role rules. */
|
|
735
|
+
declare function createAuthyonAbility(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AuthyonAbility;
|
|
736
|
+
/** Converts Authyon's `subject:action` permission strings to authorization rules. */
|
|
737
|
+
declare function createAuthyonRules(source?: AuthyonPermissionSource, options?: AuthyonAbilityOptions): AbilityRule[];
|
|
738
|
+
|
|
739
|
+
interface AbilityRequirement {
|
|
740
|
+
action: string;
|
|
741
|
+
subject: AbilitySubject;
|
|
742
|
+
field?: string;
|
|
743
|
+
}
|
|
744
|
+
interface AuthorizationContext {
|
|
745
|
+
token: string;
|
|
746
|
+
userId?: string;
|
|
747
|
+
ability: AuthyonAbility;
|
|
748
|
+
permissions: readonly string[];
|
|
749
|
+
roles: readonly string[];
|
|
750
|
+
}
|
|
751
|
+
interface AuthorizationOptions extends AuthyonAbilityOptions {
|
|
752
|
+
requirement?: AbilityRequirement;
|
|
753
|
+
/** Uses database-backed validation by default; introspection can be selected when desired. */
|
|
754
|
+
verification?: "validate" | "introspect" | "jwks";
|
|
755
|
+
/** Required for local JWKS verification. Create once and reuse its key cache. */
|
|
756
|
+
jwksVerifier?: JwksTokenVerifier;
|
|
757
|
+
/** Real end-user IP, obtained from trusted application infrastructure. */
|
|
758
|
+
clientIp?: string;
|
|
759
|
+
}
|
|
760
|
+
declare class AuthorizationError extends Error {
|
|
761
|
+
readonly status: 401 | 403 | 503;
|
|
762
|
+
readonly code: "authorization.unauthenticated" | "authorization.forbidden" | "authorization.unavailable";
|
|
763
|
+
constructor(status: 401 | 403 | 503, code: "authorization.unauthenticated" | "authorization.forbidden" | "authorization.unavailable", message: string);
|
|
764
|
+
}
|
|
765
|
+
/** Validates a bearer token and creates an Authyon-backed ability for server runtimes. */
|
|
766
|
+
declare function authorizeToken(client: AuthyonServerClient, token: string, options?: AuthorizationOptions): Promise<AuthorizationContext>;
|
|
767
|
+
/** Works with Web Request objects used by Next.js Route Handlers and other Fetch runtimes. */
|
|
768
|
+
declare function authorizeRequest(client: AuthyonServerClient, request: Pick<Request, "headers">, options?: RequestAuthorizationOptions): Promise<AuthorizationContext>;
|
|
769
|
+
interface RequestAuthorizationOptions extends AuthorizationOptions {
|
|
770
|
+
/** Platform-aware resolver. Only read headers that your trusted proxy overwrites. */
|
|
771
|
+
resolveClientIp?: (request: Pick<Request, "headers">) => string | undefined;
|
|
772
|
+
}
|
|
773
|
+
interface ExpressRequestLike {
|
|
774
|
+
headers: Record<string, string | string[] | undefined>;
|
|
775
|
+
ip?: string;
|
|
776
|
+
socket?: {
|
|
777
|
+
remoteAddress?: string;
|
|
778
|
+
};
|
|
779
|
+
authyon?: AuthorizationContext;
|
|
780
|
+
}
|
|
781
|
+
interface ExpressResponseLike {
|
|
782
|
+
status(code: number): ExpressResponseLike;
|
|
783
|
+
json(body: unknown): unknown;
|
|
784
|
+
}
|
|
785
|
+
type ExpressNext = (error?: unknown) => void;
|
|
786
|
+
interface ExpressAuthorizationMiddlewareOptions extends AuthorizationOptions {
|
|
787
|
+
/** Property assigned to the request. Defaults to `authyon`. */
|
|
788
|
+
requestProperty?: string;
|
|
789
|
+
/** Pass authorization errors to Express error middleware instead of writing a JSON response. */
|
|
790
|
+
passErrorsToNext?: boolean;
|
|
791
|
+
/** Trust Express's proxy-aware `request.ip`. Disabled by default. */
|
|
792
|
+
trustProxy?: boolean;
|
|
793
|
+
/** Overrides all built-in IP resolution; return only a trusted, single client IP. */
|
|
794
|
+
resolveClientIp?: (request: ExpressRequestLike) => string | undefined;
|
|
517
795
|
}
|
|
796
|
+
/** Dependency-free middleware compatible with Express and similar Connect frameworks. */
|
|
797
|
+
declare function createExpressAuthorizationMiddleware(client: AuthyonServerClient, options?: ExpressAuthorizationMiddlewareOptions): (request: ExpressRequestLike & Record<string, unknown>, response: ExpressResponseLike, next: ExpressNext) => Promise<void>;
|
|
798
|
+
/** Converts an authorization error into a Fetch Response suitable for Next.js. */
|
|
799
|
+
declare function createAuthorizationErrorResponse(error: unknown): Response;
|
|
518
800
|
|
|
519
|
-
export { type AuditEvent, AuthyonError, AuthyonServerClient, type AuthyonServerClientOptions, type ClientCredentials, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, type IntrospectResult, type JsonWebKeySet, type LoginActivity, type OpenIdConfiguration, type Organization, type
|
|
801
|
+
export { type AbilityConditions, type AbilityEvent, type AbilityListener, type AbilityRequirement, type AbilityRule, type AbilitySubject, type AuditEvent, type AuthorizationContext, AuthorizationError, type AuthorizationOptions, AuthyonAbility, AuthyonAbilityBuilder, type AuthyonAbilityOptions, AuthyonError, type AuthyonErrorAction, type AuthyonErrorCategory, type AuthyonErrorInterpretation, type AuthyonJwksDiscoveryOptions, type AuthyonJwtClaims, type AuthyonPermissionSource, AuthyonServerClient, AuthyonServerClientBuilder, type AuthyonServerClientOptions, type ClientCredentials, type ClientRequestContext, type CreateOrganizationInput, type CreatePermissionInput, type CreateUserInput, type EnvironmentUser, ErrorCodes, type ExpressAuthorizationMiddlewareOptions, type ExpressNext, type ExpressRequestLike, type ExpressResponseLike, FetchHttpAdapter, type HttpAdapter, type HttpAdapterRequest, type HttpLogEvent, type HttpLogger, type HttpLoggerOptions, type IntrospectResult, type JsonWebKeySet, JwksTokenVerifier, type JwksTokenVerifierOptions, type KnownErrorCode, LoggingHttpAdapter, type LoginActivity, type OpenIdConfiguration, type Organization, type Paged, type PaginationOptions, type Permission, type PermissionsByRole, type RequestAuthorizationOptions, type ReservedPermissions, type Role, type RoleGrant, type RoleInput, type RoleList, type RolePermissions, type SafeJwtAlgorithm, type TenantMembership, TenantScopedClient, type TokenResult, TokenVerificationError, type TokenVerificationErrorCode, type UpdateOrganizationInput, type User, type ValidateResult, type VerifiedAccessToken, authorizeRequest, authorizeToken, createAuthorizationErrorResponse, createAuthyonAbility, createAuthyonRules, createClient, createExpressAuthorizationMiddleware, normalizeClientIp };
|