oauth2-server/src/Grant/AuthCode.php

257 lines
7.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-03-10 01:04:23 +05:30
* @copyright Copyright (c) Alex Bilbie
2013-02-14 01:06:56 +05:30
* @license http://mit-license.org/
2014-03-10 01:35:38 +05:30
* @link https://github.com/thephpleague/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
2014-04-06 23:44:46 +05:30
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Request;
use League\OAuth2\Server\Exception;
2014-04-06 23:44:46 +05:30
use League\OAuth2\Server\Entity\Client;
use League\OAuth2\Server\Entity\RefreshToken;
use League\OAuth2\Server\Entity\Session;
use League\OAuth2\Server\Entity\AccessToken;
use League\OAuth2\Server\Entity\Scope;
use League\OAuth2\Server\Entity\AuthCode as AC;
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
*/
2014-04-06 23:44:46 +05:30
class AuthCode extends AbstractGrant
{
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
*/
2014-03-10 01:04:23 +05:30
protected $server = 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
*
2014-03-10 01:04:23 +05:30
* @throws
* @return array Authorise request parameters
*/
2014-03-10 01:04:23 +05:30
public function checkAuthoriseParams()
{
2014-04-06 23:44:46 +05:30
// Get required params
2014-03-10 01:04:23 +05:30
$clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('client_id');
}
2014-03-10 01:04:23 +05:30
$redirectUri = $this->server->getRequest()->request->get('redirect_uri', null);
if (is_null($redirectUri)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('redirect_uri');
}
2014-03-10 01:04:23 +05:30
$state = $this->server->getRequest()->request->get('state', null);
if ($this->server->stateParamRequired() === true && is_null($state)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('state');
}
2014-03-10 01:04:23 +05:30
$responseType = $this->server->getRequest()->request->get('response_type', null);
if (is_null($responseType)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('response_type');
}
// Ensure response type is one that is recognised
2014-03-10 01:04:23 +05:30
if ( ! in_array($responseType, $this->server->getResponseTypes())) {
2014-04-25 15:54:48 +05:30
throw new Exception\UnsupportedResponseTypeException();
}
2014-03-10 01:04:23 +05:30
// Validate client ID and redirect URI
$client = $this->server->getStorage('client')->get(
$clientId,
null,
$redirectUri,
$this->getIdentifier()
);
2014-03-10 01:04:23 +05:30
if (($client instanceof Client) === false) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidClientException();
}
2014-03-10 01:04:23 +05:30
// Validate any scopes that are in the request
$scopeParam = $this->server->getRequest()->request->get('scope', '');
$scopes = $this->validateScopes($scopeParam);
return [
'client' => $client,
'redirect_uri' => $redirectUri,
'state' => $state,
'response_type' => $responseType,
'scopes' => $scopes
];
}
/**
* 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
*/
2014-04-06 23:44:46 +05:30
public function newAuthoriseRequest($type, $typeId, $authParams = [])
{
2013-04-29 04:30:04 +05:30
// Create a new session
2014-03-10 01:04:23 +05:30
$session = new Session($this->server);
$session->setOwner($type, $typeId);
$session->associateClient($authParams['client']);
2014-04-06 23:44:46 +05:30
$session->save();
2013-04-29 15:55:23 +05:30
2014-04-06 23:44:46 +05:30
// Create a new auth code
$authCode = new AC($this->server);
2014-04-07 01:38:20 +05:30
$authCode->setToken(SecureKey::generate());
2014-04-06 23:44:46 +05:30
$authCode->setRedirectUri($authParams['redirect_uri']);
2013-04-29 15:55:23 +05:30
foreach ($authParams['scopes'] as $scope) {
2014-04-06 23:44:46 +05:30
$authCode->associateScope($scope);
}
2013-04-29 04:30:04 +05:30
2014-04-06 23:44:46 +05:30
$authCode->setSession($session);
$authCode->save();
return $authCode->generateRedirectUri($authParams['state']);
}
/**
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
2014-04-06 23:44:46 +05:30
$clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('client_id');
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
$clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('client_secret');
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
$redirectUri = $this->server->getRequest()->request->get('redirect_uri', null);
if (is_null($redirectUri)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('redirect_uri');
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
// Validate client ID and client secret
$client = $this->server->getStorage('client')->get(
$clientId,
$clientSecret,
$redirectUri,
$this->getIdentifier()
);
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
if (($client instanceof Client) === false) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidClientException();
2013-02-01 16:20:32 +05:30
}
2014-04-06 23:44:46 +05:30
// Validate the auth code
$authCode = $this->server->getRequest()->request->get('code', null);
if (is_null($authCode)) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('code');
2013-02-01 20:11:52 +05:30
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
$code = $this->server->getStorage('auth_code')->get($authCode);
if (($code instanceof AC) === false) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('code');
2013-02-01 16:20:32 +05:30
}
2014-04-06 23:44:46 +05:30
// Check redirect URI presented matches redirect URI originally used in authorise request
if ($code->getRedirectUri() !== $redirectUri) {
2014-04-25 15:54:48 +05:30
throw new Exception\InvalidRequestException('redirect_uri');
2014-04-06 23:44:46 +05:30
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
$session = $code->getSession();
$authCodeScopes = $code->getScopes();
2013-04-29 04:31:34 +05:30
2014-04-06 23:44:46 +05:30
// Generate the access token
$accessToken = new AccessToken($this->server);
2014-04-07 01:38:20 +05:30
$accessToken->setToken(SecureKey::generate());
2014-04-06 23:44:46 +05:30
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
2013-04-29 04:32:00 +05:30
2014-04-06 23:44:46 +05:30
foreach ($authCodeScopes as $authCodeScope) {
$session->associateScope($authCodeScope);
}
2013-02-01 16:20:32 +05:30
2014-04-06 23:44:46 +05:30
$response = [
'access_token' => $accessToken->getToken(),
2013-09-07 22:29:44 +05:30
'token_type' => 'Bearer',
2014-04-06 23:44:46 +05:30
'expires' => $accessToken->getExpireTime(),
'expires_in' => $this->server->getAccessTokenTTL()
];
2013-02-01 16:20:32 +05:30
2013-04-29 16:01:07 +05:30
// Associate a refresh token if set
2014-03-10 01:04:23 +05:30
if ($this->server->hasGrantType('refresh_token')) {
2014-04-06 23:44:46 +05:30
$refreshToken = new RefreshToken($this->server);
2014-04-07 01:38:20 +05:30
$refreshToken->setToken(SecureKey::generate());
2014-04-06 23:44:46 +05:30
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$response['refresh_token'] = $refreshToken->getToken();
}
// Expire the auth code
$code->expire();
// Save all the things
$session->save();
$accessToken->setSession($session);
$accessToken->save();
if ($this->server->hasGrantType('refresh_token')) {
$refreshToken->setAccessToken($accessToken);
$refreshToken->save();
2013-02-01 16:20:32 +05:30
}
return $response;
}
2013-09-07 22:29:44 +05:30
}