|
| 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