Skip to content

Commit 38a15b2

Browse files
committed
Add resume option for OIDC authentication
1 parent b3ee011 commit 38a15b2

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
### Added
11+
12+
- Resume previously authenticated sessions via `AuthProvider.resume()`
13+
1014
## [2.10.0] - 2026-02-25
1115

1216
### Added

openeo.d.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ declare namespace OpenEO {
8686
* @param {?string} token
8787
*/
8888
setToken(token: string | null): void;
89+
/**
90+
* Tries to resume an existing session.
91+
*
92+
* @param {...any} args
93+
* @returns {boolean} `true` if the session could be resumed, `false` otherwise
94+
*/
95+
resume(...args: any[]): boolean;
8996
/**
9097
* Abstract method that extending classes implement the login process with.
9198
*
@@ -569,6 +576,15 @@ declare namespace OpenEO {
569576
* @type {Array.<OidcClient>}
570577
*/
571578
defaultClients: Array<OidcClient>;
579+
/**
580+
* Additional parameters to include in authorization requests.
581+
*
582+
* As defined by the API, these parameters MUST be included when
583+
* requesting the authorization endpoint.
584+
*
585+
* @type {object.<string, *>}
586+
*/
587+
authorizationParameters: Record<string, any>;
572588
/**
573589
* The detected default Client.
574590
*
@@ -608,7 +624,16 @@ declare namespace OpenEO {
608624
* @see https://github.com/IdentityModel/oidc-client-js/wiki#other-optional-settings
609625
* @see {OidcProvider#refreshTokenScope}
610626
*/
611-
login(options?: Record<string, any>, requestRefreshToken?: boolean): Promise<void>;
627+
login(options?: object<string, any>, requestRefreshToken?: boolean): Promise<void>;
628+
/**
629+
* Restores a previously established OIDC session from storage.
630+
*
631+
* @async
632+
* @param {object.<string, *>} [options={}] - Additional options passed to the OIDC UserManager.
633+
* @returns {Promise<boolean>} `true` if the session could be resumed, `false` otherwise.
634+
* @see https://github.com/IdentityModel/oidc-client-js/wiki#usermanager
635+
*/
636+
resume(options?: object<string, any>): Promise<boolean>;
612637
/**
613638
* Returns the options for the OIDC client library.
614639
*

src/authprovider.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ class AuthProvider {
127127
}
128128
}
129129

130+
/**
131+
* Tries to resume an existing session.
132+
*
133+
* @param {...any} args
134+
* @returns {boolean} `true` if the session could be resumed, `false` otherwise
135+
*/
136+
async resume(...args) { // eslint-disable-line no-unused-vars
137+
return false;
138+
}
139+
130140
/**
131141
* Abstract method that extending classes implement the login process with.
132142
*

src/oidcprovider.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ class OidcProvider extends AuthProvider {
202202
}
203203
}
204204

205+
/**
206+
* Restores a previously established OIDC session from storage.
207+
*
208+
* @async
209+
* @param {object.<string, *>} [options={}] - Additional options passed to the OIDC UserManager.
210+
* @returns {Promise<boolean>} `true` if the session could be resumed, `false` otherwise.
211+
* @see https://github.com/IdentityModel/oidc-client-js/wiki#usermanager
212+
*/
213+
async resume(options = {}) {
214+
this.manager = new Oidc.UserManager(this.getOptions(options));
215+
this.addListener('UserLoaded', async () => this.setUser(await this.manager.getUser()), 'js-client');
216+
this.addListener('AccessTokenExpired', () => this.setUser(null), 'js-client');
217+
218+
let user = await this.manager.getUser();
219+
if (user && user.expired && user.refresh_token) {
220+
user = await this.manager.signinSilent();
221+
}
222+
223+
if (user && !user.expired) {
224+
this.setUser(user);
225+
return true;
226+
}
227+
228+
return false;
229+
}
230+
205231
/**
206232
* Logout from the established session.
207233
*
@@ -395,4 +421,15 @@ OidcProvider.grants = [
395421
'implicit'
396422
];
397423

424+
/**
425+
* The WebStorageStateStore class from oidc-client.
426+
*
427+
* Can be used to configure the `userStore` option for the UserManager,
428+
* e.g. to persist tokens in localStorage:
429+
* `new OidcProvider.WebStorageStateStore({ store: window.localStorage })`
430+
*
431+
* @type {Oidc.WebStorageStateStore}
432+
*/
433+
OidcProvider.WebStorageStateStore = Oidc.WebStorageStateStore;
434+
398435
module.exports = OidcProvider;

0 commit comments

Comments
 (0)