AuthorizeController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  12. * @var mixed
  13. */
  14. private $nonce;
  15. /**
  16. * Set not authorized response
  17. *
  18. * @param RequestInterface $request
  19. * @param ResponseInterface $response
  20. * @param string $redirect_uri
  21. * @param null $user_id
  22. */
  23. protected function setNotAuthorizedResponse(RequestInterface $request, ResponseInterface $response, $redirect_uri, $user_id = null)
  24. {
  25. $prompt = $request->query('prompt', 'consent');
  26. if ($prompt == 'none') {
  27. if (is_null($user_id)) {
  28. $error = 'login_required';
  29. $error_message = 'The user must log in';
  30. } else {
  31. $error = 'interaction_required';
  32. $error_message = 'The user must grant access to your application';
  33. }
  34. } else {
  35. $error = 'consent_required';
  36. $error_message = 'The user denied access to your application';
  37. }
  38. $response->setRedirect($this->config['redirect_status_code'], $redirect_uri, $this->getState(), $error, $error_message);
  39. }
  40. /**
  41. * @TODO: add dependency injection for the parameters in this method
  42. *
  43. * @param RequestInterface $request
  44. * @param ResponseInterface $response
  45. * @param mixed $user_id
  46. * @return array
  47. */
  48. protected function buildAuthorizeParameters($request, $response, $user_id)
  49. {
  50. if (!$params = parent::buildAuthorizeParameters($request, $response, $user_id)) {
  51. return;
  52. }
  53. // Generate an id token if needed.
  54. if ($this->needsIdToken($this->getScope()) && $this->getResponseType() == self::RESPONSE_TYPE_AUTHORIZATION_CODE) {
  55. $params['id_token'] = $this->responseTypes['id_token']->createIdToken($this->getClientId(), $user_id, $this->nonce);
  56. }
  57. // add the nonce to return with the redirect URI
  58. $params['nonce'] = $this->nonce;
  59. return $params;
  60. }
  61. /**
  62. * @param RequestInterface $request
  63. * @param ResponseInterface $response
  64. * @return bool
  65. */
  66. public function validateAuthorizeRequest(RequestInterface $request, ResponseInterface $response)
  67. {
  68. if (!parent::validateAuthorizeRequest($request, $response)) {
  69. return false;
  70. }
  71. $nonce = $request->query('nonce');
  72. // Validate required nonce for "id_token" and "id_token token"
  73. if (!$nonce && in_array($this->getResponseType(), array(self::RESPONSE_TYPE_ID_TOKEN, self::RESPONSE_TYPE_ID_TOKEN_TOKEN))) {
  74. $response->setError(400, 'invalid_nonce', 'This application requires you specify a nonce parameter');
  75. return false;
  76. }
  77. $this->nonce = $nonce;
  78. return true;
  79. }
  80. /**
  81. * Array of valid response types
  82. *
  83. * @return array
  84. */
  85. protected function getValidResponseTypes()
  86. {
  87. return array(
  88. self::RESPONSE_TYPE_ACCESS_TOKEN,
  89. self::RESPONSE_TYPE_AUTHORIZATION_CODE,
  90. self::RESPONSE_TYPE_ID_TOKEN,
  91. self::RESPONSE_TYPE_ID_TOKEN_TOKEN,
  92. self::RESPONSE_TYPE_CODE_ID_TOKEN,
  93. );
  94. }
  95. /**
  96. * Returns whether the current request needs to generate an id token.
  97. *
  98. * ID Tokens are a part of the OpenID Connect specification, so this
  99. * method checks whether OpenID Connect is enabled in the server settings
  100. * and whether the openid scope was requested.
  101. *
  102. * @param string $request_scope - A space-separated string of scopes.
  103. * @return boolean - TRUE if an id token is needed, FALSE otherwise.
  104. */
  105. public function needsIdToken($request_scope)
  106. {
  107. // see if the "openid" scope exists in the requested scope
  108. return $this->scopeUtil->checkScope('openid', $request_scope);
  109. }
  110. /**
  111. * @return mixed
  112. */
  113. public function getNonce()
  114. {
  115. return $this->nonce;
  116. }
  117. }