-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathauthentication.js
169 lines (138 loc) · 4.37 KB
/
authentication.js
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
import {inject} from 'aurelia-dependency-injection';
import {BaseConfig} from './base-config';
import {Storage} from './storage';
import {joinUrl, isObject, isString} from './auth-utilities';
@inject(Storage, BaseConfig)
export class Authentication {
constructor(storage, config) {
this.storage = storage;
this.config = config.current;
this.tokenName = this.config.tokenPrefix ?
this.config.tokenPrefix + '_' + this.config.tokenName : this.config.tokenName;
this.idTokenName = this.config.tokenPrefix ?
this.config.tokenPrefix + '_' + this.config.idTokenName : this.config.idTokenName;
}
getLoginRoute() {
return this.config.loginRoute;
}
getLoginRedirect() {
return this.initialUrl || this.config.loginRedirect;
}
getLoginUrl() {
return this.config.baseUrl ?
joinUrl(this.config.baseUrl, this.config.loginUrl) : this.config.loginUrl;
}
getSignupUrl() {
return this.config.baseUrl ?
joinUrl(this.config.baseUrl, this.config.signupUrl) : this.config.signupUrl;
}
getProfileUrl() {
return this.config.baseUrl ?
joinUrl(this.config.baseUrl, this.config.profileUrl) : this.config.profileUrl;
}
getToken() {
return this.storage.get(this.tokenName);
}
getPayload() {
let token = this.storage.get(this.tokenName);
return this.decomposeToken(token);
}
decomposeToken(token) {
if (token && token.split('.').length === 3) {
let base64Url = token.split('.')[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
try {
return JSON.parse(decodeURIComponent(escape(window.atob(base64))));
} catch (error) {
return null;
}
}
}
setInitialUrl(url) {
this.initialUrl = url;
}
setToken(response, redirect) {
// access token handling
let accessToken = response && response[this.config.responseTokenProp];
let tokenToStore;
if (accessToken) {
if (isObject(accessToken) && isObject(accessToken.data)) {
response = accessToken;
} else if (isString(accessToken)) {
tokenToStore = accessToken;
}
}
if (!tokenToStore && response) {
tokenToStore = this.config.tokenRoot && response[this.config.tokenRoot] ?
response[this.config.tokenRoot][this.config.tokenName] : response[this.config.tokenName];
}
if (tokenToStore) {
this.storage.set(this.tokenName, tokenToStore);
}
// id token handling
let idToken = response && response[this.config.responseIdTokenProp];
if (idToken) {
this.storage.set(this.idTokenName, idToken);
}
if (this.config.loginRedirect && !redirect) {
window.location.href = this.getLoginRedirect();
} else if (redirect && isString(redirect)) {
window.location.href = window.encodeURI(redirect);
}
}
removeToken() {
this.storage.remove(this.tokenName);
}
isAuthenticated() {
let token = this.storage.get(this.tokenName);
// There's no token, so user is not authenticated.
if (!token) {
return false;
}
// There is a token, but in a different format. Return true.
if (token.split('.').length !== 3) {
return true;
}
let exp;
try {
let base64Url = token.split('.')[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
exp = JSON.parse(window.atob(base64)).exp;
} catch (error) {
return false;
}
if (exp) {
return Math.round(new Date().getTime() / 1000) <= exp;
}
return true;
}
logout(redirect) {
return new Promise(resolve => {
this.storage.remove(this.tokenName);
if (this.config.logoutRedirect && !redirect) {
window.location.href = this.config.logoutRedirect;
} else if (isString(redirect)) {
window.location.href = redirect;
}
resolve();
});
}
get tokenInterceptor() {
let config = this.config;
let storage = this.storage;
let auth = this;
return {
request(request) {
if (auth.isAuthenticated() && config.httpInterceptor) {
let tokenName = config.tokenPrefix ? `${config.tokenPrefix}_${config.tokenName}` : config.tokenName;
let token = storage.get(tokenName);
if (config.authHeader && config.authToken) {
token = `${config.authToken} ${token}`;
}
request.headers.set(config.authHeader, token);
}
return request;
}
};
}
}