-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into simple-icons-names
- Loading branch information
Showing
217 changed files
with
2,509 additions
and
1,957 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
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,43 @@ | ||
'use strict' | ||
|
||
class AuthHelper { | ||
constructor({ userKey, passKey, isRequired = false }, privateConfig) { | ||
if (!userKey && !passKey) { | ||
throw Error('Expected userKey or passKey to be set') | ||
} | ||
|
||
this._userKey = userKey | ||
this._passKey = passKey | ||
this.user = userKey ? privateConfig[userKey] : undefined | ||
this.pass = passKey ? privateConfig[passKey] : undefined | ||
this.isRequired = isRequired | ||
} | ||
|
||
get isConfigured() { | ||
return ( | ||
(this._userKey ? Boolean(this.user) : true) && | ||
(this._passKey ? Boolean(this.pass) : true) | ||
) | ||
} | ||
|
||
get isValid() { | ||
if (this.isRequired) { | ||
return this.isConfigured | ||
} else { | ||
const configIsEmpty = !this.user && !this.pass | ||
return this.isConfigured || configIsEmpty | ||
} | ||
} | ||
|
||
get basicAuth() { | ||
const { user, pass } = this | ||
return this.isConfigured ? { user, pass } : undefined | ||
} | ||
|
||
get bearerAuthHeader() { | ||
const { pass } = this | ||
return this.isConfigured ? { Authorization: `Bearer ${pass}` } : undefined | ||
} | ||
} | ||
|
||
module.exports = { AuthHelper } |
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,96 @@ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const { test, given, forCases } = require('sazerac') | ||
const { AuthHelper } = require('./auth-helper') | ||
|
||
describe('AuthHelper', function() { | ||
it('throws without userKey or passKey', function() { | ||
expect(() => new AuthHelper({}, {})).to.throw( | ||
Error, | ||
'Expected userKey or passKey to be set' | ||
) | ||
}) | ||
|
||
describe('isValid', function() { | ||
function validate(config, privateConfig) { | ||
return new AuthHelper(config, privateConfig).isValid | ||
} | ||
test(validate, () => { | ||
forCases([ | ||
// Fully configured user + pass. | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass', isRequired: true }, | ||
{ myci_user: 'admin', myci_pass: 'abc123' } | ||
), | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass' }, | ||
{ myci_user: 'admin', myci_pass: 'abc123' } | ||
), | ||
// Fully configured user or pass. | ||
given( | ||
{ userKey: 'myci_user', isRequired: true }, | ||
{ myci_user: 'admin' } | ||
), | ||
given( | ||
{ passKey: 'myci_pass', isRequired: true }, | ||
{ myci_pass: 'abc123' } | ||
), | ||
given({ userKey: 'myci_user' }, { myci_user: 'admin' }), | ||
given({ passKey: 'myci_pass' }, { myci_pass: 'abc123' }), | ||
// Empty config. | ||
given({ userKey: 'myci_user', passKey: 'myci_pass' }, {}), | ||
given({ userKey: 'myci_user' }, {}), | ||
given({ passKey: 'myci_pass' }, {}), | ||
]).expect(true) | ||
|
||
forCases([ | ||
// Partly configured. | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass', isRequired: true }, | ||
{ myci_user: 'admin' } | ||
), | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass' }, | ||
{ myci_user: 'admin' } | ||
), | ||
// Missing required config. | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass', isRequired: true }, | ||
{} | ||
), | ||
given({ userKey: 'myci_user', isRequired: true }, {}), | ||
given({ passKey: 'myci_pass', isRequired: true }, {}), | ||
]).expect(false) | ||
}) | ||
}) | ||
|
||
describe('basicAuth', function() { | ||
function validate(config, privateConfig) { | ||
return new AuthHelper(config, privateConfig).basicAuth | ||
} | ||
test(validate, () => { | ||
forCases([ | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass', isRequired: true }, | ||
{ myci_user: 'admin', myci_pass: 'abc123' } | ||
), | ||
given( | ||
{ userKey: 'myci_user', passKey: 'myci_pass' }, | ||
{ myci_user: 'admin', myci_pass: 'abc123' } | ||
), | ||
]).expect({ user: 'admin', pass: 'abc123' }) | ||
given({ userKey: 'myci_user' }, { myci_user: 'admin' }).expect({ | ||
user: 'admin', | ||
pass: undefined, | ||
}) | ||
given({ passKey: 'myci_pass' }, { myci_pass: 'abc123' }).expect({ | ||
user: undefined, | ||
pass: 'abc123', | ||
}) | ||
given({ userKey: 'myci_user', passKey: 'myci_pass' }, {}).expect( | ||
undefined | ||
) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.