quankee-framework-keycloak 1.0.1 → 1.3.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/fesm2022/quankee-framework-keycloak.mjs +96 -0
- package/fesm2022/quankee-framework-keycloak.mjs.map +1 -0
- package/index.d.ts +5 -0
- package/lib/core/authenticated-user.d.ts +6 -0
- package/lib/core/keycloak-token.d.ts +25 -0
- package/lib/core/keycloak.service.d.ts +34 -0
- package/package.json +40 -37
- package/public-api.d.ts +3 -0
- package/ng-package.json +0 -7
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { Injectable } from '@angular/core';
|
|
3
|
+
import Keycloak from 'keycloak-js';
|
|
4
|
+
|
|
5
|
+
class KeycloakService {
|
|
6
|
+
keycloak;
|
|
7
|
+
initialized = false;
|
|
8
|
+
constructor() { }
|
|
9
|
+
async init(config) {
|
|
10
|
+
this.keycloak = new Keycloak({
|
|
11
|
+
url: config.url,
|
|
12
|
+
realm: config.realm,
|
|
13
|
+
clientId: config.clientId,
|
|
14
|
+
});
|
|
15
|
+
const initOptions = {
|
|
16
|
+
onLoad: config.onLoad ?? "login-required",
|
|
17
|
+
checkLoginIframe: config.checkLoginIframe ?? false,
|
|
18
|
+
silentCheckSsoRedirectUri: config.silentCheckSsoRedirectUri,
|
|
19
|
+
pkceMethod: config.pkceMethod ?? "S256",
|
|
20
|
+
flow: config.flow ?? "standard",
|
|
21
|
+
};
|
|
22
|
+
try {
|
|
23
|
+
const authenticated = await this.keycloak.init(initOptions);
|
|
24
|
+
this.initialized = true;
|
|
25
|
+
return authenticated;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error("Keycloak initialization failed", error);
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
login() {
|
|
33
|
+
this.keycloak?.login();
|
|
34
|
+
}
|
|
35
|
+
logout(redirectUri) {
|
|
36
|
+
this.keycloak?.logout({ redirectUri });
|
|
37
|
+
}
|
|
38
|
+
accountManagement() {
|
|
39
|
+
this.keycloak?.accountManagement();
|
|
40
|
+
}
|
|
41
|
+
getToken() {
|
|
42
|
+
return this.keycloak?.token;
|
|
43
|
+
}
|
|
44
|
+
getParsedToken() {
|
|
45
|
+
return this.keycloak?.tokenParsed;
|
|
46
|
+
}
|
|
47
|
+
async refreshToken(minValidity = 5) {
|
|
48
|
+
if (!this.keycloak)
|
|
49
|
+
return false;
|
|
50
|
+
try {
|
|
51
|
+
return await this.keycloak.updateToken(minValidity);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
console.error("Token refresh failed", e);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getUsername() {
|
|
59
|
+
return this.getParsedToken()?.preferred_username;
|
|
60
|
+
}
|
|
61
|
+
isLoggedIn() {
|
|
62
|
+
return !!this.keycloak?.authenticated;
|
|
63
|
+
}
|
|
64
|
+
async getUser() {
|
|
65
|
+
if (!this.keycloak?.token || !this.keycloak.tokenParsed)
|
|
66
|
+
return;
|
|
67
|
+
const profile = await this.keycloak.loadUserProfile();
|
|
68
|
+
return {
|
|
69
|
+
token: this.keycloak.token,
|
|
70
|
+
parsedToken: this.keycloak.tokenParsed,
|
|
71
|
+
profile,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
getKeycloak() {
|
|
75
|
+
return this.keycloak;
|
|
76
|
+
}
|
|
77
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: KeycloakService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
78
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: KeycloakService, providedIn: 'root' });
|
|
79
|
+
}
|
|
80
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: KeycloakService, decorators: [{
|
|
81
|
+
type: Injectable,
|
|
82
|
+
args: [{
|
|
83
|
+
providedIn: 'root',
|
|
84
|
+
}]
|
|
85
|
+
}], ctorParameters: () => [] });
|
|
86
|
+
|
|
87
|
+
/*
|
|
88
|
+
* Public API Surface of quankee-framework-keycloak
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Generated bundle index. Do not edit.
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
export { KeycloakService };
|
|
96
|
+
//# sourceMappingURL=quankee-framework-keycloak.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quankee-framework-keycloak.mjs","sources":["../../../projects/quankee-framework-keycloak/src/lib/core/keycloak.service.ts","../../../projects/quankee-framework-keycloak/src/public-api.ts","../../../projects/quankee-framework-keycloak/src/quankee-framework-keycloak.ts"],"sourcesContent":["import {Injectable} from '@angular/core';\r\nimport Keycloak from 'keycloak-js';\r\nimport {AuthenticatedUser} from './authenticated-user';\r\nimport {KeycloakToken} from './keycloak-token';\r\n\r\n// Your own type definition for 'onLoad' because keycloak-js doesn't export it\r\nexport type AppKeycloakOnLoad = \"login-required\" | \"check-sso\";\r\n\r\nexport type KeycloakPkceMethod = \"S256\";\r\n\r\n// Your own config interface\r\nexport interface KeycloakConfig {\r\n url: string;\r\n realm: string;\r\n clientId: string;\r\n onLoad?: AppKeycloakOnLoad;\r\n checkLoginIframe?: boolean;\r\n silentCheckSsoRedirectUri?: string;\r\n pkceMethod?: KeycloakPkceMethod;\r\n flow?: \"standard\" | \"implicit\";\r\n}\r\n\r\n\r\n@Injectable({\r\n providedIn: 'root',\r\n})\r\nexport class KeycloakService{\r\n\r\n private keycloak?: Keycloak;\r\n\r\n private initialized = false;\r\n\r\n constructor() {}\r\n\r\n async init(config: KeycloakConfig): Promise<boolean> {\r\n this.keycloak = new Keycloak({\r\n url: config.url,\r\n realm: config.realm,\r\n clientId: config.clientId,\r\n });\r\n\r\n const initOptions: {\r\n onLoad: \"login-required\" | \"check-sso\";\r\n checkLoginIframe: boolean;\r\n silentCheckSsoRedirectUri?: string;\r\n pkceMethod: KeycloakPkceMethod;\r\n flow: \"standard\" | \"implicit\";\r\n } = {\r\n onLoad: config.onLoad ?? \"login-required\",\r\n checkLoginIframe: config.checkLoginIframe ?? false,\r\n silentCheckSsoRedirectUri: config.silentCheckSsoRedirectUri,\r\n pkceMethod: config.pkceMethod ?? \"S256\",\r\n flow: config.flow ?? \"standard\",\r\n };\r\n\r\n try {\r\n const authenticated = await this.keycloak.init(initOptions);\r\n this.initialized = true;\r\n return authenticated;\r\n } catch (error) {\r\n console.error(\"Keycloak initialization failed\", error);\r\n return false;\r\n }\r\n\r\n }\r\n\r\n login(): void {\r\n this.keycloak?.login();\r\n }\r\n\r\n logout(redirectUri?: string): void {\r\n this.keycloak?.logout({ redirectUri });\r\n }\r\n\r\n accountManagement(): void {\r\n this.keycloak?.accountManagement();\r\n }\r\n\r\n getToken(): string | undefined {\r\n return this.keycloak?.token;\r\n }\r\n\r\n getParsedToken(): KeycloakToken | undefined {\r\n return this.keycloak?.tokenParsed as KeycloakToken;\r\n }\r\n\r\n async refreshToken(minValidity = 5): Promise<boolean> {\r\n if (!this.keycloak) return false;\r\n try {\r\n return await this.keycloak.updateToken(minValidity);\r\n } catch (e) {\r\n console.error(\"Token refresh failed\", e);\r\n return false;\r\n }\r\n }\r\n\r\n getUsername(): string | undefined {\r\n return this.getParsedToken()?.preferred_username;\r\n }\r\n\r\n isLoggedIn(): boolean {\r\n return !!this.keycloak?.authenticated;\r\n }\r\n\r\n async getUser(): Promise<AuthenticatedUser | undefined> {\r\n if (!this.keycloak?.token || !this.keycloak.tokenParsed) return;\r\n\r\n const profile = await this.keycloak.loadUserProfile();\r\n return {\r\n token: this.keycloak.token,\r\n parsedToken: this.keycloak.tokenParsed as KeycloakToken,\r\n profile,\r\n };\r\n }\r\n\r\n getKeycloak(): Keycloak | undefined {\r\n return this.keycloak;\r\n }\r\n\r\n\r\n}\r\n","/*\r\n * Public API Surface of quankee-framework-keycloak\r\n */\r\n\r\nexport * from './lib/core/keycloak-token';\r\nexport * from './lib/core/authenticated-user';\r\nexport * from './lib/core/keycloak.service';\r\n\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MA0Ba,eAAe,CAAA;AAEjB,IAAA,QAAQ;IAET,WAAW,GAAG,KAAK;AAE3B,IAAA,WAAA,GAAA;IAEA,MAAM,IAAI,CAAC,MAAsB,EAAA;AAC/B,QAAA,IAAI,CAAC,QAAQ,GAAG,IAAI,QAAQ,CAAC;YAC3B,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,QAAQ,EAAE,MAAM,CAAC,QAAQ;AAC1B,SAAA,CAAC;AAEF,QAAA,MAAM,WAAW,GAMb;AACF,YAAA,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,gBAAgB;AACzC,YAAA,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,KAAK;YAClD,yBAAyB,EAAE,MAAM,CAAC,yBAAyB;AAC3D,YAAA,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,MAAM;AACvC,YAAA,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,UAAU;SAChC;AAED,QAAA,IAAI;YACF,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC;AAC3D,YAAA,IAAI,CAAC,WAAW,GAAG,IAAI;AACvB,YAAA,OAAO,aAAa;;QACpB,OAAO,KAAK,EAAE;AACd,YAAA,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC;AACtD,YAAA,OAAO,KAAK;;;IAKhB,KAAK,GAAA;AACH,QAAA,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE;;AAGxB,IAAA,MAAM,CAAC,WAAoB,EAAA;QACzB,IAAI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AA;AAAf,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,eAAe,cAFd,MAAM,EAAA,CAAA;;4FAEP,eAAe,EAAA,UAAA,EAAA,CAAA;kBAH3B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACzBD;;AAEG;;ACFH;;AAEG;;;;"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface KeycloakToken {
|
|
2
|
+
exp: number;
|
|
3
|
+
iat: number;
|
|
4
|
+
jti: string;
|
|
5
|
+
iss: string;
|
|
6
|
+
sub: string;
|
|
7
|
+
typ: string;
|
|
8
|
+
azp?: string;
|
|
9
|
+
session_state?: string;
|
|
10
|
+
acr?: string;
|
|
11
|
+
realm_access?: {
|
|
12
|
+
roles: string[];
|
|
13
|
+
};
|
|
14
|
+
resource_access?: {
|
|
15
|
+
[clientId: string]: {
|
|
16
|
+
roles: string[];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
scope?: string;
|
|
20
|
+
email?: string;
|
|
21
|
+
preferred_username?: string;
|
|
22
|
+
given_name?: string;
|
|
23
|
+
family_name?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import Keycloak from 'keycloak-js';
|
|
2
|
+
import { AuthenticatedUser } from './authenticated-user';
|
|
3
|
+
import { KeycloakToken } from './keycloak-token';
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
export type AppKeycloakOnLoad = "login-required" | "check-sso";
|
|
6
|
+
export type KeycloakPkceMethod = "S256";
|
|
7
|
+
export interface KeycloakConfig {
|
|
8
|
+
url: string;
|
|
9
|
+
realm: string;
|
|
10
|
+
clientId: string;
|
|
11
|
+
onLoad?: AppKeycloakOnLoad;
|
|
12
|
+
checkLoginIframe?: boolean;
|
|
13
|
+
silentCheckSsoRedirectUri?: string;
|
|
14
|
+
pkceMethod?: KeycloakPkceMethod;
|
|
15
|
+
flow?: "standard" | "implicit";
|
|
16
|
+
}
|
|
17
|
+
export declare class KeycloakService {
|
|
18
|
+
private keycloak?;
|
|
19
|
+
private initialized;
|
|
20
|
+
constructor();
|
|
21
|
+
init(config: KeycloakConfig): Promise<boolean>;
|
|
22
|
+
login(): void;
|
|
23
|
+
logout(redirectUri?: string): void;
|
|
24
|
+
accountManagement(): void;
|
|
25
|
+
getToken(): string | undefined;
|
|
26
|
+
getParsedToken(): KeycloakToken | undefined;
|
|
27
|
+
refreshToken(minValidity?: number): Promise<boolean>;
|
|
28
|
+
getUsername(): string | undefined;
|
|
29
|
+
isLoggedIn(): boolean;
|
|
30
|
+
getUser(): Promise<AuthenticatedUser | undefined>;
|
|
31
|
+
getKeycloak(): Keycloak | undefined;
|
|
32
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<KeycloakService, never>;
|
|
33
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<KeycloakService>;
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,37 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "quankee-framework-keycloak",
|
|
3
|
-
"description": "Quankee Framework Keycloak Lib",
|
|
4
|
-
"version": "1.0
|
|
5
|
-
"author": {
|
|
6
|
-
"email": "info@quankee.co.mz",
|
|
7
|
-
"name": "Quankee Software, Lda",
|
|
8
|
-
"license": "MIT",
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "https://gitlab.com/quankee-framework/quankee-framework-common-ts"
|
|
12
|
-
},
|
|
13
|
-
"homepage": "https://gitlab.com/quankee-framework/quankee-framework-common-ts#readme",
|
|
14
|
-
"publishConfig": {
|
|
15
|
-
"registry": "https://registry.npmjs.org/"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
"peerDependencies": {
|
|
19
|
-
"@angular/common": "^19.2.0",
|
|
20
|
-
"@angular/core": "^19.2.0",
|
|
21
|
-
"keycloak-angular": "20.0.0",
|
|
22
|
-
"keycloak-js": "26.2.0",
|
|
23
|
-
"jwt-decode": "4.0.0"
|
|
24
|
-
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "quankee-framework-keycloak",
|
|
3
|
+
"description": "Quankee Framework Keycloak Lib",
|
|
4
|
+
"version": "1.3.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"email": "info@quankee.co.mz",
|
|
7
|
+
"name": "Quankee Software, Lda",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://gitlab.com/quankee-framework/quankee-framework-common-ts"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://gitlab.com/quankee-framework/quankee-framework-common-ts#readme",
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"registry": "https://registry.npmjs.org/"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"@angular/common": "^19.2.0",
|
|
20
|
+
"@angular/core": "^19.2.0",
|
|
21
|
+
"keycloak-angular": "20.0.0",
|
|
22
|
+
"keycloak-js": "26.2.0",
|
|
23
|
+
"jwt-decode": "4.0.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"tslib": "^2.3.0"
|
|
27
|
+
},
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"module": "fesm2022/quankee-framework-keycloak.mjs",
|
|
30
|
+
"typings": "index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
"./package.json": {
|
|
33
|
+
"default": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./index.d.ts",
|
|
37
|
+
"default": "./fesm2022/quankee-framework-keycloak.mjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/public-api.d.ts
ADDED