forked from benbaran/adal-angular4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adal.service.ts
195 lines (160 loc) · 6.43 KB
/
adal.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/// <reference path="adal-angular.d.ts" />
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/bindCallback';
import * as lib from 'adal-angular';
@Injectable()
export class AdalService {
private context: adal.AuthenticationContext = <any>null;
private user: adal.User = {
authenticated: false,
username: '',
error: '',
token: '',
profile: {}
};
constructor() { }
public init(configOptions: adal.Config) {
if (!configOptions) {
throw new Error('You must set config, when calling init.');
}
// redirect and logout_redirect are set to current location by default
const existingHash = window.location.hash;
let pathDefault = window.location.href;
if (existingHash) {
pathDefault = pathDefault.replace(existingHash, '');
}
configOptions.redirectUri = configOptions.redirectUri || pathDefault;
configOptions.postLogoutRedirectUri = configOptions.postLogoutRedirectUri || pathDefault;
// create instance with given config
this.context = lib.inject(configOptions);
window.AuthenticationContext = this.context.constructor;
// loginresource is used to set authenticated status
this.updateDataFromCache(<any>this.context.config.loginResource);
}
public get config(): adal.Config {
return this.context.config;
}
public get userInfo(): adal.User {
return this.user;
}
public login(): void {
this.context.login();
}
public loginInProgress(): boolean {
return this.context.loginInProgress();
}
public logOut(): void {
this.context.logOut();
}
public handleWindowCallback(): void {
const hash = window.location.hash;
if (this.context.isCallback(hash)) {
const requestInfo = this.context.getRequestInfo(hash);
this.context.saveTokenFromHash(requestInfo);
if (requestInfo.requestType === this.context.REQUEST_TYPE.LOGIN) {
this.updateDataFromCache(<any>this.context.config.loginResource);
} else if (requestInfo.requestType === this.context.REQUEST_TYPE.RENEW_TOKEN) {
this.context.callback = window.parent.callBackMappedToRenewStates[requestInfo.stateResponse];
}
if (requestInfo.stateMatch) {
if (typeof this.context.callback === 'function') {
if (requestInfo.requestType === this.context.REQUEST_TYPE.RENEW_TOKEN) {
// Idtoken or Accestoken can be renewed
if (requestInfo.parameters['access_token']) {
this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION)
, requestInfo.parameters['access_token']);
} else if (requestInfo.parameters['id_token']) {
this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION)
, requestInfo.parameters['id_token']);
} else if (requestInfo.parameters['error']) {
this.context.callback(this.context._getItem(this.context.CONSTANTS.STORAGE.ERROR_DESCRIPTION), null);
this.context._renewFailed = true;
}
}
}
}
}
// Remove hash from url
if (window.location.hash) {
if (window.history.replaceState) {
window.history.replaceState('', '/', window.location.pathname)
} else {
window.location.hash = '';
}
}
}
public getCachedToken(resource: string): string {
return this.context.getCachedToken(resource);
}
public acquireToken(resource: string) {
const _this = this; // save outer this for inner function
let errorMessage: string;
return Observable.bindCallback(acquireTokenInternal, function (token: string) {
if (!token && errorMessage) {
throw (errorMessage);
}
return token;
})();
function acquireTokenInternal(cb: any) {
let s: any = null;
_this.context.acquireToken(resource, (error: string, tokenOut: string) => {
if (error) {
_this.context.error('Error when acquiring token for resource: ' + resource, error);
errorMessage = error;
cb(<any>null);
} else {
cb(tokenOut);
s = tokenOut;
}
});
return s;
}
}
public getUser(): Observable<any> {
return Observable.bindCallback((cb: any) => {
this.context.getUser(function (error: string, user: any) {
if (error) {
this.context.error('Error when getting user', error);
cb(null);
} else {
cb(user);
}
});
})();
}
public clearCache(): void {
this.context.clearCache();
}
public clearCacheForResource(resource: string): void {
this.context.clearCacheForResource(resource);
}
public info(message: string): void {
this.context.info(message);
}
public verbose(message: string): void {
this.context.verbose(message);
}
public GetResourceForEndpoint(url: string): string {
return this.context.getResourceForEndpoint(url);
}
public refreshDataFromCache() {
this.updateDataFromCache(<any>this.context.config.loginResource);
}
private updateDataFromCache(resource: string): void {
const token = this.context.getCachedToken(resource);
this.user.authenticated = token !== null && token.length > 0;
const user = this.context.getCachedUser() || { username: '', profile: <any>undefined };
if (user) {
this.user.username = user.username;
this.user.profile = user.profile;
this.user.token = token;
this.user.error = this.context.getLoginError();
} else {
this.user.username = '';
this.user.profile = {};
this.user.token = '';
this.user.error = '';
}
}
}