Skip to content

implements trait #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions src/RedisAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RedisAccessToken extends RedisAdapter implements AccessTokenInterface
{
/**
* Get access token from Redis storage.
*
*
* @param string $token
* @return \League\OAuth2\Server\Entity\AccessTokenEntity|null
*/
Expand All @@ -22,56 +22,56 @@ public function get($token)
return null;
}

return (new AccessTokenEntity($this->getServer()))
->setToken($access['id'])
return (new AccessTokenEntity($this->server))
->setId($access['id'])
->setExpireTime($access['expire_time']);
}

/**
* Get access token from Redis storage by an associated refresh token.
*
*
* @param \League\OAuth2\Server\Entity\RefreshTokenEntity $refreshToken
* @return \League\OAuth2\Server\Entity\AccessTokenEntity|null
*/
public function getByRefreshToken(RefreshTokenEntity $refreshToken)
{
if (! $refresh = $this->getValue($refreshToken->getToken(), 'oauth_refresh_tokens')) {
if (! $refresh = $this->getValue($refreshToken->getId(), 'oauth_refresh_tokens')) {
return null;
}

return $this->get($refresh['access_token']);
return $this->get($refresh['access_token_id']);
}

/**
* Get associated access token scopes from Redis storage.
*
* @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token
*
* @param \League\OAuth2\Server\Entity\AccessTokenEntity $token
* @return array
*/
public function getScopes(AbstractTokenEntity $token)
public function getScopes(AccessTokenEntity $token)
{
$scopes = [];

foreach ($this->getSet($token->getToken(), 'oauth_access_token_scopes') as $scope) {
foreach ($this->getSet($token->getId(), 'oauth_access_token_scopes') as $scope) {
if (! $scope = $this->getValue($scope['id'], 'oauth_scopes')) {
continue;
}

$scopes[] = (new ScopeEntity($this->getServer()))
->setId($scope['id'])
->setDescription($scope['description']);
$scopes[] = (new ScopeEntity($this->server))->hydrate([
'id' => $scope['id'],
'description' => $scope['description']
]);
}

return $scopes;
}

/**
* Creates a new access token in Redis storage.
*
*
* @param string $token
* @param int $expireTime
* @param string|int $sessionId
* @return \League\OAuth2\Server\Entity\AccessTokenEntity
*/
public function create($token, $expireTime, $sessionId)
{
Expand All @@ -80,42 +80,38 @@ public function create($token, $expireTime, $sessionId)
'expire_time' => $expireTime,
'session_id' => $sessionId
];

$this->setValue($token, 'oauth_access_tokens', $payload);
$this->pushSet(null, 'oauth_access_tokens', $token);

return (new AccessTokenEntity($this->getServer()))
->setToken($token)
->setExpireTime($expireTime);
}

/**
* Associate a scope with an access token in Redis storage.
*
*
* @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token
* @param \League\OAuth2\Server\Entity\ScopeEntity $scope
* @return void
*/
public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope)
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope)
{
$this->pushSet($token->getToken(), 'oauth_access_token_scopes', ['id' => $scope->getId()]);
$this->pushSet($token->getId(), 'oauth_access_token_scopes', ['id' => $scope->getId()]);
}

/**
* Delete an access token from Redis storage.
*
*
* @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token
* @return void
*/
public function delete(AbstractTokenEntity $token)
public function delete(AccessTokenEntity $token)
{
// Deletes the access token entry.
$this->deleteKey($token->getToken(), 'oauth_access_tokens');
$this->deleteKey($token->getId(), 'oauth_access_tokens');

// Deletes the access token entry from the access tokens set.
$this->deleteSet(null, 'oauth_access_tokens', $token->getToken());
$this->deleteSet(null, 'oauth_access_tokens', $token->getId());

// Deletes the access tokens associated scopes.
$this->deleteKey($token->getToken(), 'oauth_access_token_scopes');
$this->deleteKey($token->getId(), 'oauth_access_token_scopes');
}
}
26 changes: 13 additions & 13 deletions src/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Closure;
use Predis\Client;
use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Storage\AbstractStorage;

