Skip to content

Commit c8ea6aa

Browse files
xkureckHejdaJakub
authored andcommitted
fix: fix refresh token
* fix validity of access token * fix storing the refresh token right after the login in
1 parent 9a26d36 commit c8ea6aa

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

libs/perun/services/src/lib/auth.service.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export class AuthService {
3737

3838
getClientConfig(): AuthConfig {
3939
const filterValue = this.setIdpFilter();
40+
41+
//The window of time (in seconds) to allow the current time to deviate when validating id_token's iat and exp values. Default value is 10 minutes. This set it up to 1 sec.
42+
const clockSkewInSec = 1;
43+
4044
const customQueryParams = !filterValue ? {} : { acr_values: filterValue };
4145
if (
4246
this.store.get('oidc_client', 'oauth_scopes').split(' ').includes('offline_access') &&
@@ -57,6 +61,7 @@ export class AuthService {
5761
postLogoutRedirectUri: this.store.get('oidc_client', 'oauth_post_logout_redirect_uri'),
5862
responseType: this.store.get('oidc_client', 'oauth_response_type'),
5963
scope: this.store.get('oidc_client', 'oauth_scopes'),
64+
clockSkewInSec: clockSkewInSec,
6065
// sessionChecksEnabled: true,
6166
customQueryParams: customQueryParams,
6267
};
@@ -113,6 +118,7 @@ export class AuthService {
113118

114119
if (currentPathname === '/api-callback') {
115120
return this.handleAuthCallback()
121+
.then(() => localStorage.setItem('refresh_token', this.oauthService.getRefreshToken()))
116122
.then(() => this.startRefreshToken())
117123
.then(() => this.redirectToOriginDestination());
118124
} else {
@@ -126,9 +132,7 @@ export class AuthService {
126132
return this.isLoggedInPromise().then((isLoggedIn) => {
127133
if (isLoggedIn) {
128134
this.oauthService.events.pipe(filter((e) => e.type === 'token_expires')).subscribe(() => {
129-
this.oauthService.refreshToken().then((response) => {
130-
localStorage.setItem('refresh_token', response['refresh_token']);
131-
});
135+
this.refreshAndStoreToken()
132136
});
133137
return true;
134138
}
@@ -208,7 +212,7 @@ export class AuthService {
208212
sessionStorage.setItem('refresh_token', localStorage.getItem('refresh_token'));
209213
return this.oauthService
210214
.loadDiscoveryDocument()
211-
.then(() => this.oauthService.refreshToken())
215+
.then(() => this.refreshAndStoreToken())
212216
.then(() => Promise.resolve())
213217
.catch((err) => err);
214218
} else {
@@ -242,9 +246,6 @@ export class AuthService {
242246

243247
return false;
244248
}
245-
this.oauthService
246-
.loadDiscoveryDocument()
247-
.then(() => localStorage.setItem('refresh_token', this.oauthService.getRefreshToken()));
248249
return true;
249250
});
250251
}
@@ -291,4 +292,13 @@ export class AuthService {
291292
public getIdpFilter(): string {
292293
return this.filterShortname;
293294
}
295+
296+
private refreshAndStoreToken(): Promise<boolean> {
297+
return this.oauthService.refreshToken().then((response) => {
298+
localStorage.setItem('refresh_token', response['refresh_token']);
299+
return true;
300+
}, () => {
301+
return false;
302+
});
303+
}
294304
}

0 commit comments

Comments
 (0)