keycloak-angular 16.1.0 → 19.0.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.
Files changed (39) hide show
  1. package/README.md +244 -150
  2. package/fesm2022/keycloak-angular.mjs +1324 -51
  3. package/fesm2022/keycloak-angular.mjs.map +1 -1
  4. package/index.d.ts +3 -0
  5. package/lib/directives/has-roles.directive.d.ts +95 -0
  6. package/lib/features/keycloak.feature.d.ts +43 -0
  7. package/lib/features/with-refresh-token.feature.d.ts +66 -0
  8. package/lib/guards/auth.guard.d.ts +75 -0
  9. package/lib/interceptors/custom-bearer-token.interceptor.d.ts +97 -0
  10. package/lib/interceptors/include-bearer-token.interceptor.d.ts +111 -0
  11. package/lib/interceptors/keycloak.interceptor.d.ts +71 -0
  12. package/lib/{core → legacy/core}/core.module.d.ts +5 -0
  13. package/lib/legacy/core/interceptors/keycloak-bearer.interceptor.d.ts +53 -0
  14. package/lib/legacy/core/interfaces/keycloak-event.d.ts +74 -0
  15. package/lib/legacy/core/interfaces/keycloak-options.d.ts +146 -0
  16. package/lib/legacy/core/services/keycloak-auth-guard.d.ts +50 -0
  17. package/lib/legacy/core/services/keycloak.service.d.ts +316 -0
  18. package/lib/{keycloak-angular.module.d.ts → legacy/keycloak-angular.module.d.ts} +5 -0
  19. package/lib/legacy/public_api.d.ts +14 -0
  20. package/lib/provide-keycloak.d.ts +74 -0
  21. package/lib/services/auto-refresh-token.service.d.ts +47 -0
  22. package/lib/services/user-activity.service.d.ts +66 -0
  23. package/lib/signals/keycloak-events-signal.d.ts +118 -0
  24. package/package.json +4 -6
  25. package/public_api.d.ts +19 -7
  26. package/esm2022/keycloak-angular.mjs +0 -2
  27. package/esm2022/lib/core/core.module.mjs +0 -33
  28. package/esm2022/lib/core/interceptors/keycloak-bearer.interceptor.mjs +0 -51
  29. package/esm2022/lib/core/interfaces/keycloak-event.mjs +0 -12
  30. package/esm2022/lib/core/interfaces/keycloak-options.mjs +0 -2
  31. package/esm2022/lib/core/services/keycloak-auth-guard.mjs +0 -17
  32. package/esm2022/lib/core/services/keycloak.service.mjs +0 -204
  33. package/esm2022/lib/keycloak-angular.module.mjs +0 -15
  34. package/esm2022/public_api.mjs +0 -7
  35. package/lib/core/interceptors/keycloak-bearer.interceptor.d.ts +0 -14
  36. package/lib/core/interfaces/keycloak-event.d.ts +0 -14
  37. package/lib/core/interfaces/keycloak-options.d.ts +0 -22
  38. package/lib/core/services/keycloak-auth-guard.d.ts +0 -11
  39. package/lib/core/services/keycloak.service.d.ts +0 -42
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @license
3
+ * Copyright Mauricio Gemelli Vigolo and contributors.
4
+ *
5
+ * Use of this source code is governed by a MIT-style license that can be
6
+ * found in the LICENSE file at https://github.com/mauriciovigolo/keycloak-angular/blob/main/LICENSE.md
7
+ */
8
+ import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
9
+ import { KeycloakService } from './keycloak.service';
10
+ /**
11
+ * A simple guard implementation out of the box. This class should be inherited and
12
+ * implemented by the application. The only method that should be implemented is #isAccessAllowed.
13
+ * The reason for this is that the authorization flow is usually not unique, so in this way you will
14
+ * have more freedom to customize your authorization flow.
15
+ *
16
+ * @deprecated Class based guards are deprecated in Keycloak Angular and will be removed in future versions.
17
+ * Use the new `createAuthGuard` function to create a Guard for your application.
18
+ * More info: https://github.com/mauriciovigolo/keycloak-angular/docs/migration-guides/v19.md
19
+ */
20
+ export declare abstract class KeycloakAuthGuard implements CanActivate {
21
+ protected router: Router;
22
+ protected keycloakAngular: KeycloakService;
23
+ /**
24
+ * Indicates if the user is authenticated or not.
25
+ */
26
+ protected authenticated: boolean;
27
+ /**
28
+ * Roles of the logged user. It contains the clientId and realm user roles.
29
+ */
30
+ protected roles: string[];
31
+ constructor(router: Router, keycloakAngular: KeycloakService);
32
+ /**
33
+ * CanActivate checks if the user is logged in and get the full list of roles (REALM + CLIENT)
34
+ * of the logged user. This values are set to authenticated and roles params.
35
+ *
36
+ * @param route
37
+ * @param state
38
+ */
39
+ canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean | UrlTree>;
40
+ /**
41
+ * Create your own customized authorization flow in this method. From here you already known
42
+ * if the user is authenticated (this.authenticated) and the user roles (this.roles).
43
+ *
44
+ * Return a UrlTree if the user should be redirected to another route.
45
+ *
46
+ * @param route
47
+ * @param state
48
+ */
49
+ abstract isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean | UrlTree>;
50
+ }
@@ -0,0 +1,316 @@
1
+ import { HttpHeaders, HttpRequest } from '@angular/common/http';
2
+ import { Subject } from 'rxjs';
3
+ import { ExcludedUrlRegex, KeycloakOptions } from '../interfaces/keycloak-options';
4
+ import { KeycloakEventLegacy } from '../interfaces/keycloak-event';
5
+ import * as i0 from "@angular/core";
6
+ /**
7
+ * Service to expose existent methods from the Keycloak JS adapter, adding new
8
+ * functionalities to improve the use of keycloak in Angular v > 4.3 applications.
9
+ *
10
+ * This class should be injected in the application bootstrap, so the same instance will be used
11
+ * along the web application.
12
+ *
13
+ * @deprecated This service is deprecated and will be removed in future versions.
14
+ * Use the new `provideKeycloak` function to load Keycloak in an Angular application.
15
+ * More info: https://github.com/mauriciovigolo/keycloak-angular/docs/migration-guides/v19.md
16
+ */
17
+ export declare class KeycloakService {
18
+ /**
19
+ * Keycloak-js instance.
20
+ */
21
+ private _instance;
22
+ /**
23
+ * User profile as KeycloakProfile interface.
24
+ */
25
+ private _userProfile;
26
+ /**
27
+ * Flag to indicate if the bearer will not be added to the authorization header.
28
+ */
29
+ private _enableBearerInterceptor;
30
+ /**
31
+ * When the implicit flow is choosen there must exist a silentRefresh, as there is
32
+ * no refresh token.
33
+ */
34
+ private _silentRefresh;
35
+ /**
36
+ * Indicates that the user profile should be loaded at the keycloak initialization,
37
+ * just after the login.
38
+ */
39
+ private _loadUserProfileAtStartUp;
40
+ /**
41
+ * The bearer prefix that will be appended to the Authorization Header.
42
+ */
43
+ private _bearerPrefix;
44
+ /**
45
+ * Value that will be used as the Authorization Http Header name.
46
+ */
47
+ private _authorizationHeaderName;
48
+ /**
49
+ * @deprecated
50
+ * The excluded urls patterns that must skip the KeycloakBearerInterceptor.
51
+ */
52
+ private _excludedUrls;
53
+ /**
54
+ * Observer for the keycloak events
55
+ */
56
+ private _keycloakEvents$;
57
+ /**
58
+ * The amount of required time remaining before expiry of the token before the token will be refreshed.
59
+ */
60
+ private _updateMinValidity;
61
+ /**
62
+ * Returns true if the request should have the token added to the headers by the KeycloakBearerInterceptor.
63
+ */
64
+ shouldAddToken: (request: HttpRequest<unknown>) => boolean;
65
+ /**
66
+ * Returns true if the request being made should potentially update the token.
67
+ */
68
+ shouldUpdateToken: (request: HttpRequest<unknown>) => boolean;
69
+ /**
70
+ * Binds the keycloak-js events to the keycloakEvents Subject
71
+ * which is a good way to monitor for changes, if needed.
72
+ *
73
+ * The keycloakEvents returns the keycloak-js event type and any
74
+ * argument if the source function provides any.
75
+ */
76
+ private bindsKeycloakEvents;
77
+ /**
78
+ * Loads all bearerExcludedUrl content in a uniform type: ExcludedUrl,
79
+ * so it becomes easier to handle.
80
+ *
81
+ * @param bearerExcludedUrls array of strings or ExcludedUrl that includes
82
+ * the url and HttpMethod.
83
+ */
84
+ private loadExcludedUrls;
85
+ /**
86
+ * Handles the class values initialization.
87
+ *
88
+ * @param options
89
+ */
90
+ private initServiceValues;
91
+ /**
92
+ * Keycloak initialization. It should be called to initialize the adapter.
93
+ * Options is an object with 2 main parameters: config and initOptions. The first one
94
+ * will be used to create the Keycloak instance. The second one are options to initialize the
95
+ * keycloak instance.
96
+ *
97
+ * @param options
98
+ * Config: may be a string representing the keycloak URI or an object with the
99
+ * following content:
100
+ * - url: Keycloak json URL
101
+ * - realm: realm name
102
+ * - clientId: client id
103
+ *
104
+ * initOptions:
105
+ * Options to initialize the Keycloak adapter, matches the options as provided by Keycloak itself.
106
+ *
107
+ * enableBearerInterceptor:
108
+ * Flag to indicate if the bearer will added to the authorization header.
109
+ *
110
+ * loadUserProfileInStartUp:
111
+ * Indicates that the user profile should be loaded at the keycloak initialization,
112
+ * just after the login.
113
+ *
114
+ * bearerExcludedUrls:
115
+ * String Array to exclude the urls that should not have the Authorization Header automatically
116
+ * added.
117
+ *
118
+ * authorizationHeaderName:
119
+ * This value will be used as the Authorization Http Header name.
120
+ *
121
+ * bearerPrefix:
122
+ * This value will be included in the Authorization Http Header param.
123
+ *
124
+ * tokenUpdateExcludedHeaders:
125
+ * Array of Http Header key/value maps that should not trigger the token to be updated.
126
+ *
127
+ * updateMinValidity:
128
+ * This value determines if the token will be refreshed based on its expiration time.
129
+ *
130
+ * @returns
131
+ * A Promise with a boolean indicating if the initialization was successful.
132
+ */
133
+ init(options?: KeycloakOptions): Promise<boolean>;
134
+ /**
135
+ * Redirects to login form on (options is an optional object with redirectUri and/or
136
+ * prompt fields).
137
+ *
138
+ * @param options
139
+ * Object, where:
140
+ * - redirectUri: Specifies the uri to redirect to after login.
141
+ * - prompt:By default the login screen is displayed if the user is not logged-in to Keycloak.
142
+ * To only authenticate to the application if the user is already logged-in and not display the
143
+ * login page if the user is not logged-in, set this option to none. To always require
144
+ * re-authentication and ignore SSO, set this option to login .
145
+ * - maxAge: Used just if user is already authenticated. Specifies maximum time since the
146
+ * authentication of user happened. If user is already authenticated for longer time than
147
+ * maxAge, the SSO is ignored and he will need to re-authenticate again.
148
+ * - loginHint: Used to pre-fill the username/email field on the login form.
149
+ * - action: If value is 'register' then user is redirected to registration page, otherwise to
150
+ * login page.
151
+ * - locale: Specifies the desired locale for the UI.
152
+ * @returns
153
+ * A void Promise if the login is successful and after the user profile loading.
154
+ */
155
+ login(options?: Keycloak.KeycloakLoginOptions): Promise<void>;
156
+ /**
157
+ * Redirects to logout.
158
+ *
159
+ * @param redirectUri
160
+ * Specifies the uri to redirect to after logout.
161
+ * @returns
162
+ * A void Promise if the logout was successful, cleaning also the userProfile.
163
+ */
164
+ logout(redirectUri?: string): Promise<void>;
165
+ /**
166
+ * Redirects to registration form. Shortcut for login with option
167
+ * action = 'register'. Options are same as for the login method but 'action' is set to
168
+ * 'register'.
169
+ *
170
+ * @param options
171
+ * login options
172
+ * @returns
173
+ * A void Promise if the register flow was successful.
174
+ */
175
+ register(options?: Keycloak.KeycloakLoginOptions): Promise<void>;
176
+ /**
177
+ * Check if the user has access to the specified role. It will look for roles in
178
+ * realm and the given resource, but will not check if the user is logged in for better performance.
179
+ *
180
+ * @param role
181
+ * role name
182
+ * @param resource
183
+ * resource name. If not specified, `clientId` is used
184
+ * @returns
185
+ * A boolean meaning if the user has the specified Role.
186
+ */
187
+ isUserInRole(role: string, resource?: string): boolean;
188
+ /**
189
+ * Return the roles of the logged user. The realmRoles parameter, with default value
190
+ * true, will return the resource roles and realm roles associated with the logged user. If set to false
191
+ * it will only return the resource roles. The resource parameter, if specified, will return only resource roles
192
+ * associated with the given resource.
193
+ *
194
+ * @param realmRoles
195
+ * Set to false to exclude realm roles (only client roles)
196
+ * @param resource
197
+ * resource name If not specified, returns roles from all resources
198
+ * @returns
199
+ * Array of Roles associated with the logged user.
200
+ */
201
+ getUserRoles(realmRoles?: boolean, resource?: string): string[];
202
+ /**
203
+ * Check if user is logged in.
204
+ *
205
+ * @returns
206
+ * A boolean that indicates if the user is logged in.
207
+ */
208
+ isLoggedIn(): boolean;
209
+ /**
210
+ * Returns true if the token has less than minValidity seconds left before
211
+ * it expires.
212
+ *
213
+ * @param minValidity
214
+ * Seconds left. (minValidity) is optional. Default value is 0.
215
+ * @returns
216
+ * Boolean indicating if the token is expired.
217
+ */
218
+ isTokenExpired(minValidity?: number): boolean;
219
+ /**
220
+ * If the token expires within _updateMinValidity seconds the token is refreshed. If the
221
+ * session status iframe is enabled, the session status is also checked.
222
+ * Returns a promise telling if the token was refreshed or not. If the session is not active
223
+ * anymore, the promise is rejected.
224
+ *
225
+ * @param minValidity
226
+ * Seconds left. (minValidity is optional, if not specified updateMinValidity - default 20 is used)
227
+ * @returns
228
+ * Promise with a boolean indicating if the token was succesfully updated.
229
+ */
230
+ updateToken(minValidity?: number): Promise<boolean>;
231
+ /**
232
+ * Loads the user profile.
233
+ * Returns promise to set functions to be invoked if the profile was loaded
234
+ * successfully, or if the profile could not be loaded.
235
+ *
236
+ * @param forceReload
237
+ * If true will force the loadUserProfile even if its already loaded.
238
+ * @returns
239
+ * A promise with the KeycloakProfile data loaded.
240
+ */
241
+ loadUserProfile(forceReload?: boolean): Promise<import("keycloak-js").KeycloakProfile>;
242
+ /**
243
+ * Returns the authenticated token.
244
+ */
245
+ getToken(): Promise<string>;
246
+ /**
247
+ * Returns the logged username.
248
+ *
249
+ * @returns
250
+ * The logged username.
251
+ */
252
+ getUsername(): string;
253
+ /**
254
+ * Clear authentication state, including tokens. This can be useful if application
255
+ * has detected the session was expired, for example if updating token fails.
256
+ * Invoking this results in onAuthLogout callback listener being invoked.
257
+ */
258
+ clearToken(): void;
259
+ /**
260
+ * Adds a valid token in header. The key & value format is:
261
+ * Authorization Bearer <token>.
262
+ * If the headers param is undefined it will create the Angular headers object.
263
+ *
264
+ * @param headers
265
+ * Updated header with Authorization and Keycloak token.
266
+ * @returns
267
+ * An observable with with the HTTP Authorization header and the current token.
268
+ */
269
+ addTokenToHeader(headers?: HttpHeaders): import("rxjs").Observable<HttpHeaders>;
270
+ /**
271
+ * Returns the original Keycloak instance, if you need any customization that
272
+ * this Angular service does not support yet. Use with caution.
273
+ *
274
+ * @returns
275
+ * The KeycloakInstance from keycloak-js.
276
+ */
277
+ getKeycloakInstance(): Keycloak.KeycloakInstance;
278
+ /**
279
+ * @deprecated
280
+ * Returns the excluded URLs that should not be considered by
281
+ * the http interceptor which automatically adds the authorization header in the Http Request.
282
+ *
283
+ * @returns
284
+ * The excluded urls that must not be intercepted by the KeycloakBearerInterceptor.
285
+ */
286
+ get excludedUrls(): ExcludedUrlRegex[];
287
+ /**
288
+ * Flag to indicate if the bearer will be added to the authorization header.
289
+ *
290
+ * @returns
291
+ * Returns if the bearer interceptor was set to be disabled.
292
+ */
293
+ get enableBearerInterceptor(): boolean;
294
+ /**
295
+ * Keycloak subject to monitor the events triggered by keycloak-js.
296
+ * The following events as available (as described at keycloak docs -
297
+ * https://www.keycloak.org/docs/latest/securing_apps/index.html#callback-events):
298
+ * - OnAuthError
299
+ * - OnAuthLogout
300
+ * - OnAuthRefreshError
301
+ * - OnAuthRefreshSuccess
302
+ * - OnAuthSuccess
303
+ * - OnReady
304
+ * - OnTokenExpire
305
+ * In each occurrence of any of these, this subject will return the event type,
306
+ * described at {@link KeycloakEventTypeLegacy} enum and the function args from the keycloak-js
307
+ * if provided any.
308
+ *
309
+ * @returns
310
+ * A subject with the {@link KeycloakEventLegacy} which describes the event type and attaches the
311
+ * function args.
312
+ */
313
+ get keycloakEvents$(): Subject<KeycloakEventLegacy>;
314
+ static ɵfac: i0.ɵɵFactoryDeclaration<KeycloakService, never>;
315
+ static ɵprov: i0.ɵɵInjectableDeclaration<KeycloakService>;
316
+ }
@@ -1,5 +1,10 @@
1
1
  import * as i0 from "@angular/core";
