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

Commit

Permalink
fix(ostoken): add openshift token
Browse files Browse the repository at this point in the history
  • Loading branch information
dgutride authored and joshuawilson committed Mar 17, 2017
1 parent 949bffe commit 93b8a78
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { LoginModule } from './login.module';

export { AUTH_API_URL } from './src/app/shared/auth-api';
export { SSO_API_URL } from './src/app/shared/sso-api';
export { AlmUserName } from './src/app/user/alm-user-name.pipe';
export { AuthenticationService } from './src/app/auth/authentication.service';
export { Broadcaster } from './src/app/shared/broadcaster.service';
Expand Down
45 changes: 44 additions & 1 deletion src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Http, Response, Headers, RequestOptions } from '@angular/http';

import { Broadcaster } from '../shared/broadcaster.service';
import { Token } from '../user/token';
import { AUTH_API_URL } from '../shared/auth-api';
import { Observable } from 'rxjs';
import { SSO_API_URL } from '../shared/sso-api';

@Injectable()
export class AuthenticationService {
private refreshInterval: number;
private apiUrl: string;
private ssoUrl: string;
private clearTimeoutId: any;
private clearOpenShiftTimeoutId: any;

constructor(private broadcaster: Broadcaster,
@Inject(AUTH_API_URL) apiUrl: string,
@Inject(SSO_API_URL) ssoUrl: string,
private http: Http) {
this.apiUrl = apiUrl;
this.ssoUrl = ssoUrl;
}

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

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

this.onLogIn();
return true;
}
Expand All @@ -32,6 +42,7 @@ export class AuthenticationService {
logout() {
localStorage.removeItem('auth_token');
localStorage.removeItem('refresh_token');
localStorage.removeItem('openshift_token');
clearTimeout(this.clearTimeoutId);
this.refreshInterval = null;
this.broadcaster.broadcast('logout', 1);
Expand All @@ -52,6 +63,38 @@ export class AuthenticationService {
if (this.isLoggedIn()) return localStorage.getItem('auth_token');
}

getOpenShiftToken(): Observable<string> {
if (localStorage.getItem('openshift_token')) {
return Observable.of(localStorage.getItem('openshift_token'));
} else {
if (this.isLoggedIn()) {
let headers = new Headers({'Content-Type': 'application/json'});
let osTokenUrl = this.ssoUrl + 'auth/realms/fabric8/broker/openshift-v3/token';
let token = this.getToken();
headers.set('Authorization', `Bearer ${token}`);
let options = new RequestOptions({ headers: headers });
return this.http.get(osTokenUrl, options)
.map((response: Response) => {
let token = response.json() as Token;
this.clearOpenShiftTimeoutId = null;
localStorage.setItem('openshift_token', token.access_token);

let refreshInMs = Math.round(token.expires_in * .9) * 1000;
console.log('Clearing openshift token in: ' + refreshInMs + ' milliseconds.');
setTimeout(() => this.clearOpenShiftToken(), refreshInMs);
return token.access_token;
});
} else {
// user is not logged in, return empty
return Observable.of('');
}
}
}

clearOpenShiftToken() {
localStorage.removeItem('openshift_token');
}

setupRefreshTimer(refreshInSeconds: number) {
if (!this.clearTimeoutId) {
let refreshInMs = Math.round(refreshInSeconds * .9) * 1000;
Expand Down
3 changes: 3 additions & 0 deletions src/app/shared/sso-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { OpaqueToken } from '@angular/core';

export let SSO_API_URL = new OpaqueToken('sso.api.url');

0 comments on commit 93b8a78

Please sign in to comment.