Skip to content

Commit 75d8ab8

Browse files
authored
Merge pull request #70 from benbaran/fix_token_refresh
Looks good to me. I'll rebuild and test before publishing to NPM.
2 parents 8a9357d + 6ad77aa commit 75d8ab8

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

adal-angular.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ declare namespace adal {
4949
authenticated: any;
5050
error: any;
5151
token: any;
52+
loginCached: boolean;
5253
}
5354

5455
/**

adal.service.ts

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
/// <reference path="adal-angular.d.ts" />
22

33
import { Injectable } from '@angular/core';
4-
import { Observable, bindCallback } from 'rxjs';
4+
5+
import { Observable, bindCallback, timer } from 'rxjs';
56
import { map } from 'rxjs/operators';
7+
68
import * as lib from 'adal-angular';
79

810
@Injectable()
911
export class AdalService {
1012

11-
private context: adal.AuthenticationContext = null as any;
13+
14+
private context: adal.AuthenticationContext = <any>null;
15+
private loginRefreshTimer = <any>null;
16+
1217

1318
private user: adal.User = {
1419
authenticated: false,
1520
userName: '',
1621
error: '',
1722
token: '',
18-
profile: {}
23+
profile: {},
24+
loginCached: false
1925
};
2026

2127
constructor() { }
@@ -42,7 +48,19 @@ export class AdalService {
4248
window.AuthenticationContext = this.context.constructor;
4349

4450
// loginresource is used to set authenticated status
45-
this.updateDataFromCache(this.context.config.loginResource as string);
51+
52+
this.updateDataFromCache();
53+
54+
if (this.user.loginCached && !this.user.authenticated && window.self == window.top) {
55+
this.refreshLoginToken();
56+
} else if (this.user.loginCached && this.user.authenticated && !this.loginRefreshTimer && window.self == window.top) {
57+
// Get expiration of login token
58+
let exp = this.context._getItem(this.context.CONSTANTS.STORAGE.EXPIRATION_KEY + <any>this.context.config.loginResource);
59+
this.loginRefreshTimer = timer(exp - this.now() - 300).subscribe((x) => {
60+
this.refreshLoginToken()
61+
});
62+
}
63+
4664
}
4765

4866
public get config(): adal.Config {
@@ -71,8 +89,7 @@ export class AdalService {
7189
const requestInfo = this.context.getRequestInfo(hash);
7290
this.context.saveTokenFromHash(requestInfo);
7391
if (requestInfo.requestType === this.context.REQUEST_TYPE.LOGIN) {
74-
this.updateDataFromCache(this.context.config.loginResource as string);
75-
92+
this.updateDataFromCache();
7693
} else if (requestInfo.requestType === this.context.REQUEST_TYPE.RENEW_TOKEN) {
7794
this.context.callback = window.parent.callBackMappedToRenewStates[requestInfo.stateResponse];
7895
}
@@ -165,24 +182,56 @@ export class AdalService {
165182
return this.context.getResourceForEndpoint(url);
166183
}
167184

168-
public refreshDataFromCache(): void {
169-
this.updateDataFromCache(this.context.config.loginResource as string);
185+
186+
public refreshDataFromCache() {
187+
this.updateDataFromCache();
188+
170189
}
171190

172-
private updateDataFromCache(resource: string): void {
173-
const token = this.context.getCachedToken(resource);
191+
private updateDataFromCache(): void {
192+
const token = this.context.getCachedToken(<any>this.context.config.loginResource);
174193
this.user.authenticated = token !== null && token.length > 0;
175-
const user = this.context.getCachedUser() || { userName: '', profile: undefined };
194+
195+
const user = this.context.getCachedUser();
196+
176197
if (user) {
177198
this.user.userName = user.userName;
178199
this.user.profile = user.profile;
179200
this.user.token = token;
180201
this.user.error = this.context.getLoginError();
202+
this.user.loginCached = true;
181203
} else {
182204
this.user.userName = '';
183205
this.user.profile = {};
184206
this.user.token = '';
185-
this.user.error = '';
207+
this.user.error = this.context.getLoginError();
208+
this.user.loginCached = false;
186209
}
187210
}
211+
212+
private refreshLoginToken(): void {
213+
if (!this.user.loginCached) throw ("User not logged in");
214+
this.acquireToken(<any>this.context.config.loginResource).subscribe((token: string) => {
215+
this.user.token = token;
216+
if (this.user.authenticated == false) {
217+
this.user.authenticated = true;
218+
this.user.error = '';
219+
window.location.reload();
220+
} else {
221+
// Get expiration of login token
222+
let exp = this.context._getItem(this.context.CONSTANTS.STORAGE.EXPIRATION_KEY + <any>this.context.config.loginResource);
223+
if (this.loginRefreshTimer) this.loginRefreshTimer.unsubscribe();
224+
this.loginRefreshTimer = timer(exp - this.now() - 300).subscribe((x) => {
225+
this.refreshLoginToken()
226+
});
227+
}
228+
}, (error: string) => {
229+
this.user.authenticated = false;
230+
this.user.error = this.context.getLoginError();
231+
});
232+
}
233+
234+
private now(): number {
235+
return Math.round(new Date().getTime() / 1000.0);
236+
};
188237
}

0 commit comments

Comments
 (0)