This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
jwt-authentication-result.js
65 lines (52 loc) · 1.69 KB
/
jwt-authentication-result.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
'use strict';
var nJwt = require('njwt');
function JwtAuthenticationResult(application,data) {
if (!(this instanceof JwtAuthenticationResult)) {
return new JwtAuthenticationResult(application,data);
}
/*
Take all the properties of the data response, and put them
on this object - but convert underscores to camelcase cuz
that's the node way bro
*/
Object.keys(data).reduce(function(a,key){
var newKey = key.replace(/_([A-Za-z])/g, function (g) { return g[1].toUpperCase(); });
a[newKey] = data[key];
return a;
},this);
/*
Assign application after the key reduction above,
otherwise it will get replaced with the objet literal
from the response data
*/
this.application = application;
var apiKey = application.dataStore.requestExecutor.options.client.apiKey;
if(this.accessToken){
try {
this.accessToken = nJwt.verify(this.accessToken, apiKey.secret);
this.account = {
href: this.accessToken.body.sub
};
} catch (err) {
this.error = err;
}
}
if(this.refreshToken){
try {
this.refreshToken = nJwt.verify(this.refreshToken, apiKey.secret);
} catch (err) {
this.error = err;
}
}
}
JwtAuthenticationResult.prototype.account = null;
JwtAuthenticationResult.prototype.jwt = null;
JwtAuthenticationResult.prototype.expandedJwt = null;
JwtAuthenticationResult.prototype.getAccount = function getAccount(callback) {
if (this.error) {
return callback(this.error);
}
// workaround because I don't have access to a stormpath client
this.application.dataStore.getResource(this.account.href, require('../resource/Account'), callback);
};
module.exports = JwtAuthenticationResult;