oauth2-server/src/Grant/Password.php

187 lines
5.8 KiB
PHP
Raw Normal View History

2013-02-12 16:22:14 +00:00
<?php
/**
* OAuth 2.0 Password grant
*
2014-01-08 16:15:29 +00:00
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
2013-02-12 16:22:14 +00:00
namespace League\OAuth2\Server\Grant;
2013-05-08 11:06:09 -07:00
use League\OAuth2\Server\AuthorizationServer;
2014-01-16 16:50:16 +00:00
use League\OAuth2\Server\Entity\AccessToken;
use League\OAuth2\Server\Entity\Client;
use League\OAuth2\Server\Entity\RefreshToken as RT;
2014-01-16 16:50:16 +00:00
use League\OAuth2\Server\Entity\Session;
use League\OAuth2\Server\Entity\Scope;
2013-12-24 17:01:56 +00:00
use League\OAuth2\Server\Exception\ClientException;
use League\OAuth2\Server\Exception\InvalidGrantTypeException;
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-12 16:22:14 +00:00
/**
* Password grant class
*/
2014-01-08 16:15:29 +00:00
class Password extends AbstractGrant
{
/**
* Grant identifier
* @var string
*/
2013-02-12 16:22:14 +00:00
protected $identifier = 'password';
/**
* Response type
* @var string
*/
2014-01-10 12:30:13 +00:00
protected $responseType;
/**
* Callback to authenticate a user's name and password
* @var function
*/
2014-01-10 12:30:13 +00:00
protected $callback;
/**
* Access token expires in override
* @var int
*/
2014-01-10 12:30:13 +00:00
protected $accessTokenTTL;
/**
* Set the callback to verify a user's username and password
2013-05-08 14:10:58 -07:00
* @param callable $callback The callback function
* @return void
*/
2013-12-24 17:01:56 +00:00
public function setVerifyCredentialsCallback(callable $callback)
2013-02-12 16:22:14 +00:00
{
$this->callback = $callback;
}
/**
* Return the callback function
2013-05-08 14:10:58 -07:00
* @return callable
*/
2013-02-12 16:45:33 +00:00
protected function getVerifyCredentialsCallback()
2013-02-12 16:22:14 +00:00
{
if (is_null($this->callback) || ! is_callable($this->callback)) {
2013-12-24 17:01:56 +00:00
throw new InvalidGrantTypeException('Null or non-callable callback set on Password grant');
2013-02-12 16:22:14 +00:00
}
2013-02-12 16:45:33 +00:00
return $this->callback;
2013-02-12 16:22:14 +00:00
}
/**
* Complete the password grant
* @param null|array $inputParams
* @return array
*/
public function completeFlow($inputParams = null)
2013-02-12 16:22:14 +00:00
{
// Get the required params
2013-12-24 17:01:56 +00:00
$clientId = $this->server->getRequest()->request->get('client_id', null);
if (is_null($clientId)) {
throw new ClientException(
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_id'),
2013-12-24 17:01:56 +00:00
0
);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
$clientSecret = $this->server->getRequest()->request->get('client_secret', null);
if (is_null($clientSecret)) {
throw new ClientException(
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'client_secret'),
2013-12-24 17:01:56 +00:00
0
);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
// Validate client ID and client secret
2014-01-10 12:30:13 +00:00
$client = $this->server->getStorage('client')->get(
2013-12-24 17:01:56 +00:00
$clientId,
$clientSecret,
null,
$this->getIdentifier()
);
2013-02-12 16:22:14 +00:00
2014-01-08 16:15:29 +00:00
if (($client instanceof Client) === false) {
throw new ClientException(AuthorizationServer::getExceptionMessage('invalid_client'), 8);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
$username = $this->server->getRequest()->request->get('username', null);
if (is_null($username)) {
throw new ClientException(
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'username'),
2013-12-24 17:01:56 +00:00
0
);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
$password = $this->server->getRequest()->request->get('password', null);
if (is_null($password)) {
throw new ClientException(
sprintf(AuthorizationServer::getExceptionMessage('invalid_request'), 'password'),
2013-12-24 17:01:56 +00:00
0
);
2013-02-12 16:22:14 +00:00
}
// Check if user's username and password are correct
2013-12-24 17:01:56 +00:00
$userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password);
2013-02-12 16:22:14 +00:00
if ($userId === false) {
2014-01-08 16:15:29 +00:00
throw new ClientException($this->server->getExceptionMessage('invalid_credentials'), 0);
2013-02-12 16:22:14 +00:00
}
// Validate any scopes that are in the request
2013-12-24 17:01:56 +00:00
$scopeParam = $this->server->getRequest()->request->get('scope', '');
$scopes = $this->validateScopes($scopeParam);
2013-12-24 17:01:56 +00:00
// Create a new session
2014-01-08 16:15:29 +00:00
$session = new Session($this->server);
2013-12-24 17:01:56 +00:00
$session->setOwner('user', $userId);
$session->associateClient($client);
2013-03-22 11:41:04 +00:00
2013-12-24 17:01:56 +00:00
// Generate an access token
2014-01-08 16:15:29 +00:00
$accessToken = new AccessToken($this->server);
2014-04-06 21:08:20 +01:00
$accessToken->setToken(SecureKey::generate());
2014-01-08 16:15:29 +00:00
$accessToken->setExpireTime($this->server->getAccessTokenTTL() + time());
2013-12-24 17:01:56 +00:00
// Associate scopes with the session and access token
foreach ($scopes as $scope) {
2013-12-24 17:01:56 +00:00
$accessToken->associateScope($scope);
$session->associateScope($scope);
}
2013-12-24 17:01:56 +00:00
$response = [
2014-01-08 16:15:29 +00:00
'access_token' => $accessToken->getToken(),
2013-12-24 17:01:56 +00:00
'token_type' => 'Bearer',
'expires' => $accessToken->getExpireTime(),
2014-01-08 16:15:29 +00:00
'expires_in' => $this->server->getAccessTokenTTL()
2013-12-24 17:01:56 +00:00
];
2013-02-12 16:22:14 +00:00
2013-12-24 17:01:56 +00:00
// Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) {
$refreshToken = new RT($this->server);
2014-04-06 21:08:20 +01:00
$refreshToken->setToken(SecureKey::generate());
2014-01-08 16:15:29 +00:00
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$response['refresh_token'] = $refreshToken->getToken();
}
2013-12-24 17:01:56 +00:00
// Save everything
2014-01-10 12:30:13 +00:00
$session->save();
2013-12-24 17:01:56 +00:00
$accessToken->setSession($session);
2014-01-10 12:30:13 +00:00
$accessToken->save();
2013-02-12 16:22:14 +00:00
2013-12-24 17:01:56 +00:00
if ($this->server->hasGrantType('refresh_token')) {
$refreshToken->setAccessToken($accessToken);
2014-01-10 12:30:13 +00:00
$refreshToken->save();
2013-02-12 16:22:14 +00:00
}
return $response;
}
2013-09-07 18:00:13 +01:00
}