@edirect/auth-fastify 0.0.0-stage → 11.2.5
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/README.md +87 -2
- package/package.json +34 -3
- package/src/auth.service.d.ts +34 -0
- package/src/auth.service.d.ts.map +1 -0
- package/src/auth.service.js +117 -0
- package/src/config.d.ts +33 -0
- package/src/config.d.ts.map +1 -0
- package/src/config.js +74 -0
- package/src/constants.d.ts +57 -0
- package/src/constants.d.ts.map +1 -0
- package/src/constants.js +74 -0
- package/src/decorators.d.ts +17 -0
- package/src/decorators.d.ts.map +1 -0
- package/src/decorators.js +16 -0
- package/src/hooks/auth-service.d.ts +20 -0
- package/src/hooks/auth-service.d.ts.map +1 -0
- package/src/hooks/auth-service.js +135 -0
- package/src/hooks/auto.d.ts +16 -0
- package/src/hooks/auto.d.ts.map +1 -0
- package/src/hooks/auto.js +104 -0
- package/src/hooks/keycloak.d.ts +21 -0
- package/src/hooks/keycloak.d.ts.map +1 -0
- package/src/hooks/keycloak.js +139 -0
- package/src/hooks/token-exchange.d.ts +11 -0
- package/src/hooks/token-exchange.d.ts.map +1 -0
- package/src/hooks/token-exchange.js +58 -0
- package/src/index.d.ts +19 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +39 -0
- package/src/interfaces/entity-user.interface.d.ts +14 -0
- package/src/interfaces/entity-user.interface.d.ts.map +1 -0
- package/src/interfaces/entity-user.interface.js +19 -0
- package/src/interfaces/entity.interface.d.ts +25 -0
- package/src/interfaces/entity.interface.d.ts.map +1 -0
- package/src/interfaces/entity.interface.js +2 -0
- package/src/interfaces/index.d.ts +26 -0
- package/src/interfaces/index.d.ts.map +1 -0
- package/src/interfaces/index.js +8 -0
- package/src/interfaces/keycloak-token.interface.d.ts +84 -0
- package/src/interfaces/keycloak-token.interface.d.ts.map +1 -0
- package/src/interfaces/keycloak-token.interface.js +2 -0
- package/src/interfaces/user.interface.d.ts +40 -0
- package/src/interfaces/user.interface.d.ts.map +1 -0
- package/src/interfaces/user.interface.js +91 -0
- package/src/plugin.d.ts +44 -0
- package/src/plugin.d.ts.map +1 -0
- package/src/plugin.js +103 -0
- package/src/providers/keycloak-token-exchange.service.d.ts +15 -0
- package/src/providers/keycloak-token-exchange.service.d.ts.map +1 -0
- package/src/providers/keycloak-token-exchange.service.js +62 -0
- package/src/providers/memory-cache.d.ts +16 -0
- package/src/providers/memory-cache.d.ts.map +1 -0
- package/src/providers/memory-cache.js +24 -0
- package/src/providers/well-known.service.d.ts +18 -0
- package/src/providers/well-known.service.d.ts.map +1 -0
- package/src/providers/well-known.service.js +41 -0
- package/src/realm-env-key.d.ts +3 -0
- package/src/realm-env-key.d.ts.map +1 -0
- package/src/realm-env-key.js +27 -0
- package/src/types.d.ts +27 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +2 -0
- package/src/util.d.ts +18 -0
- package/src/util.d.ts.map +1 -0
- package/src/util.js +41 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type { KeycloakAccessToken, KeycloakResponseAccessToken, OAuth2ParamsInterface, WellKnown, } from './keycloak-token.interface';
|
|
2
|
+
export type { EntityInterface, FieldsInterface } from './entity.interface';
|
|
3
|
+
export type { EntityUserInterface } from './entity-user.interface';
|
|
4
|
+
export type { UserInterface, JWTToken } from './user.interface';
|
|
5
|
+
export { keycloakAccessTokenToUserInterface, getEntityAttributeByClient, } from './user.interface';
|
|
6
|
+
export { keycloakAccessTokenToEntityUserInterface } from './entity-user.interface';
|
|
7
|
+
export interface AuthServiceInterface {
|
|
8
|
+
getToken(): string;
|
|
9
|
+
setToken(token: string): void;
|
|
10
|
+
getUser(): import('./user.interface').UserInterface;
|
|
11
|
+
setUser(user: import('./user.interface').UserInterface): void;
|
|
12
|
+
getEntityUser(): import('./entity-user.interface').EntityUserInterface;
|
|
13
|
+
setEntityHeader(): void;
|
|
14
|
+
hasRoles(roles: string[]): boolean;
|
|
15
|
+
hasRole(role: string): boolean;
|
|
16
|
+
hasPermissions(permissions: string[]): boolean;
|
|
17
|
+
hasPermission(permission: string): boolean;
|
|
18
|
+
isAdmin(): boolean;
|
|
19
|
+
isUser(): boolean;
|
|
20
|
+
isService(): boolean;
|
|
21
|
+
isCrm(): boolean;
|
|
22
|
+
isBroker(): boolean;
|
|
23
|
+
isAgentLicensed(): boolean;
|
|
24
|
+
isAgentUnlicensed(): boolean;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,qBAAqB,EACrB,SAAS,GACV,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC3E,YAAY,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EACL,kCAAkC,EAClC,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,wCAAwC,EAAE,MAAM,yBAAyB,CAAC;AAEnF,MAAM,WAAW,oBAAoB;IACnC,QAAQ,IAAI,MAAM,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,OAAO,IAAI,OAAO,kBAAkB,EAAE,aAAa,CAAC;IACpD,OAAO,CAAC,IAAI,EAAE,OAAO,kBAAkB,EAAE,aAAa,GAAG,IAAI,CAAC;IAC9D,aAAa,IAAI,OAAO,yBAAyB,EAAE,mBAAmB,CAAC;IACvE,eAAe,IAAI,IAAI,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACnC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC/B,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC/C,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC;IAC3C,OAAO,IAAI,OAAO,CAAC;IACnB,MAAM,IAAI,OAAO,CAAC;IAClB,SAAS,IAAI,OAAO,CAAC;IACrB,KAAK,IAAI,OAAO,CAAC;IACjB,QAAQ,IAAI,OAAO,CAAC;IACpB,eAAe,IAAI,OAAO,CAAC;IAC3B,iBAAiB,IAAI,OAAO,CAAC;CAC9B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keycloakAccessTokenToEntityUserInterface = exports.getEntityAttributeByClient = exports.keycloakAccessTokenToUserInterface = void 0;
|
|
4
|
+
var user_interface_1 = require("./user.interface");
|
|
5
|
+
Object.defineProperty(exports, "keycloakAccessTokenToUserInterface", { enumerable: true, get: function () { return user_interface_1.keycloakAccessTokenToUserInterface; } });
|
|
6
|
+
Object.defineProperty(exports, "getEntityAttributeByClient", { enumerable: true, get: function () { return user_interface_1.getEntityAttributeByClient; } });
|
|
7
|
+
var entity_user_interface_1 = require("./entity-user.interface");
|
|
8
|
+
Object.defineProperty(exports, "keycloakAccessTokenToEntityUserInterface", { enumerable: true, get: function () { return entity_user_interface_1.keycloakAccessTokenToEntityUserInterface; } });
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export interface OAuth2ParamsInterface {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
wellKnownPath: string;
|
|
4
|
+
grantType: 'client_credentials' | 'password';
|
|
5
|
+
clientId: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
scope?: string;
|
|
8
|
+
username?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
adminPath?: string;
|
|
11
|
+
}
|
|
12
|
+
export type WellKnown = {
|
|
13
|
+
issuer: string;
|
|
14
|
+
token_endpoint: string;
|
|
15
|
+
jwks_uri: string;
|
|
16
|
+
};
|
|
17
|
+
export type KeycloakAccessToken = {
|
|
18
|
+
exp: number;
|
|
19
|
+
iat: number;
|
|
20
|
+
jti: string;
|
|
21
|
+
iss: string;
|
|
22
|
+
aud: string[];
|
|
23
|
+
sub: string;
|
|
24
|
+
typ: string;
|
|
25
|
+
azp: string;
|
|
26
|
+
session_state?: string;
|
|
27
|
+
acr: string;
|
|
28
|
+
'allowed-origins': string[];
|
|
29
|
+
realm_access: {
|
|
30
|
+
roles: string[];
|
|
31
|
+
};
|
|
32
|
+
resource_access: Record<string, {
|
|
33
|
+
roles: string[];
|
|
34
|
+
}>;
|
|
35
|
+
scope: string;
|
|
36
|
+
sid: string;
|
|
37
|
+
email_verified: boolean;
|
|
38
|
+
name?: string;
|
|
39
|
+
preferred_username: string;
|
|
40
|
+
given_name?: string;
|
|
41
|
+
family_name?: string;
|
|
42
|
+
email?: string;
|
|
43
|
+
groups?: string[];
|
|
44
|
+
phone_number?: string;
|
|
45
|
+
clientHost?: string;
|
|
46
|
+
clientId?: string;
|
|
47
|
+
clientAddress?: string;
|
|
48
|
+
targets?: Array<{
|
|
49
|
+
role?: string;
|
|
50
|
+
permissions?: string[];
|
|
51
|
+
scopes?: {
|
|
52
|
+
entities?: string[];
|
|
53
|
+
instances?: string[];
|
|
54
|
+
};
|
|
55
|
+
}>;
|
|
56
|
+
account_id?: string;
|
|
57
|
+
partner_id?: string;
|
|
58
|
+
branches?: string[];
|
|
59
|
+
brands?: string[];
|
|
60
|
+
verticals?: string[];
|
|
61
|
+
markets?: string[];
|
|
62
|
+
entity?: {
|
|
63
|
+
accountId: string;
|
|
64
|
+
entityId: string;
|
|
65
|
+
entityUserId?: string;
|
|
66
|
+
rootEntityId?: string;
|
|
67
|
+
branches?: string[];
|
|
68
|
+
};
|
|
69
|
+
entities?: Array<{
|
|
70
|
+
entityId: string;
|
|
71
|
+
} & Record<string, unknown>>;
|
|
72
|
+
};
|
|
73
|
+
export type KeycloakResponseAccessToken = {
|
|
74
|
+
access_token: string;
|
|
75
|
+
expires_in: number;
|
|
76
|
+
refresh_expires_in: number;
|
|
77
|
+
refresh_token?: string;
|
|
78
|
+
token_type: string;
|
|
79
|
+
id_token: string;
|
|
80
|
+
'not-before-policy': number;
|
|
81
|
+
session_state?: '087e93d0-63b5-4b41-b379-81d5227e33d3';
|
|
82
|
+
scope: string;
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=keycloak-token.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keycloak-token.interface.d.ts","sourceRoot":"","sources":["../../../src/interfaces/keycloak-token.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,oBAAoB,GAAG,UAAU,CAAC;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,EAAE,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,YAAY,EAAE;QACZ,KAAK,EAAE,MAAM,EAAE,CAAC;KACjB,CAAC;IACF,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,cAAc,EAAE,OAAO,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,KAAK,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;QACvB,MAAM,CAAC,EAAE;YAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;YAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;SAAE,CAAC;KACxD,CAAC,CAAC;IAIH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB,MAAM,CAAC,EAAE;QACP,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;IAEF,QAAQ,CAAC,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAClE,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE,sCAAsC,CAAC;IACvD,KAAK,EAAE,MAAM,CAAC;CACf,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { KeycloakAccessToken } from './keycloak-token.interface';
|
|
2
|
+
import { EntityUserInterface } from './entity-user.interface';
|
|
3
|
+
export interface JWTToken {
|
|
4
|
+
exp: number;
|
|
5
|
+
iat: number;
|
|
6
|
+
jti: string;
|
|
7
|
+
iss: string;
|
|
8
|
+
aud: string;
|
|
9
|
+
sub?: string;
|
|
10
|
+
typ?: string;
|
|
11
|
+
azp?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface UserInterface extends JWTToken {
|
|
14
|
+
clientId?: string;
|
|
15
|
+
accountId: string;
|
|
16
|
+
permissions: string[];
|
|
17
|
+
roles: string[];
|
|
18
|
+
scope: string;
|
|
19
|
+
username?: string;
|
|
20
|
+
entityUser: EntityUserInterface;
|
|
21
|
+
targets?: TargetInterface[];
|
|
22
|
+
}
|
|
23
|
+
interface TargetInterface {
|
|
24
|
+
role?: string;
|
|
25
|
+
permissions: string[];
|
|
26
|
+
scopes: TargetScopeInterface;
|
|
27
|
+
}
|
|
28
|
+
export interface TargetScopeInterface {
|
|
29
|
+
entities: string[];
|
|
30
|
+
instances: string[];
|
|
31
|
+
}
|
|
32
|
+
export declare function keycloakAccessTokenToUserInterface(data: KeycloakAccessToken, client: string): UserInterface;
|
|
33
|
+
/**
|
|
34
|
+
* Tries root claims, then `entity`, then legacy `entities[]`. The formats are
|
|
35
|
+
* mutually exclusive, so at most one tier contributes for a given token.
|
|
36
|
+
*/
|
|
37
|
+
export declare function getEntityAttributeByClient(data: KeycloakAccessToken, client: string, attribute: 'entityId' | 'accountId' | 'entityUserId' | 'rootEntityId'): string | undefined;
|
|
38
|
+
export declare function getEntityAttributeByClient(data: KeycloakAccessToken, client: string, attribute: 'branches' | 'entities'): string[] | undefined;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=user.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.interface.d.ts","sourceRoot":"","sources":["../../../src/interfaces/user.interface.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EACL,mBAAmB,EAEpB,MAAM,yBAAyB,CAAC;AAIjC,MAAM,WAAW,QAAQ;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,aAAc,SAAQ,QAAQ;IAC7C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE,MAAM,EAAE,CAAC;IAKhB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,mBAAmB,CAAC;IAEhC,OAAO,CAAC,EAAE,eAAe,EAAE,CAAC;CAC7B;AAED,UAAU,eAAe;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,MAAM,EAAE,oBAAoB,CAAC;CAC9B;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,MAAM,EAAE,CAAC;CACrB;AAUD,wBAAgB,kCAAkC,CAChD,IAAI,EAAE,mBAAmB,EACzB,MAAM,EAAE,MAAM,GACb,aAAa,CAiCf;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,IAAI,EAAE,mBAAmB,EACzB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,UAAU,GAAG,WAAW,GAAG,cAAc,GAAG,cAAc,GACpE,MAAM,GAAG,SAAS,CAAC;AACtB,wBAAgB,0BAA0B,CACxC,IAAI,EAAE,mBAAmB,EACzB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,UAAU,GAAG,UAAU,GACjC,MAAM,EAAE,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keycloakAccessTokenToUserInterface = keycloakAccessTokenToUserInterface;
|
|
4
|
+
exports.getEntityAttributeByClient = getEntityAttributeByClient;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
7
|
+
const get_1 = tslib_1.__importDefault(require("lodash/get"));
|
|
8
|
+
const entity_user_interface_1 = require("./entity-user.interface");
|
|
9
|
+
const debug = (0, debug_1.default)('auth-module:user-interface');
|
|
10
|
+
// `entityUserId` is absent: it has no root-format equivalent.
|
|
11
|
+
const ROOT_CLAIM_BY_ATTRIBUTE = {
|
|
12
|
+
accountId: 'account_id',
|
|
13
|
+
entityId: 'partner_id',
|
|
14
|
+
rootEntityId: 'partner_id',
|
|
15
|
+
branches: 'branches',
|
|
16
|
+
};
|
|
17
|
+
function keycloakAccessTokenToUserInterface(data, client) {
|
|
18
|
+
const entityId = getEntityAttributeByClient(data, data.azp, 'entityId');
|
|
19
|
+
const permissions = getPermissions(data, client);
|
|
20
|
+
const result = {
|
|
21
|
+
exp: data.exp,
|
|
22
|
+
iat: data.iat,
|
|
23
|
+
jti: data.jti,
|
|
24
|
+
iss: data.iss,
|
|
25
|
+
aud: client,
|
|
26
|
+
sub: data?.sub,
|
|
27
|
+
typ: data?.typ,
|
|
28
|
+
azp: client,
|
|
29
|
+
clientId: client,
|
|
30
|
+
// Genuinely absent on root tokens without `account_id` and on legacy
|
|
31
|
+
// records lacking it, but the published type has always declared it
|
|
32
|
+
// required. Narrowed here rather than widened there, to avoid a breaking
|
|
33
|
+
// change; the rest of this literal stays checked.
|
|
34
|
+
accountId: getEntityAttributeByClient(data, data.azp, 'accountId'),
|
|
35
|
+
permissions,
|
|
36
|
+
roles: getRoles(data, client),
|
|
37
|
+
scope: data.scope,
|
|
38
|
+
username: [entityId, data?.email].filter(Boolean).join('#'),
|
|
39
|
+
entityUser: (0, entity_user_interface_1.keycloakAccessTokenToEntityUserInterface)(data),
|
|
40
|
+
targets: getTargets(data, permissions),
|
|
41
|
+
};
|
|
42
|
+
debug('[INFO] UserInterface = %s', JSON.stringify(result));
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
function getEntityAttributeByClient(data, client, attribute) {
|
|
46
|
+
// Presence, not mapping, decides: an entity-format token has a mapped claim
|
|
47
|
+
// but no value for it, and must still reach the tiers below.
|
|
48
|
+
const rootClaim = ROOT_CLAIM_BY_ATTRIBUTE[attribute];
|
|
49
|
+
const rootValue = rootClaim ? (0, get_1.default)(data, rootClaim) : undefined;
|
|
50
|
+
if (rootValue !== undefined)
|
|
51
|
+
return rootValue;
|
|
52
|
+
if (data?.entity)
|
|
53
|
+
return (0, get_1.default)(data.entity, attribute);
|
|
54
|
+
const entities = data?.entities || [];
|
|
55
|
+
const entityData = entities.find(i => i.entityId === client.replace('-client', '')) ??
|
|
56
|
+
entities.find(i => i.entityId === 'edirect');
|
|
57
|
+
return (0, get_1.default)(entityData, attribute);
|
|
58
|
+
}
|
|
59
|
+
function getRoles(data, client) {
|
|
60
|
+
const filterRoles = (roles) => roles.filter((i) => i.includes('ROLE_')) || [];
|
|
61
|
+
const roles = filterRoles(getRealmAccessRoles(data)).concat(filterRoles(getResourceAccessRoles(data, client)));
|
|
62
|
+
return [...new Set(roles)];
|
|
63
|
+
}
|
|
64
|
+
function getPermissions(data, client) {
|
|
65
|
+
const filterRoles = (roles) => roles.filter((i) => !i.includes('ROLE_')) || [];
|
|
66
|
+
const permissions = filterRoles(getRealmAccessRoles(data)).concat(filterRoles(getResourceAccessRoles(data, client)));
|
|
67
|
+
return [...new Set(permissions)];
|
|
68
|
+
}
|
|
69
|
+
function getRealmAccessRoles(data) {
|
|
70
|
+
return data?.realm_access?.roles || [];
|
|
71
|
+
}
|
|
72
|
+
function getResourceAccessRoles(data, client) {
|
|
73
|
+
return (0, get_1.default)(data?.resource_access || {}, client)?.roles || [];
|
|
74
|
+
}
|
|
75
|
+
function getTargets(data, permissions) {
|
|
76
|
+
return [flattenTargets(data?.targets)].map((target) => ({
|
|
77
|
+
permissions,
|
|
78
|
+
scopes: target?.scopes || { entities: [], instances: [] },
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
function flattenTargets(targets = []) {
|
|
82
|
+
return targets.reduce((previous, current) => {
|
|
83
|
+
const currentEntities = current?.scopes?.entities || [];
|
|
84
|
+
const scopesEntities = previous.scopes.entities.concat(currentEntities);
|
|
85
|
+
previous.scopes.entities = scopesEntities;
|
|
86
|
+
const currentInstances = current?.scopes?.instances || [];
|
|
87
|
+
const scopesInstances = previous.scopes.instances.concat(currentInstances);
|
|
88
|
+
previous.scopes.instances = scopesInstances;
|
|
89
|
+
return previous;
|
|
90
|
+
}, { permissions: [], scopes: { entities: [], instances: [] } });
|
|
91
|
+
}
|
package/src/plugin.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest } from 'fastify';
|
|
2
|
+
import { type ConfigSource } from './config';
|
|
3
|
+
import type { OAuth2ParamsInterface } from './interfaces';
|
|
4
|
+
import { type AuthCache } from './providers/memory-cache';
|
|
5
|
+
import './types';
|
|
6
|
+
/**
|
|
7
|
+
* Selects a request by path. A string matches exactly, or as a prefix when it
|
|
8
|
+
* ends with `*`. A function receives the request.
|
|
9
|
+
*/
|
|
10
|
+
export type RouteMatcher = string | RegExp | ((request: FastifyRequest) => boolean);
|
|
11
|
+
export interface AuthFastifyOptions {
|
|
12
|
+
/**
|
|
13
|
+
* - `auto` (default): provider is picked from the token claims and
|
|
14
|
+
* `KEYCLOAK_*` / `AUTH_SERVICE_JWKS` are read from the config, with the
|
|
15
|
+
* per-realm override. Same as `AuthMiddleware` + `AuthGuard`.
|
|
16
|
+
* - `keycloak`: always Keycloak, with `keycloak` params. Same as
|
|
17
|
+
* `KeycloakAuthMiddleware` + `KeycloakAuthGuard`.
|
|
18
|
+
* - `auth-service`: always the Auth Service, with `authService` params. Same
|
|
19
|
+
* as `AuthServiceAuthMiddleware` + `AuthServiceAuthGuard`.
|
|
20
|
+
*/
|
|
21
|
+
mode?: 'auto' | 'keycloak' | 'auth-service';
|
|
22
|
+
keycloak?: OAuth2ParamsInterface;
|
|
23
|
+
/** Only `baseUrl` is used; the JWKS is read from `<baseUrl>/oidc/jwks`. */
|
|
24
|
+
authService?: Pick<OAuth2ParamsInterface, 'baseUrl'>;
|
|
25
|
+
/** Defaults to `process.env`. Accepts the `ConfigService` of `@edirect/config`. */
|
|
26
|
+
config?: ConfigSource;
|
|
27
|
+
/** Requests that skip authentication and authorization. */
|
|
28
|
+
exclude?: RouteMatcher[];
|
|
29
|
+
/**
|
|
30
|
+
* Exchanges external Keycloak tokens for internal ones before
|
|
31
|
+
* authentication.
|
|
32
|
+
*/
|
|
33
|
+
tokenExchange?: {
|
|
34
|
+
params: OAuth2ParamsInterface;
|
|
35
|
+
/** Requests the exchange applies to. Defaults to every request. */
|
|
36
|
+
paths?: RouteMatcher[];
|
|
37
|
+
/** Defaults to an in-memory cache. */
|
|
38
|
+
cache?: AuthCache;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
declare function authFastify(fastify: FastifyInstance, options: AuthFastifyOptions): Promise<void>;
|
|
42
|
+
export declare const authFastifyPlugin: typeof authFastify;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAgB,cAAc,EAAE,MAAM,SAAS,CAAC;AAG7E,OAAO,EAAoB,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAS/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAE1D,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAE3E,OAAO,SAAS,CAAC;AAEjB;;;GAGG;AACH,MAAM,MAAM,YAAY,GACtB,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC;AAE3D,MAAM,WAAW,kBAAkB;IACjC;;;;;;;;OAQG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,cAAc,CAAC;IAC5C,QAAQ,CAAC,EAAE,qBAAqB,CAAC;IACjC,2EAA2E;IAC3E,WAAW,CAAC,EAAE,IAAI,CAAC,qBAAqB,EAAE,SAAS,CAAC,CAAC;IACrD,mFAAmF;IACnF,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,2DAA2D;IAC3D,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;IACzB;;;OAGG;IACH,aAAa,CAAC,EAAE;QACd,MAAM,EAAE,qBAAqB,CAAC;QAC9B,mEAAmE;QACnE,KAAK,CAAC,EAAE,YAAY,EAAE,CAAC;QACvB,sCAAsC;QACtC,KAAK,CAAC,EAAE,SAAS,CAAC;KACnB,CAAC;CACH;AAkBD,iBAAe,WAAW,CACxB,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,kBAAkB,iBAmF5B;AAED,eAAO,MAAM,iBAAiB,oBAG5B,CAAC"}
|
package/src/plugin.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authFastifyPlugin = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fastify_plugin_1 = tslib_1.__importDefault(require("fastify-plugin"));
|
|
6
|
+
const auth_service_1 = require("./auth.service");
|
|
7
|
+
const config_1 = require("./config");
|
|
8
|
+
const auth_service_2 = require("./hooks/auth-service");
|
|
9
|
+
const auto_1 = require("./hooks/auto");
|
|
10
|
+
const keycloak_1 = require("./hooks/keycloak");
|
|
11
|
+
const token_exchange_1 = require("./hooks/token-exchange");
|
|
12
|
+
const keycloak_token_exchange_service_1 = require("./providers/keycloak-token-exchange.service");
|
|
13
|
+
const memory_cache_1 = require("./providers/memory-cache");
|
|
14
|
+
const well_known_service_1 = require("./providers/well-known.service");
|
|
15
|
+
require("./types");
|
|
16
|
+
const matches = (matchers, request) => {
|
|
17
|
+
const path = request.url.split('?')[0];
|
|
18
|
+
return matchers.some(matcher => {
|
|
19
|
+
if (typeof matcher === 'function')
|
|
20
|
+
return matcher(request);
|
|
21
|
+
if (matcher instanceof RegExp)
|
|
22
|
+
return matcher.test(path);
|
|
23
|
+
return matcher.endsWith('*')
|
|
24
|
+
? path.startsWith(matcher.slice(0, -1))
|
|
25
|
+
: path === matcher;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
async function authFastify(fastify, options) {
|
|
29
|
+
const mode = options.mode ?? 'auto';
|
|
30
|
+
const config = options.config ?? config_1.processEnvConfig;
|
|
31
|
+
const exclude = options.exclude ?? [];
|
|
32
|
+
let authenticate;
|
|
33
|
+
let authorize;
|
|
34
|
+
if (mode === 'keycloak') {
|
|
35
|
+
const params = options.keycloak;
|
|
36
|
+
if (!params) {
|
|
37
|
+
throw new Error('auth-fastify: mode "keycloak" needs the "keycloak" option');
|
|
38
|
+
}
|
|
39
|
+
authenticate = (req, reply) => (0, keycloak_1.keycloakAuthenticate)(req, reply, params);
|
|
40
|
+
authorize = (req, reply) => (0, keycloak_1.keycloakAuthorize)(req, reply, params);
|
|
41
|
+
}
|
|
42
|
+
else if (mode === 'auth-service') {
|
|
43
|
+
if (!options.authService) {
|
|
44
|
+
throw new Error('auth-fastify: mode "auth-service" needs the "authService" option');
|
|
45
|
+
}
|
|
46
|
+
const verifier = new auth_service_2.AuthServiceTokenVerifier(options.authService);
|
|
47
|
+
authenticate = (req, reply) => (0, auth_service_2.authServiceAuthenticate)(req, reply, verifier);
|
|
48
|
+
authorize = auth_service_2.authServiceAuthorize;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
authenticate = (0, auto_1.createAutoAuthenticate)(config);
|
|
52
|
+
authorize = (0, auto_1.createAutoAuthorize)(config);
|
|
53
|
+
}
|
|
54
|
+
let exchange;
|
|
55
|
+
if (options.tokenExchange) {
|
|
56
|
+
const { params, cache = new memory_cache_1.MemoryAuthCache() } = options.tokenExchange;
|
|
57
|
+
const exchangeService = new keycloak_token_exchange_service_1.KeycloakTokenExchangeService(new well_known_service_1.WellKnownService(cache), params);
|
|
58
|
+
exchange = (0, token_exchange_1.createTokenExchange)(exchangeService, cache, params);
|
|
59
|
+
}
|
|
60
|
+
const exchangePaths = options.tokenExchange?.paths;
|
|
61
|
+
const authServices = new WeakMap();
|
|
62
|
+
fastify.decorateRequest('auth', {
|
|
63
|
+
getter() {
|
|
64
|
+
let service = authServices.get(this);
|
|
65
|
+
if (!service) {
|
|
66
|
+
service = new auth_service_1.AuthService(this);
|
|
67
|
+
authServices.set(this, service);
|
|
68
|
+
}
|
|
69
|
+
return service;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
for (const name of [
|
|
73
|
+
'user',
|
|
74
|
+
'externalUser',
|
|
75
|
+
'token',
|
|
76
|
+
'externalToken',
|
|
77
|
+
'tokenProvider',
|
|
78
|
+
]) {
|
|
79
|
+
fastify.decorateRequest(name, null);
|
|
80
|
+
}
|
|
81
|
+
const skip = (request) => request.routeOptions.config?.auth === false || matches(exclude, request);
|
|
82
|
+
// Runs after the body is parsed, like Nest middleware, so `body.token` works.
|
|
83
|
+
fastify.addHook('preValidation', async (request, reply) => {
|
|
84
|
+
if (skip(request))
|
|
85
|
+
return;
|
|
86
|
+
if (exchange && (!exchangePaths || matches(exchangePaths, request))) {
|
|
87
|
+
if (!(await exchange(request, reply)))
|
|
88
|
+
return reply;
|
|
89
|
+
}
|
|
90
|
+
if (!(await authenticate(request, reply)))
|
|
91
|
+
return reply;
|
|
92
|
+
});
|
|
93
|
+
fastify.addHook('preValidation', async (request, reply) => {
|
|
94
|
+
if (skip(request))
|
|
95
|
+
return;
|
|
96
|
+
if (!(await authorize(request, reply)))
|
|
97
|
+
return reply;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
exports.authFastifyPlugin = (0, fastify_plugin_1.default)(authFastify, {
|
|
101
|
+
fastify: '5.x',
|
|
102
|
+
name: '@edirect/auth-fastify',
|
|
103
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { KeycloakResponseAccessToken, OAuth2ParamsInterface } from '../interfaces';
|
|
2
|
+
import type { WellKnownService } from './well-known.service';
|
|
3
|
+
/** Keycloak token exchange (RFC 8693), same operations as `@edirect/auth`. */
|
|
4
|
+
export declare class KeycloakTokenExchangeService {
|
|
5
|
+
private readonly wellKnownService;
|
|
6
|
+
private readonly params;
|
|
7
|
+
private timeout;
|
|
8
|
+
constructor(wellKnownService: WellKnownService, params: OAuth2ParamsInterface);
|
|
9
|
+
isExternalToken(token: string): Promise<boolean>;
|
|
10
|
+
exchangeInternalTokenForInternalToken(internalToken: string, audience?: string): Promise<KeycloakResponseAccessToken>;
|
|
11
|
+
exchangeExternalTokenForInternalToken(externalToken: string, subjectIssuer: string, audience?: string): Promise<KeycloakResponseAccessToken>;
|
|
12
|
+
exchangeInternalTokenForExternalToken(internalToken: string, requestedIssuer: string): Promise<KeycloakResponseAccessToken>;
|
|
13
|
+
private exchange;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=keycloak-token-exchange.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keycloak-token-exchange.service.d.ts","sourceRoot":"","sources":["../../../src/providers/keycloak-token-exchange.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,2BAA2B,EAC3B,qBAAqB,EACtB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAI7D,8EAA8E;AAC9E,qBAAa,4BAA4B;IAIrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAJzB,OAAO,CAAC,OAAO,CAAiD;gBAG7C,gBAAgB,EAAE,gBAAgB,EAClC,MAAM,EAAE,qBAAqB;IAG1C,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQtD,qCAAqC,CACnC,aAAa,EAAE,MAAM,EACrB,QAAQ,CAAC,EAAE,MAAM;IAQnB,qCAAqC,CACnC,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,QAAQ,CAAC,EAAE,MAAM;IASnB,qCAAqC,CACnC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,MAAM;YAQX,QAAQ;CA8BvB"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KeycloakTokenExchangeService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const jwt = tslib_1.__importStar(require("jsonwebtoken"));
|
|
7
|
+
const TOKEN_EXCHANGE_GRANT = 'urn:ietf:params:oauth:grant-type:token-exchange';
|
|
8
|
+
/** Keycloak token exchange (RFC 8693), same operations as `@edirect/auth`. */
|
|
9
|
+
class KeycloakTokenExchangeService {
|
|
10
|
+
wellKnownService;
|
|
11
|
+
params;
|
|
12
|
+
timeout = parseInt(process.env.HTTP_TIMEOUT ?? '30000');
|
|
13
|
+
constructor(wellKnownService, params) {
|
|
14
|
+
this.wellKnownService = wellKnownService;
|
|
15
|
+
this.params = params;
|
|
16
|
+
}
|
|
17
|
+
async isExternalToken(token) {
|
|
18
|
+
const { issuer } = await this.wellKnownService.getWellKnownInfo(this.params);
|
|
19
|
+
const decoded = jwt.decode(token);
|
|
20
|
+
return decoded.iss !== issuer;
|
|
21
|
+
}
|
|
22
|
+
exchangeInternalTokenForInternalToken(internalToken, audience) {
|
|
23
|
+
return this.exchange(internalToken, {
|
|
24
|
+
subject_token: internalToken,
|
|
25
|
+
audience: audience || this.params.clientId,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exchangeExternalTokenForInternalToken(externalToken, subjectIssuer, audience) {
|
|
29
|
+
return this.exchange(externalToken, {
|
|
30
|
+
subject_token: externalToken,
|
|
31
|
+
subject_issuer: subjectIssuer,
|
|
32
|
+
audience: audience || this.params.clientId,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exchangeInternalTokenForExternalToken(internalToken, requestedIssuer) {
|
|
36
|
+
return this.exchange(internalToken, {
|
|
37
|
+
subject_token: internalToken,
|
|
38
|
+
requested_issuer: requestedIssuer,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async exchange(bearer, fields) {
|
|
42
|
+
const { token_endpoint } = await this.wellKnownService.getWellKnownInfo(this.params);
|
|
43
|
+
const body = new URLSearchParams({
|
|
44
|
+
client_id: this.params.clientId,
|
|
45
|
+
client_secret: this.params.clientSecret,
|
|
46
|
+
grant_type: this.params.grantType,
|
|
47
|
+
scope: this.params.scope || 'openid',
|
|
48
|
+
});
|
|
49
|
+
body.set('grant_type', TOKEN_EXCHANGE_GRANT);
|
|
50
|
+
for (const [key, value] of Object.entries(fields))
|
|
51
|
+
body.set(key, value);
|
|
52
|
+
const { data } = await axios_1.default.post(token_endpoint, body, {
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
55
|
+
Authorization: `Bearer ${bearer}`,
|
|
56
|
+
},
|
|
57
|
+
timeout: this.timeout,
|
|
58
|
+
});
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.KeycloakTokenExchangeService = KeycloakTokenExchangeService;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache used for the well-known document and exchanged tokens. Structurally
|
|
3
|
+
* compatible with `cache-manager`, so a shared cache (for example Redis) can be
|
|
4
|
+
* passed in. `ttl` is in milliseconds.
|
|
5
|
+
*/
|
|
6
|
+
export interface AuthCache {
|
|
7
|
+
get<T>(key: string): Promise<T | undefined> | T | undefined;
|
|
8
|
+
set<T>(key: string, value: T, ttl?: number): Promise<unknown> | unknown;
|
|
9
|
+
}
|
|
10
|
+
/** Process-local cache with per-entry expiry. */
|
|
11
|
+
export declare class MemoryAuthCache implements AuthCache {
|
|
12
|
+
private readonly entries;
|
|
13
|
+
get<T>(key: string): T | undefined;
|
|
14
|
+
set<T>(key: string, value: T, ttl?: number): void;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=memory-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-cache.d.ts","sourceRoot":"","sources":["../../../src/providers/memory-cache.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;IAC5D,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CACzE;AAED,iDAAiD;AACjD,qBAAa,eAAgB,YAAW,SAAS;IAC/C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAGpB;IAEJ,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAUlC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,SAAI,GAAG,IAAI;CAM7C"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryAuthCache = void 0;
|
|
4
|
+
/** Process-local cache with per-entry expiry. */
|
|
5
|
+
class MemoryAuthCache {
|
|
6
|
+
entries = new Map();
|
|
7
|
+
get(key) {
|
|
8
|
+
const entry = this.entries.get(key);
|
|
9
|
+
if (!entry)
|
|
10
|
+
return undefined;
|
|
11
|
+
if (entry.expiresAt <= Date.now()) {
|
|
12
|
+
this.entries.delete(key);
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
return entry.value;
|
|
16
|
+
}
|
|
17
|
+
set(key, value, ttl = 0) {
|
|
18
|
+
this.entries.set(key, {
|
|
19
|
+
value,
|
|
20
|
+
expiresAt: ttl > 0 ? Date.now() + ttl : Number.POSITIVE_INFINITY,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.MemoryAuthCache = MemoryAuthCache;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { OAuth2ParamsInterface, WellKnown } from '../interfaces';
|
|
2
|
+
import type { AuthCache } from './memory-cache';
|
|
3
|
+
/**
|
|
4
|
+
* Reads the OpenID well-known document, cached for one day. The host of
|
|
5
|
+
* `issuer`, `token_endpoint` and `jwks_uri` is replaced by the configured
|
|
6
|
+
* `baseUrl`, as `@edirect/auth` does.
|
|
7
|
+
*/
|
|
8
|
+
export declare class WellKnownService {
|
|
9
|
+
private readonly cache;
|
|
10
|
+
private defaultTimeout;
|
|
11
|
+
constructor(cache: AuthCache);
|
|
12
|
+
getWellKnownInfo(params: Pick<OAuth2ParamsInterface, 'baseUrl' | 'wellKnownPath'>, options?: {
|
|
13
|
+
timeout?: number;
|
|
14
|
+
skipCache?: boolean;
|
|
15
|
+
}): Promise<WellKnown>;
|
|
16
|
+
private rebase;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=well-known.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"well-known.service.d.ts","sourceRoot":"","sources":["../../../src/providers/well-known.service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAIhD;;;;GAIG;AACH,qBAAa,gBAAgB;IAGf,OAAO,CAAC,QAAQ,CAAC,KAAK;IAFlC,OAAO,CAAC,cAAc,CAAiD;gBAE1C,KAAK,EAAE,SAAS;IAEvC,gBAAgB,CACpB,MAAM,EAAE,IAAI,CAAC,qBAAqB,EAAE,SAAS,GAAG,eAAe,CAAC,EAChE,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAO,GACtD,OAAO,CAAC,SAAS,CAAC;IAmBrB,OAAO,CAAC,MAAM;CAOf"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WellKnownService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Reads the OpenID well-known document, cached for one day. The host of
|
|
9
|
+
* `issuer`, `token_endpoint` and `jwks_uri` is replaced by the configured
|
|
10
|
+
* `baseUrl`, as `@edirect/auth` does.
|
|
11
|
+
*/
|
|
12
|
+
class WellKnownService {
|
|
13
|
+
cache;
|
|
14
|
+
defaultTimeout = parseInt(process.env.HTTP_TIMEOUT ?? '30000');
|
|
15
|
+
constructor(cache) {
|
|
16
|
+
this.cache = cache;
|
|
17
|
+
}
|
|
18
|
+
async getWellKnownInfo(params, options = {}) {
|
|
19
|
+
const url = new URL(params.baseUrl);
|
|
20
|
+
url.pathname = params.wellKnownPath;
|
|
21
|
+
const cached = await this.cache.get(url.href);
|
|
22
|
+
if (cached && !options.skipCache)
|
|
23
|
+
return cached;
|
|
24
|
+
const { data } = await axios_1.default.get(url.href, {
|
|
25
|
+
timeout: options.timeout ?? this.defaultTimeout,
|
|
26
|
+
});
|
|
27
|
+
data.issuer = this.rebase(data.issuer, url);
|
|
28
|
+
data.token_endpoint = this.rebase(data.token_endpoint, url);
|
|
29
|
+
data.jwks_uri = this.rebase(data.jwks_uri, url);
|
|
30
|
+
await this.cache.set(url.href, data, ONE_DAY_MS);
|
|
31
|
+
return data;
|
|
32
|
+
}
|
|
33
|
+
rebase(value, base) {
|
|
34
|
+
const target = new URL(value);
|
|
35
|
+
target.protocol = base.protocol;
|
|
36
|
+
target.host = base.host;
|
|
37
|
+
target.port = base.port;
|
|
38
|
+
return target.href;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.WellKnownService = WellKnownService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realm-env-key.d.ts","sourceRoot":"","sources":["../../src/realm-env-key.ts"],"names":[],"mappings":"AAKA,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,UAWxD;AAED,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAOjE"}
|