-
Notifications
You must be signed in to change notification settings - Fork 3
/
jwt.js
282 lines (247 loc) · 8.13 KB
/
jwt.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import RSVP from 'rsvp';
import fetch from 'fetch';
import {
run
} from '@ember/runloop';
import {
isEmpty
} from '@ember/utils';
import {
warn
} from '@ember/debug';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
export default BaseAuthenticator.extend({
/**
The endpoint on the server that the authentication request is sent to.
@property serverTokenEndpoint
@type String
@default '/api/token-auth'
@public
*/
serverTokenEndpoint: '/api/token-auth',
/**
The endpoint on the server that the refresh request is sent to.
@property serverRefreshTokenEndpoint
@type String
@default '/api/token-refresh'
@public
*/
serverRefreshTokenEndpoint: '/api/token-refresh',
/**
The identification attribute name. __This will be used in the request.__
@property identificationAttributeName
@type String
@default 'username'
@public
*/
identificationAttributeName: 'username',
/**
The password attribute name. __This will be used in the request.__
@property passwordAttributeName
@type String
@default 'password'
@public
*/
passwordAttributeName: 'password',
/**
Time (ms) before the JWT expires to call the serverRefreshTokenEndpoint
@property refreshTokenOffset
@type Integer
@default '1000'
@public
*/
refreshTokenOffset: 1000,
/**
Time (ms) after a call to serverRefreshTokenEndpoint during which no
further refresh token calls will be made.
Used to reduce the number of refresh token calls made when the same
app is simultaneously open in multiple tabs/windows.
For example: if the JWT is set to expire 30s after being issued, and the
'refreshTokenAfter' is set at 25s, requests may only be sent out in the
last 5 seconds.
@property refreshTokenAfter
@type Integer
@default '25000'
@public
*/
refreshTokenAfter: 25000,
_refreshTokenTimeout: null,
/**
Restores the session from a session data object; __will return a resolving
promise when there is a non-empty `access_token` in the session data__ and
a rejecting promise otherwise.
@method restore
@param {Object} data The data to restore the session from
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session becoming or remaining authenticated
@public
*/
restore(data) {
if (this._refreshTokenTimeout) {
run.cancel(this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
}
return this._refreshAccessToken(data);
},
/**
Authenticates the session with the specified `identification` & `password`.
Issues a `POST` request to the serverTokenEndpoint and receives the JWT token in response.
If the credentials are valid and thus authentication succeeds, a promise that resolves with the
server's response is returned, otherwise a promise that rejects with the error as returned by
the server is returned.
This method also schedules refresh requests for the access token before it expires.
TODO: make the refresh token support optional
@method authenticate
@param {String} identification The resource owner username
@param {String} password The resource owner password
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session becoming authenticated
@public
*/
authenticate(identification, password) {
return new RSVP.Promise((resolve, reject) => {
const {
identificationAttributeName,
passwordAttributeName
} = this.getProperties('identificationAttributeName', 'passwordAttributeName');
const data = {
[identificationAttributeName]: identification,
[passwordAttributeName]: password
};
const serverTokenEndpoint = this.get('serverTokenEndpoint');
this.makeRequest(serverTokenEndpoint, data)
.then((response) => {
return this._validateTokenAndScheduleRefresh(response);
})
.then((response) => {
run(() => {
resolve(response);
});
})
.catch((reason) => {
if (reason.responseJSON) {
reason = reason.responseJSON;
}
run(null, reject, reason);
});
});
},
/**
Deletes the JWT token
@method invalidate
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session being invalidated
@public
*/
invalidate() {
run.cancel(this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
return RSVP.Promise.resolve();
},
/**
Makes a request to the JWT server.
@method makeRequest
@param {String} url The request URL
@param {Object} data The request data
@param {Object} headers Additional headers to send in request
@return {Promise} A promise that resolves with the response object
@protected
*/
makeRequest(url, data, headers = {}) {
headers['Content-Type'] = 'application/json';
const options = {
body: JSON.stringify(data),
headers,
method: 'POST'
};
return new RSVP.Promise((resolve, reject) => {
fetch(url, options).then((response) => {
response.text().then((text) => {
let json = text ? JSON.parse(text) : {};
if (!response.ok) {
response.responseJSON = json;
reject(response);
} else {
window.localStorage.setItem('jwtLastRefreshAt', Date.now());
resolve(json);
}
});
}).catch(reject);
});
},
/**
Validate that the response contains a valid JWT token
*/
_validate(data) {
// Validate that a token is present
if (isEmpty(data['token'])) {
return false;
}
let jwtToken = data['token'].split('.');
// Validate the three elements of a JWT are present
if (jwtToken.length !== 3) {
return false;
}
// Validate the JWT header
let jwtHeader = JSON.parse(atob(jwtToken[0]));
if (!jwtHeader.alg) {
return false;
}
// Validate the JWT payload:
// iat: issued at time
// exp: expiration time
let jwtPayload = JSON.parse(atob(jwtToken[1]));
if (isNaN(jwtPayload['iat']) || isNaN(jwtPayload['exp'])) {
return false;
}
return true;
},
_scheduleTokenRefresh(data) {
const jwtPayload = JSON.parse(atob(data.token.split('.')[1]));
const jwtPayloadExpiresAt = jwtPayload.exp;
const offset = 1000; // Refresh 1 sec before JWT expires
const now = Date.now();
const waitMs = (jwtPayloadExpiresAt * 1000) - now - offset; //expiresAt is in sec
if (this._refreshTokenTimeout) {
run.cancel(this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
}
// Reschedule if the JWT is still valid
if (waitMs > 0) {
this._refreshTokenTimeout = run.later(this, this._refreshAccessToken, data, waitMs);
}
},
_refreshAccessToken(data) {
var timeElapsedSinceLastRefresh = Date.now() - window.localStorage.getItem('jwtLastRefreshAt')
if (timeElapsedSinceLastRefresh <= this.get('refreshTokenAfter')) {
// Request attempted too soon! Reschedule
return this._validateTokenAndScheduleRefresh(data);
}
const serverRefreshTokenEndpoint = this.get('serverRefreshTokenEndpoint');
return new RSVP.Promise((resolve, reject) => {
this.makeRequest(serverRefreshTokenEndpoint, data)
.then((response) => {
return this._validateTokenAndScheduleRefresh(response);
})
.then((response) => {
run(() => {
this.trigger('sessionDataUpdated', response);
resolve(response);
});
})
.catch((reason) => {
if (reason.responseJSON) {
reason = JSON.stringify(reason.responseJSON);
}
warn(`JWT token could not be refreshed: ${reason}.`, false, {
id: 'ember-simple-auth-jwt.failedJWTTokenRefresh'
});
reject();
});
});
},
_validateTokenAndScheduleRefresh(response) {
if (!this._validate(response)) {
return RSVP.Promise.reject('token is missing or invalid in server response');
}
this._scheduleTokenRefresh(response);
return RSVP.Promise.resolve(response);
}
});