Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add callback extras to strategy options #295

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/passport_strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function OpenIDConnectStrategy({
passReqToCallback = false,
sessionKey,
usePKCE,
extras = {},
} = {}, verify) {
if (!(client instanceof BaseClient)) {
throw new TypeError('client must be an instance of openid-client Client');
Expand All @@ -50,6 +51,7 @@ function OpenIDConnectStrategy({
this._usePKCE = usePKCE;
this._key = sessionKey || `oidc:${url.parse(this._issuer.issuer).hostname}`;
this._params = cloneDeep(params);
this._extras = cloneDeep(extras);

if (!this._params.response_type) this._params.response_type = resolveResponseType.call(client);
if (!this._params.redirect_uri) this._params.redirect_uri = resolveRedirectUri.call(client);
Expand Down Expand Up @@ -147,7 +149,7 @@ OpenIDConnectStrategy.prototype.authenticate = function authenticate(req, option
response_type: responseType,
};

const tokenset = await client.callback(opts.redirect_uri, reqParams, checks);
const tokenset = await client.callback(opts.redirect_uri, reqParams, checks, this._extras);

const passReq = this._passReqToCallback;
const loadUserinfo = this._verify.length > (passReq ? 3 : 2) && client.issuer.userinfo_endpoint;
Expand Down
34 changes: 34 additions & 0 deletions test/passport/passport_strategy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ describe('OpenIDConnectStrategy', () => {
strategy.authenticate(req);
});

describe('authenticate', function () {
it('forwards options.extras to callback as extras param', async function () {
const extras = {
clientAssertionPayload: {
aud: 'https://oidc.corp.com/default-oidc-provider',
},
};

const params = {
redirect_uri: 'http://domain.inc/oauth2/callback',
};

const strategy = new Strategy({ client: this.client, params, extras }, () => {});
const req = new MockRequest('GET', '/login/oidc');
req.session = { 'oidc:op.example.com': sinon.match.object };

/* Fake callback params */
const callbackParams = { code: 'some-code' };
sinon.stub(this.client, 'callbackParams').callsFake(() => callbackParams);

this.client.callback = sinon.spy();

strategy.authenticate(req, {});
sinon.assert.calledOnce(this.client.callback);
sinon.assert.calledWith(
this.client.callback,
params.redirect_uri,
callbackParams,
sinon.match.object,
extras,
);
});
});

describe('initate', function () {
it('starts authentication requests for GETs', function () {
const params = { foo: 'bar' };
Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ export interface StrategyOptions<TClient extends Client> {
* Authorization Request parameters. The strategy will use these.
*/
params?: AuthorizationParameters;

/**
* "extras" argument value passed to the client.callback() call.
*/
extras?: CallbackExtras;
/**
* Boolean specifying whether the verify function should get the request object as first argument instead.
* Default: 'false'
Expand Down