Skip to content

Commit 60654f7

Browse files
author
Anthony Bouch
committed
Merge pull request #5 from stevejhiggs/master
Add payloadFunc option. This allows easy validation of payloads
2 parents fdc3f5b + 2cfb6b6 commit 60654f7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ The hapi-auth-signature scheme takes the following options.
2020
included when `isValid` is `true`, but there are cases when the application needs to know who tried to authenticate even when it fails
2121
(e.g. with authentication mode `'try'`).
2222

23+
- `payloadFunc` - (optional) payload validation function with the signature `function(request, callback)` where:
24+
- `request` - hapi request object.
25+
- `callback` - a callback function with the signature `function(err, isValid)` where:
26+
- `err` - an internal error.
27+
- `isValid` - `true` if the payload is verified, otherwise `false`.
28+
2329
The validation function shown below is based on an hmac strategy with a key identifier and secret key stored in a user record. [http-signature](https://github.com/joyent/node-http-signature) supports the following algorithms:
2430

2531
* rsa-sha1

lib/index.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,38 @@ internals.implementation = function (server, options) {
4646
credentials = credentials || null;
4747

4848
if (err) {
49-
return reply(err, null, { credentials: credentials, log: { tags: ['auth', 'signature'], data: err } });
49+
return reply(err, null, {credentials: credentials, log: {tags: ['auth', 'signature'], data: err}});
5050
}
5151

5252
if (!isValid) {
53-
return reply(Boom.unauthorized('Bad signature', 'Signature'), null, { credentials: credentials });
53+
return reply(Boom.unauthorized('Bad signature', 'Signature'), null, {credentials: credentials});
5454
}
5555

5656
if (!credentials ||
5757
typeof credentials !== 'object') {
5858

59-
return reply(Boom.badImplementation('Bad credentials object received for Signature auth validation'), null, { log: { tags: ['auth', 'credentials'] } });
59+
return reply(Boom.badImplementation('Bad credentials object received for Signature auth validation'), null, {log: {tags: ['auth', 'credentials']}});
6060
}
6161

6262
// Authenticated
6363

64-
return reply.continue({ credentials: credentials });
64+
return reply.continue({credentials: credentials});
6565
});
66+
},
67+
payload: function (request, reply) {
68+
if (settings.payloadFunc) {
69+
settings.payloadFunc(request, function (err, isValid) {
70+
if (err) {
71+
return reply(err, null, {log: {tags: ['auth', 'signature'], data: err}});
72+
}
73+
74+
if (!isValid) {
75+
return reply(Boom.unauthorized('Bad signature', 'Signature'), null, {});
76+
}
77+
});
78+
}
79+
80+
reply.continue();
6681
}
6782
};
6883

0 commit comments

Comments
 (0)