Skip to content

Commit

Permalink
feat: allow third party initiated login requests to trigger strategy
Browse files Browse the repository at this point in the history
closes #510
closes #564
  • Loading branch information
panva committed Feb 5, 2023
1 parent 363c215 commit 568709a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/passport_strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ OpenIDConnectStrategy.prototype.authenticate = function authenticate(req, option
const reqParams = client.callbackParams(req);
const sessionKey = this._key;

/* start authentication request */
if (Object.keys(reqParams).length === 0) {
const { 0: parameter, length } = Object.keys(reqParams);

/**
* Start authentication request if this has no authorization response parameters or
* this might a login initiated from a third party as per
* https://openid.net/specs/openid-connect-core-1_0.html#ThirdPartyInitiatedLogin.
*/
if (length === 0 || (length === 1 && parameter === 'iss')) {
// provide options object with extra authentication parameters
const params = {
state: random(),
Expand Down
22 changes: 22 additions & 0 deletions test/passport/passport_strategy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ describe('OpenIDConnectStrategy', () => {
);
});

it('starts authentication requests for TPIL GETs', function () {
const params = { iss: 'https://op.example.com' };
const strategy = new Strategy({ client: this.client, params }, () => {});

const req = new MockRequest('GET', '/login/oidc');
req.session = {};

strategy.redirect = sinon.spy();
strategy.authenticate(req);

expect(strategy.redirect.calledOnce).to.be.true;
const target = strategy.redirect.firstCall.args[0];
expect(target).to.include('redirect_uri=');
expect(target).to.include('scope=');
expect(req.session).to.have.property('oidc:op.example.com');
expect(req.session['oidc:op.example.com']).to.have.keys(
'state',
'response_type',
'code_verifier',
);
});

it('starts authentication requests for POSTs', function () {
const strategy = new Strategy({ client: this.client }, () => {});

Expand Down

0 comments on commit 568709a

Please sign in to comment.