class RedisAdapter extends Adapter
class RedisAdapter extends AbstractStorage
{
/**
* Create a new redis adpater instance.
*
*
* @param \Predis\Client $redis
* @return void
*/
Expand All @@ -21,7 +21,7 @@ public function __construct(Client $redis)

/**
* Get a value from the Redis store.
*
*
* @param string $key
* @param string $table
* @return mixed
Expand All @@ -43,7 +43,7 @@ public function getValue($key, $table)

/**
* Set a value in the Redis store.
*
*
* @param string $key
* @param string $table
* @param mixed $value
Expand All @@ -60,7 +60,7 @@ public function setValue($key, $table, $value)

/**
* Push a value onto a set.
*
*
* @param string $key
* @param string $table
* @param mixed $value
Expand All @@ -81,7 +81,7 @@ public function pushSet($key, $table, $value)

/**
* Get a set from the Redis store.
*
*
* @param string $key
* @param string $table
* @return array
Expand Down Expand Up @@ -109,7 +109,7 @@ public function getSet($key, $table)

/**
* Delete a value from a set.
*
*
* @param string $key
* @param string $table
* @param string $value
Expand All @@ -128,7 +128,7 @@ public function deleteSet($key, $table, $value)

/**
* Delete a key from the Redis store.
*
*
* @param string $key
* @param string $table
* @return int
Expand All @@ -148,7 +148,7 @@ public function deleteKey($key, $table)
* Get a matching set member by using a callback to run the
* comparison. If the callback returns a non-null response
* then that response is assumed to be a match.
*
*
* @param string $key
* @param string $table
* @param \Closure $callback
Expand All @@ -165,7 +165,7 @@ public function getMatchingMember($key, $table, Closure $callback)

/**
* Increment the value of a key by one.
*
*
* @param string $table
* @return int
*/
Expand All @@ -178,7 +178,7 @@ public function increment($table)

/**
* Prepare a value for storage in Redis.
*
*
* @param mixed $value
* @return string
*/
Expand All @@ -193,7 +193,7 @@ protected function prepareValue($value)

/**
* Prefix a key with its table.
*
*
* @param string $key
* @param string $table
* @return string
Expand Down
53 changes: 28 additions & 25 deletions src/RedisAuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RedisAuthCode extends RedisAdapter implements AuthCodeInterface
{
/**
* Get an authorization code from Redis storage.
*
*
* @param string $code
* @return \League\OAuth2\Server\Entity\AuthCodeEntity|null
*/
Expand All @@ -20,85 +20,88 @@ public function get($code)
return null;
}

return (new AuthCodeEntity($this->getServer()))
->setToken($code['id'])
if ($code['expire_time'] >= time()) {
return null;
}

return (new AuthCodeEntity($this->server))
->setId($code['id'])
->setRedirectUri($code['client_redirect_uri'])
->setExpireTime($code['expire_time']);

}

/**
* Get associated authorization code scopes from Redis storage.
*
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $code
* @return array
*/
public function getScopes(AuthCodeEntity $code)
{
$scopes = [];

foreach ($this->getSet($code->getToken(), 'oauth_auth_code_scopes') as $scope) {
foreach ($this->getSet($code->getId(), 'oauth_auth_code_scopes') as $scope) {
if (! $scope = $this->getValue($scope['id'], 'oauth_scopes')) {
continue;
}

$scopes[] = (new ScopeEntity($this->getServer()))
->setId($scope['id'])
->setDescription($scope['description']);
$scopes[] = (new ScopeEntity($this->server))->hydrate([
'id' => $scope['id'],
'description' => $scope['description']
]);
}

return $scopes;
}

/**
* Creates a new authorization code in Redis storage.
*
*
* @param string $code
* @param int $expireTime
* @param string|int $sessionId
* @return \League\OAuth2\Server\Entity\AuthCodeEntity
*/
public function create($code, $expireTime, $sessionId)
public function create($code, $expireTime, $sessionId, $redirectUri)
{
$payload = [
'id' => $code,
'expire_time' => $expireTime,
'session_id' => $sessionId
'id' => $code,
'expire_time' => $expireTime,
'session_id' => $sessionId,
'client_redirect_uri' => $redirectUri
];

$this->setValue($code, 'oauth_auth_codes', $payload);
$this->pushSet(null, 'oauth_auth_codes', $code);

return (new AuthCodeEntity($this->getServer()))
->setToken($code)
->setExpireTime($expireTime);
}

/**
* Associate a scope with an authorization code in Redis storage.
*
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $code
* @param \League\OAuth2\Server\Entity\ScopeEntity $scope
* @return void
*/
public function associateScope(AuthCodeEntity $code, ScopeEntity $scope)
{
$this->pushSet($code->getToken(), 'oauth_auth_code_scopes', ['id' => $scope->getId()]);
$this->pushSet($code->getId(), 'oauth_auth_code_scopes', ['id' => $scope->getId()]);
}

/**
* Delete an authorization code from Redis storage.
*
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $code
* @return void
*/
public function delete(AuthCodeEntity $code)
{
// Deletes the authorization code entry.
$this->deleteKey($code->getToken(), 'oauth_auth_codes');
$this->deleteKey($code->getId(), 'oauth_auth_codes');

// Deletes the authorization code entry from the authorization codes set.
$this->deleteSet(null, 'oauth_auth_codes', $code->getToken());
$this->deleteSet(null, 'oauth_auth_codes', $code->getId());

// Deletes the authorization codes associated scopes.
$this->deleteKey($code->getToken(), 'oauth_auth_code_scopes');
$this->deleteKey($code->getId(), 'oauth_auth_code_scopes');
}
}
Loading