@open-pioneer/authentication-keycloak 0.1.2 → 0.2.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/CHANGELOG.md +21 -0
- package/KeycloakAuthPlugin.js +25 -16
- package/KeycloakAuthPlugin.js.map +1 -1
- package/README.md +10 -10
- package/package.json +7 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @open-pioneer/authentication-keycloak
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f48bb02: Update Keycloak JavaScript adapter to v25.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 5c62522: Update wording and harmonize structure
|
|
12
|
+
- e0b2fae: Update dependencies
|
|
13
|
+
- f48bb02: Refactor: use reactivity API internally to maintain the current state.
|
|
14
|
+
- Updated dependencies [5c62522]
|
|
15
|
+
- Updated dependencies [58ce24f]
|
|
16
|
+
- Updated dependencies [50550d3]
|
|
17
|
+
- Updated dependencies [e0b2fae]
|
|
18
|
+
- Updated dependencies [6cc7fcd]
|
|
19
|
+
- @open-pioneer/authentication@0.3.6
|
|
20
|
+
- @open-pioneer/notifier@0.3.6
|
|
21
|
+
- @open-pioneer/core@1.3.0
|
|
22
|
+
- @open-pioneer/runtime@2.1.7
|
|
23
|
+
|
|
3
24
|
## 0.1.2
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/KeycloakAuthPlugin.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { reactive, watch } from '@conterra/reactivity-core';
|
|
2
|
+
import { createLogger, EventEmitter, destroyResource } from '@open-pioneer/core';
|
|
2
3
|
import Keycloak from 'keycloak-js';
|
|
3
4
|
|
|
4
5
|
const LOG = createLogger("authentication-keycloak:KeycloakAuthPlugin");
|
|
5
6
|
class KeycloakAuthPlugin extends EventEmitter {
|
|
6
|
-
#state = {
|
|
7
|
-
kind: "pending"
|
|
8
|
-
};
|
|
9
7
|
#notifier;
|
|
10
8
|
#intl;
|
|
11
9
|
#keycloakOptions;
|
|
@@ -14,12 +12,22 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
14
12
|
#loginOptions;
|
|
15
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
14
|
#timerId;
|
|
15
|
+
#watcher;
|
|
16
|
+
#state = reactive({
|
|
17
|
+
kind: "pending"
|
|
18
|
+
});
|
|
17
19
|
constructor(options) {
|
|
18
20
|
super();
|
|
19
21
|
this.#notifier = options.references.notifier;
|
|
20
22
|
this.#intl = options.intl;
|
|
21
23
|
this.#logoutOptions = { redirectUri: void 0 };
|
|
22
24
|
this.#loginOptions = { redirectUri: void 0 };
|
|
25
|
+
this.#watcher = watch(
|
|
26
|
+
() => [this.#state.value],
|
|
27
|
+
() => {
|
|
28
|
+
this.emit("changed");
|
|
29
|
+
}
|
|
30
|
+
);
|
|
23
31
|
try {
|
|
24
32
|
this.#keycloakOptions = getKeycloakConfig(options.properties);
|
|
25
33
|
} catch (e) {
|
|
@@ -31,10 +39,9 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
31
39
|
throw new Error("Failed to construct keycloak instance", { cause: e });
|
|
32
40
|
}
|
|
33
41
|
this.#init().catch((e) => {
|
|
34
|
-
this.#
|
|
42
|
+
this.#updateState({
|
|
35
43
|
kind: "pending"
|
|
36
|
-
};
|
|
37
|
-
this.emit("changed");
|
|
44
|
+
});
|
|
38
45
|
this.#notifier.notify({
|
|
39
46
|
level: "error",
|
|
40
47
|
title: this.#intl.formatMessage({
|
|
@@ -49,10 +56,11 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
49
56
|
}
|
|
50
57
|
destroy() {
|
|
51
58
|
clearInterval(this.#timerId);
|
|
59
|
+
this.#watcher = destroyResource(this.#watcher);
|
|
52
60
|
this.#timerId = void 0;
|
|
53
61
|
}
|
|
54
62
|
getAuthState() {
|
|
55
|
-
return this.#state;
|
|
63
|
+
return this.#state.value;
|
|
56
64
|
}
|
|
57
65
|
getLoginBehavior() {
|
|
58
66
|
const doLogin = () => {
|
|
@@ -80,7 +88,7 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
80
88
|
throw new Error("Failed to initialize keycloak session", { cause: error });
|
|
81
89
|
}
|
|
82
90
|
if (isAuthenticated) {
|
|
83
|
-
this.#
|
|
91
|
+
this.#updateState({
|
|
84
92
|
kind: "authenticated",
|
|
85
93
|
sessionInfo: {
|
|
86
94
|
userId: this.#keycloak.subject ? this.#keycloak.subject : "undefined",
|
|
@@ -92,18 +100,16 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
92
100
|
userName: this.#keycloak.idTokenParsed?.preferred_username
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
|
-
};
|
|
96
|
-
this.emit("changed");
|
|
103
|
+
});
|
|
97
104
|
LOG.debug(`User ${this.#keycloak.subject} is authenticated`);
|
|
98
105
|
if (refreshOptions.autoRefresh) {
|
|
99
106
|
LOG.debug("Starting auto-refresh", refreshOptions);
|
|
100
107
|
this.__refresh(refreshOptions.interval, refreshOptions.timeLeft);
|
|
101
108
|
}
|
|
102
109
|
} else {
|
|
103
|
-
this.#
|
|
110
|
+
this.#updateState({
|
|
104
111
|
kind: "not-authenticated"
|
|
105
|
-
};
|
|
106
|
-
this.emit("changed");
|
|
112
|
+
});
|
|
107
113
|
LOG.debug("User is not authenticated");
|
|
108
114
|
}
|
|
109
115
|
}
|
|
@@ -113,14 +119,17 @@ class KeycloakAuthPlugin extends EventEmitter {
|
|
|
113
119
|
this.#timerId = setInterval(() => {
|
|
114
120
|
this.#keycloak.updateToken(timeLeft).catch((e) => {
|
|
115
121
|
LOG.error("Failed to refresh token", e);
|
|
116
|
-
this.#
|
|
122
|
+
this.#updateState({
|
|
117
123
|
kind: "not-authenticated"
|
|
118
|
-
};
|
|
124
|
+
});
|
|
119
125
|
this.emit("changed");
|
|
120
126
|
this.destroy();
|
|
121
127
|
});
|
|
122
128
|
}, interval);
|
|
123
129
|
}
|
|
130
|
+
#updateState(newState) {
|
|
131
|
+
this.#state.value = newState;
|
|
132
|
+
}
|
|
124
133
|
}
|
|
125
134
|
const DEFAULT_AUTO_REFRESH_OPT = {
|
|
126
135
|
autoRefresh: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KeycloakAuthPlugin.js","sources":["KeycloakAuthPlugin.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport {\n AuthPlugin,\n AuthPluginEvents,\n AuthState,\n LoginBehavior\n} from \"@open-pioneer/authentication\";\nimport { EventEmitter, createLogger } from \"@open-pioneer/core\";\nimport { NotificationService } from \"@open-pioneer/notifier\";\nimport {\n Service,\n ServiceOptions,\n type DECLARE_SERVICE_INTERFACE,\n PackageIntl\n} from \"@open-pioneer/runtime\";\nimport Keycloak, {\n type KeycloakConfig,\n type KeycloakInitOptions,\n type KeycloakLoginOptions,\n type KeycloakLogoutOptions\n} from \"keycloak-js\";\nimport { KeycloakOptions, KeycloakProperties, RefreshOptions } from \"./api\";\n\nconst LOG = createLogger(\"authentication-keycloak:KeycloakAuthPlugin\");\n\ninterface References {\n notifier: NotificationService;\n}\n\nexport class KeycloakAuthPlugin\n extends EventEmitter<AuthPluginEvents>\n implements Service, AuthPlugin\n{\n declare [DECLARE_SERVICE_INTERFACE]: \"authentication-keycloak.KeycloakAuthPlugin\";\n\n #state: AuthState = {\n kind: \"pending\"\n };\n #notifier: NotificationService;\n #intl: PackageIntl;\n #keycloakOptions: KeycloakOptions;\n #keycloak: Keycloak;\n #logoutOptions: KeycloakLogoutOptions;\n #loginOptions: KeycloakLoginOptions;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n #timerId: any;\n\n constructor(options: ServiceOptions<References>) {\n super();\n this.#notifier = options.references.notifier;\n this.#intl = options.intl;\n this.#logoutOptions = { redirectUri: undefined };\n this.#loginOptions = { redirectUri: undefined };\n\n try {\n this.#keycloakOptions = getKeycloakConfig(options.properties);\n } catch (e) {\n throw new Error(\"Invalid keycloak configuration\", { cause: e });\n }\n\n try {\n this.#keycloak = new Keycloak(this.#keycloakOptions.keycloakConfig);\n } catch (e) {\n throw new Error(\"Failed to construct keycloak instance\", { cause: e });\n }\n this.#init().catch((e) => {\n // Stay in pending state when an error happens.\n // There is currently no useful way to signal an error using the plugin API,\n // going into 'not-authenticated' could lead to unexpected behavior (e.g. redirect loops).\n // See https://github.com/open-pioneer/trails-core-packages/issues/47\n this.#state = {\n kind: \"pending\"\n };\n this.emit(\"changed\");\n this.#notifier.notify({\n level: \"error\",\n title: this.#intl.formatMessage({\n id: \"loginFailed.title\"\n }),\n message: this.#intl.formatMessage({\n id: \"loginFailed.message\"\n })\n });\n\n LOG.error(\"Failed to check if user is authenticated\", e);\n });\n }\n\n destroy() {\n clearInterval(this.#timerId);\n this.#timerId = undefined;\n }\n\n getAuthState(): AuthState {\n return this.#state;\n }\n\n getLoginBehavior(): LoginBehavior {\n const doLogin = () => {\n LOG.debug(\"Login with options\", this.#loginOptions);\n this.#keycloak.login(this.#loginOptions);\n };\n return {\n kind: \"effect\",\n login: doLogin\n };\n }\n\n logout() {\n LOG.debug(\"Logout with options\", this.#logoutOptions);\n this.#keycloak.logout(this.#logoutOptions);\n }\n\n async #init() {\n const keycloakOptions = this.#keycloakOptions;\n const initOptions = this.#keycloakOptions.keycloakInitOptions;\n const refreshOptions = keycloakOptions.refreshOptions;\n\n let isAuthenticated: boolean;\n try {\n isAuthenticated = await this.#keycloak.init(initOptions);\n } catch (e) {\n // Note: keycloak.init() can also throw an exception, in addition to a rejected promise.\n // It may also just throw a string..\n const error = typeof e === \"string\" ? new Error(e) : e;\n throw new Error(\"Failed to initialize keycloak session\", { cause: error });\n }\n\n if (isAuthenticated) {\n this.#state = {\n kind: \"authenticated\",\n sessionInfo: {\n userId: this.#keycloak.subject ? this.#keycloak.subject : \"undefined\",\n userName: this.#keycloak.idTokenParsed?.preferred_username,\n attributes: {\n keycloak: this.#keycloak,\n familyName: this.#keycloak.idTokenParsed?.family_name,\n givenName: this.#keycloak.idTokenParsed?.given_name,\n userName: this.#keycloak.idTokenParsed?.preferred_username\n }\n }\n };\n this.emit(\"changed\");\n\n LOG.debug(`User ${this.#keycloak.subject} is authenticated`);\n\n if (refreshOptions.autoRefresh) {\n LOG.debug(\"Starting auto-refresh\", refreshOptions);\n this.__refresh(refreshOptions.interval, refreshOptions.timeLeft);\n }\n } else {\n this.#state = {\n kind: \"not-authenticated\"\n };\n this.emit(\"changed\");\n LOG.debug(\"User is not authenticated\");\n }\n }\n\n // Mocked in test\n private __refresh(interval: number, timeLeft: number) {\n clearInterval(this.#timerId);\n this.#timerId = setInterval(() => {\n this.#keycloak.updateToken(timeLeft).catch((e) => {\n LOG.error(\"Failed to refresh token\", e);\n this.#state = {\n kind: \"not-authenticated\"\n };\n this.emit(\"changed\");\n this.destroy();\n });\n }, interval);\n }\n}\n\nconst DEFAULT_AUTO_REFRESH_OPT = {\n autoRefresh: true,\n interval: 6000,\n timeLeft: 70\n};\n\nconst DEFAULT_INIT_OPT = {\n onLoad: \"check-sso\",\n pkceMethod: \"S256\"\n};\n\nexport function getKeycloakConfig(properties: Partial<KeycloakProperties>): KeycloakOptions {\n const { keycloakOptions } = properties;\n\n const { refreshOptions, keycloakInitOptions, keycloakConfig } = keycloakOptions!;\n\n return {\n refreshOptions: { ...getRefreshOpt(refreshOptions) },\n keycloakInitOptions: { ...getInitOpt(keycloakInitOptions) },\n keycloakConfig: { ...getConfigOpt(keycloakConfig) }\n };\n}\n\nfunction getRefreshOpt(autoRefreshOptions: RefreshOptions | undefined): RefreshOptions {\n if (!autoRefreshOptions || isObjectEmpty(autoRefreshOptions)) {\n LOG.warn(\n `The autorefresh options of the Keycloak configuration should be set to ensure automatic refreshes at specified intervals.` +\n ` Defaulting to '${DEFAULT_AUTO_REFRESH_OPT}'.`\n );\n return Object.assign({}, { ...DEFAULT_AUTO_REFRESH_OPT });\n }\n return autoRefreshOptions;\n}\n\nfunction getInitOpt(keycloakInitOptions: KeycloakInitOptions | undefined): KeycloakInitOptions {\n if (!keycloakInitOptions || isObjectEmpty(keycloakInitOptions)) {\n LOG.warn(\n `The Keycloak init options of the keycloak configuration should be set.` +\n ` Defaulting to '${DEFAULT_INIT_OPT}'.`\n );\n return Object.assign({}, { ...DEFAULT_INIT_OPT }) as KeycloakInitOptions;\n }\n return keycloakInitOptions;\n}\n\nfunction getConfigOpt(keycloakConfig: KeycloakConfig | undefined): KeycloakConfig {\n if (!keycloakConfig || isObjectEmpty(keycloakConfig)) {\n throw new Error(\n `KeycloakConfig not found: The Keycloak configuration options are required by the plugin to perform login and logout operations`\n );\n }\n return keycloakConfig;\n}\n\nconst isObjectEmpty = (objectName: unknown) => {\n return objectName && Object.keys(objectName).length === 0 && objectName.constructor === Object;\n};\n"],"names":[],"mappings":";;;AAwBA,MAAM,GAAA,GAAM,aAAa,4CAA4C,CAAA,CAAA;AAM9D,MAAM,2BACD,YAEZ,CAAA;AAAA,EAGI,MAAoB,GAAA;AAAA,IAChB,IAAM,EAAA,SAAA;AAAA,GACV,CAAA;AAAA,EACA,SAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,gBAAA,CAAA;AAAA,EACA,SAAA,CAAA;AAAA,EACA,cAAA,CAAA;AAAA,EACA,aAAA,CAAA;AAAA;AAAA,EAEA,QAAA,CAAA;AAAA,EAEA,YAAY,OAAqC,EAAA;AAC7C,IAAM,KAAA,EAAA,CAAA;AACN,IAAK,IAAA,CAAA,SAAA,GAAY,QAAQ,UAAW,CAAA,QAAA,CAAA;AACpC,IAAA,IAAA,CAAK,QAAQ,OAAQ,CAAA,IAAA,CAAA;AACrB,IAAK,IAAA,CAAA,cAAA,GAAiB,EAAE,WAAA,EAAa,KAAU,CAAA,EAAA,CAA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,kBAAkB,CAAG,EAAA;AAC1D,IAAI,GAAA,CAAA,IAAA;AAAA,MACA,4IACuB,wBAAwB,CAAA,EAAA,CAAA;AAAA,KACnD,CAAA;AACA,IAAA,OAAO,OAAO,MAAO,CAAA,IAAI,EAAE,GAAG,0BAA0B,CAAA,CAAA;AAAA,GAC5D;AACA,EAAO,OAAA,kBAAA,CAAA;AACX,CAAA;AAEA,SAAS,WAAW,mBAA2E,EAAA;AAC3F,EAAA,IAAI,CAAC,mBAAA,IAAuB,aAAc,CAAA,mBAAmB,CAAG,EAAA;AAC5D,IAAI,GAAA,CAAA,IAAA;AAAA,MACA,yFACuB,gBAAgB,CAAA,EAAA,CAAA;AAAA,KAC3C,CAAA;AACA,IAAA,OAAO,OAAO,MAAO,CAAA,IAAI,EAAE,GAAG,kBAAkB,CAAA,CAAA;AAAA,GACpD;AACA,EAAO,OAAA,mBAAA,CAAA;AACX,CAAA;AAEA,SAAS,aAAa,cAA4D,EAAA;AAC9E,EAAA,IAAI,CAAC,cAAA,IAAkB,aAAc,CAAA,cAAc,CAAG,EAAA;AAClD,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,CAAA,8HAAA,CAAA;AAAA,KACJ,CAAA;AAAA,GACJ;AACA,EAAO,OAAA,cAAA,CAAA;AACX,CAAA;AAEA,MAAM,aAAA,GAAgB,CAAC,UAAwB,KAAA;AAC3C,EAAO,OAAA,UAAA,IAAc,OAAO,IAAK,CAAA,UAAU,EAAE,MAAW,KAAA,CAAA,IAAK,WAAW,WAAgB,KAAA,MAAA,CAAA;AAC5F,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"KeycloakAuthPlugin.js","sources":["KeycloakAuthPlugin.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { reactive, watch } from \"@conterra/reactivity-core\";\nimport {\n AuthPlugin,\n AuthPluginEvents,\n AuthState,\n LoginBehavior\n} from \"@open-pioneer/authentication\";\nimport { EventEmitter, Resource, createLogger, destroyResource } from \"@open-pioneer/core\";\nimport { NotificationService } from \"@open-pioneer/notifier\";\nimport {\n PackageIntl,\n Service,\n ServiceOptions,\n type DECLARE_SERVICE_INTERFACE\n} from \"@open-pioneer/runtime\";\nimport Keycloak, {\n type KeycloakConfig,\n type KeycloakInitOptions,\n type KeycloakLoginOptions,\n type KeycloakLogoutOptions\n} from \"keycloak-js\";\nimport { KeycloakOptions, KeycloakProperties, RefreshOptions } from \"./api\";\n\nconst LOG = createLogger(\"authentication-keycloak:KeycloakAuthPlugin\");\n\ninterface References {\n notifier: NotificationService;\n}\n\nexport class KeycloakAuthPlugin\n extends EventEmitter<AuthPluginEvents>\n implements Service, AuthPlugin\n{\n declare [DECLARE_SERVICE_INTERFACE]: \"authentication-keycloak.KeycloakAuthPlugin\";\n\n #notifier: NotificationService;\n #intl: PackageIntl;\n #keycloakOptions: KeycloakOptions;\n #keycloak: Keycloak;\n #logoutOptions: KeycloakLogoutOptions;\n #loginOptions: KeycloakLoginOptions;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n #timerId: any;\n #watcher: Resource | undefined;\n\n #state = reactive<AuthState>({\n kind: \"pending\"\n });\n\n constructor(options: ServiceOptions<References>) {\n super();\n this.#notifier = options.references.notifier;\n this.#intl = options.intl;\n this.#logoutOptions = { redirectUri: undefined };\n this.#loginOptions = { redirectUri: undefined };\n\n // Backwards compatibility: emit \"changed\" event when the state changes\n this.#watcher = watch(\n () => [this.#state.value],\n () => {\n this.emit(\"changed\");\n }\n );\n\n try {\n this.#keycloakOptions = getKeycloakConfig(options.properties);\n } catch (e) {\n throw new Error(\"Invalid keycloak configuration\", { cause: e });\n }\n\n try {\n this.#keycloak = new Keycloak(this.#keycloakOptions.keycloakConfig);\n } catch (e) {\n throw new Error(\"Failed to construct keycloak instance\", { cause: e });\n }\n this.#init().catch((e) => {\n // TODO: Handle error\n //\n // Stay in pending state when an error happens.\n // There is currently no useful way to signal an error using the plugin API,\n // going into 'not-authenticated' could lead to unexpected behavior (e.g. redirect loops).\n // See https://github.com/open-pioneer/trails-core-packages/issues/47\n this.#updateState({\n kind: \"pending\"\n });\n this.#notifier.notify({\n level: \"error\",\n title: this.#intl.formatMessage({\n id: \"loginFailed.title\"\n }),\n message: this.#intl.formatMessage({\n id: \"loginFailed.message\"\n })\n });\n\n LOG.error(\"Failed to check if user is authenticated\", e);\n });\n }\n\n destroy() {\n clearInterval(this.#timerId);\n this.#watcher = destroyResource(this.#watcher);\n this.#timerId = undefined;\n }\n\n getAuthState(): AuthState {\n return this.#state.value;\n }\n\n getLoginBehavior(): LoginBehavior {\n const doLogin = () => {\n LOG.debug(\"Login with options\", this.#loginOptions);\n this.#keycloak.login(this.#loginOptions);\n };\n return {\n kind: \"effect\",\n login: doLogin\n };\n }\n\n logout() {\n LOG.debug(\"Logout with options\", this.#logoutOptions);\n this.#keycloak.logout(this.#logoutOptions);\n }\n\n async #init() {\n const keycloakOptions = this.#keycloakOptions;\n const initOptions = this.#keycloakOptions.keycloakInitOptions;\n const refreshOptions = keycloakOptions.refreshOptions;\n\n let isAuthenticated: boolean;\n try {\n isAuthenticated = await this.#keycloak.init(initOptions);\n } catch (e) {\n // Note: keycloak.init() can also throw an exception, in addition to a rejected promise.\n // It may also just throw a string..\n const error = typeof e === \"string\" ? new Error(e) : e;\n throw new Error(\"Failed to initialize keycloak session\", { cause: error });\n }\n\n if (isAuthenticated) {\n this.#updateState({\n kind: \"authenticated\",\n sessionInfo: {\n userId: this.#keycloak.subject ? this.#keycloak.subject : \"undefined\",\n userName: this.#keycloak.idTokenParsed?.preferred_username,\n attributes: {\n keycloak: this.#keycloak,\n familyName: this.#keycloak.idTokenParsed?.family_name,\n givenName: this.#keycloak.idTokenParsed?.given_name,\n userName: this.#keycloak.idTokenParsed?.preferred_username\n }\n }\n });\n\n LOG.debug(`User ${this.#keycloak.subject} is authenticated`);\n\n if (refreshOptions.autoRefresh) {\n LOG.debug(\"Starting auto-refresh\", refreshOptions);\n this.__refresh(refreshOptions.interval, refreshOptions.timeLeft);\n }\n } else {\n this.#updateState({\n kind: \"not-authenticated\"\n });\n LOG.debug(\"User is not authenticated\");\n }\n }\n\n // Mocked in test\n private __refresh(interval: number, timeLeft: number) {\n clearInterval(this.#timerId);\n this.#timerId = setInterval(() => {\n this.#keycloak.updateToken(timeLeft).catch((e) => {\n LOG.error(\"Failed to refresh token\", e);\n this.#updateState({\n kind: \"not-authenticated\"\n });\n this.emit(\"changed\");\n this.destroy();\n });\n }, interval);\n }\n\n #updateState(newState: AuthState) {\n this.#state.value = newState;\n }\n}\n\nconst DEFAULT_AUTO_REFRESH_OPT = {\n autoRefresh: true,\n interval: 6000,\n timeLeft: 70\n};\n\nconst DEFAULT_INIT_OPT = {\n onLoad: \"check-sso\",\n pkceMethod: \"S256\"\n};\n\nexport function getKeycloakConfig(properties: Partial<KeycloakProperties>): KeycloakOptions {\n const { keycloakOptions } = properties;\n\n const { refreshOptions, keycloakInitOptions, keycloakConfig } = keycloakOptions!;\n\n return {\n refreshOptions: { ...getRefreshOpt(refreshOptions) },\n keycloakInitOptions: { ...getInitOpt(keycloakInitOptions) },\n keycloakConfig: { ...getConfigOpt(keycloakConfig) }\n };\n}\n\nfunction getRefreshOpt(autoRefreshOptions: RefreshOptions | undefined): RefreshOptions {\n if (!autoRefreshOptions || isObjectEmpty(autoRefreshOptions)) {\n LOG.warn(\n `The autorefresh options of the Keycloak configuration should be set to ensure automatic refreshes at specified intervals.` +\n ` Defaulting to '${DEFAULT_AUTO_REFRESH_OPT}'.`\n );\n return Object.assign({}, { ...DEFAULT_AUTO_REFRESH_OPT });\n }\n return autoRefreshOptions;\n}\n\nfunction getInitOpt(keycloakInitOptions: KeycloakInitOptions | undefined): KeycloakInitOptions {\n if (!keycloakInitOptions || isObjectEmpty(keycloakInitOptions)) {\n LOG.warn(\n `The Keycloak init options of the keycloak configuration should be set.` +\n ` Defaulting to '${DEFAULT_INIT_OPT}'.`\n );\n return Object.assign({}, { ...DEFAULT_INIT_OPT }) as KeycloakInitOptions;\n }\n return keycloakInitOptions;\n}\n\nfunction getConfigOpt(keycloakConfig: KeycloakConfig | undefined): KeycloakConfig {\n if (!keycloakConfig || isObjectEmpty(keycloakConfig)) {\n throw new Error(\n `KeycloakConfig not found: The Keycloak configuration options are required by the plugin to perform login and logout operations`\n );\n }\n return keycloakConfig;\n}\n\nconst isObjectEmpty = (objectName: unknown) => {\n return objectName && Object.keys(objectName).length === 0 && objectName.constructor === Object;\n};\n"],"names":[],"mappings":";;;;AAyBA,MAAM,GAAA,GAAM,aAAa,4CAA4C,CAAA,CAAA;AAM9D,MAAM,2BACD,YAEZ,CAAA;AAAA,EAGI,SAAA,CAAA;AAAA,EACA,KAAA,CAAA;AAAA,EACA,gBAAA,CAAA;AAAA,EACA,SAAA,CAAA;AAAA,EACA,cAAA,CAAA;AAAA,EACA,aAAA,CAAA;AAAA;AAAA,EAEA,QAAA,CAAA;AAAA,EACA,QAAA,CAAA;AAAA,EAEA,SAAS,QAAoB,CAAA;AAAA,IACzB,IAAM,EAAA,SAAA;AAAA,GACT,CAAA,CAAA;AAAA,EAED,YAAY,OAAqC,EAAA;AAC7C,IAAM,KAAA,EAAA,CAAA;AACN,IAAK,IAAA,CAAA,SAAA,GAAY,QAAQ,UAAW,CAAA,QAAA,CAAA;AACpC,IAAA,IAAA,CAAK,QAAQ,OAAQ,CAAA,IAAA,CAAA;AACrB,IAAK,IAAA,CAAA,cAAA,GAAiB,EAAE,WAAA,EAAa,KAAU,CAAA,EAAA,CAAA;AAC/C,IAAK,IAAA,CAAA,aAAA,GAAgB,EAAE,WAAA,EAAa,KAAU,CAAA,EAAA,CAAA;AAG9C,IAAA,IAAA,CAAK,QAAW,GAAA,KAAA;AAAA,MACZ,MAAM,CAAC,IAAK,CAAA,MAAA,CAAO,KAAK,CAAA;AAAA,MACxB,MAAM;AACF,QAAA,IAAA,CAAK,KAAK,SAAS,CAAA,CAAA;AAAA,OACvB;AAAA,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B,CAAG,EAAA;AAC1D,IAAI,GAAA,CAAA,IAAA;AAAA,MACA,4IACuB,wBAAwB,CAAA,EAAA,CAAA;AAAA,KACnD,CAAA;AACA,IAAA,OAAO,OAAO,MAAO,CAAA,IAAI,EAAE,GAAG,0BAA0B,CAAA,CAAA;AAAA,GAC5D;AACA,EAAO,OAAA,kBAAA,CAAA;AACX,CAAA;AAEA,SAAS,WAAW,mBAA2E,EAAA;AAC3F,EAAA,IAAI,CAAC,mBAAA,IAAuB,aAAc,CAAA,mBAAmB,CAAG,EAAA;AAC5D,IAAI,GAAA,CAAA,IAAA;AAAA,MACA,yFACuB,gBAAgB,CAAA,EAAA,CAAA;AAAA,KAC3C,CAAA;AACA,IAAA,OAAO,OAAO,MAAO,CAAA,IAAI,EAAE,GAAG,kBAAkB,CAAA,CAAA;AAAA,GACpD;AACA,EAAO,OAAA,mBAAA,CAAA;AACX,CAAA;AAEA,SAAS,aAAa,cAA4D,EAAA;AAC9E,EAAA,IAAI,CAAC,cAAA,IAAkB,aAAc,CAAA,cAAc,CAAG,EAAA;AAClD,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,CAAA,8HAAA,CAAA;AAAA,KACJ,CAAA;AAAA,GACJ;AACA,EAAO,OAAA,cAAA,CAAA;AACX,CAAA;AAEA,MAAM,aAAA,GAAgB,CAAC,UAAwB,KAAA;AAC3C,EAAO,OAAA,UAAA,IAAc,OAAO,IAAK,CAAA,UAAU,EAAE,MAAW,KAAA,CAAA,IAAK,WAAW,WAAgB,KAAA,MAAA,CAAA;AAC5F,CAAA;;;;"}
|
package/README.md
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
# @open-pioneer/authentication-keycloak
|
|
2
2
|
|
|
3
|
-
This package
|
|
3
|
+
This package provides a [Keycloak](https://www.keycloak.org/) plugin for the [authentication package](https://github.com/open-pioneer/trails-core-packages/blob/main/src/packages/authentication/README.md#implementing-an-authentication-plugin).
|
|
4
4
|
|
|
5
|
-
The package implements an
|
|
6
|
-
For
|
|
7
|
-
|
|
8
|
-
> **_NOTE:_** The package only works with the central [authentication package](https://github.com/open-pioneer/trails-core-packages/blob/main/src/packages/authentication/README.md#implementing-an-authentication-plugin).
|
|
5
|
+
The package implements an authentication flow using the [Keycloak JavaScript](#https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter) adapter.
|
|
6
|
+
For more information about Keycloak, see the [Keycloak documentation](https://www.keycloak.org/).
|
|
9
7
|
|
|
10
8
|
## Usage
|
|
11
9
|
|
|
12
10
|
To use the package in your app, first import the `<ForceAuth />` component from the [authentication package](https://github.com/open-pioneer/trails-core-packages/blob/main/src/packages/authentication/README.md#enforcing-authentication) to make sure that only logged in users can use the application.
|
|
13
11
|
|
|
14
12
|
`ForceAuth` renders its children (your application) if the user is authenticated.
|
|
15
|
-
Otherwise, it
|
|
13
|
+
Otherwise, it redirects the user to the Keycloak authentication provider.
|
|
14
|
+
|
|
15
|
+
To access the `SessionInfo` for the currently logged in user, use the `useAuthState` hook provided by the authentication package.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
The following example shows a basic implementation of the functions described before:
|
|
18
18
|
|
|
19
19
|
```tsx
|
|
20
20
|
// AppUI.tsx
|
|
@@ -44,7 +44,7 @@ export function AppUI() {
|
|
|
44
44
|
### Keycloak configuration properties
|
|
45
45
|
|
|
46
46
|
To configure the `authentication-keycloak` package, adjust these properties.
|
|
47
|
-
For more details on the configuration properties,
|
|
47
|
+
For more details on the configuration properties, visit the [API Reference](https://www.keycloak.org/docs/latest/securing_apps/index.html#api-reference).
|
|
48
48
|
|
|
49
49
|
| Property | Type | Description | Default |
|
|
50
50
|
| ------------------- | :-----------------: | --------------------------------------------------------------------------------------------------------------------: | --------------------------------------------------: |
|
|
@@ -112,13 +112,13 @@ const element = createCustomElement({
|
|
|
112
112
|
|
|
113
113
|
### Error reporting
|
|
114
114
|
|
|
115
|
-
In case of an error during authentication (
|
|
115
|
+
In case of an error during authentication (if the Keycloak client library throws an error during `init`), a notification is presented to the user via the `NotificationService` (technical details can be found in the developer console).
|
|
116
116
|
You should therefore embed the `<Notifier />` into your application as well.
|
|
117
117
|
Note that the Notifier should not be nested in `<ForceAuth />`, because it would not be rendered in case of an authentication problem.
|
|
118
118
|
|
|
119
119
|
### Accessing the Keycloak token in your application
|
|
120
120
|
|
|
121
|
-
After a successful login, the Keycloak token can be accessed from the `SessionInfo` of the `AuthService
|
|
121
|
+
After a successful login, the Keycloak token can be accessed from the `SessionInfo` of the `AuthService` as in the following sample:
|
|
122
122
|
|
|
123
123
|
```ts
|
|
124
124
|
//SampleTokenInterceptor.ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/authentication-keycloak",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "Implements authentication against Keycloak servers.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
"directory": "src/packages/authentication-keycloak"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"@open-pioneer/
|
|
20
|
-
"@open-pioneer/
|
|
21
|
-
"@open-pioneer/
|
|
17
|
+
"@conterra/reactivity-core": "^0.4.0",
|
|
18
|
+
"keycloak-js": "^25.0.2",
|
|
19
|
+
"@open-pioneer/authentication": "^0.3.6",
|
|
20
|
+
"@open-pioneer/runtime": "^2.1.7",
|
|
21
|
+
"@open-pioneer/core": "^1.3.0",
|
|
22
|
+
"@open-pioneer/notifier": "^0.3.6"
|
|
22
23
|
},
|
|
23
24
|
"exports": {
|
|
24
25
|
"./package.json": "./package.json",
|