mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-16 10:18:55 +05:30
First commit of token types
This commit is contained in:
parent
b3c3676381
commit
c5ffd05eee
@ -11,6 +11,8 @@
|
||||
|
||||
namespace League\OAuth2\Server;
|
||||
|
||||
use League\OAuth2\Server\Exception;
|
||||
use League\OAuth2\Server\TokenType\TokenTypeInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
@ -32,6 +34,12 @@ abstract class AbstractServer
|
||||
*/
|
||||
protected $storages = [];
|
||||
|
||||
/**
|
||||
* Token type
|
||||
* @var TokenTypeInterface
|
||||
*/
|
||||
protected $tokenType;
|
||||
|
||||
/**
|
||||
* Sets the Request Object
|
||||
* @param \Symfony\Component\HttpFoundation\Request The Request Object
|
||||
@ -72,4 +80,23 @@ abstract class AbstractServer
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -218,19 +218,16 @@ class AuthCodeGrant extends AbstractGrant
|
||||
$session->associateScope($authCodeScope);
|
||||
}
|
||||
|
||||
$response = [
|
||||
'access_token' => $accessToken->getToken(),
|
||||
'token_type' => 'Bearer',
|
||||
'expires' => $accessToken->getExpireTime(),
|
||||
'expires_in' => $this->server->getAccessTokenTTL()
|
||||
];
|
||||
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
|
||||
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
|
||||
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
|
||||
|
||||
// Associate a refresh token if set
|
||||
if ($this->server->hasGrantType('refresh_token')) {
|
||||
$refreshToken = new RefreshTokenEntity($this->server);
|
||||
$refreshToken->setToken(SecureKey::generate());
|
||||
$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
|
||||
@ -246,6 +243,6 @@ class AuthCodeGrant extends AbstractGrant
|
||||
$refreshToken->save();
|
||||
}
|
||||
|
||||
return $response;
|
||||
return $this->server->getTokenType()->generateResponse();
|
||||
}
|
||||
}
|
||||
|
@ -101,13 +101,10 @@ class ClientCredentialsGrant extends AbstractGrant
|
||||
$accessToken->setSession($session);
|
||||
$accessToken->save($this->server->getStorage('access_token'));
|
||||
|
||||
$response = [
|
||||
'access_token' => $accessToken->getToken(),
|
||||
'token_type' => 'Bearer',
|
||||
'expires' => $accessToken->getExpireTime(),
|
||||
'expires_in' => $this->server->getAccessTokenTTL()
|
||||
];
|
||||
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
|
||||
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
|
||||
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
|
||||
|
||||
return $response;
|
||||
return $this->server->getTokenType()->generateResponse();
|
||||
}
|
||||
}
|
||||
|
@ -137,19 +137,16 @@ class PasswordGrant extends AbstractGrant
|
||||
$session->associateScope($scope);
|
||||
}
|
||||
|
||||
$response = [
|
||||
'access_token' => $accessToken->getToken(),
|
||||
'token_type' => 'Bearer',
|
||||
'expires' => $accessToken->getExpireTime(),
|
||||
'expires_in' => $this->server->getAccessTokenTTL()
|
||||
];
|
||||
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
|
||||
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
|
||||
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
|
||||
|
||||
// Associate a refresh token if set
|
||||
if ($this->server->hasGrantType('refresh_token')) {
|
||||
$refreshToken = new RefreshTokenEntity($this->server);
|
||||
$refreshToken->setToken(SecureKey::generate());
|
||||
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
|
||||
$response['refresh_token'] = $refreshToken->getToken();
|
||||
$this->server->getTokenType()->set('refresh_token', $refreshToken->getToken());
|
||||
}
|
||||
|
||||
// Save everything
|
||||
@ -162,6 +159,6 @@ class PasswordGrant extends AbstractGrant
|
||||
$refreshToken->save();
|
||||
}
|
||||
|
||||
return $response;
|
||||
return $this->server->getTokenType()->generateResponse();
|
||||
}
|
||||
}
|
||||
|
@ -131,12 +131,9 @@ class RefreshTokenGrant extends AbstractGrant
|
||||
$oldAccessToken->expire($this->server->getStorage('access_token'));
|
||||
$newAccessToken->save($this->server->getStorage('access_token'));
|
||||
|
||||
$response = [
|
||||
'access_token' => $newAccessToken->getToken(),
|
||||
'token_type' => 'Bearer',
|
||||
'expires' => $newAccessToken->getExpireTime(),
|
||||
'expires_in' => $this->server->getAccessTokenTTL()
|
||||
];
|
||||
$this->server->getTokenType()->set('access_token', $accessToken->getToken());
|
||||
$this->server->getTokenType()->set('expires', $accessToken->getExpireTime());
|
||||
$this->server->getTokenType()->set('expires_in', $this->server->getAccessTokenTTL());
|
||||
|
||||
// Expire the old refresh token
|
||||
$oldRefreshToken->expire($this->server->getStorage('refresh_token'));
|
||||
@ -148,8 +145,8 @@ class RefreshTokenGrant extends AbstractGrant
|
||||
$newRefreshToken->setAccessToken($newAccessToken);
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
41
src/TokenType/AbstractTokenType.php
Normal file
41
src/TokenType/AbstractTokenType.php
Normal 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
49
src/TokenType/Bearer.php
Normal 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
17
src/TokenType/Mac.php
Normal 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
|
||||
{
|
||||
|
||||
}
|
21
src/TokenType/TokenTypeInterface.php
Normal file
21
src/TokenType/TokenTypeInterface.php
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user