From d2267dbd245402c24cd3558fd69cff3f1c4f6d76 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 12 Feb 2013 16:22:14 +0000 Subject: [PATCH] Added Password grant --- src/OAuth2/Grant/Password.php | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/OAuth2/Grant/Password.php diff --git a/src/OAuth2/Grant/Password.php b/src/OAuth2/Grant/Password.php new file mode 100644 index 00000000..b3fbaea0 --- /dev/null +++ b/src/OAuth2/Grant/Password.php @@ -0,0 +1,134 @@ +identifier; + } + + public function getResponseType() + { + return $this->responseType; + } + + public function setVerifyCredentialsCallback($callback) + { + $this->callback = $callback; + } + + protected function getVerifyCredentialsCallback($username, $password) + { + if (is_null($this->callback) || ! is_callable($this->callback)) { + throw new Exception\InvalidGrantTypeException('Null or non-callable callback set'); + } + + return call_user_func($this->callback, $username, $password); + } + + public function completeFlow($inputParams = null, $authParams = array()) + { + // Client ID + $authParams['client_id'] = (isset($inputParams['client_id'])) ? + $inputParams['client_id'] : + AuthServer::getRequest()->post('client_id'); + + if (is_null($authParams['client_id'])) { + throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_id'), 0); + } + + // Client secret + $authParams['client_secret'] = (isset($inputParams['client_secret'])) ? + $inputParams['client_secret'] : + AuthServer::getRequest()->post('client_secret'); + + if (is_null($authParams['client_secret'])) { + throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_secret'), 0); + } + + // Validate client ID and redirect URI + $clientDetails = AuthServer::getStorage('client')->getClient($authParams['client_id'], $authParams['client_secret']); + + if ($clientDetails === false) { + throw new Exception\ClientException(AuthServer::getExceptionMessage('invalid_client'), 8); + } + + $authParams['client_details'] = $clientDetails; + + // User's username + $authParams['username'] = (isset($inputParams['username'])) ? + $inputParams['username'] : + AuthServer::getRequest()->post('username'); + + if (is_null($authParams['username'])) { + throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'username'), 0); + } + + // User's password + $authParams['password'] = (isset($inputParams['password'])) ? + $inputParams['password'] : + AuthServer::getRequest()->post('password'); + + if (is_null($authParams['password'])) { + throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'password'), 0); + } + + // Check if user's username and password are correct + $userId = call_user_func($this->getVerifyCredentialsCallback, $params['username'], $params['password']); + + if ($userId === false) { + throw new Exception\ClientException(AuthServer::getExceptionMessage('invalid_credentials'), 0); + } + + // Generate an access token + $accessToken = SecureKey::make(); + $refreshToken = (AuthServer::hasGrantType('refresh_token')) ? SecureKey::make() : null; + + $accessTokenExpires = time() + AuthServer::getExpiresIn(); + $accessTokenExpiresIn = AuthServer::getExpiresIn(); + + // Delete any existing sessions just to be sure + AuthServer::getStorage('session')->deleteSession($authParams['client_id'], 'user', $userId); + + // Create a new session + AuthServer::getStorage('session')->createSession( + $authParams['client_id'], + null, + 'user', + $userId, + null, + $accessToken, + $refreshToken, + $accessTokenExpires, + 'granted' + ); + + $response = array( + 'access_token' => $accessToken, + 'token_type' => 'bearer', + 'expires' => $accessTokenExpires, + 'expires_in' => $accessTokenExpiresIn + ); + + if (AuthServer::hasGrantType('refresh_token')) { + $response['refresh_token'] = $refreshToken; + } + + return $response; + } + +} \ No newline at end of file