Skip to content

Commit

Permalink
Add support for providing clientSecret as a function. (#476)
Browse files Browse the repository at this point in the history
Partially resolves #462
  • Loading branch information
Ginden committed Mar 12, 2021
1 parent 8077093 commit feb15b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ The `server.auth.strategy()` method requires the following strategy options:
object will be merged with the Wreck request object used to call the token endpoint. Such an
object can contain custom HTTP headers or TLS options (e.g.
`{ agent: new Https.Agent({ cert: myClientCert, key: myClientKey}) }`).
To allow dynamically updating secret, this option can be passed as a *function* returning string
to be used as `clientSecret`.
- `forceHttps` - A boolean indicating whether or not you want the redirect_uri to be forced to
https. Useful if your hapi application runs as http, but is accessed through https.
- `location` - Set the base redirect_uri manually if it cannot be inferred properly from server
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ internals.schema = Joi.object({

clientSecret: Joi.alternatives()
.try(Joi.string().allow(''))
.try(Joi.function())
.conditional('provider.protocol', {
not: 'oauth',
then: Joi.object()
Expand Down
3 changes: 3 additions & 0 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ exports.v2 = function (settings) {
if (typeof settings.clientSecret === 'string') {
query.client_secret = settings.clientSecret;
}
else if (typeof settings.clientSecret === 'function') {
query.client_secret = settings.clientSecret();
}
}

const requestOptions = {
Expand Down
42 changes: 42 additions & 0 deletions test/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,48 @@ describe('Bell', () => {
expect(res.headers.location).to.contain(mock.uri + '/auth?special=true&client_id=test&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flogin&state=');
});

it('uses clientSecret() function', async (flags) => {

const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

let callCount = 0;
const secretGetter = () => {

callCount += 1;
return 'secret';
};

server.auth.strategy('custom', 'bell', {
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: 'test',
clientSecret: secretGetter,
provider: mock.provider
});

server.route({
method: '*',
path: '/login',
options: {
auth: 'custom',
handler: function (request, h) {

return request.auth.credentials;
}
}
});

const res1 = await server.inject('/login');
const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';';

const res2 = await mock.server.inject(res1.headers.location);

await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(callCount, 'callCount').to.equal(1);
});

it('forces https in redirect_uri when set in options', async (flags) => {

const mock = await Mock.v2(flags);
Expand Down

0 comments on commit feb15b6

Please sign in to comment.