First commit of token types

This commit is contained in:
Alex Bilbie 2014-04-23 17:02:50 +01:00
parent b3c3676381
commit c5ffd05eee
9 changed files with 174 additions and 31 deletions

View File

@ -11,6 +11,8 @@
namespace League\OAuth2\Server; namespace League\OAuth2\Server;
use League\OAuth2\Server\Exception;
use League\OAuth2\Server\TokenType\TokenTypeInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
@ -32,6 +34,12 @@ abstract class AbstractServer
*/ */
protected $storages = []; protected $storages = [];
/**
* Token type
* @var TokenTypeInterface
*/
protected $tokenType;
/** /**
* Sets the Request Object * Sets the Request Object
* @param \Symfony\Component\HttpFoundation\Request The Request Object * @param \Symfony\Component\HttpFoundation\Request The Request Object
@ -72,4 +80,23 @@ abstract class AbstractServer
return $this->storages[$obj]; return $this->storages[$obj];
} }
/**
* Set the access token type
* @param TokenTypeInterface $tokenType The token type
* @return void
*/
public function setTokenType(TokenTypeInterface $tokenType)
{
$this->tokenType = $tokenType;
}
/**
* Get the access token type
* @return TokenTypeInterface
*/
public function getTokenType()
{
return $this->tokenType;
}
} }

View File

@ -218,19 +218,16 @@ class AuthCodeGrant extends AbstractGrant
$session->associateScope($authCodeScope); $session->associateScope($authCodeScope);
} }
$response = [ $this->server->getTokenType()->set('access_token', $accessToken->getToken());
'access_token' => $accessToken->getToken(), $this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
'token_type' => 'Bearer', $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
'expires' => $accessToken->getExpireTime(),
'expires_in' => $this->server->getAccessTokenTTL()
];
// Associate a refresh token if set // Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) { if ($this->server->hasGrantType('refresh_token')) {
$refreshToken = new RefreshTokenEntity($this->server); $refreshToken = new RefreshTokenEntity($this->server);
$refreshToken->setToken(SecureKey::generate()); $refreshToken->setToken(SecureKey::generate());
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time()); $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$response['refresh_token'] = $refreshToken->getToken(); $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
} }
// Expire the auth code // Expire the auth code
@ -246,6 +243,6 @@ class AuthCodeGrant extends AbstractGrant
$refreshToken->save(); $refreshToken->save();
} }
return $response; return $this->server->getTokenType()->generateResponse();
} }
} }

View File

@ -101,13 +101,10 @@ class ClientCredentialsGrant extends AbstractGrant
$accessToken->setSession($session); $accessToken->setSession($session);
$accessToken->save($this->server->getStorage('access_token')); $accessToken->save($this->server->getStorage('access_token'));
$response = [ $this->server->getTokenType()->set('access_token', $accessToken->getToken());
'access_token' => $accessToken->getToken(), $this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
'token_type' => 'Bearer', $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
'expires' => $accessToken->getExpireTime(),
'expires_in' => $this->server->getAccessTokenTTL()
];
return $response; return $this->server->getTokenType()->generateResponse();
} }
} }

View File

@ -137,19 +137,16 @@ class PasswordGrant extends AbstractGrant
$session->associateScope($scope); $session->associateScope($scope);
} }
$response = [ $this->server->getTokenType()->set('access_token', $accessToken->getToken());
'access_token' => $accessToken->getToken(), $this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
'token_type' => 'Bearer', $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
'expires' => $accessToken->getExpireTime(),
'expires_in' => $this->server->getAccessTokenTTL()
];
// Associate a refresh token if set // Associate a refresh token if set
if ($this->server->hasGrantType('refresh_token')) { if ($this->server->hasGrantType('refresh_token')) {
$refreshToken = new RefreshTokenEntity($this->server); $refreshToken = new RefreshTokenEntity($this->server);
$refreshToken->setToken(SecureKey::generate()); $refreshToken->setToken(SecureKey::generate());
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time()); $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
$response['refresh_token'] = $refreshToken->getToken(); $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
} }
// Save everything // Save everything
@ -162,6 +159,6 @@ class PasswordGrant extends AbstractGrant
$refreshToken->save(); $refreshToken->save();
} }
return $response; return $this->server->getTokenType()->generateResponse();
} }
} }

View File

@ -131,12 +131,9 @@ class RefreshTokenGrant extends AbstractGrant
$oldAccessToken->expire($this->server->getStorage('access_token')); $oldAccessToken->expire($this->server->getStorage('access_token'));
$newAccessToken->save($this->server->getStorage('access_token')); $newAccessToken->save($this->server->getStorage('access_token'));
$response = [ $this->server->getTokenType()->set('access_token', $accessToken->getToken());
'access_token' => $newAccessToken->getToken(), $this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
'token_type' => 'Bearer', $this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
'expires' => $newAccessToken->getExpireTime(),
'expires_in' => $this->server->getAccessTokenTTL()
];
// Expire the old refresh token // Expire the old refresh token
$oldRefreshToken->expire($this->server->getStorage('refresh_token')); $oldRefreshToken->expire($this->server->getStorage('refresh_token'));
@ -148,8 +145,8 @@ class RefreshTokenGrant extends AbstractGrant
$newRefreshToken->setAccessToken($newAccessToken); $newRefreshToken->setAccessToken($newAccessToken);
$newRefreshToken->save($this->server->getStorage('refresh_token')); $newRefreshToken->save($this->server->getStorage('refresh_token'));
$response['refresh_token'] = $newRefreshToken->getToken(); $this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
return $response; return $this->server->getTokenType()->generateResponse();
} }
} }

View File

@ -0,0 +1,41 @@
<?php
/**
* OAuth 2.0 Abstract Token Type
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\TokenType;
abstract class AbstractBearer
{
/**
* Response array
* @var array
*/
protected $response = [];
/**
* Set a key/value response pair
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->responsekey[$key] = $value;
}
/**
* Get a key from the response array
* @param string $key
* @return mixed
*/
private function get($key)
{
return isset($this->response[$key]) ? $this->response[$key] : null;
}
}

49
src/TokenType/Bearer.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/**
* OAuth 2.0 Bearer Token Type
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\TokenType;
class Bearer extends AbstractBearer implements TokenTypeInterface
{
protected $response = [];
/**
* {@inheritdoc}
*/
public function set($key, $value)
{
$this->responsekey[$key] = $value;
}
private function get($key)
{
return isset($this->response[$key]) ? $this->response[$key] : null;
}
/**
* {@inheritdoc}
*/
public function generateResponse()
{
$return = [
'access_token' => $this->get('refresh_token'),
'token_type' => 'Bearer',
'expires' => $this->get('expires'),
'expires_in' => $this->get('expires_in')
];
if (!is_null($this->get('refresh_token'))) {
$return['refresh_token'] = $this->get('refresh_token');
}
return $return;
}
}

17
src/TokenType/Mac.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* OAuth 2.0 MAC Token Type
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\TokenType;
class Mac extends AbstractBearer implements TokenTypeInterface
{
}

View File

@ -0,0 +1,21 @@
<?php
/**
* OAuth 2.0 Token Type Interface
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\TokenType;
interface TokenTypeInterface
{
/**
* Generate a response
* @return array
*/
public function generateResponse();
}