-
-
Notifications
You must be signed in to change notification settings - Fork 51
feature(pkce): added pkce support #86
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6e3298a
feature(pkce): added pkce support
jankapunkt 5d95e40
Merge branch 'development' into feature-pkce
Uzlopak 6bafe0e
Merge branch 'v4.3.0-dev' into feature-pkce
jankapunkt 3242fc2
save code challenge with authorization code
martinssonj c599cb4
Use default code challenge method plain if missing
martinssonj c597a24
only add code challenge properties to code when codeChallenge and cod…
martinssonj 19f7dc4
feature(pkce): save code challenge with authorization code
jankapunkt bdd941e
Merge branch 'v4.3.0-dev' into feature-pkce
jankapunkt 6e6edcb
fix(tests): move same line statements in seperate lines
jankapunkt 2411f92
compliance(pkce): throw InvalidRequestError if code request contains …
jankapunkt b799985
test(pkce): added test for bypassed saving unsupported code challenge…
jankapunkt 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
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,77 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
const { base64URLEncode } = require('../utils/string-util'); | ||
const { createHash } = require('../utils/crypto-util'); | ||
const codeChallengeRegexp = /^([a-zA-Z0-9.\-_~]){43,128}$/; | ||
/** | ||
* Export `TokenUtil`. | ||
*/ | ||
|
||
const pkce = { | ||
/** | ||
* Return hash for code-challenge method-type. | ||
* | ||
* @param method {String} the code challenge method | ||
* @param verifier {String} the code_verifier | ||
* @return {String|undefined} | ||
*/ | ||
getHashForCodeChallenge: function({ method, verifier }) { | ||
// to prevent undesired side-effects when passing some wird values | ||
// to createHash or base64URLEncode we first check if the values are right | ||
if (pkce.isValidMethod(method) && typeof verifier === 'string' && verifier.length > 0) { | ||
if (method === 'plain') { | ||
return verifier; | ||
} | ||
|
||
if (method === 'S256') { | ||
const hash = createHash({ data: verifier }); | ||
return base64URLEncode(hash); | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Check if the request is a PCKE request. We assume PKCE if grant type is | ||
* 'authorization_code' and code verifier is present. | ||
* | ||
* @param grantType {String} | ||
* @param codeVerifier {String} | ||
* @return {boolean} | ||
*/ | ||
isPKCERequest: function ({ grantType, codeVerifier }) { | ||
return grantType === 'authorization_code' && !!codeVerifier; | ||
}, | ||
|
||
/** | ||
* Matches a code verifier (or code challenge) against the following criteria: | ||
* | ||
* code-verifier = 43*128unreserved | ||
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* ALPHA = %x41-5A / %x61-7A | ||
* DIGIT = %x30-39 | ||
* | ||
* @see: https://datatracker.ietf.org/doc/html/rfc7636#section-4.1 | ||
* @param codeChallenge {String} | ||
* @return {Boolean} | ||
*/ | ||
codeChallengeMatchesABNF: function (codeChallenge) { | ||
return typeof codeChallenge === 'string' && | ||
!!codeChallenge.match(codeChallengeRegexp); | ||
}, | ||
|
||
/** | ||
* Checks if the code challenge method is one of the supported methods | ||
* 'sha256' or 'plain' | ||
* | ||
* @param method {String} | ||
* @return {boolean} | ||
*/ | ||
isValidMethod: function (method) { | ||
return method === 'S256' || method === 'plain'; | ||
} | ||
}; | ||
|
||
module.exports = pkce; |
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,24 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
|
||
/** | ||
* Export `StringUtil`. | ||
*/ | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param algorithm {String} the hash algorithm, default is 'sha256' | ||
* @param data {Buffer|String|TypedArray|DataView} the data to hash | ||
* @param encoding {String|undefined} optional, the encoding to calculate the | ||
* digest | ||
* @return {Buffer|String} if {encoding} undefined a {Buffer} is returned, otherwise a {String} | ||
*/ | ||
createHash: function({ algorithm = 'sha256', data = undefined, encoding = undefined }) { | ||
return crypto | ||
.createHash(algorithm) | ||
.update(data) | ||
.digest(encoding); | ||
} | ||
}; |
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,19 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Export `StringUtil`. | ||
*/ | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param str | ||
* @return {string} | ||
*/ | ||
base64URLEncode: function(str) { | ||
return str.toString('base64') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
} | ||
}; |
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
Oops, something went wrong.
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.