Skip to content
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

Add PKCE support #658

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* BREAKING: Remove support for node v4
* new: Added revoke-handler to revoke access token
* new: Added implicit grant flow
* new: Switch from jshint to eslint
* new: Switch from jshint to eslin
* fix: authorization_code grant should not be required in implicit flowt
Comment on lines +8 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be typo,

Suggested change
* new: Switch from jshint to eslin
* fix: authorization_code grant should not be required in implicit flowt
* new: Switch from jshint to eslint
* fix: authorization_code grant should not be required in implicit flow


### 3.1.0
* new: Added package-lock.json
Expand Down
138 changes: 78 additions & 60 deletions docs/model/spec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,29 @@ This model function is **required** if the ``authorization_code`` grant is used.

An ``Object`` representing the authorization code and associated data.

+--------------------+--------+--------------------------------------------------------------+
| Name | Type | Description |
+====================+========+==============================================================+
| code | Object | The return value. |
+--------------------+--------+--------------------------------------------------------------+
| code.code | String | The authorization code passed to ``getAuthorizationCode()``. |
+--------------------+--------+--------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+--------------------+--------+--------------------------------------------------------------+
| [code.redirectUri] | String | The redirect URI of the authorization code. |
+--------------------+--------+--------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+--------------------+--------+--------------------------------------------------------------+
| code.client | Object | The client associated with the authorization code. |
+--------------------+--------+--------------------------------------------------------------+
| code.client.id | String | A unique string identifying the client. |
+--------------------+--------+--------------------------------------------------------------+
| code.user | Object | The user associated with the authorization code. |
+--------------------+--------+--------------------------------------------------------------+
+----------------------------+--------+---------------------------------------------------------------+
| Name | Type | Description |
+============================+========+===============================================================+
| code | Object | The return value. |
+----------------------------+--------+---------------------------------------------------------------+
| code.code | String | The authorization code passed to ``getAuthorizationCode()``. |
+----------------------------+--------+---------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+----------------------------+--------+---------------------------------------------------------------+
| [code.redirectUri] | String | The redirect URI of the authorization code. |
+----------------------------+--------+---------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+----------------------------+--------+---------------------------------------------------------------+
| code.client | Object | The client associated with the authorization code. |
+----------------------------+--------+---------------------------------------------------------------+
| code.client.id | String | A unique string identifying the client. |
+----------------------------+--------+---------------------------------------------------------------+
| code.user | Object | The user associated with the authorization code. |
+----------------------------+--------+---------------------------------------------------------------+
| [code.codeChallenge] | String | The code challenge string used with PKCE (RFC7636). |
+----------------------------+--------+---------------------------------------------------------------+
| [code.codeChallengeMethod] | String | The string for the code challenge method (`S256` or `plain`). |
+----------------------------+--------+---------------------------------------------------------------+

``code.client`` and ``code.user`` can carry additional properties that will be ignored by *oauth2-server*.

Expand All @@ -379,7 +383,9 @@ An ``Object`` representing the authorization code and associated data.
redirectUri: code.redirect_uri,
scope: code.scope,
client: client, // with 'id' property
user: user
user: user,
codeChallenge: code.code_challenge,
codeChallengeMethod: code.code_challenge_method
};
});
}
Expand Down Expand Up @@ -665,51 +671,59 @@ This model function is **required** if the ``authorization_code`` grant is used.

**Arguments:**

