-
Notifications
You must be signed in to change notification settings - Fork 951
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 support for authorization code grant with PKCE for distributed apps #1067
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ test/config/test.sqlite | |
vendor | ||
composer.lock | ||
.idea | ||
.phpunit.result.cache |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace OAuth2\Controller; | ||
|
||
use OAuth2\Storage\ClientCredentialsInterface; | ||
use OAuth2\Storage\ClientInterface; | ||
use OAuth2\ScopeInterface; | ||
use OAuth2\RequestInterface; | ||
|
@@ -61,6 +62,16 @@ class AuthorizeController implements AuthorizeControllerInterface | |
*/ | ||
protected $scopeUtil; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $code_challenge; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $code_challenge_method; | ||
|
||
/** | ||
* Constructor | ||
* | ||
|
@@ -88,6 +99,8 @@ public function __construct(ClientInterface $clientStorage, array $responseTypes | |
'require_exact_redirect_uri' => true, | ||
'redirect_status_code' => 302, | ||
'enforce_pkce' => false, | ||
'enforce_pkce_for_public_clients' => false, | ||
'supported_code_challenge_methods' => ['plain', 'S256'], | ||
), $config); | ||
|
||
if (is_null($scopeUtil)) { | ||
|
@@ -139,7 +152,7 @@ public function handleAuthorizeRequest(RequestInterface $request, ResponseInterf | |
|
||
$authResult = $this->responseTypes[$this->response_type]->getAuthorizeResponse($params, $user_id); | ||
|
||
list($redirect_uri, $uri_params) = $authResult; | ||
[$redirect_uri, $uri_params] = $authResult; | ||
|
||
if (empty($redirect_uri) && !empty($registered_redirect_uri)) { | ||
$redirect_uri = $registered_redirect_uri; | ||
|
@@ -186,6 +199,8 @@ protected function buildAuthorizeParameters($request, $response, $user_id) | |
'client_id' => $this->client_id, | ||
'redirect_uri' => $this->redirect_uri, | ||
'response_type' => $this->response_type, | ||
'code_challenge' => $this->code_challenge, | ||
'code_challenge_method' => $this->code_challenge_method, | ||
); | ||
|
||
return $params; | ||
|
@@ -257,7 +272,7 @@ public function validateAuthorizeRequest(RequestInterface $request, ResponseInte | |
$response_type = $request->query('response_type', $request->request('response_type')); | ||
|
||
// for multiple-valued response types - make them alphabetical | ||
if (false !== strpos($response_type, ' ')) { | ||
if ($response_type && false !== strpos($response_type, ' ')) { | ||
$types = explode(' ', $response_type); | ||
sort($types); | ||
$response_type = ltrim(implode(' ', $types)); | ||
|
@@ -334,13 +349,50 @@ public function validateAuthorizeRequest(RequestInterface $request, ResponseInte | |
return false; | ||
} | ||
|
||
$code_challenge = $request->query('code_challenge'); | ||
$code_challenge_method = $request->query('code_challenge_method'); | ||
if ($code_challenge) { | ||
if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $code_challenge) !== 1) { | ||
$response->setError(400, 'invalid_code_challenge', 'The PKCE code challenge supplied is invalid'); | ||
|
||
return false; | ||
} | ||
|
||
$supported_challenge_methods = $this->config['supported_code_challenge_methods'] ?? ['plain', 'S256']; | ||
if (!$code_challenge_method) { | ||
$response->setError(400, 'missing_code_challenge_method', 'This application requires you specify a PKCE code challenge method. Supported methods: ' . implode(', ', $supported_challenge_methods) | ||
); | ||
|
||
return false; | ||
} | ||
if (!in_array($code_challenge_method, $supported_challenge_methods, true)) { | ||
$response->setError(400, 'invalid_code_challenge_method', 'The PKCE code challenge method supplied is invalid. Supported methods: ' . implode(', ', $supported_challenge_methods) | ||
); | ||
|
||
return false; | ||
} | ||
} else { | ||
$isPublic = false; | ||
if ($this->clientStorage instanceof ClientCredentialsInterface) { | ||
$isPublic = $this->clientStorage->isPublicClient($client_id); | ||
Comment on lines
+376
to
+377
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ not sure why there are two interfaces, but the property is only typed to the higher level one which doesn't have the method we need. |
||
} | ||
|
||
if ($this->config['enforce_pkce'] || ($this->config['enforce_pkce_for_public_clients'] && $isPublic)) { | ||
$response->setError(400, 'missing_code_challenge', 'This application requires you provide a PKCE code challenge'); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
// save the input data and return true | ||
$this->scope = $requestedScope; | ||
$this->state = $state; | ||
$this->client_id = $client_id; | ||
// Only save the SUPPLIED redirect URI (@see http://tools.ietf.org/html/rfc6749#section-4.1.3) | ||
$this->redirect_uri = $supplied_redirect_uri; | ||
$this->response_type = $response_type; | ||
$this->code_challenge = $code_challenge; | ||
$this->code_challenge_method = $code_challenge_method; | ||
|
||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
use OAuth2\ResponseInterface; | ||
|
||
/** | ||
* @see OAuth2\Controller\AuthorizeControllerInterface | ||
* @see \OAuth2\Controller\AuthorizeControllerInterface | ||
*/ | ||
class AuthorizeController extends BaseAuthorizeController implements AuthorizeControllerInterface | ||
{ | ||
|
@@ -16,16 +16,6 @@ class AuthorizeController extends BaseAuthorizeController implements AuthorizeCo | |
*/ | ||
private $nonce; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $code_challenge; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ these properties and their behavior moved to the parent class |
||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $code_challenge_method; | ||
|
||
/** | ||
* Set not authorized response | ||
* | ||
|
@@ -75,10 +65,6 @@ protected function buildAuthorizeParameters($request, $response, $user_id) | |
// add the nonce to return with the redirect URI | ||
$params['nonce'] = $this->nonce; | ||
|
||
// Add PKCE code challenge. | ||
$params['code_challenge'] = $this->code_challenge; | ||
$params['code_challenge_method'] = $this->code_challenge_method; | ||
|
||
return $params; | ||
} | ||
|
||
|
@@ -104,32 +90,6 @@ public function validateAuthorizeRequest(RequestInterface $request, ResponseInte | |
|
||
$this->nonce = $nonce; | ||
|
||
$code_challenge = $request->query('code_challenge'); | ||
$code_challenge_method = $request->query('code_challenge_method'); | ||
|
||
if ($this->config['enforce_pkce']) { | ||
if (!$code_challenge) { | ||
$response->setError(400, 'missing_code_challenge', 'This application requires you provide a PKCE code challenge'); | ||
|
||
return false; | ||
} | ||
|
||
if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $code_challenge) !== 1) { | ||
$response->setError(400, 'invalid_code_challenge', 'The PKCE code challenge supplied is invalid'); | ||
|
||
return false; | ||
} | ||
|
||
if (!in_array($code_challenge_method, array('plain', 'S256'), true)) { | ||
$response->setError(400, 'missing_code_challenge_method', 'This application requires you specify a PKCE code challenge method'); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
$this->code_challenge = $code_challenge; | ||
$this->code_challenge_method = $code_challenge_method; | ||
|
||
return true; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,8 @@ public function __construct($storage = array(), array $config = array(), array $ | |
'require_exact_redirect_uri' => true, | ||
'allow_implicit' => false, | ||
'enforce_pkce' => false, | ||
'enforce_pkce_for_public_clients' => false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ same comment about default value |
||
'supported_code_challenge_methods' => array('plain', 'S256'), | ||
'allow_credentials_in_request_body' => true, | ||
'allow_public_clients' => true, | ||
'always_issue_new_refresh_token' => false, | ||
|
@@ -578,7 +580,7 @@ protected function createDefaultAuthorizeController() | |
} | ||
} | ||
|
||
$config = array_intersect_key($this->config, array_flip(explode(' ', 'allow_implicit enforce_state require_exact_redirect_uri enforce_pkce'))); | ||
$config = array_intersect_key($this->config, array_flip(explode(' ', 'allow_implicit enforce_state require_exact_redirect_uri enforce_pkce enforce_pkce_for_public_clients supported_code_challenge_methods'))); | ||
|
||
if ($this->config['use_openid_connect']) { | ||
return new OpenIDAuthorizeController($this->storages['client'], $this->responseTypes, $config, $this->getScopeUtil()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ this should probably default to true for new installs, but that could be a breaking change for existing installs. Leaving this at false allows for releasing as a minor version. Would recommend defaulting to true for the next major version.