This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update oidcUtil to allow a callback without userinfo [revisited] (#949)
* Update oidcUtil to allow a callback without userinfo * replace undefined done reference with last argument * fall back to fixed parameters number in verification callback * add UT for passport strategy verification callback * preserve conditional branch for response w/o token * remove debug output * remove extra blank line and place semicolons consistently * bump oidc-middleware to 4.0.2 and update CHANGELOG Co-authored-by: Bradley Cornford <[email protected]>
- Loading branch information
1 parent
58c2961
commit 5b0cd78
Showing
4 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const passport = require('passport'); | ||
const OpenIdClient = require('openid-client'); | ||
const oidcUtil = require('../../src/oidcUtil.js'); | ||
|
||
function createMockOpenIdClient(config={}) { | ||
const Issuer = OpenIdClient.Issuer; | ||
|
||
const defaultConfig = { | ||
issuer: 'https://foo', | ||
token_endpoint: 'https://foo/token', | ||
userinfo_endpoint: 'https://foo/userinfo' | ||
}; | ||
const issuer = new Issuer({ | ||
...defaultConfig, | ||
...config | ||
}); | ||
const client = new issuer.Client({ | ||
client_id: 'foo', | ||
client_secret: 'foo', | ||
}); | ||
|
||
client.callback = jest.fn(() => ({ | ||
access_token: 'token_value' | ||
})); | ||
|
||
client.userinfo = jest.fn(() => ({ | ||
cid: '123' | ||
})); | ||
|
||
return client; | ||
} | ||
|
||
function createMockRedirectRequest() { | ||
const request = jest.mock(); | ||
request.method = 'GET'; | ||
request.url = 'http://foo/authorization-code/callback?code=foo'; | ||
request.session = { | ||
'oidc:foo': { | ||
response_type: 'code', | ||
}, | ||
}; | ||
return request; | ||
} | ||
|
||
describe('oidcUtil', function () { | ||
describe('Passport strategy setup', function () { | ||
beforeEach(function () { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('includes verification function which tolerates authentication repsonses w/o userInfo', function (next) { | ||
const passportStrategySetter = jest.spyOn(passport, 'use').mockImplementation(() => {}); | ||
|
||
const context = { | ||
options: { | ||
scope: ['openid'] | ||
}, | ||
client: createMockOpenIdClient({ | ||
userinfo_endpoint: undefined | ||
}) | ||
}; | ||
oidcUtil.bootstrapPassportStrategy(context); | ||
const passportStrategy = passportStrategySetter.mock.calls[0][1]; | ||
|
||
passportStrategy.success = function (response) { | ||
expect(response.userinfo).toBe(undefined); | ||
expect(response.tokens).toEqual({access_token: 'token_value'}); | ||
next(); | ||
} | ||
passportStrategy.error = function(error) { | ||
expect(error).toEqual(undefined); | ||
}; | ||
passportStrategy.authenticate(createMockRedirectRequest()); | ||
}); | ||
|
||
it('includes verification function which returns userinfo whenever it is available', function (next) { | ||
const passportStrategySetter = jest.spyOn(passport, 'use').mockImplementation(() => {}); | ||
const context = { | ||
options: { | ||
scope: ['openid', 'profile'] | ||
}, | ||
client: createMockOpenIdClient() | ||
}; | ||
|
||
oidcUtil.bootstrapPassportStrategy(context); | ||
const passportStrategy = passportStrategySetter.mock.calls[0][1]; | ||
|
||
passportStrategy.success = function (response) { | ||
expect(response.userinfo).toEqual({cid: '123'}); | ||
expect(response.tokens).toEqual({access_token: 'token_value'}); | ||
next(); | ||
}; | ||
passportStrategy.error = function(error) { | ||
expect(error).toEqual(undefined); | ||
}; | ||
passportStrategy.authenticate(createMockRedirectRequest()); | ||
}) | ||
}) | ||
}) |