-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from node-oauth/feature-pkce Contributors: @martinssonj @jankapunkt @Uzlopak
- Loading branch information
Showing
13 changed files
with
716 additions
and
10 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
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 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 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 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.