Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(storage): store github/os tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dgutride authored and joshuawilson committed Apr 13, 2017
1 parent 8fd4ef7 commit 92e4bd9
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class AuthenticationService {
private ssoUrl: string;
private clearTimeoutId: any;
private refreshTokens: Subject<Token> = new Subject();
readonly openshift = 'openshift-v3';
readonly github = 'github';

constructor(
private broadcaster: Broadcaster,
Expand All @@ -31,21 +33,22 @@ export class AuthenticationService {
) {
this.apiUrl = apiUrl;
this.ssoUrl = ssoUrl;
this.openShiftToken = this.createFederatedToken('openshift-v3', (response: Response) => response.json() as Token);
this.gitHubToken = this.createFederatedToken('github', (response: Response) => this.queryAsToken(response.text()));
this.openShiftToken = this.createFederatedToken(this.openshift, (response: Response) => response.json() as Token);
this.gitHubToken = this.createFederatedToken(this.github, (response: Response) => this.queryAsToken(response.text()));
}

logIn(tokenParameter: string): boolean {
let tokenJson = decodeURIComponent(tokenParameter);
let token = this.processTokenResponse(JSON.parse(tokenJson));
this.setupRefreshTimer(token.expires_in);

// make sure old tokens are cleared out when we login again
localStorage.removeItem(this.openshift + '_token');
localStorage.removeItem(this.github + '_token');

// kick off initial token refresh
this.refreshTokens.next(token);

// make sure old openshift token is cleared out when we login again
localStorage.removeItem('openshift_token');

this.onLogIn();
return true;
}
Expand All @@ -71,6 +74,8 @@ export class AuthenticationService {
let token = localStorage.getItem('auth_token');
if (token) {
if (!this.clearTimeoutId) {
// kick off initial token refresh
this.refreshTokens.next({} as Token);
this.setupRefreshTimer(15);
}
return true;
Expand All @@ -83,6 +88,9 @@ export class AuthenticationService {
}

getOpenShiftToken(): Observable<string> {
if (localStorage.getItem(this.openshift + '_token')) {
return Observable.of(localStorage.getItem(this.openshift + '_token'));
}
return this.openShiftToken;
}

Expand Down Expand Up @@ -138,6 +146,7 @@ export class AuthenticationService {
let options = new RequestOptions({ headers: headers });
return this.http.get(tokenUrl, options)
.map(response => processToken(response))
.do(token => localStorage.setItem(broker + '_token', token.access_token))
.map(t => t.access_token);
})
.publishReplay(1);
Expand All @@ -160,7 +169,8 @@ export class AuthenticationService {
private clearSessionData(): void {
localStorage.removeItem('auth_token');
localStorage.removeItem('refresh_token');
localStorage.removeItem('openshift_token');
localStorage.removeItem(this.openshift + '_token');
localStorage.removeItem(this.github + '_token');
clearTimeout(this.clearTimeoutId);
this.refreshInterval = null;
}
Expand Down

0 comments on commit 92e4bd9

Please sign in to comment.