2013-02-01 10:50:32 +00:00
|
|
|
<?php
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
|
|
|
* OAuth 2.0 Auth code grant
|
|
|
|
*
|
2013-05-08 11:29:24 -07:00
|
|
|
* @package php-loep/oauth2-server
|
2013-02-13 19:36:56 +00:00
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2013-05-08 11:29:24 -07:00
|
|
|
* @copyright Copyright (c) 2013 PHP League of Extraordinary Packages
|
2013-02-13 19:36:56 +00:00
|
|
|
* @license http://mit-license.org/
|
2013-05-08 11:30:53 -07:00
|
|
|
* @link http://github.com/php-loep/oauth2-server
|
2013-02-13 19:36:56 +00:00
|
|
|
*/
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
namespace League\OAuth2\Server\Grant;
|
2013-05-08 11:06:09 -07:00
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
use League\OAuth2\Server\Request;
|
|
|
|
use League\OAuth2\Server\Authorization;
|
|
|
|
use League\OAuth2\Server\Exception;
|
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
|
|
|
use League\OAuth2\Server\Storage\SessionInterface;
|
|
|
|
use League\OAuth2\Server\Storage\ClientInterface;
|
|
|
|
use League\OAuth2\Server\Storage\ScopeInterface;
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
|
|
|
* Auth code grant class
|
|
|
|
*/
|
2013-02-01 10:50:32 +00:00
|
|
|
class AuthCode implements GrantTypeInterface {
|
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
|
|
|
* Grant identifier
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 14:41:52 +00:00
|
|
|
protected $identifier = 'authorization_code';
|
2013-02-13 19:36:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Response type
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-01 10:50:32 +00:00
|
|
|
protected $responseType = 'code';
|
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
/**
|
|
|
|
* AuthServer instance
|
|
|
|
* @var AuthServer
|
|
|
|
*/
|
|
|
|
protected $authServer = null;
|
|
|
|
|
2013-05-06 15:04:00 -07:00
|
|
|
/**
|
|
|
|
* Access token expires in override
|
|
|
|
* @var int
|
|
|
|
*/
|
2013-05-07 15:16:30 -07:00
|
|
|
protected $accessTokenTTL = null;
|
2013-05-06 15:04:00 -07:00
|
|
|
|
2013-05-07 15:20:32 -07:00
|
|
|
/**
|
|
|
|
* The TTL of the auth token
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $authTokenTTL = 600;
|
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2013-05-08 14:10:48 -07:00
|
|
|
* @param Authorization $authServer Authorization server instance
|
2013-03-06 16:59:18 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-08 11:42:23 -07:00
|
|
|
public function __construct(Authorization $authServer)
|
2013-03-06 16:59:18 +00:00
|
|
|
{
|
|
|
|
$this->authServer = $authServer;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
|
|
|
* Return the identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-01 10:50:32 +00:00
|
|
|
public function getIdentifier()
|
|
|
|
{
|
|
|
|
return $this->identifier;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
|
|
|
* Return the response type
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-01 10:50:32 +00:00
|
|
|
public function getResponseType()
|
|
|
|
{
|
|
|
|
return $this->responseType;
|
|
|
|
}
|
|
|
|
|
2013-05-06 15:04:00 -07:00
|
|
|
/**
|
|
|
|
* Override the default access token expire time
|
2013-05-07 15:16:30 -07:00
|
|
|
* @param int $accessTokenTTL
|
2013-05-06 15:04:00 -07:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-07 15:16:30 -07:00
|
|
|
public function setAccessTokenTTL($accessTokenTTL)
|
2013-05-06 15:04:00 -07:00
|
|
|
{
|
2013-05-07 15:16:30 -07:00
|
|
|
$this->accessTokenTTL = $accessTokenTTL;
|
2013-05-06 15:04:00 -07:00
|
|
|
}
|
|
|
|
|
2013-05-07 15:20:32 -07:00
|
|
|
/**
|
|
|
|
* Override the default access token expire time
|
|
|
|
* @param int $authTokenTTL
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAuthTokenTTL($authTokenTTL)
|
|
|
|
{
|
|
|
|
$this->authTokenTTL = $authTokenTTL;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
/**
|
2013-03-31 13:37:02 +01:00
|
|
|
* Check authorise parameters
|
|
|
|
*
|
|
|
|
* @param array $inputParams Optional array of parsed $_GET keys
|
|
|
|
* @throws \OAuth2\Exception\ClientException
|
|
|
|
* @return array Authorise request parameters
|
|
|
|
*/
|
|
|
|
public function checkAuthoriseParams($inputParams = array())
|
|
|
|
{
|
|
|
|
// Auth params
|
|
|
|
$authParams = $this->authServer->getParam(array('client_id', 'redirect_uri', 'response_type', 'scope', 'state'), 'get', $inputParams);
|
|
|
|
|
|
|
|
if (is_null($authParams['client_id'])) {
|
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_id'), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($authParams['redirect_uri'])) {
|
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'redirect_uri'), 0);
|
|
|
|
}
|
|
|
|
|
2013-03-31 13:57:24 +01:00
|
|
|
if ($this->authServer->stateParamRequired() === true && is_null($authParams['state'])) {
|
2013-03-31 13:37:02 +01:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'state'), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate client ID and redirect URI
|
2013-05-08 19:36:00 -07:00
|
|
|
$clientDetails = $this->authServer->getStorage('client')->getClient($authParams['client_id'], null, $authParams['redirect_uri'], $this->identifier);
|
2013-03-31 13:37:02 +01:00
|
|
|
|
|
|
|
if ($clientDetails === false) {
|
|
|
|
throw new Exception\ClientException($this->authServer->getExceptionMessage('invalid_client'), 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
$authParams['client_details'] = $clientDetails;
|
|
|
|
|
|
|
|
if (is_null($authParams['response_type'])) {
|
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'response_type'), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure response type is one that is recognised
|
|
|
|
if ( ! in_array($authParams['response_type'], $this->authServer->getResponseTypes())) {
|
|
|
|
throw new Exception\ClientException($this->authServer->getExceptionMessage('unsupported_response_type'), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate scopes
|
|
|
|
$scopes = explode($this->authServer->getScopeDelimeter(), $authParams['scope']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($scopes); $i++) {
|
|
|
|
$scopes[$i] = trim($scopes[$i]);
|
|
|
|
if ($scopes[$i] === '') unset($scopes[$i]); // Remove any junk scopes
|
|
|
|
}
|
|
|
|
|
2013-05-09 10:02:41 -07:00
|
|
|
if ($this->authServer->scopeParamRequired() === true && $this->authServer->getDefaultScope() === null && count($scopes) === 0) {
|
2013-03-31 13:37:02 +01:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'scope'), 0);
|
2013-05-09 10:15:36 -07:00
|
|
|
} elseif (count($scopes) === 0 && $this->authServer->getDefaultScope() !== null) {
|
|
|
|
if (is_array($this->authServer->getDefaultScope())) {
|
|
|
|
$scopes = $this->authServer->getDefaultScope();
|
|
|
|
} else {
|
|
|
|
$scopes = array($this->authServer->getDefaultScope());
|
|
|
|
}
|
2013-03-31 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$authParams['scopes'] = array();
|
|
|
|
|
|
|
|
foreach ($scopes as $scope) {
|
|
|
|
$scopeDetails = $this->authServer->getStorage('scope')->getScope($scope, $authParams['client_id'], $this->identifier);
|
|
|
|
|
|
|
|
if ($scopeDetails === false) {
|
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_scope'), $scope), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
$authParams['scopes'][] = $scopeDetails;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $authParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a new authorise request
|
|
|
|
*
|
|
|
|
* @param string $type The session owner's type
|
|
|
|
* @param string $typeId The session owner's ID
|
|
|
|
* @param array $authParams The authorise request $_GET parameters
|
|
|
|
* @return string An authorisation code
|
|
|
|
*/
|
|
|
|
public function newAuthoriseRequest($type, $typeId, $authParams = array())
|
|
|
|
{
|
|
|
|
// Generate an auth code
|
|
|
|
$authCode = SecureKey::make();
|
|
|
|
|
|
|
|
// Remove any old sessions the user might have
|
|
|
|
$this->authServer->getStorage('session')->deleteSession($authParams['client_id'], $type, $typeId);
|
|
|
|
|
2013-04-29 00:01:07 +01:00
|
|
|
// List of scopes IDs
|
|
|
|
$scopeIds = array();
|
2013-03-31 13:37:02 +01:00
|
|
|
foreach ($authParams['scopes'] as $scope)
|
|
|
|
{
|
2013-04-29 00:01:07 +01:00
|
|
|
$scopeIds[] = $scope['id'];
|
2013-03-31 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
2013-04-29 00:00:04 +01:00
|
|
|
// Create a new session
|
2013-04-29 11:25:23 +01:00
|
|
|
$sessionId = $this->authServer->getStorage('session')->createSession($authParams['client_id'], $type, $typeId);
|
|
|
|
|
|
|
|
// Associate a redirect URI
|
|
|
|
$this->authServer->getStorage('session')->associateRedirectUri($sessionId, $authParams['redirect_uri']);
|
|
|
|
|
|
|
|
// Associate the auth code
|
2013-05-07 15:20:32 -07:00
|
|
|
$this->authServer->getStorage('session')->associateAuthCode($sessionId, $authCode, time() + $this->authTokenTTL, implode(',', $scopeIds));
|
2013-04-29 00:00:04 +01:00
|
|
|
|
2013-03-31 13:37:02 +01:00
|
|
|
return $authCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-13 19:36:56 +00:00
|
|
|
* Complete the auth code grant
|
|
|
|
* @param null|array $inputParams
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-02-13 18:25:10 +00:00
|
|
|
public function completeFlow($inputParams = null)
|
2013-02-01 10:50:32 +00:00
|
|
|
{
|
2013-02-13 19:36:56 +00:00
|
|
|
// Get the required params
|
2013-03-06 16:59:18 +00:00
|
|
|
$authParams = $this->authServer->getParam(array('client_id', 'client_secret', 'redirect_uri', 'code'), 'post', $inputParams);
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-02-13 19:36:56 +00:00
|
|
|
if (is_null($authParams['client_id'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_id'), 0);
|
2013-02-01 14:41:52 +00:00
|
|
|
}
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-02-01 14:41:52 +00:00
|
|
|
if (is_null($authParams['client_secret'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_secret'), 0);
|
2013-02-01 14:41:52 +00:00
|
|
|
}
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-02-01 14:41:52 +00:00
|
|
|
if (is_null($authParams['redirect_uri'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'redirect_uri'), 0);
|
2013-02-01 14:41:52 +00:00
|
|
|
}
|
2013-02-01 10:50:32 +00:00
|
|
|
|
|
|
|
// Validate client ID and redirect URI
|
2013-03-27 14:47:07 +00:00
|
|
|
$clientDetails = $this->authServer->getStorage('client')->getClient($authParams['client_id'], $authParams['client_secret'], $authParams['redirect_uri'], $this->identifier);
|
2013-02-01 10:50:32 +00:00
|
|
|
|
|
|
|
if ($clientDetails === false) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException($this->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;
|
|
|
|
|
2013-02-13 18:25:10 +00:00
|
|
|
// Validate the authorization code
|
2013-02-01 14:41:52 +00:00
|
|
|
if (is_null($authParams['code'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'code'), 0);
|
2013-02-01 14:41:52 +00:00
|
|
|
}
|
2013-02-01 10:50:32 +00:00
|
|
|
|
|
|
|
// Verify the authorization code matches the client_id and the request_uri
|
2013-03-06 16:59:18 +00:00
|
|
|
$session = $this->authServer->getStorage('session')->validateAuthCode($authParams['client_id'], $authParams['redirect_uri'], $authParams['code']);
|
2013-02-01 10:50:32 +00:00
|
|
|
|
|
|
|
if ( ! $session) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_grant'), 'code'), 9);
|
2013-02-01 10:50:32 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 00:01:51 +01:00
|
|
|
// A session ID was returned so update it with an access token and remove the authorisation code
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-02-01 14:41:52 +00:00
|
|
|
$accessToken = SecureKey::make();
|
2013-05-08 10:35:13 -07:00
|
|
|
$accessTokenExpiresIn = ($this->accessTokenTTL !== null) ? $this->accessTokenTTL : $this->authServer->getAccessTokenTTL();
|
2013-05-06 15:04:00 -07:00
|
|
|
$accessTokenExpires = time() + $accessTokenExpiresIn;
|
2013-02-01 10:50:32 +00:00
|
|
|
|
2013-04-29 11:31:07 +01:00
|
|
|
// Remove the auth code
|
|
|
|
$this->authServer->getStorage('session')->removeAuthCode($session['id']);
|
2013-04-29 00:01:34 +01:00
|
|
|
|
2013-04-29 11:31:07 +01:00
|
|
|
// Create an access token
|
2013-04-29 11:53:20 +01:00
|
|
|
$accessTokenId = $this->authServer->getStorage('session')->associateAccessToken($session['id'], $accessToken, $accessTokenExpires);
|
2013-04-29 00:02:00 +01:00
|
|
|
|
2013-04-29 00:01:07 +01:00
|
|
|
// Associate scopes with the access token
|
|
|
|
if ( ! is_null($session['scope_ids'])) {
|
|
|
|
$scopeIds = explode(',', $session['scope_ids']);
|
|
|
|
|
|
|
|
foreach ($scopeIds as $scopeId) {
|
|
|
|
$this->authServer->getStorage('session')->associateScope($accessTokenId, $scopeId);
|
|
|
|
}
|
|
|
|
}
|
2013-02-01 10:50:32 +00:00
|
|
|
|
|
|
|
$response = array(
|
|
|
|
'access_token' => $accessToken,
|
|
|
|
'token_type' => 'bearer',
|
|
|
|
'expires' => $accessTokenExpires,
|
|
|
|
'expires_in' => $accessTokenExpiresIn
|
|
|
|
);
|
|
|
|
|
2013-04-29 11:31:07 +01:00
|
|
|
// Associate a refresh token if set
|
2013-03-06 16:59:18 +00:00
|
|
|
if ($this->authServer->hasGrantType('refresh_token')) {
|
2013-04-29 11:31:07 +01:00
|
|
|
$refreshToken = SecureKey::make();
|
2013-05-08 10:34:25 -07:00
|
|
|
$refreshTokenTTL = time() + $this->authServer->getGrantType('refresh_token')->getRefreshTokenTTL();
|
2013-05-09 07:55:10 -07:00
|
|
|
$this->authServer->getStorage('session')->associateRefreshToken($accessTokenId, $refreshToken, $refreshTokenTTL, $authParams['client_id']);
|
2013-02-01 10:50:32 +00:00
|
|
|
$response['refresh_token'] = $refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|