+------------------------+----------+---------------------------------------------------------------------+
| Name | Type | Description |
+========================+==========+=====================================================================+
| code | Object | The code to be saved. |
+------------------------+----------+---------------------------------------------------------------------+
| code.authorizationCode | String | The authorization code to be saved. |
+------------------------+----------+---------------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+------------------------+----------+---------------------------------------------------------------------+
| code.redirectUri | String | The redirect URI associated with the authorization code. |
+------------------------+----------+---------------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+------------------------+----------+---------------------------------------------------------------------+
| client | Object | The client associated with the authorization code. |
+------------------------+----------+---------------------------------------------------------------------+
| user | Object | The user associated with the authorization code. |
+------------------------+----------+---------------------------------------------------------------------+
| [callback] | Function | Node-style callback to be used instead of the returned ``Promise``. |
+------------------------+----------+---------------------------------------------------------------------+
+----------------------------+----------+---------------------------------------------------------------------+
| Name | Type | Description |
+============================+==========+=====================================================================+
| code | Object | The code to be saved. |
+----------------------------+----------+---------------------------------------------------------------------+
| code.authorizationCode | String | The authorization code to be saved. |
+----------------------------+----------+---------------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+----------------------------+----------+---------------------------------------------------------------------+
| code.redirectUri | String | The redirect URI associated with the authorization code. |
+----------------------------+----------+---------------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+----------------------------+----------+---------------------------------------------------------------------+
| [code.codeChallenge] | String | The code challenge string used with PKCE (RFC7636). |
+----------------------------+----------+---------------------------------------------------------------------+
| [code.codeChallengeMethod] | String | The string for the code challenge method (`S256` or `plain`). |
+----------------------------+----------+---------------------------------------------------------------------+
| client | Object | The client associated with the authorization code. |
+----------------------------+----------+---------------------------------------------------------------------+
| user | Object | The user associated with the authorization code. |
+----------------------------+----------+---------------------------------------------------------------------+
| [callback] | Function | Node-style callback to be used instead of the returned ``Promise``. |
+----------------------------+----------+---------------------------------------------------------------------+

.. todo:: Is ``code.scope`` really optional?

**Return value:**

An ``Object`` representing the authorization code and associated data.

+------------------------+--------+---------------------------------------------------------------+
| Name | Type | Description |
+========================+========+===============================================================+
| code | Object | The return value. |
+------------------------+--------+---------------------------------------------------------------+
| code.authorizationCode | String | The authorization code passed to ``saveAuthorizationCode()``. |
+------------------------+--------+---------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+------------------------+--------+---------------------------------------------------------------+
| code.redirectUri | String | The redirect URI associated with the authorization code. |
+------------------------+--------+---------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+------------------------+--------+---------------------------------------------------------------+
| code.client | Object | The client associated with the authorization code. |
+------------------------+--------+---------------------------------------------------------------+
| code.client.id | String | A unique string identifying the client. |
+------------------------+--------+---------------------------------------------------------------+
| code.user | Object | The user associated with the authorization code. |
+------------------------+--------+---------------------------------------------------------------+
+----------------------------+--------+----------------------------------------------------------------+
| Name | Type | Description |
+============================+========+================================================================+
| code | Object | The return value. |
+----------------------------+--------+----------------------------------------------------------------+
| code.authorizationCode | String | The authorization code passed to ``saveAuthorizationCode()``. |
+----------------------------+--------+----------------------------------------------------------------+
| code.expiresAt | Date | The expiry time of the authorization code. |
+----------------------------+--------+----------------------------------------------------------------+
| code.redirectUri | String | The redirect URI associated with the authorization code. |
+----------------------------+--------+----------------------------------------------------------------+
| [code.scope] | String | The authorized scope of the authorization code. |
+----------------------------+--------+----------------------------------------------------------------+
| code.client | Object | The client associated with the authorization code. |
+----------------------------+--------+----------------------------------------------------------------+
| code.client.id | String | A unique string identifying the client. |
+----------------------------+--------+----------------------------------------------------------------+
| code.user | Object | The user associated with the authorization code. |
+----------------------------+--------+----------------------------------------------------------------+
| [code.codeChallenge] | String | The code challenge string used with PKCE (RFC7636). |
+----------------------------+--------+----------------------------------------------------------------+
| [code.codeChallengeMethod] | String | The string for the code challenge method (`S256` or `plain` |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| [code.codeChallengeMethod] | String | The string for the code challenge method (`S256` or `plain` |
| [code.codeChallengeMethod] | String | The string for the code challenge method (`S256` or `plain`). |

+----------------------------+--------+----------------------------------------------------------------+

``code.client`` and ``code.user`` can carry additional properties that will be ignored by *oauth2-server*.

Expand All @@ -725,7 +739,9 @@ An ``Object`` representing the authorization code and associated data.
redirect_uri: code.redirectUri,
scope: code.scope,
client_id: client.id,
user_id: user.id
user_id: user.id,
code_challenge: code.codeChallenge,
code_challenge_method: code.codeChallengeMethod
};
return db.saveAuthorizationCode(authCode)
.then(function(authorizationCode) {
Expand All @@ -735,7 +751,9 @@ An ``Object`` representing the authorization code and associated data.
redirectUri: authorizationCode.redirect_uri,
scope: authorizationCode.scope,
client: {id: authorizationCode.client_id},
user: {id: authorizationCode.user_id}
user: {id: authorizationCode.user_id},
codeChallenge: authorizationCode.code_challenge,
codeChallengeMethod: authorizationCode.code_challenge_method
};
});
}
Expand Down
30 changes: 30 additions & 0 deletions lib/grant-types/authorization-code-grant-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var promisify = require('promisify-any').use(Promise);
var ServerError = require('../errors/server-error');
var is = require('../validator/is');
var util = require('util');
var crypto = require('crypto');
var stringUtil = require('../utils/string-util');

