Skip to content

Instantly share code, notes, and snippets.

@HNeukermans
Created October 26, 2016 13:54
Show Gist options
  • Select an option

  • Save HNeukermans/b282bb6779c91e7b1c98caca24a2c5b4 to your computer and use it in GitHub Desktop.

Select an option

Save HNeukermans/b282bb6779c91e7b1c98caca24a2c5b4 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import 'expose?AuthenticationContext!../../../node_modules/adal-angular/lib/adal.js';
import { AuthContext } from './authentication.context';
import { Observable, AsyncSubject, BehaviorSubject } from 'rxjs';
let createRawAuthContext: adal.AuthenticationContextStatic = AuthenticationContext;
declare let Logging: adal.Logging;
@Injectable()
export class AuthProvider {
private _context: AuthContext = null;
constructor() {
console.log('Constructing AuthProvider...');
}
getContext(): AuthContext {
console.log('getContext...');
if (this._context != null) return this._context;
let config = this.getConfig();
let rawContext = new createRawAuthContext(config);
this.enableRawLogging();
this._context = <AuthContext> this.extend(rawContext);
return this._context;
}
private getConfig(): adal.Config {
let config: adal.Config = {
tenant: 'xxx',
clientId: 'xxxx', /
postLogoutRedirectUri: window.location.origin + '/',
redirectUri: window.location.origin + '/'
};
return config;
}
private extend(context: adal.AuthenticationContext): any {
(<any> context).isLoggedIn = function () {
return context.getCachedUser() != null;
};
(<any> context).processAdRedirect = function (): Observable<string> {
let observable = new BehaviorSubject('init');
console.log('process ad redirect...');
//observable.next('login:succes');
context.verbose('Processing the hash: ' + location.hash);
if (context.isCallback(location.hash)) {
let requestInfo = context.getRequestInfo(location.hash);
context.saveTokenFromHash(requestInfo);
console.log('is statematch: ' + requestInfo.stateMatch);
console.log('is requestType: ' + requestInfo.requestType);
if (requestInfo.stateMatch) {
if (requestInfo.requestType === "RENEW_TOKEN") {
//handle renewed tokens
} else if (requestInfo.requestType === "LOGIN") {
observable.next('login:succes');
//handle login successfully
//updateDataFromCache(_adal.config.loginResource);
}
}
}
//observable.next('login:succes');
return observable;
};
return context;
}
private enableRawLogging(): void {
Logging.level = 3;
Logging.log = this.log;
}
private log(message: string): void {
console.log('adal logging...');
console.log(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment