-
Notifications
You must be signed in to change notification settings - Fork 6
CME-983: Refactor OAuth2 authorization handling by introducing a clie… #717
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
Open
AntonyLeons
wants to merge
9
commits into
master
Choose a base branch
from
CME-983-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d24dff2
CME-983: Refactor OAuth2 authorization handling by introducing a clie…
AntonyLeons 035509c
CME-983: Enhance logging by adding redaction of Authorization headers…
AntonyLeons bb35061
Refactor redactAuthorizationHeader to use Object.assign for header cl…
AntonyLeons dc42182
Merge branch 'master' into CME-983-1
AntonyLeons 8b86509
Merge branch 'master' into CME-983-1
AntonyLeons 370a374
Refactor getBasicAuthHeader and redactAuthorizationHeader for clarity
AntonyLeons 21d87ce
Update ECMAScript version to 2018 in ESLint configuration files
AntonyLeons 7605c58
RDM- :
AntonyLeons dd7b6c8
Merge branch 'master' into CME-983-1
AntonyLeons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| const config = require('config'); | ||
|
|
||
| const AUTHORIZATION_HEADER = 'Authorization'; | ||
| const CLIENT_ID_CONFIG_KEY = 'idam.oauth2.client_id'; | ||
| const CLIENT_SECRET_CONFIG_KEY = 'secrets.ccd.ccd-api-gateway-oauth2-client-secret'; | ||
|
|
||
| const missingConfigError = (configKey) => { | ||
| const error = new Error(`Missing required config: ${configKey}`); | ||
| error.status = 500; | ||
| error.code = 'OAUTH2_CLIENT_CONFIG_MISSING'; | ||
| return error; | ||
| }; | ||
|
|
||
| const getRequiredConfig = (configKey) => { | ||
| const value = config.get(configKey); | ||
|
|
||
| if (!value) { | ||
| throw missingConfigError(configKey); | ||
| } | ||
|
|
||
| return value; | ||
| }; | ||
|
|
||
| const getBasicAuthHeader = () => { | ||
| const clientId = getRequiredConfig(CLIENT_ID_CONFIG_KEY); | ||
| const clientSecret = getRequiredConfig(CLIENT_SECRET_CONFIG_KEY); | ||
|
|
||
| const credentials = Buffer.from(`${clientId}:${clientSecret}`).toString('base64'); | ||
| return `Basic ${credentials}`; | ||
| }; | ||
|
|
||
| const redactAuthorizationHeader = (headers) => { | ||
| const result = { ...headers }; | ||
| if (result[AUTHORIZATION_HEADER]) { | ||
| result[AUTHORIZATION_HEADER] = 'Basic [REDACTED]'; | ||
| } | ||
| if (result[AUTHORIZATION_HEADER.toLowerCase()]) { | ||
| result[AUTHORIZATION_HEADER.toLowerCase()] = 'Basic [REDACTED]'; | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getBasicAuthHeader, | ||
| redactAuthorizationHeader | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
| const chai = require('chai'); | ||
| const expect = chai.expect; | ||
| const proxyquire = require('proxyquire'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| describe('OAuth2 client auth helper', () => { | ||
| let config; | ||
| let clientAuth; | ||
|
|
||
| beforeEach(() => { | ||
| config = { | ||
| get: sinon.stub() | ||
| }; | ||
|
|
||
| clientAuth = proxyquire('../../app/oauth2/client-auth', { | ||
| 'config': config | ||
| }); | ||
| }); | ||
|
|
||
| it('should build the Basic authorization header from configured credentials', () => { | ||
| config.get.withArgs('idam.oauth2.client_id').returns('ccd_gateway'); | ||
| config.get.withArgs('secrets.ccd.ccd-api-gateway-oauth2-client-secret').returns('abc123def456'); | ||
|
|
||
| expect(clientAuth.getBasicAuthHeader()) | ||
| .to.equal(`Basic ${Buffer.from('ccd_gateway:abc123def456').toString('base64')}`); | ||
| }); | ||
|
|
||
| it('should throw when the OAuth2 client secret is missing', () => { | ||
| config.get.withArgs('idam.oauth2.client_id').returns('ccd_gateway'); | ||
| config.get.withArgs('secrets.ccd.ccd-api-gateway-oauth2-client-secret').returns(''); | ||
|
|
||
| expect(() => clientAuth.getBasicAuthHeader()) | ||
| .to.throw('Missing required config: secrets.ccd.ccd-api-gateway-oauth2-client-secret'); | ||
| }); | ||
|
|
||
| it('should throw when the OAuth2 client ID is missing', () => { | ||
| config.get.withArgs('idam.oauth2.client_id').returns(''); | ||
| config.get.withArgs('secrets.ccd.ccd-api-gateway-oauth2-client-secret').returns('abc123def456'); | ||
|
|
||
| expect(() => clientAuth.getBasicAuthHeader()) | ||
| .to.throw('Missing required config: idam.oauth2.client_id'); | ||
| }); | ||
|
|
||
| it('should redact Authorization headers before logging', () => { | ||
| expect(clientAuth.redactAuthorizationHeader({ | ||
| Authorization: 'Basic Y2NkX2dhdGV3YXk6YWJjMTIzZGVmNDU2', | ||
| 'Content-Type': 'application/x-www-form-urlencoded' | ||
| })).to.deep.equal({ | ||
| Authorization: 'Basic [REDACTED]', | ||
| 'Content-Type': 'application/x-www-form-urlencoded' | ||
| }); | ||
| }); | ||
|
|
||
| it('should redact lowercase authorization headers before logging', () => { | ||
| expect(clientAuth.redactAuthorizationHeader({ | ||
| authorization: 'Basic Y2NkX2dhdGV3YXk6YWJjMTIzZGVmNDU2', | ||
| 'content-type': 'application/x-www-form-urlencoded' | ||
| })).to.deep.equal({ | ||
| authorization: 'Basic [REDACTED]', | ||
| 'content-type': 'application/x-www-form-urlencoded' | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.