oauth2-server/src/Grant/PasswordGrant.php

169 lines
5.2 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
2014-05-02 17:21:53 +01:00
use League\OAuth2\Server\Entity\ClientEntity;
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Entity\RefreshTokenEntity;
use League\OAuth2\Server\Entity\SessionEntity;
2014-05-01 14:32:54 +01:00
use League\OAuth2\Server\Exception;
use League\OAuth2\Server\Util\SecureKey;
2013-02-12 16:22:14 +00:00
/**
* Password grant class
*/
2014-05-02 17:24:55 +01:00
class PasswordGrant extends AbstractGrant
2014-01-08 16:15:29 +00:00
{
/**
* 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
2014-05-03 10:53:57 +01:00
* @param callable $callback The callback function
2013-05-08 14:10:58 -07:00
* @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)) {
2014-05-01 14:32:54 +01:00
throw new Exception\ServerErrorException('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
* @return array
*/
2014-06-23 08:20:34 +01:00
public function completeFlow()
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)) {
$clientId = $this->server->getRequest()->getUser();
if (is_null($clientId)) {
throw new Exception\InvalidRequestException('client_id');
}
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)) {
2014-07-03 15:03:58 +07:00
$clientSecret = $this->server->getRequest()->getPassword();
if (is_null($clientSecret)) {
throw new Exception\InvalidRequestException('client_secret');
}
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-05-02 17:21:53 +01:00
if (($client instanceof ClientEntity) === false) {
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidClientException();
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)) {
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidRequestException('username');
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)) {
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidRequestException('password');
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-05-01 14:32:54 +01:00
throw new Exception\InvalidCredentialsException();
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-05-02 17:21:53 +01:00
$session = new SessionEntity($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-05-02 17:21:53 +01:00
$accessToken = new AccessTokenEntity($this->server);
2014-07-11 18:27:03 +01:00
$accessToken->setId(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);
}
2014-07-11 18:27:03 +01:00
$this->server->getTokenType()->set('access_token', $accessToken->getId());
2014-04-23 17:02:50 +01:00
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
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')) {
2014-05-02 17:21:53 +01:00
$refreshToken = new RefreshTokenEntity($this->server);
2014-07-11 18:27:03 +01:00
$refreshToken->setId(SecureKey::generate());
2014-01-08 16:15:29 +00:00
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
2014-07-11 18:27:03 +01:00
$this->server->getTokenType()->set('refresh_token', $refreshToken->getId());
}
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
}
2014-04-23 17:02:50 +01:00
return $this->server->getTokenType()->generateResponse();
2013-02-12 16:22:14 +00:00
}
2013-09-07 18:00:13 +01:00
}