@open-pioneer/authentication-keycloak 0.1.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 +17 -0
- package/KeycloakAuthPlugin.d.ts +20 -0
- package/KeycloakAuthPlugin.js +174 -0
- package/KeycloakAuthPlugin.js.map +1 -0
- package/LICENSE +202 -0
- package/README.md +148 -0
- package/api.d.ts +54 -0
- package/i18n/de.yaml +4 -0
- package/i18n/en.yaml +4 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +80 -0
- package/services.d.ts +1 -0
- package/services.js +2 -0
- package/services.js.map +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @open-pioneer/authentication-keycloak
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 1b69137: Initial release
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [5d3aafd]
|
|
12
|
+
- Updated dependencies [64645aa]
|
|
13
|
+
- Updated dependencies [b3c60f2]
|
|
14
|
+
- @open-pioneer/core@1.2.3
|
|
15
|
+
- @open-pioneer/authentication@0.3.3
|
|
16
|
+
- @open-pioneer/notifier@0.3.3
|
|
17
|
+
- @open-pioneer/runtime@2.1.4
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AuthPlugin, AuthPluginEvents, AuthState, LoginBehavior } from "@open-pioneer/authentication";
|
|
2
|
+
import { EventEmitter } from "@open-pioneer/core";
|
|
3
|
+
import { NotificationService } from "@open-pioneer/notifier";
|
|
4
|
+
import { Service, ServiceOptions, type DECLARE_SERVICE_INTERFACE } from "@open-pioneer/runtime";
|
|
5
|
+
import { KeycloakOptions, KeycloakProperties } from "./api";
|
|
6
|
+
interface References {
|
|
7
|
+
notifier: NotificationService;
|
|
8
|
+
}
|
|
9
|
+
export declare class KeycloakAuthPlugin extends EventEmitter<AuthPluginEvents> implements Service, AuthPlugin {
|
|
10
|
+
#private;
|
|
11
|
+
[DECLARE_SERVICE_INTERFACE]: "authentication-keycloak.KeycloakAuthPlugin";
|
|
12
|
+
constructor(options: ServiceOptions<References>);
|
|
13
|
+
destroy(): void;
|
|
14
|
+
getAuthState(): AuthState;
|
|
15
|
+
getLoginBehavior(): LoginBehavior;
|
|
16
|
+
logout(): void;
|
|
17
|
+
private __refresh;
|
|
18
|
+
}
|
|
19
|
+
export declare function getKeycloakConfig(properties: Partial<KeycloakProperties>): KeycloakOptions;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { createLogger, EventEmitter } from '@open-pioneer/core';
|
|
2
|
+
import Keycloak from 'keycloak-js';
|
|
3
|
+
|
|
4
|
+
const LOG = createLogger("authentication-keycloak:KeycloakAuthPlugin");
|
|
5
|
+
class KeycloakAuthPlugin extends EventEmitter {
|
|
6
|
+
#state = {
|
|
7
|
+
kind: "pending"
|
|
8
|
+
};
|
|
9
|
+
#notifier;
|
|
10
|
+
#intl;
|
|
11
|
+
#keycloakOptions;
|
|
12
|
+
#keycloak;
|
|
13
|
+
#logoutOptions;
|
|
14
|
+
#loginOptions;
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
#timerId;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super();
|
|
19
|
+
this.#notifier = options.references.notifier;
|
|
20
|
+
this.#intl = options.intl;
|
|
21
|
+
this.#logoutOptions = { redirectUri: void 0 };
|
|
22
|
+
this.#loginOptions = { redirectUri: void 0 };
|
|
23
|
+
try {
|
|
24
|
+
this.#keycloakOptions = getKeycloakConfig(options.properties);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
throw new Error("Invalid keycloak configuration", { cause: e });
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
this.#keycloak = new Keycloak(this.#keycloakOptions.keycloakConfig);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
throw new Error("Failed to construct keycloak instance", { cause: e });
|
|
32
|
+
}
|
|
33
|
+
this.#init().catch((e) => {
|
|
34
|
+
this.#state = {
|
|
35
|
+
kind: "pending"
|
|
36
|
+
};
|
|
37
|
+
this.emit("changed");
|
|
38
|
+
this.#notifier.notify({
|
|
39
|
+
level: "error",
|
|
40
|
+
title: this.#intl.formatMessage({
|
|
41
|
+
id: "loginFailed.title"
|
|
42
|
+
}),
|
|
43
|
+
message: this.#intl.formatMessage({
|
|
44
|
+
id: "loginFailed.message"
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
LOG.error("Failed to check if user is authenticated", e);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
destroy() {
|
|
51
|
+
clearInterval(this.#timerId);
|
|
52
|
+
this.#timerId = void 0;
|
|
53
|
+
}
|
|
54
|
+
getAuthState() {
|
|
55
|
+
return this.#state;
|
|
56
|
+
}
|
|
57
|
+
getLoginBehavior() {
|
|
58
|
+
const doLogin = () => {
|
|
59
|
+
LOG.debug("Login with options", this.#loginOptions);
|
|
60
|
+
this.#keycloak.login(this.#loginOptions);
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
kind: "effect",
|
|
64
|
+
login: doLogin
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
logout() {
|
|
68
|
+
LOG.debug("Logout with options", this.#logoutOptions);
|
|
69
|
+
this.#keycloak.logout(this.#logoutOptions);
|
|
70
|
+
}
|
|
71
|
+
async #init() {
|
|
72
|
+
const keycloakOptions = this.#keycloakOptions;
|
|
73
|
+
const initOptions = this.#keycloakOptions.keycloakInitOptions;
|
|
74
|
+
const refreshOptions = keycloakOptions.refreshOptions;
|
|
75
|
+
let isAuthenticated;
|
|
76
|
+
try {
|
|
77
|
+
isAuthenticated = await this.#keycloak.init(initOptions);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
const error = typeof e === "string" ? new Error(e) : e;
|
|
80
|
+
throw new Error("Failed to initialize keycloak session", { cause: error });
|
|
81
|
+
}
|
|
82
|
+
if (isAuthenticated) {
|
|
83
|
+
this.#state = {
|
|
84
|
+
kind: "authenticated",
|
|
85
|
+
sessionInfo: {
|
|
86
|
+
userId: this.#keycloak.subject ? this.#keycloak.subject : "undefined",
|
|
87
|
+
userName: this.#keycloak.idTokenParsed?.preferred_username,
|
|
88
|
+
attributes: {
|
|
89
|
+
keycloak: this.#keycloak,
|
|
90
|
+
familyName: this.#keycloak.idTokenParsed?.family_name,
|
|
91
|
+
givenName: this.#keycloak.idTokenParsed?.given_name,
|
|
92
|
+
userName: this.#keycloak.idTokenParsed?.preferred_username
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
this.emit("changed");
|
|
97
|
+
LOG.debug(`User ${this.#keycloak.subject} is authenticated`);
|
|
98
|
+
if (refreshOptions.autoRefresh) {
|
|
99
|
+
LOG.debug("Starting auto-refresh", refreshOptions);
|
|
100
|
+
this.__refresh(refreshOptions.interval, refreshOptions.timeLeft);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
this.#state = {
|
|
104
|
+
kind: "not-authenticated"
|
|
105
|
+
};
|
|
106
|
+
this.emit("changed");
|
|
107
|
+
LOG.debug("User is not authenticated");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Mocked in test
|
|
111
|
+
__refresh(interval, timeLeft) {
|
|
112
|
+
clearInterval(this.#timerId);
|
|
113
|
+
this.#timerId = setInterval(() => {
|
|
114
|
+
this.#keycloak.updateToken(timeLeft).catch((e) => {
|
|
115
|
+
LOG.error("Failed to refresh token", e);
|
|
116
|
+
this.#state = {
|
|
117
|
+
kind: "not-authenticated"
|
|
118
|
+
};
|
|
119
|
+
this.emit("changed");
|
|
120
|
+
this.destroy();
|
|
121
|
+
});
|
|
122
|
+
}, interval);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const DEFAULT_AUTO_REFRESH_OPT = {
|
|
126
|
+
autoRefresh: true,
|
|
127
|
+
interval: 6e3,
|
|
128
|
+
timeLeft: 70
|
|
129
|
+
};
|
|
130
|
+
const DEFAULT_INIT_OPT = {
|
|
131
|
+
onLoad: "check-sso",
|
|
132
|
+
pkceMethod: "S256"
|
|
133
|
+
};
|
|
134
|
+
function getKeycloakConfig(properties) {
|
|
135
|
+
const { keycloakOptions } = properties;
|
|
136
|
+
const { refreshOptions, keycloakInitOptions, keycloakConfig } = keycloakOptions;
|
|
137
|
+
return {
|
|
138
|
+
refreshOptions: { ...getRefreshOpt(refreshOptions) },
|
|
139
|
+
keycloakInitOptions: { ...getInitOpt(keycloakInitOptions) },
|
|
140
|
+
keycloakConfig: { ...getConfigOpt(keycloakConfig) }
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function getRefreshOpt(autoRefreshOptions) {
|
|
144
|
+
if (!autoRefreshOptions || isObjectEmpty(autoRefreshOptions)) {
|
|
145
|
+
LOG.warn(
|
|
146
|
+
`The autorefresh options of the Keycloak configuration should be set to ensure automatic refreshes at specified intervals. Defaulting to '${DEFAULT_AUTO_REFRESH_OPT}'.`
|
|
147
|
+
);
|
|
148
|
+
return Object.assign({}, { ...DEFAULT_AUTO_REFRESH_OPT });
|
|
149
|
+
}
|
|
150
|
+
return autoRefreshOptions;
|
|
151
|
+
}
|
|
152
|
+
function getInitOpt(keycloakInitOptions) {
|
|
153
|
+
if (!keycloakInitOptions || isObjectEmpty(keycloakInitOptions)) {
|
|
154
|
+
LOG.warn(
|
|
155
|
+
`The Keycloak init options of the keycloak configuration should be set. Defaulting to '${DEFAULT_INIT_OPT}'.`
|
|
156
|
+
);
|
|
157
|
+
return Object.assign({}, { ...DEFAULT_INIT_OPT });
|
|
158
|
+
}
|
|
159
|
+
return keycloakInitOptions;
|
|
160
|
+
}
|
|
161
|
+
function getConfigOpt(keycloakConfig) {
|
|
162
|
+
if (!keycloakConfig || isObjectEmpty(keycloakConfig)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`KeycloakConfig not found: The Keycloak configuration options are required by the plugin to perform login and logout operations`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
return keycloakConfig;
|
|
168
|
+
}
|
|
169
|
+
const isObjectEmpty = (objectName) => {
|
|
170
|
+
return objectName && Object.keys(objectName).length === 0 && objectName.constructor === Object;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export { KeycloakAuthPlugin, getKeycloakConfig };
|
|
174
|
+
//# sourceMappingURL=KeycloakAuthPlugin.js.map
|
|
@@ -0,0 +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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
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# @open-pioneer/authentication-keycloak
|
|
2
|
+
|
|
3
|
+
This package provide a plugin for the [authentication package](https://github.com/open-pioneer/trails-core-packages/blob/main/src/packages/authentication/README.md#implementing-an-authentication-plugin) that supports [Keycloak](https://www.keycloak.org/).
|
|
4
|
+
|
|
5
|
+
The package implements an actual authentication flow using the [Keycloak JavaScript](#https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter) adapter.
|
|
6
|
+
For further Information, how Keycloak works please visit the official documentation of [Keycloak](https://www.keycloak.org/).
|
|
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).
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
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
|
+
|
|
14
|
+
`ForceAuth` renders its children (your application) if the user is authenticated.
|
|
15
|
+
Otherwise, it redirect the user to the Keycloak authentication provider.
|
|
16
|
+
|
|
17
|
+
To access the `SessionInfo` for the current logged in user, you can use the `useAuthState` hook provided by the authentication package.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
// AppUI.tsx
|
|
21
|
+
import { ForceAuth, useAuthState } from "@open-pioneer/authentication";
|
|
22
|
+
import { Notifier } from "@open-pioneer/notifier";
|
|
23
|
+
import { useService } from "open-pioneer:react-hooks";
|
|
24
|
+
|
|
25
|
+
export function AppUI() {
|
|
26
|
+
const authService = useService<AuthService>("authentication.AuthService");
|
|
27
|
+
const authState = useAuthState(authService);
|
|
28
|
+
const sessionInfo = authState.kind == "authenticated" ? authState.sessionInfo : undefined;
|
|
29
|
+
const userName = sessionInfo?.attributes?.userName as string;
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<>
|
|
33
|
+
{/* recommended for error reporting: */}
|
|
34
|
+
<Notifier />
|
|
35
|
+
<ForceAuth>
|
|
36
|
+
<Text>Logged in as: {userName}</Text>
|
|
37
|
+
<TheRestOfYourApplication />
|
|
38
|
+
</ForceAuth>
|
|
39
|
+
</>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Keycloak configuration properties
|
|
45
|
+
|
|
46
|
+
To configure the `authentication-keycloak` package, adjust these properties.
|
|
47
|
+
For more details on the configuration properties, please visit the official documentation [API Reference](https://www.keycloak.org/docs/latest/securing_apps/index.html#api-reference).
|
|
48
|
+
|
|
49
|
+
| Property | Type | Description | Default |
|
|
50
|
+
| ------------------- | :-----------------: | --------------------------------------------------------------------------------------------------------------------: | --------------------------------------------------: |
|
|
51
|
+
| refreshOptions | RefreshOptions | Configure token refresh behavior and manage access token lifecycle in client applications. | `{autoRefresh: true, interval: 6000, timeLeft: 70}` |
|
|
52
|
+
| keycloakInitOptions | KeycloakInitOptions | Configure Keycloak's behavior during client application initialization. | `{onLoad: "check-sso", pkceMethod: "S256"}` |
|
|
53
|
+
| keycloakConfig | KeycloakConfig | The configuration settings required to establish a connection between the client application and the Keycloak server. | |
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
interface RefreshOptions {
|
|
57
|
+
autoRefresh: boolean;
|
|
58
|
+
interval: number;
|
|
59
|
+
timeLeft: number;
|
|
60
|
+
}
|
|
61
|
+
interface KeycloakInitOptions {
|
|
62
|
+
onLoad: string;
|
|
63
|
+
pkceMethod: string;
|
|
64
|
+
scope: string;
|
|
65
|
+
}
|
|
66
|
+
interface KeycloakConfig {
|
|
67
|
+
url: string;
|
|
68
|
+
realm: string;
|
|
69
|
+
clientId: string;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Using the configuration in an app
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
// app.ts
|
|
77
|
+
import { KeycloakProperties } from "@open-pioneer/authentication-keycloak";
|
|
78
|
+
import { createCustomElement } from "@open-pioneer/runtime";
|
|
79
|
+
import * as appMetadata from "open-pioneer:app";
|
|
80
|
+
import { AppUI } from "./AppUI";
|
|
81
|
+
|
|
82
|
+
const element = createCustomElement({
|
|
83
|
+
component: AppUI,
|
|
84
|
+
appMetadata,
|
|
85
|
+
config: {
|
|
86
|
+
properties: {
|
|
87
|
+
"@open-pioneer/authentication-keycloak": {
|
|
88
|
+
keycloakOptions: {
|
|
89
|
+
refreshOptions: {
|
|
90
|
+
autoRefresh: true,
|
|
91
|
+
interval: 6000,
|
|
92
|
+
timeLeft: 70
|
|
93
|
+
},
|
|
94
|
+
keycloakInitOptions: {
|
|
95
|
+
onLoad: "check-sso",
|
|
96
|
+
pkceMethod: "S256"
|
|
97
|
+
// additional configuration, for example:
|
|
98
|
+
// scope: "openid address phone"
|
|
99
|
+
},
|
|
100
|
+
keycloakConfig: {
|
|
101
|
+
url: "http://keycloak-server/base_path",
|
|
102
|
+
realm: "myrealm",
|
|
103
|
+
clientId: "myapp"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} satisfies KeycloakProperties // for auto completion / validation
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ...
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Error reporting
|
|
114
|
+
|
|
115
|
+
In case of an error during authentication (i.e. if the keycloak client lib throws an error during `init`), a notification will be presented to the user via the `NotificationService` (technical details can be found in the developer console).
|
|
116
|
+
You should therefore embed the `<Notifier />` into your application as well.
|
|
117
|
+
Note that the Notifier should not be nested in `<ForceAuth />`, because it would not be rendered in case of an authentication problem.
|
|
118
|
+
|
|
119
|
+
### Accessing the Keycloak token in your application
|
|
120
|
+
|
|
121
|
+
After a successful login, the Keycloak token can be accessed from the `SessionInfo` of the `AuthService`.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
//SampleTokenInterceptor.ts
|
|
125
|
+
import { AuthService } from "@open-pioneer/authentication";
|
|
126
|
+
import { ServiceOptions } from "@open-pioneer/runtime";
|
|
127
|
+
// ...
|
|
128
|
+
|
|
129
|
+
class SampleTokenInterceptor implements Interceptor {
|
|
130
|
+
private authService: AuthService;
|
|
131
|
+
|
|
132
|
+
constructor(options: ServiceOptions<References>) {
|
|
133
|
+
this.authService = options.references.authService;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
beforeRequest({ target, options }: BeforeRequestParams): void {
|
|
137
|
+
const authState = this.authService.getAuthState();
|
|
138
|
+
const sessionInfo = authState.kind == "authenticated" ? authState.sessionInfo : undefined;
|
|
139
|
+
const keycloak = sessionInfo?.attributes?.keycloak;
|
|
140
|
+
const token = (keycloak as { token: string }).token;
|
|
141
|
+
// ...
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
Apache-2.0 (see `LICENSE` file)
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { KeycloakConfig, KeycloakInitOptions } from "keycloak-js";
|
|
2
|
+
/**
|
|
3
|
+
* The central configuration properties of the plugin.
|
|
4
|
+
*
|
|
5
|
+
* An object of this type should be used as configuration for this package.
|
|
6
|
+
*/
|
|
7
|
+
export interface KeycloakProperties {
|
|
8
|
+
/**
|
|
9
|
+
* These properties are required by the Keycloak JavaScript adapter.
|
|
10
|
+
*/
|
|
11
|
+
keycloakOptions: KeycloakOptions;
|
|
12
|
+
}
|
|
13
|
+
export interface KeycloakOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The configuration details for connecting to Keycloak.
|
|
16
|
+
* 'url': The URL of your Keycloak server.
|
|
17
|
+
* 'realm': The realm within Keycloak.
|
|
18
|
+
* 'clientId': The ID of the client application registered in Keycloak.
|
|
19
|
+
*/
|
|
20
|
+
keycloakConfig: KeycloakConfig;
|
|
21
|
+
/**
|
|
22
|
+
* Control the automatic refreshing of authentication tokens.
|
|
23
|
+
* 'autoRefresh': Whether token refreshing should happen automatically.
|
|
24
|
+
* 'interval': The interval (in milliseconds) at which token refreshing should occur.
|
|
25
|
+
* 'timeLeft': The remaining time (in milliseconds) before token expiration.
|
|
26
|
+
*/
|
|
27
|
+
refreshOptions: RefreshOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Define how Keycloak initializes.
|
|
30
|
+
* This properties can be used:
|
|
31
|
+
* 'onLoad': Specifies when Keycloak should initialize.
|
|
32
|
+
* 'pkceMethod': The method used for PKCE for enhanced security.
|
|
33
|
+
* 'scope': The scope of the authentication.
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
keycloakInitOptions: Partial<KeycloakInitOptions>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Control the automatic refreshing of authentication tokens.
|
|
40
|
+
*/
|
|
41
|
+
export interface RefreshOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Whether token refreshing should happen automatically.
|
|
44
|
+
*/
|
|
45
|
+
autoRefresh: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The interval (in milliseconds) at which token refreshing should occur.
|
|
48
|
+
*/
|
|
49
|
+
interval: number;
|
|
50
|
+
/**
|
|
51
|
+
* The remaining time (in milliseconds) before token expiration.
|
|
52
|
+
*/
|
|
53
|
+
timeLeft: number;
|
|
54
|
+
}
|
package/i18n/de.yaml
ADDED
package/i18n/en.yaml
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api";
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@open-pioneer/authentication-keycloak",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Implements authentication against Keycloak servers.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"open-pioneer-trails"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/open-pioneer",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/open-pioneer/core-packages",
|
|
14
|
+
"directory": "src/packages/authentication-keycloak"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"keycloak-js": "^23.0.7",
|
|
18
|
+
"@open-pioneer/authentication": "^0.3.3",
|
|
19
|
+
"@open-pioneer/runtime": "^2.1.4",
|
|
20
|
+
"@open-pioneer/notifier": "^0.3.3",
|
|
21
|
+
"@open-pioneer/core": "^1.2.3"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./package.json": "./package.json",
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./index.js",
|
|
27
|
+
"types": "./index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./services": {
|
|
30
|
+
"import": "./services.js",
|
|
31
|
+
"types": "./services.d.ts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"openPioneerFramework": {
|
|
35
|
+
"services": [
|
|
36
|
+
{
|
|
37
|
+
"serviceName": "KeycloakAuthPlugin",
|
|
38
|
+
"provides": [
|
|
39
|
+
{
|
|
40
|
+
"interfaceName": "authentication.AuthPlugin"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"interfaceName": "authentication-keycloak.KeycloakAuthPlugin"
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"references": [
|
|
47
|
+
{
|
|
48
|
+
"referenceName": "notifier",
|
|
49
|
+
"type": "unique",
|
|
50
|
+
"interfaceName": "notifier.NotificationService"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
"servicesModule": "./services",
|
|
56
|
+
"i18n": {
|
|
57
|
+
"languages": [
|
|
58
|
+
"en",
|
|
59
|
+
"de"
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"ui": {
|
|
63
|
+
"references": []
|
|
64
|
+
},
|
|
65
|
+
"properties": [
|
|
66
|
+
{
|
|
67
|
+
"propertyName": "keycloakOptions",
|
|
68
|
+
"defaultValue": {
|
|
69
|
+
"refreshOptions": null,
|
|
70
|
+
"keycloakInitOptions": null,
|
|
71
|
+
"keycloakConfig": null,
|
|
72
|
+
"keycloakLogoutOptions": null,
|
|
73
|
+
"keycloakLoginOptions": null
|
|
74
|
+
},
|
|
75
|
+
"required": false
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
"packageFormatVersion": "1.0.0"
|
|
79
|
+
}
|
|
80
|
+
}
|
package/services.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { KeycloakAuthPlugin } from "./KeycloakAuthPlugin";
|
package/services.js
ADDED
package/services.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|