Skip to content

Commit eb9fff9

Browse files
committed
Merge pull request dwyl#124 from mcortesi/access-token-from-request
Set token in request for access from handler
2 parents dcda8ed + fd73386 commit eb9fff9

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ There are _several_ options for generating secret keys.
240240
The _easist_ way is to simply copy paste a _**strong random string**_ of alpha-numeric characters from https://www.grc.com/passwords.htm
241241
(_if you want a longer key simply refresh the page and copy-paste multiple random strings_)
242242

243+
## Want to access the JWT token after validation?
244+
245+
[@mcortesi](https://github.com/mcortesi) requested the ability to
246+
[access the JWT token](https://github.com/dwyl/hapi-auth-jwt2/issues/55) used for authentication.
247+
248+
We added support for that. You can access the extracted JWT token in your handler or any other function
249+
within the request lifecycle with the `request.auth.token` property.
250+
251+
Take in consideration, that this is the *encoded token*, and it's only useful if you want to use to make
252+
request to other servers using the user's token. For information inside the token, just use the
253+
`request.auth.credentials` property.
254+
243255
## Want to send/store your JWT in a Cookie?
244256

245257
[@benjaminlees](https://github.com/benjaminlees)

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ internals.implementation = function (server, options) {
7171
return reply(Boom.unauthorized('Invalid credentials', 'Token'), null, { credentials: credentials || decoded });
7272
}
7373
else {
74-
return reply.continue({ credentials: credentials || decoded });
74+
request.auth.token = token;
75+
return reply.continue({ credentials: credentials || decoded, artifacts: token });
7576
}
7677
});
7778
}

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.2.0",
3+
"version": "5.2.1",
44
"description": "Hapi.js Authentication Plugin/Scheme using JSON Web Tokens (JWT)",
55
"main": "lib/index.js",
66
"repository": {

test/server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var privado = function(req, reply) {
2929
return reply('worked');
3030
};
3131

32+
var sendToken = function(req, reply) {
33+
return reply(req.auth.token);
34+
};
35+
3236
server.register(require('../'), function () {
3337

3438
server.auth.strategy('jwt', 'jwt', {
@@ -39,6 +43,7 @@ server.register(require('../'), function () {
3943

4044
server.route([
4145
{ method: 'GET', path: '/', handler: home, config: { auth: false } },
46+
{ method: 'GET', path: '/token', handler: sendToken, config: { auth: 'jwt' } },
4247
{ method: 'POST', path: '/privado', handler: privado, config: { auth: 'jwt' } },
4348
{ method: 'POST', path: '/required', handler: privado, config: { auth: { mode: 'required', strategy: 'jwt' } } },
4449
{ method: 'POST', path: '/optional', handler: privado, config: { auth: { mode: 'optional', strategy: 'jwt' } } },

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,20 @@ test("Auth mode 'try' should pass with valid token", function(t) {
328328
t.end();
329329
});
330330
});
331+
332+
test("Scheme should set token in request.auth.token", function(t) {
333+
// use the token as the 'authorization' header in requests
334+
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
335+
var options = {
336+
method: "GET",
337+
url: "/token",
338+
headers: { authorization: "Bearer " + token }
339+
};
340+
// server.inject lets us similate an http request
341+
server.inject(options, function(response) {
342+
// console.log(" - - - - RESPONSE: ")
343+
// console.log(response.result);
344+
t.equal(response.result, token, 'Token is accesible from handler');
345+
t.end();
346+
});
347+
});

0 commit comments

Comments
 (0)