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

260 lines
9.9 KiB
PHP
Raw Normal View History

2013-02-01 16:20:32 +05:30
<?php
2013-02-14 01:06:56 +05:30
/**
* OAuth 2.0 Auth code grant
*
2014-01-08 21:45:29 +05:30
* @package league/oauth2-server
2013-02-14 01:06:56 +05:30
* @author Alex Bilbie <hello@alexbilbie.com>
2014-01-08 21:45:29 +05:30
* @copyright Copyright (c) PHP League of Extraordinary Packages
2013-02-14 01:06:56 +05:30
* @license http://mit-license.org/
2013-05-09 00:00:53 +05:30
* @link http://github.com/php-loep/oauth2-server
2013-02-14 01:06:56 +05:30
*/
2013-02-01 16:20:32 +05:30
namespace League\OAuth2\Server\Grant;
2013-05-08 23:36:09 +05:30
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 16:20:32 +05:30
2013-02-14 01:06:56 +05:30
/**
* Auth code grant class
*/
2013-02-01 16:20:32 +05:30
class AuthCode implements GrantTypeInterface {
use GrantTrait;
2013-02-14 01:06:56 +05:30
/**
* Grant identifier
* @var string
*/
2013-02-01 20:11:52 +05:30
protected $identifier = 'authorization_code';
2013-02-14 01:06:56 +05:30
/**
* Response type
* @var string
*/
2013-02-01 16:20:32 +05:30
protected $responseType = 'code';
/**
* AuthServer instance
* @var AuthServer
*/
protected $authServer = null;
/**
* Access token expires in override
* @var int
*/
protected $accessTokenTTL = null;
2013-05-08 03:50:32 +05:30
/**
* The TTL of the auth token
* @var integer
*/
protected $authTokenTTL = 600;
/**
* Override the default access token expire time
* @param int $authTokenTTL
* @return void
*/
public function setAuthTokenTTL($authTokenTTL)
{
$this->authTokenTTL = $authTokenTTL;
}
2013-02-14 01:06:56 +05:30
/**
* 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 18:27:24 +05:30
if ($this->authServer->stateParamRequired() === true && is_null($authParams['state'])) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'state'), 0);
}
// Validate client ID and redirect URI
2013-05-09 08:06:00 +05:30
$clientDetails = $this->authServer->getStorage('client')->getClient($authParams['client_id'], null, $authParams['redirect_uri'], $this->identifier);
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
}
if ($this->authServer->scopeParamRequired() === true && $this->authServer->getDefaultScope() === null && count($scopes) === 0) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'scope'), 0);
} elseif (count($scopes) === 0 && $this->authServer->getDefaultScope() !== null) {
if (is_array($this->authServer->getDefaultScope())) {
$scopes = $this->authServer->getDefaultScope();
} else {
$scopes = array($this->authServer->getDefaultScope());
}
}
$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 04:30:04 +05:30
// Create a new session
2013-04-29 15:55:23 +05:30
$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-11 05:56:23 +05:30
$authCodeId = $this->authServer->getStorage('session')->associateAuthCode($sessionId, $authCode, time() + $this->authTokenTTL);
// Associate the scopes to the auth code
foreach ($authParams['scopes'] as $scope) {
$this->authServer->getStorage('session')->associateAuthCodeScope($authCodeId, $scope['id']);
}
2013-04-29 04:30:04 +05:30
return $authCode;
}
/**
2013-02-14 01:06:56 +05:30
* Complete the auth code grant
* @param null|array $inputParams
* @return array
*/
public function completeFlow($inputParams = null)
2013-02-01 16:20:32 +05:30
{
2013-02-14 01:06:56 +05:30
// Get the required params
$authParams = $this->authServer->getParam(array('client_id', 'client_secret', 'redirect_uri', 'code'), 'post', $inputParams);
2013-02-01 16:20:32 +05:30
2013-02-14 01:06:56 +05:30
if (is_null($authParams['client_id'])) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_id'), 0);
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2013-02-01 20:11:52 +05:30
if (is_null($authParams['client_secret'])) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_secret'), 0);
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2013-02-01 20:11:52 +05:30
if (is_null($authParams['redirect_uri'])) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'redirect_uri'), 0);
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
// Validate client ID and redirect URI
$clientDetails = $this->authServer->getStorage('client')->getClient($authParams['client_id'], $authParams['client_secret'], $authParams['redirect_uri'], $this->identifier);
2013-02-01 16:20:32 +05:30
if ($clientDetails === false) {
throw new Exception\ClientException($this->authServer->getExceptionMessage('invalid_client'), 8);
2013-02-01 16:20:32 +05:30
}
2013-02-01 20:11:52 +05:30
$authParams['client_details'] = $clientDetails;
// Validate the authorization code
2013-02-01 20:11:52 +05:30
if (is_null($authParams['code'])) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'code'), 0);
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
// Verify the authorization code matches the client_id and the request_uri
2013-05-11 05:38:10 +05:30
$authCodeDetails = $this->authServer->getStorage('session')->validateAuthCode($authParams['client_id'], $authParams['redirect_uri'], $authParams['code']);
2013-02-01 16:20:32 +05:30
2013-05-11 05:38:10 +05:30
if ( ! $authCodeDetails) {
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_grant'), 'code'), 9);
2013-02-01 16:20:32 +05:30
}
2013-05-11 05:38:20 +05:30
// Get any associated scopes
$scopes = $this->authServer->getStorage('session')->getAuthCodeScopes($authCodeDetails['authcode_id']);
2013-02-01 16:20:32 +05:30
2013-05-11 05:38:10 +05:30
// A session ID was returned so update it with an access token and remove the authorisation code
2013-02-01 20:11:52 +05:30
$accessToken = SecureKey::make();
$accessTokenExpiresIn = ($this->accessTokenTTL !== null) ? $this->accessTokenTTL : $this->authServer->getAccessTokenTTL();
$accessTokenExpires = time() + $accessTokenExpiresIn;
2013-02-01 16:20:32 +05:30
2013-04-29 16:01:07 +05:30
// Remove the auth code
2013-05-11 05:38:10 +05:30
$this->authServer->getStorage('session')->removeAuthCode($authCodeDetails['session_id']);
2013-04-29 04:31:34 +05:30
2013-04-29 16:01:07 +05:30
// Create an access token
2013-05-11 05:38:10 +05:30
$accessTokenId = $this->authServer->getStorage('session')->associateAccessToken($authCodeDetails['session_id'], $accessToken, $accessTokenExpires);
2013-04-29 04:32:00 +05:30
// Associate scopes with the access token
2013-05-11 05:38:20 +05:30
if (count($scopes) > 0) {
foreach ($scopes as $scope) {
$this->authServer->getStorage('session')->associateScope($accessTokenId, $scope['scope_id']);
}
}
2013-02-01 16:20:32 +05:30
$response = array(
'access_token' => $accessToken,
2013-09-07 22:29:44 +05:30
'token_type' => 'Bearer',
2013-02-01 16:20:32 +05:30
'expires' => $accessTokenExpires,
'expires_in' => $accessTokenExpiresIn
);
2013-04-29 16:01:07 +05:30
// Associate a refresh token if set
if ($this->authServer->hasGrantType('refresh_token')) {
2013-04-29 16:01:07 +05:30
$refreshToken = SecureKey::make();
2013-05-08 23:04:25 +05:30
$refreshTokenTTL = time() + $this->authServer->getGrantType('refresh_token')->getRefreshTokenTTL();
$this->authServer->getStorage('session')->associateRefreshToken($accessTokenId, $refreshToken, $refreshTokenTTL, $authParams['client_id']);
2013-02-01 16:20:32 +05:30
$response['refresh_token'] = $refreshToken;
}
return $response;
}
2013-09-07 22:29:44 +05:30
}