oauth2-server/src/Grant/ClientCredentialsGrant.php

110 lines
3.2 KiB
PHP
Raw Normal View History

2013-02-01 21:21:51 +05:30
<?php
/**
2013-03-06 23:27:48 +05:30
* OAuth 2.0 Client credentials grant
*
2014-01-08 21:45:29 +05:30
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-10 01:04:37 +05:30
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-10 01:35:38 +05:30
* @link https://github.com/thephpleague/oauth2-server
*/
2013-02-01 21:21:51 +05:30
namespace League\OAuth2\Server\Grant;
2013-02-01 21:21:51 +05:30
2014-05-02 21:51:53 +05:30
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Entity\ClientEntity;
use League\OAuth2\Server\Entity\SessionEntity;
2014-05-01 19:02:54 +05:30
use League\OAuth2\Server\Exception;
use League\OAuth2\Server\Util\SecureKey;
2013-02-01 21:21:51 +05:30
/**
* Client credentials grant class
*/
2014-05-02 21:54:55 +05:30
class ClientCredentialsGrant extends AbstractGrant
2013-12-24 22:31:56 +05:30
{
/**
* Grant identifier
* @var string
*/
2013-02-01 21:21:51 +05:30
protected $identifier = 'client_credentials';
/**
* Response type
* @var string
*/
2013-02-01 21:21:51 +05:30
protected $responseType = null;
/**
* AuthServer instance
* @var AuthServer
*/
2013-12-24 22:31:56 +05:30
protected $server = null;
/**
* Access token expires in override
* @var int
*/
protected $accessTokenTTL = null;
/**
* Complete the client credentials grant
* @param null|array $inputParams
* @return array
*/
2013-12-24 22:31:56 +05:30
public function completeFlow()
2013-02-01 21:21:51 +05:30
{
// Get the required params
2013-12-24 22:31:56 +05:30
$clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) {
2014-05-01 19:02:54 +05:30
throw new Exception\InvalidRequestException('client_id');
2013-02-01 21:21:51 +05:30
}
2013-12-24 22:31:56 +05:30
$clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) {
2014-05-01 19:02:54 +05:30
throw new Exception\InvalidRequestException('client_secret');
2013-02-01 21:21:51 +05:30
}
// Validate client ID and client secret
2014-01-10 18:00:13 +05:30
$client = $this->server->getStorage('client')->get(
2013-12-24 22:31:56 +05:30
$clientId,
$clientSecret,
null,
$this->getIdentifier()
);
2013-02-01 21:21:51 +05:30
2014-05-02 21:51:53 +05:30
if (($client instanceof ClientEntity) === false) {
2014-05-01 19:02:54 +05:30
throw new Exception\InvalidClientException();
2013-02-01 21:21:51 +05:30
}
// Validate any scopes that are in the request
2013-12-24 22:31:56 +05:30
$scopeParam = $this->server->getRequest()->request->get('scope', '');
$scopes = $this->validateScopes($scopeParam);
2013-12-24 22:31:56 +05:30
// Create a new session
2014-05-02 21:51:53 +05:30
$session = new SessionEntity($this->server);
2013-12-24 22:31:56 +05:30
$session->setOwner('client', $client->getId());
$session->associateClient($client);
2013-12-24 22:31:56 +05:30
// Generate an access token
2014-05-02 21:51:53 +05:30
$accessToken = new AccessTokenEntity($this->server);
2014-07-11 22:57:03 +05:30
$accessToken->setId(SecureKey::generate());
2014-01-08 21:45:29 +05:30
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
2013-12-24 22:31:56 +05:30
// Associate scopes with the session and access token
foreach ($scopes as $scope) {
2013-12-24 22:31:56 +05:30
$accessToken->associateScope($scope);
$session->associateScope($scope);
}
2013-12-24 22:31:56 +05:30
// Save everything
2014-01-08 21:45:29 +05:30
$session->save($this->server->getStorage('session'));
2013-12-24 22:31:56 +05:30
$accessToken->setSession($session);
2014-01-08 21:45:29 +05:30
$accessToken->save($this->server->getStorage('access_token'));
2014-07-11 22:57:03 +05:30
$this->server->getTokenType()->set('access_token', $accessToken->getId());
2014-04-23 21:32:50 +05:30
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
return $this->server->getTokenType()->generateResponse();
2013-02-01 21:21:51 +05:30
}
}