-
Notifications
You must be signed in to change notification settings - Fork 92
add oauth2 response format #72
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 |
---|---|---|
|
@@ -23,6 +23,11 @@ class AuthController extends AbstractActionController | |
*/ | ||
protected $server; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $apiProblemErrorResponse = true; | ||
|
||
/** | ||
* Constructor | ||
* | ||
|
@@ -33,6 +38,22 @@ public function __construct(OAuth2Server $server) | |
$this->server = $server; | ||
} | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
public function isApiProblemErrorResponse() | ||
{ | ||
return $this->apiProblemErrorResponse; | ||
} | ||
|
||
/** | ||
* @param boolean $apiProblemErrorResponse | ||
*/ | ||
public function setApiProblemErrorResponse($apiProblemErrorResponse) | ||
{ | ||
$this->apiProblemErrorResponse = $apiProblemErrorResponse; | ||
} | ||
|
||
/** | ||
* Token Action (/oauth) | ||
*/ | ||
|
@@ -52,21 +73,11 @@ public function tokenAction() | |
|
||
$oauth2request = $this->getOAuth2Request(); | ||
$response = $this->server->handleTokenRequest($oauth2request); | ||
|
||
if ($response->isClientError()) { | ||
$parameters = $response->getParameters(); | ||
$errorUri = isset($parameters['error_uri']) ? $parameters['error_uri'] : null; | ||
$error = isset($parameters['error']) ? $parameters['error'] : null; | ||
$errorDescription = isset($parameters['error_description']) ? $parameters['error_description'] : null; | ||
|
||
return new ApiProblemResponse( | ||
new ApiProblem( | ||
$response->getStatusCode(), | ||
$errorDescription, | ||
$errorUri, | ||
$error | ||
) | ||
); | ||
return $this->getErrorResponse($response); | ||
} | ||
|
||
return $this->setHttpResponse($response); | ||
} | ||
|
||
|
@@ -78,17 +89,9 @@ public function resourceAction() | |
// Handle a request for an OAuth2.0 Access Token and send the response to the client | ||
if (!$this->server->verifyResourceRequest($this->getOAuth2Request())) { | ||
$response = $this->server->getResponse(); | ||
$parameters = $response->getParameters(); | ||
$errorUri = isset($parameters['error_uri']) ? $parameters['error_uri'] : null; | ||
return new ApiProblemResponse( | ||
new ApiProblem( | ||
$response->getStatusCode(), | ||
$parameters['error_description'], | ||
$errorUri, | ||
$parameters['error'] | ||
) | ||
); | ||
return $this->getApiProblemResponse($response); | ||
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. Shouldn't this be 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. Never mind; this particular problem is not handled by the oauth2 spec, so it can and should be in a different format. |
||
} | ||
|
||
$httpResponse = $this->getResponse(); | ||
$httpResponse->setStatusCode(200); | ||
$httpResponse->getHeaders()->addHeaders(array('Content-type' => 'application/json')); | ||
|
@@ -107,17 +110,10 @@ public function authorizeAction() | |
$response = new OAuth2Response(); | ||
|
||
// validate the authorize request | ||
if (!$this->server->validateAuthorizeRequest($request, $response)) { | ||
$parameters = $response->getParameters(); | ||
$errorUri = isset($parameters['error_uri']) ? $parameters['error_uri'] : null; | ||
return new ApiProblemResponse( | ||
new ApiProblem( | ||
$response->getStatusCode(), | ||
$parameters['error_description'], | ||
$errorUri, | ||
$parameters['error'] | ||
) | ||
); | ||
$isValid = $this->server->validateAuthorizeRequest($request, $response); | ||
|
||
if (!$isValid) { | ||
return $this->getErrorResponse($response); | ||
} | ||
|
||
$authorized = $request->request('authorized', false); | ||
|
@@ -141,16 +137,7 @@ public function authorizeAction() | |
return $this->redirect()->toUrl($redirect); | ||
} | ||
|
||
$parameters = $response->getParameters(); | ||
$errorUri = isset($parameters['error_uri']) ? $parameters['error_uri'] : null; | ||
return new ApiProblemResponse( | ||
new ApiProblem( | ||
$response->getStatusCode(), | ||
$parameters['error_description'], | ||
$errorUri, | ||
$parameters['error'] | ||
) | ||
); | ||
return $this->getErrorResponse($response); | ||
} | ||
|
||
/** | ||
|
@@ -166,6 +153,42 @@ public function receiveCodeAction() | |
return $view; | ||
} | ||
|
||
/** | ||
* @param OAuth2Response $response | ||
* @return \ZF\ApiProblem\ApiProblemResponse|\Zend\Stdlib\ResponseInterface | ||
*/ | ||
protected function getErrorResponse(OAuth2Response $response) | ||
{ | ||
if ($this->isApiProblemErrorResponse()) { | ||
return $this->getApiProblemResponse($response); | ||
} else { | ||
return $this->setHttpResponse($response); | ||
} | ||
} | ||
|
||
/** | ||
* Map OAuth2Response to ApiProblemResponse | ||
* | ||
* @param OAuth2Response $response | ||
* @return ApiProblemResponse | ||
*/ | ||
protected function getApiProblemResponse(OAuth2Response $response) | ||
{ | ||
$parameters = $response->getParameters(); | ||
$errorUri = isset($parameters['error_uri']) ? $parameters['error_uri'] : null; | ||
$error = isset($parameters['error']) ? $parameters['error'] : null; | ||
$errorDescription = isset($parameters['error_description']) ? $parameters['error_description'] : null; | ||
|
||
return new ApiProblemResponse( | ||
new ApiProblem( | ||
$response->getStatusCode(), | ||
$errorDescription, | ||
$errorUri, | ||
$error | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Create an OAuth2 request based on the ZF2 request object | ||
* | ||
|
@@ -210,6 +233,9 @@ protected function getOAuth2Request() | |
if (isset($server['PHP_AUTH_PW'])) { | ||
$headers['PHP_AUTH_PW'] = $server['PHP_AUTH_PW']; | ||
} | ||
if (isset($server['HTTP_AUTHORIZATION'])) { | ||
$headers['AUTHORIZATION'] = $server['HTTP_AUTHORIZATION']; | ||
} | ||
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. The above is not necessary, as the ZF2 request already ensures we have the authorization header present. 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. Our code doesn't work without this check. |
||
|
||
// Ensure the bodyParams are passed as an array | ||
$bodyParams = $this->bodyParams() ?: array(); | ||
|
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.
Convert incoming variable to (bool)