ngx-adal-8
Version:
Angular 2+ ADAL Wrapper library
332 lines (326 loc) • 9.51 kB
JavaScript
import { Injectable, Inject, ɵɵdefineInjectable, ɵɵinject, NgModule, Optional, SkipSelf } from '@angular/core';
import { map, mergeMap } from 'rxjs/operators';
import { bindCallback } from 'rxjs';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxAdalService {
/**
* @param {?} adalConfig
*/
constructor(adalConfig) {
this.adalConfig = adalConfig;
adalConfig.redirectUri = `${window.location.origin}/${adalConfig.redirectUri}`,
adalConfig.postLogoutRedirectUri = `${window.location.origin}/${adalConfig.postLogoutRedirectUri}`,
this.context = new AuthenticationContext(adalConfig);
this.handleCallback();
}
// constructor(
// @Inject(ADAL_CONFIG) private adalConfig: any, @Inject(AUTHENTICATION_CONTEXT) private context: typeof AuthenticationContext) {
// this.handleCallback();
// }
/**
* @return {?}
*/
get LoggedInUserEmail() {
if (this.isAuthenticated) {
return this.context.getCachedUser().userName;
}
return '';
}
/**
* @return {?}
*/
get LoggedInUserName() {
if (this.isAuthenticated) {
return this.context.getCachedUser().profile.name;
}
return '';
}
/**
* @return {?}
*/
login() {
this.context.login();
}
/**
* @return {?}
*/
logout() {
this.context.logOut();
}
/**
* @param {?} url
* @return {?}
*/
GetResourceForEndpoint(url) {
return this.context.getResourceForEndpoint(url);
// let resource = null;
// if (url) {
// resource = this.context.getResourceForEndpoint(url);
// if (!resource) {
// resource = this.adalConfig.clientId;
// }
// }
// return resource;
}
/**
* @param {?} url
* @return {?}
*/
RenewToken(url) {
/** @type {?} */
const resource = this.GetResourceForEndpoint(url);
return this.context.clearCacheForResource(resource); // Trigger the ADAL token renew
}
/**
* @param {?} url
* @return {?}
*/
acquireToken(url) {
/** @type {?} */
const _this = this;
// save outer this for inner function
/** @type {?} */
let errorMessage;
return bindCallback(acquireTokenInternal)().pipe(map((/**
* @param {?} token
* @return {?}
*/
(token) => {
if (!token && errorMessage) {
throw errorMessage;
}
return token;
})));
/**
* @param {?} cb
* @return {?}
*/
function acquireTokenInternal(cb) {
/** @type {?} */
let s = null;
/** @type {?} */
let resource;
resource = _this.GetResourceForEndpoint(url);
_this.context.acquireToken(resource, (/**
* @param {?} error
* @param {?} tokenOut
* @return {?}
*/
(error, tokenOut) => {
if (error) {
_this.context.error('Error when acquiring token for resource: ' + resource, error);
errorMessage = error;
cb((/** @type {?} */ (null)));
}
else {
cb(tokenOut);
s = tokenOut;
}
}));
return s;
}
}
/**
* @param {?} url
* @return {?}
*/
getToken(url) {
/** @type {?} */
const resource = this.context.getResourceForEndpoint(url);
/** @type {?} */
const storage = this.adalConfig.cacheLocation;
// ?
/** @type {?} */
let key;
if (resource) {
key = 'adal.access.token.key' + resource;
}
else {
key = 'adal.idtoken';
}
if (storage === 'localStorage') {
return localStorage.getItem(key);
}
else {
return sessionStorage.getItem(key);
}
}
/**
* @return {?}
*/
handleCallback() {
this.context.handleWindowCallback();
}
/**
* @return {?}
*/
get userInfo() {
return this.context.getCachedUser();
}
/**
* @return {?}
*/
get accessToken() {
return this.context.getCachedToken(this.adalConfig.clientId);
}
/**
* @return {?}
*/
get isAuthenticated() {
return this.userInfo && this.accessToken ? true : false;
}
}
NgxAdalService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgxAdalService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: ['adalConfig',] }] }
];
/** @nocollapse */ NgxAdalService.ngInjectableDef = ɵɵdefineInjectable({ factory: function NgxAdalService_Factory() { return new NgxAdalService(ɵɵinject("adalConfig")); }, token: NgxAdalService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxAdalInterceptor {
/**
* @param {?} adal
*/
constructor(adal) {
this.adal = adal;
}
/**
* @param {?} request
* @param {?} next
* @return {?}
*/
intercept(request, next) {
// retrieve resource from endpoints configuration
/** @type {?} */
const resource = this.adal.GetResourceForEndpoint(request.url);
if (!resource) {
return next.handle(request);
}
if (!this.adal.isAuthenticated) {
this.adal.RenewToken(request.url);
}
return this.adal.acquireToken(request.url).pipe(mergeMap((/**
* @param {?} token
* @return {?}
*/
(token) => {
/** @type {?} */
const authorizedRequest = request.clone({
setHeaders: {
Authorization: 'Bearer ' + token,
'content-Type': 'application/json; charset=utf-8',
Accept: 'application/json'
}
});
return next.handle(authorizedRequest);
})));
}
}
NgxAdalInterceptor.decorators = [
{ type: Injectable }
];
/** @nocollapse */
NgxAdalInterceptor.ctorParameters = () => [
{ type: NgxAdalService }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxAdalGuard {
/**
* @param {?} adalSvc
*/
constructor(adalSvc) {
this.adalSvc = adalSvc;
}
/**
* @param {?} route
* @param {?} state
* @return {?}
*/
canActivate(route, state) {
if (this.adalSvc.isAuthenticated) {
return true;
}
else {
this.adalSvc.login();
return false;
}
}
/**
* @param {?} childRoute
* @param {?} state
* @return {?}
*/
canActivateChild(childRoute, state) {
return this.canActivate(childRoute, state);
}
}
NgxAdalGuard.decorators = [
{ type: Injectable }
];
/** @nocollapse */
NgxAdalGuard.ctorParameters = () => [
{ type: NgxAdalService }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
// import { ADAL_CONFIG, authContextFactory, AUTHENTICATION_CONTEXT } from './di_tokens';
class NgxAdalModule {
/**
* @param {?} parentModule
*/
constructor(parentModule) {
// https://angular.io/styleguide#!#04-12
if (parentModule) {
throw new Error('NgxAdalModule is already loaded. Import it in the AppModule only');
}
}
/**
* @param {?} adalConfig
* @return {?}
*/
static forRoot(adalConfig) {
return {
ngModule: NgxAdalModule,
providers: [
[NgxAdalService, { provide: 'adalConfig', useValue: adalConfig }]
]
};
}
}
NgxAdalModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
providers: [
NgxAdalGuard,
NgxAdalService,
{
provide: HTTP_INTERCEPTORS,
useClass: NgxAdalInterceptor,
multi: true
}
]
},] }
];
/** @nocollapse */
NgxAdalModule.ctorParameters = () => [
{ type: NgxAdalModule, decorators: [{ type: Optional }, { type: SkipSelf }] }
];
export { NgxAdalGuard, NgxAdalInterceptor, NgxAdalModule, NgxAdalService };
//# sourceMappingURL=ngx-adal-8.js.map