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_authenticator_it.js
244 lines (199 loc) · 7.28 KB
/
jwt_authenticator_it.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
var common = require('../common');
var helpers = require('./helpers');
var assert = common.assert;
var stormpath = require('../../');
var nJwt = require('njwt');
var JwtAuthenticationResult = require('../../lib/jwt/jwt-authentication-result');
function assertUnauthenticatedResponse(done){
return function(err){
assert.isNotNull(err);
assert.equal(err.statusCode,401);
done();
};
}
function assertJwtAuthenticationResult(done){
return function(err,response){
assert.isNull(err);
assert.instanceOf(response, JwtAuthenticationResult);
assert.isDefined(response, 'jwt');
assert.isDefined(response, 'expandedJwt');
assert.isDefined(response.account);
done();
};
}
describe('JwtAuthenticator',function(){
var application, application2, passwordGrantResponse;
var newAccount;
var unsignedToken;
var expiredToken;
/*
Create two applications, one with an oauth policy that
has a very short access token ttl - for testing token expiration
*/
before(function(done){
newAccount = helpers.fakeAccount();
unsignedToken = nJwt.create({hello:'world'},'not a secret').compact();
helpers.createApplication(function(err,app){
if(err){
done(err);
}else{
application = app;
expiredToken = nJwt.create(
{hello:'world'},
application.dataStore.requestExecutor.options.client.apiKey.secret
).setExpiration(new Date().getTime())
.compact();
application.createAccount(newAccount,function(err){
if(err){
done(err);
}else{
new stormpath.OAuthPasswordGrantRequestAuthenticator(application)
.authenticate({
username: newAccount.email,
password: newAccount.password
},function(err,accessTokenResponse){
passwordGrantResponse = accessTokenResponse;
});
helpers.createApplication(function(err,app2){
if(err){
done(err);
}else{
application2 = app2;
/*
We are setting the token ttl to 10 seconds, beacuse
we want to test expired tokens in this test
*/
application2.getOAuthPolicy(function(err,policy){
if(err){
done(err);
}else{
policy.accessTokenTtl = 'PT10S';
policy.save(function(err){
if(err){
done(err);
}else{
application2.createAccount(newAccount,done);
}
});
}
});
}
});
}
});
}
});
});
after(function(done){
helpers.cleanupApplicationAndStores(application,function(err){
if (err) {
return done(err);
}
helpers.cleanupApplicationAndStores(application2, done);
});
});
it('should be constructable with new operator',function(){
var authenticator = new stormpath.JwtAuthenticator(application);
assert.instanceOf(authenticator,stormpath.JwtAuthenticator);
});
it('should be constructable without new operator',function(){
var authenticator = stormpath.JwtAuthenticator(application);
assert.instanceOf(authenticator,stormpath.JwtAuthenticator);
});
it('should throw if not called with a request and callback',function(){
var authenticator = stormpath.JwtAuthenticator(application);
assert.throws(authenticator.authenticate);
});
it('should return 401 if no auth information is given',function(done){
var authenticator = stormpath.JwtAuthenticator(application);
authenticator.authenticate({},assertUnauthenticatedResponse(done));
});
it('should return 401 if the Authorization header is not Bearer',function(done){
var authenticator = stormpath.JwtAuthenticator(application);
authenticator.authenticate({
headers: {
authorization: 'Basic abc'
}
},assertUnauthenticatedResponse(done));
});
it('should reject expired tokens',function(done){
new stormpath.OAuthPasswordGrantRequestAuthenticator(application2)
.authenticate({
username: newAccount.username,
password: newAccount.password
},function(err,passwordGrantResponse){
if(err){
done(err);
}else{
setTimeout(function(){
new stormpath.JwtAuthenticator(application2)
.authenticate({
headers: {
authorization: 'Bearer ' + passwordGrantResponse.accessToken.toString()
}
},assertUnauthenticatedResponse(done));
},10000);
}
});
});
describe('with local authentication',function(){
var authenticator;
before(function(){
authenticator = new stormpath.JwtAuthenticator(application).withLocalValidation();
});
it('should validate access tokens from Bearer header and return a JwtAuthenticationResult',function(done){
authenticator.authenticate(passwordGrantResponse.accessToken.toString(),assertJwtAuthenticationResult(done));
});
it('should return 401 if the access token is not signed by the application',function(done){
authenticator.authenticate(unsignedToken,assertUnauthenticatedResponse(done));
});
it('should return 401 if the token is expired',function(done){
authenticator.authenticate(expiredToken,assertUnauthenticatedResponse(done));
});
});
describe('with remote authentication',function(){
var authenticator;
before(function(){
authenticator = new stormpath.JwtAuthenticator(application);
});
it('should return 401 if the access token is not signed by the application',function(done){
authenticator.authenticate(unsignedToken,assertUnauthenticatedResponse(done));
});
it('should return 401 if the token is expired',function(done){
authenticator.authenticate(expiredToken,assertUnauthenticatedResponse(done));
});
it('should validate access tokens and return a JwtAuthenticationResult',function(done){
authenticator.authenticate(passwordGrantResponse.accessToken.toString(),assertJwtAuthenticationResult(done));
});
});
describe('JwtAuthenticationResult', function() {
var authenticator;
var authResult;
before(function(done) {
authenticator = new stormpath.JwtAuthenticator(application);
authenticator.authenticate(passwordGrantResponse.accessToken.toString(), function(err, result) {
if (err) {
return done(err);
}
authResult = result;
done();
});
});
describe('#getAccount()', function() {
it('should support expansion options for the Account query', function(done) {
authResult.getAccount({expand: 'groups'}, function(err, account) {
if (err) {
return done(err);
}
assert.instanceOf(account, require('../../lib/resource/Account'));
assert.isDefined(account.groups);
assert.isDefined(account.groups.size);
assert.isDefined(account.groups.limit);
assert.isDefined(account.groups.offset);
assert.isDefined(account.groups.items);
done();
});
});
});
});
});