2
2
  import * as i1 from "./core/core.module";
3
+ /**
4
+ * @deprecated NgModules are deprecated in Keycloak Angular and will be removed in future versions.
5
+ * Use the new `provideKeycloak` function to load Keycloak in an Angular application.
6
+ * More info: https://github.com/mauriciovigolo/keycloak-angular/docs/migration-guides/v19.md
7
+ */
3
8
  export declare class KeycloakAngularModule {
4
9
  static ɵfac: i0.ɵɵFactoryDeclaration<KeycloakAngularModule, never>;
5
10
  static ɵmod: i0.ɵɵNgModuleDeclaration<KeycloakAngularModule, never, [typeof i1.CoreModule], never>;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @license
3
+ * Copyright Mauricio Gemelli Vigolo All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by a MIT-style license that can be
6
+ * found in the LICENSE file at https://github.com/mauriciovigolo/keycloak-angular/blob/main/LICENSE.md
7
+ */
8
+ export { KeycloakEventLegacy, KeycloakEventTypeLegacy } from './core/interfaces/keycloak-event';
9
+ export { KeycloakOptions } from './core/interfaces/keycloak-options';
10
+ export { KeycloakAuthGuard } from './core/services/keycloak-auth-guard';
11
+ export { KeycloakService } from './core/services/keycloak.service';
12
+ export { KeycloakBearerInterceptor } from './core/interceptors/keycloak-bearer.interceptor';
13
+ export { CoreModule } from './core/core.module';
14
+ export { KeycloakAngularModule } from './keycloak-angular.module';
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @license
3
+ * Copyright Mauricio Gemelli Vigolo All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by a MIT-style license that can be
6
+ * found in the LICENSE file at https://github.com/mauriciovigolo/keycloak-angular/blob/main/LICENSE.md
7
+ */
8
+ import { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js';
9
+ import { EnvironmentProviders, Provider } from '@angular/core';
10
+ import { KeycloakFeature } from './features/keycloak.feature';
11
+ /**
12
+ * Options for configuring Keycloak and additional providers.
13
+ */
14
+ export type ProvideKeycloakOptions = {
15
+ /**
16
+ * Keycloak configuration, including the server URL, realm, and client ID.
17
+ */
18
+ config: KeycloakConfig;
19
+ /**
20
+ * Optional initialization options for the Keycloak instance.
21
+ * If not provided, Keycloak will not initialize automatically.
22
+ */
23
+ initOptions?: KeycloakInitOptions;
24
+ /**
25
+ * Optional array of additional Angular providers or environment providers.
26
+ */
27
+ providers?: Array<Provider | EnvironmentProviders>;
28
+ /**
29
+ * Optional array of Keycloak features to extend the functionality of the Keycloak integration.
30
+ */
31
+ features?: Array<KeycloakFeature>;
32
+ };
33
+ /**
34
+ * Configures and provides Keycloak as a dependency in an Angular application.
35
+ *
36
+ * This function initializes a Keycloak instance with the provided configuration and
37
+ * optional initialization options. It integrates Keycloak into Angular dependency
38
+ * injection system, allowing easy consumption throughout the application. Additionally,
39
+ * it supports custom providers and Keycloak Angular features.
40
+ *
41
+ * If `initOptions` is not provided, the Keycloak instance will not be automatically initialized.
42
+ * In such cases, the application must call `keycloak.init()` explicitly.
43
+ *
44
+ * @param options - Configuration object for Keycloak:
45
+ * - `config`: The Keycloak configuration, including the server URL, realm, and client ID.
46
+ * - `initOptions` (Optional): Initialization options for the Keycloak instance.
47
+ * - `providers` (Optional): Additional Angular providers to include.
48
+ * - `features` (Optional): Keycloak Angular features to configure during initialization.
49
+ *
50
+ * @returns An `EnvironmentProviders` object integrating Keycloak setup and additional providers.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { provideKeycloak } from './keycloak.providers';
55
+ * import { bootstrapApplication } from '@angular/platform-browser';
56
+ * import { AppComponent } from './app/app.component';
57
+ *
58
+ * bootstrapApplication(AppComponent, {
59
+ * providers: [
60
+ * provideKeycloak({
61
+ * config: {
62
+ * url: 'https://auth-server.example.com',
63
+ * realm: 'my-realm',
64
+ * clientId: 'my-client',
65
+ * },
66
+ * initOptions: {
67
+ * onLoad: 'login-required',
68
+ * },
69
+ * }),
70
+ * ],
71
+ * });
72
+ * ```
73
+ */
74
+ export declare function provideKeycloak(options: ProvideKeycloakOptions): EnvironmentProviders;
@@ -0,0 +1,47 @@
1
+ import Keycloak from 'keycloak-js';
2
+ import { UserActivityService } from './user-activity.service';
3
+ import * as i0 from "@angular/core";
4
+ /**
5
+ * Configuration options for the `AutoRefreshTokenService`.
6
+ */
7
+ type AutoRefreshTokenOptions = {
8
+ /**
9
+ * Maximum allowed inactivity duration in milliseconds before
10
+ * the session times out. Default is `50000`.
11
+ */
12
+ sessionTimeout?: number;
13
+ /**
14
+ * Action to take when the session times out due to inactivity.
15
+ * Options are:
16
+ * - `'login'`: Redirect to the Keycloak login page.
17
+ * - `'logout'`: Log the user out of the session.
18
+ * - `'none'`: Do nothing.
19
+ * Default is `'logout'`.
20
+ */
21
+ onInactivityTimeout?: 'login' | 'logout' | 'none';
22
+ };
23
+ /**
24
+ * Service to automatically manage the Keycloak token refresh process
25
+ * based on user activity and token expiration events. This service
26
+ * integrates with Keycloak for session management and interacts with
27
+ * user activity monitoring to determine the appropriate action when
28
+ * the token expires.
29
+ *
30
+ * The service listens to `KeycloakSignal` for token-related events
31
+ * (e.g., `TokenExpired`) and provides configurable options for
32
+ * session timeout and inactivity handling.
33
+ */
34
+ export declare class AutoRefreshTokenService {
35
+ private readonly keycloak;
36
+ private readonly userActivity;
37
+ private options;
38
+ private initialized;
39
+ constructor(keycloak: Keycloak, userActivity: UserActivityService);
40
+ private get defaultOptions();
41
+ private executeOnInactivityTimeout;
42
+ private processTokenExpiredEvent;
43
+ start(options?: AutoRefreshTokenOptions): void;
44
+ static ɵfac: i0.ɵɵFactoryDeclaration<AutoRefreshTokenService, never>;
45
+ static ɵprov: i0.ɵɵInjectableDeclaration<AutoRefreshTokenService>;
46
+ }
47
+ export {};
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @license
3
+ * Copyright Mauricio Gemelli Vigolo All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by a MIT-style license that can be
6
+ * found in the LICENSE file at https://github.com/mauriciovigolo/keycloak-angular/blob/main/LICENSE.md
7
+ */
8
+ import { OnDestroy, NgZone } from '@angular/core';
9
+ import * as i0 from "@angular/core";
10
+ /**
11
+ * Service to monitor user activity in an Angular application.
12
+ * Tracks user interactions (e.g., mouse movement, touch, key presses, clicks, and scrolls)
13
+ * and updates the last activity timestamp. Consumers can check for user inactivity
14
+ * based on a configurable timeout.
15
+ *
16
+ * The service is supposed to be used in the client context and for safety, it checks during the startup
17
+ * if it is a browser context.
18
+ */
19
+ export declare class UserActivityService implements OnDestroy {
20
+ private ngZone;
21
+ /**
22
+ * Signal to store the timestamp of the last user activity.
23
+ * The timestamp is represented as the number of milliseconds since epoch.
24
+ */
25
+ private lastActivity;
26
+ /**
27
+ * Subject to signal the destruction of the service.
28
+ * Used to clean up RxJS subscriptions.
29
+ */
30
+ private destroy$;
31
+ /**
32
+ * Computed signal to expose the last user activity as a read-only signal.
33
+ */
34
+ readonly lastActivitySignal: import("@angular/core").Signal<number>;
35
+ constructor(ngZone: NgZone);
36
+ /**
37
+ * Starts monitoring user activity events (`mousemove`, `touchstart`, `keydown`, `click`, `scroll`)
38
+ * and updates the last activity timestamp using RxJS with debounce.
39
+ * The events are processed outside Angular zone for performance optimization.
40
+ */
41
+ startMonitoring(): void;
42
+ /**
43
+ * Updates the last activity timestamp to the current time.
44
+ * This method runs inside Angular's zone to ensure reactivity with Angular signals.
45
+ */
46
+ private updateLastActivity;
47
+ /**
48
+ * Retrieves the timestamp of the last recorded user activity.
49
+ * @returns {number} The last activity timestamp in milliseconds since epoch.
50
+ */
51
+ get lastActivityTime(): number;
52
+ /**
53
+ * Determines whether the user interacted with the application, meaning it is activily using the application, based on
54
+ * the specified duration.
55
+ * @param timeout - The inactivity timeout in milliseconds.
56
+ * @returns {boolean} `true` if the user is inactive, otherwise `false`.
57
+ */
58
+ isActive(timeout: number): boolean;
59
+ /**
60
+ * Cleans up RxJS subscriptions and resources when the service is destroyed.
61
+ * This method is automatically called by Angular when the service is removed.
62
+ */
63
+ ngOnDestroy(): void;
64
+ static ɵfac: i0.ɵɵFactoryDeclaration<UserActivityService, never>;
65
+ static ɵprov: i0.ɵɵInjectableDeclaration<UserActivityService>;
66
+ }