/**
* Constructor.
Expand Down Expand Up @@ -118,6 +120,34 @@ AuthorizationCodeGrantType.prototype.getAuthorizationCode = function(request, cl
throw new InvalidGrantError('Invalid grant: `redirect_uri` is not a valid URI');
}

if (code.codeChallenge) {
if (!request.body.code_verifier) {
throw new InvalidGrantError('Missing parameter: `code_verifier`');
}

var hash;
switch(code.codeChallengeMethod) {
case 'plain':
hash = request.body.code_verifier;
break;
case 'S256':
hash = stringUtil.base64URLEncode(crypto.createHash('sha256').update(request.body.code_verifier).digest());
break;
default:
throw new ServerError('Server error: `getAuthorizationCode()` did not return a valid `codeChallengeMethod` property');
}

if (code.codeChallenge !== hash) {
throw new InvalidGrantError('Invalid grant: code verifier is invalid');
}
}
else {
if (request.body.code_verifier) {
// No code challenge but code_verifier was passed in.
throw new InvalidGrantError('Invalid grant: code verifier is invalid');
}
}

return code;
});
};
Expand Down
20 changes: 19 additions & 1 deletion lib/handlers/token-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ TokenHandler.prototype.handle = function(request, response) {
TokenHandler.prototype.getClient = function(request, response) {
var credentials = this.getClientCredentials(request);
var grantType = request.body.grant_type;
var isPkce = this.isPKCERequest(request, grantType);

if (!credentials.clientId) {
throw new InvalidRequestError('Missing parameter: `client_id`');
}

if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret) {
if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret && !isPkce) {
throw new InvalidRequestError('Missing parameter: `client_secret`');
}

Expand Down Expand Up @@ -187,6 +188,12 @@ TokenHandler.prototype.getClientCredentials = function(request) {
return { clientId: request.body.client_id, clientSecret: request.body.client_secret };
}

if (this.isPKCERequest(request, grantType)) {
if(request.body.client_id) {
return { clientId: request.body.client_id };
}
}

if (!this.isClientAuthenticationRequired(grantType)) {
if(request.body.client_id) {
return { clientId: request.body.client_id };
Expand Down Expand Up @@ -293,6 +300,17 @@ TokenHandler.prototype.isClientAuthenticationRequired = function(grantType) {
}
};

/**
* Check if the request is a PCKE request. We assume PKCE if grant type is 'authorization_code'
* and code verifier is present.
*/
TokenHandler.prototype.isPKCERequest = function(request, grantType) {
if (grantType === 'authorization_code' && request.body.code_verifier) {
return true;
}
return false;
};

/**
* Export constructor.
*/
Expand Down
Loading