Skip to content

Commit de2a6a3

Browse files
committed
Merge pull request #112 from dwyl/try-mode
Try mode
2 parents 6b80b3f + cc429bc commit de2a6a3

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

lib/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ internals.implementation = function (server, options) {
2828
authenticate: function (request, reply) {
2929
var token = extract(request, options);
3030

31-
if (!token && request.auth.mode !== 'required') {
32-
return reply.continue({ credentials: {} });
33-
}
34-
3531
if (!token) {
3632
return reply(Boom.unauthorized(null, 'Token'));
3733
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hapi-auth-jwt2",
3-
"version": "5.1.1",
3+
"version": "5.1.2",
44
"description": "Hapi.js Authentication Plugin/Scheme using JSON Web Tokens (JWT)",
55
"main": "lib/index.js",
66
"repository": {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
var test = require('tape');
2+
var Hapi = require('hapi');
3+
var JWT = require('jsonwebtoken');
4+
var secret = 'NeverShareYourSecret';
5+
6+
test('Auth mode \'try\' should not set isAuthenticated to true when no token sent', function (t) {
7+
t.plan(3);
8+
9+
var server = new Hapi.Server({ debug: {"request": ["error", "uncaught"]} });
10+
server.connection();
11+
12+
server.register(require('../'), function (err) {
13+
t.ifError(err, 'No error registering hapi-auth-jwt2 plugin');
14+
15+
server.auth.strategy('jwt', 'jwt', {
16+
key: secret,
17+
validateFunc: function (decoded, request, callback) {
18+
return callback();
19+
},
20+
verifyOptions: {algorithms: ['HS256']}
21+
});
22+
23+
server.route({
24+
method: 'GET',
25+
path: '/try',
26+
handler: function (request, reply) {
27+
// console.log(' - - - - - - - - - - - - - - - - - - - - - - -')
28+
// console.log(request.auth);
29+
// console.log(' - - - - - - - - - - - - - - - - - - - - - - -')
30+
t.notOk(request.auth.isAuthenticated, 'isAuthenticated is false')
31+
reply('TRY');
32+
},
33+
config: {
34+
auth: {
35+
strategy: 'jwt',
36+
mode: 'try'
37+
}
38+
}
39+
});
40+
41+
var options = {method: 'GET', url: '/try'};
42+
43+
server.inject(options, function (response) {
44+
t.equal(response.statusCode, 200, 'Server returned HTTP 200');
45+
t.end();
46+
});
47+
});
48+
});
49+
50+
test('Auth mode \'optional\' should not set isAuthenticated to true when no token sent', function (t) {
51+
t.plan(3);
52+
53+
var server = new Hapi.Server();
54+
server.connection();
55+
56+
server.register(require('../'), function (err) {
57+
t.ifError(err, 'No error registering hapi-auth-jwt2 plugin');
58+
59+
server.auth.strategy('jwt', 'jwt', {
60+
key: secret,
61+
validateFunc: function (decoded, request, callback) {
62+
return callback();
63+
},
64+
verifyOptions: {algorithms: ['HS256']}
65+
});
66+
67+
server.route({
68+
method: 'GET',
69+
path: '/optional',
70+
handler: function (request, reply) {
71+
t.notOk(request.auth.isAuthenticated, 'isAuthenticated is false')
72+
reply('OPTIONAL');
73+
},
74+
config: {
75+
auth: {
76+
strategy: 'jwt',
77+
mode: 'optional'
78+
}
79+
}
80+
});
81+
82+
var options = {method: 'GET', url: '/optional'};
83+
84+
server.inject(options, function (response) {
85+
t.equal(response.statusCode, 200, 'Server returned HTTP 200');
86+
t.end();
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)