2016-12-18 04:50:53 +05:30
|
|
|
<?php
|
|
|
|
namespace api\components\OAuth2\Grants;
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
use api\components\OAuth2\Entities\AccessTokenEntity;
|
|
|
|
use api\components\OAuth2\Entities\SessionEntity;
|
2017-10-04 17:12:48 +05:30
|
|
|
use api\components\OAuth2\Utils\Scopes;
|
2017-06-17 23:35:36 +05:30
|
|
|
use League\OAuth2\Server\Entity\ClientEntity as BaseClientEntity;
|
|
|
|
use League\OAuth2\Server\Event;
|
|
|
|
use League\OAuth2\Server\Exception;
|
|
|
|
use League\OAuth2\Server\Grant\AbstractGrant;
|
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
2016-12-18 04:50:53 +05:30
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
class ClientCredentialsGrant extends AbstractGrant {
|
2016-12-18 04:50:53 +05:30
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
protected $identifier = 'client_credentials';
|
2016-12-18 04:50:53 +05:30
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthException
|
|
|
|
*/
|
|
|
|
public function completeFlow(): array {
|
|
|
|
$clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
|
|
|
|
if ($clientId === null) {
|
|
|
|
throw new Exception\InvalidRequestException('client_id');
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$clientSecret = $this->server->getRequest()->request->get('client_secret');
|
2017-06-17 23:35:36 +05:30
|
|
|
if ($clientSecret === null) {
|
|
|
|
throw new Exception\InvalidRequestException('client_secret');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate client ID and client secret
|
|
|
|
$client = $this->server->getClientStorage()->get($clientId, $clientSecret, null, $this->getIdentifier());
|
|
|
|
if (!$client instanceof BaseClientEntity) {
|
|
|
|
$this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
|
|
|
|
throw new Exception\InvalidClientException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate any scopes that are in the request
|
|
|
|
$scopeParam = $this->server->getRequest()->request->get('scope', '');
|
|
|
|
$scopes = $this->validateScopes($scopeParam, $client);
|
|
|
|
|
|
|
|
// Create a new session
|
|
|
|
$session = new SessionEntity($this->server);
|
|
|
|
$session->setOwner('client', $client->getId());
|
|
|
|
$session->associateClient($client);
|
|
|
|
|
|
|
|
// Generate an access token
|
|
|
|
$accessToken = new AccessTokenEntity($this->server);
|
|
|
|
$accessToken->setId(SecureKey::generate());
|
|
|
|
$accessToken->setExpireTime($this->getAccessTokenTTL() + time());
|
|
|
|
|
|
|
|
// Associate scopes with the session and access token
|
|
|
|
foreach ($scopes as $scope) {
|
|
|
|
$session->associateScope($scope);
|
|
|
|
$accessToken->associateScope($scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save everything
|
|
|
|
$session->save();
|
|
|
|
$accessToken->setSession($session);
|
|
|
|
$accessToken->save();
|
|
|
|
|
|
|
|
$this->server->getTokenType()->setSession($session);
|
|
|
|
$this->server->getTokenType()->setParam('access_token', $accessToken->getId());
|
|
|
|
$this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
|
2016-12-18 04:50:53 +05:30
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
return $this->server->getTokenType()->generateResponse();
|
2016-12-18 04:50:53 +05:30
|
|
|
}
|
|
|
|
|
2017-07-13 16:14:06 +05:30
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* In the earlier versions of Accounts Ely.by backend we had a comma-separated scopes
|
|
|
|
* list, while by OAuth2 standard it they should be separated by a space. Shit happens :)
|
|
|
|
* So override scopes validation function to reformat passed value.
|
2017-07-13 16:14:06 +05:30
|
|
|
*
|
|
|
|
* @param string $scopeParam
|
2017-09-19 22:36:16 +05:30
|
|
|
* @param BaseClientEntity $client
|
2017-07-13 16:14:06 +05:30
|
|
|
* @param string $redirectUri
|
|
|
|
*
|
|
|
|
* @return \League\OAuth2\Server\Entity\ScopeEntity[]
|
|
|
|
*/
|
2017-09-19 22:36:16 +05:30
|
|
|
public function validateScopes($scopeParam = '', BaseClientEntity $client, $redirectUri = null) {
|
2017-10-04 17:12:48 +05:30
|
|
|
return parent::validateScopes(Scopes::format($scopeParam), $client, $redirectUri);
|
2017-07-13 16:14:06 +05:30
|
|
|
}
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
}
|