Skip to content

Commit

Permalink
Log err.response if present
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Mar 7, 2024
1 parent 6ddb563 commit e78e2f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/server/lib/OIDCConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ export class OIDCConfig {
res.redirect(targetUrl ?? '/');
} catch (err) {
log.error(`OIDC callback failed: ${err.stack}`);
if (Object.prototype.hasOwnProperty.call(err, 'response')) {
log.error(`Response received: ${JSON.stringify(err.response)}`);
}
// Delete the session data even if the login failed.
// This way, we prevent several login attempts.
//
Expand Down
2 changes: 2 additions & 0 deletions test/nbrowser/LoginWithOIDC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// import {setupTestSuite} from 'test/nbrowser/testUtils';
// import express from 'express';

export {};

// describe('LoginWithOIDC', function () {
// this.timeout(60000);
// setupTestSuite();
Expand Down
30 changes: 30 additions & 0 deletions test/server/lib/OIDCConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,36 @@ describe('OIDCConfig', () => {
}
});
});

it('should log err.response when userinfo fails to parse response body', async () => {
// See https://github.com/panva/node-openid-client/blob/47a549cb4e36ffe2ebfe2dc9d6b69a02643cc0a9/lib/client.js#L1293
setEnvVars();
const clientStub = new ClientStub();
const config = await OIDCConfigStubbed.build(clientStub.asClient());
const req = {
session: DEFAULT_SESSION,
query: {
state: FAKE_STATE,
codeVerifier: FAKE_CODE_VERIFIER,
}
} as unknown as express.Request;
clientStub.callbackParams.returns({state: FAKE_STATE});

const err: Error & {response?: string} = new Error('userinfo failed');
err.response = 'response here';
clientStub.userinfo.rejects(err);

await config.handleCallback(
fakeSessions as unknown as Sessions,
req,
fakeRes as unknown as express.Response
);

assert.isTrue(logErrorStub.calledTwice);
assert.include(logErrorStub.firstCall.args[0], err.message);
assert.include(logErrorStub.secondCall.args[0], `"${err.response}"`);
assert.isTrue(fakeRes.status.calledOnceWith(500));
});
});

describe('getLogoutRedirectUrl', () => {
Expand Down

0 comments on commit e78e2f3

Please sign in to comment.