AuthorizeController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace OAuth2\OpenID\Controller;
  3. use OAuth2\Controller\AuthorizeController as BaseAuthorizeController;
  4. use OAuth2\RequestInterface;
  5. use OAuth2\ResponseInterface;
  6. /**
  7. * @see OAuth2\Controller\AuthorizeControllerInterface
  8. */
  9. class AuthorizeController extends BaseAuthorizeController implements AuthorizeControllerInterface
  10. {
  11. private $nonce;
  12. protected function setNotAuthorizedResponse(RequestInterface $request, ResponseInterface $response, $redirect_uri, $user_id = null)
  13. {
  14. $prompt = $request->query('prompt', 'consent');
  15. if ($prompt == 'none') {
  16. if (is_null($user_id)) {
  17. $error = 'login_required';
  18. $error_message = 'The user must log in';
  19. } else {
  20. $error = 'interaction_required';
  21. $error_message = 'The user must grant access to your application';
  22. }
  23. } else {
  24. $error = 'consent_required';
  25. $error_message = 'The user denied access to your application';
  26. }
  27. $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $this->getState(), $error, $error_message);
  28. }
  29. protected function buildAuthorizeParameters($request, $response, $user_id)
  30. {
  31. if (!$params = parent::buildAuthorizeParameters($request, $response, $user_id)) {
  32. return;
  33. }
  34. // Generate an id token if needed.
  35. if ($this->needsIdToken($this->getScope()) && $this->getResponseType() == self::RESPONSE_TYPE_AUTHORIZATION_CODE) {
  36. $params['id_token'] = $this->responseTypes['id_token']->createIdToken($this->getClientId(), $user_id, $this->nonce);
  37. }
  38. // add the nonce to return with the redirect URI
  39. $params['nonce'] = $this->nonce;
  40. return $params;
  41. }
  42. public function validateAuthorizeRequest(RequestInterface $request, ResponseInterface $response)
  43. {
  44. if (!parent::validateAuthorizeRequest($request, $response)) {
  45. return false;
  46. }
  47. $nonce = $request->query('nonce');
  48. // Validate required nonce for "id_token" and "id_token token"
  49. if (!$nonce && in_array($this->getResponseType(), array(self::RESPONSE_TYPE_ID_TOKEN, self::RESPONSE_TYPE_ID_TOKEN_TOKEN))) {
  50. $response->setError(400, 'invalid_nonce', 'This application requires you specify a nonce parameter');
  51. return false;
  52. }
  53. $this->nonce = $nonce;
  54. return true;
  55. }
  56. protected function getValidResponseTypes()
  57. {
  58. return array(
  59. self::RESPONSE_TYPE_ACCESS_TOKEN,
  60. self::RESPONSE_TYPE_AUTHORIZATION_CODE,
  61. self::RESPONSE_TYPE_ID_TOKEN,
  62. self::RESPONSE_TYPE_ID_TOKEN_TOKEN,
  63. self::RESPONSE_TYPE_CODE_ID_TOKEN,
  64. );
  65. }
  66. /**
  67. * Returns whether the current request needs to generate an id token.
  68. *
  69. * ID Tokens are a part of the OpenID Connect specification, so this
  70. * method checks whether OpenID Connect is enabled in the server settings
  71. * and whether the openid scope was requested.
  72. *
  73. * @param $request_scope
  74. * A space-separated string of scopes.
  75. *
  76. * @return
  77. * TRUE if an id token is needed, FALSE otherwise.
  78. */
  79. public function needsIdToken($request_scope)
  80. {
  81. // see if the "openid" scope exists in the requested scope
  82. return $this->scopeUtil->checkScope('openid', $request_scope);
  83. }
  84. public function getNonce()
  85. {
  86. return $this->nonce;
  87. }
  88. }