oauth2-server/src/OAuth2/Grant/AuthCode.php

97 lines
3.3 KiB
PHP
Raw Normal View History

2013-02-01 10:50:32 +00:00
<?php
2013-02-01 14:41:52 +00:00
namespace OAuth2\Grant;
2013-02-01 10:50:32 +00:00
2013-02-01 14:41:52 +00:00
use OAuth2\Request;
use OAuth2\AuthServer;
2013-02-01 10:50:32 +00:00
use OAuth2\Exception;
2013-02-01 14:41:52 +00:00
use OAuth2\Util\SecureKey;
use OAuth2\Storage\SessionInterface;
use OAuth2\Storage\ClientInterface;
use OAuth2\Storage\ScopeInterface;
2013-02-01 10:50:32 +00:00
class AuthCode implements GrantTypeInterface {
2013-02-01 14:41:52 +00:00
protected $identifier = 'authorization_code';
2013-02-01 10:50:32 +00:00
protected $responseType = 'code';
public function getIdentifier()
{
return $this->identifier;
}
public function getResponseType()
{
return $this->responseType;
}
public function completeFlow($inputParams = null)
2013-02-01 10:50:32 +00:00
{
$authParams = AuthServer::getParam(array('client_id', 'client_secret', 'redirect_uri', 'code'), 'post', $inputParams);
2013-02-01 10:50:32 +00:00
if (is_null($authParams['client_id'])) {
2013-02-01 14:41:52 +00:00
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_id'), 0);
}
2013-02-01 10:50:32 +00:00
2013-02-01 14:41:52 +00:00
if (is_null($authParams['client_secret'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_secret'), 0);
}
2013-02-01 10:50:32 +00:00
2013-02-01 14:41:52 +00:00
if (is_null($authParams['redirect_uri'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'redirect_uri'), 0);
}
2013-02-01 10:50:32 +00:00
// Validate client ID and redirect URI
2013-02-04 19:45:47 +00:00
$clientDetails = AuthServer::getStorage('client')->getClient($authParams['client_id'], $authParams['client_secret'], $authParams['redirect_uri']);
2013-02-01 10:50:32 +00:00
if ($clientDetails === false) {
2013-02-01 14:41:52 +00:00
throw new Exception\ClientException(AuthServer::getExceptionMessage('invalid_client'), 8);
2013-02-01 10:50:32 +00:00
}
2013-02-01 14:41:52 +00:00
$authParams['client_details'] = $clientDetails;
// Validate the authorization code
2013-02-01 14:41:52 +00:00
if (is_null($authParams['code'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'code'), 0);
}
2013-02-01 10:50:32 +00:00
// Verify the authorization code matches the client_id and the request_uri
2013-02-01 14:41:52 +00:00
$session = AuthServer::getStorage('session')->validateAuthCode($authParams['client_id'], $authParams['redirect_uri'], $authParams['code']);
2013-02-01 10:50:32 +00:00
if ( ! $session) {
2013-02-01 14:41:52 +00:00
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_grant'), 'code'), 9);
2013-02-01 10:50:32 +00:00
}
// A session ID was returned so update it with an access token,
// remove the authorisation code, change the stage to 'granted'
2013-02-01 14:41:52 +00:00
$accessToken = SecureKey::make();
2013-02-01 15:59:50 +00:00
$refreshToken = (AuthServer::hasGrantType('refresh_token')) ? SecureKey::make() : null;
2013-02-01 10:50:32 +00:00
2013-02-01 14:41:52 +00:00
$accessTokenExpires = time() + AuthServer::getExpiresIn();
$accessTokenExpiresIn = AuthServer::getExpiresIn();
2013-02-01 10:50:32 +00:00
AuthServer::getStorage('session')->updateSession(
2013-02-01 10:50:32 +00:00
$session['id'],
null,
$accessToken,
$refreshToken,
$accessTokenExpires,
'granted'
);
$response = array(
'access_token' => $accessToken,
'token_type' => 'bearer',
'expires' => $accessTokenExpires,
'expires_in' => $accessTokenExpiresIn
);
2013-02-01 14:41:52 +00:00
if (AuthServer::hasGrantType('refresh_token')) {
2013-02-01 10:50:32 +00:00
$response['refresh_token'] = $refreshToken;
}
return $response;
}
}