This commit is contained in:
Alex Bilbie
2016-01-17 12:43:20 +00:00
3 changed files with 176 additions and 168 deletions

View File

@@ -11,13 +11,9 @@
namespace League\OAuth2\Server\Grant;
use DateInterval;
use League\Event\Event;
use League\OAuth2\Server\Entities\AccessTokenEntity;
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use League\OAuth2\Server\Utils\SecureKey;
use Psr\Http\Message\ServerRequestInterface;
/**
@@ -38,78 +34,20 @@ class ClientCredentialsGrant extends AbstractGrant
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
DateInterval $accessTokenTTL,
\DateInterval $tokenTTL,
$scopeDelimiter = ' '
) {
// Get the required params
$clientId = isset($request->getParsedBody()['client_id'])
? $request->getParsedBody()['client_id'] // $_POST['client_id']
: (isset($request->getServerParams()['PHP_AUTH_USER'])
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
: null);
// Validate request
$client = $this->validateClient($request);
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client);
if (is_null($clientId)) {
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
}
$clientSecret = isset($request->getParsedBody()['client_secret'])
? $request->getParsedBody()['client_secret'] // $_POST['client_id']
: (isset($request->getServerParams()['PHP_AUTH_PW'])
? $request->getServerParams()['PHP_AUTH_PW'] // $_SERVER['PHP_AUTH_USER']
: null);
if (is_null($clientSecret)) {
throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing');
}
// Validate client ID and client secret
$client = $this->clientRepository->getClientEntity(
$clientId,
$clientSecret,
null,
$this->getIdentifier()
);
if (($client instanceof ClientEntityInterface) === false) {
$this->emitter->emit(new Event('client.authentication.failed', $request));
throw OAuthServerException::invalidClient();
}
// Validate any scopes that are in the request
$scopeParam = isset($request->getParsedBody()['scope'])
? $request->getParsedBody()['scope'] // $_POST['scope']
: '';
$scopes = $this->validateScopes($scopeParam, $scopeDelimiter, $client);
// Generate an access token
$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier(SecureKey::generate());
$accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL));
$accessToken->setClient($client);
$accessToken->setUserIdentifier($client->getIdentifier());
// Associate scopes with the session and access token
foreach ($scopes as $scope) {
$accessToken->addScope($scope);
}
// Save the token
// Issue and persist access token
$accessToken = $this->issueAccessToken($tokenTTL, $client, $client->getIdentifier(), $scopes);
$this->accessTokenRepository->persistNewAccessToken($accessToken);
// Inject access token into token type
// Inject access token into response type
$responseType->setAccessToken($accessToken);
return $responseType;
}
/**
* @inheritdoc
*/
public function canRespondToRequest(ServerRequestInterface $request)
{
return (
array_key_exists('grant_type', $request->getParsedBody())
&& $request->getParsedBody()['grant_type'] === 'client_credentials'
);
}
}