diff --git a/src/AuthCodeRepository.php b/src/AuthCodeRepository.php new file mode 100644 index 000000000..73f0ca248 --- /dev/null +++ b/src/AuthCodeRepository.php @@ -0,0 +1,40 @@ +create($attributes); + } + + /** + * Revoke a auth code + * + * @param string $codeId + * @return mixed + */ + public function revokeAuthCode($codeId) + { + return Passport::authCode()->where('id', $codeId)->update(['revoked' => true]); + } + + /** + * Check if the auth code has been revoked. + * + * @param string $codeId + * + * @return bool Return true if this code has been revoked + */ + public function isAuthCodeRevoked($codeId) + { + return Passport::authCode()->where('id', $codeId)->where('revoked', 1)->exists(); + } +} diff --git a/src/Bridge/AuthCodeRepository.php b/src/Bridge/AuthCodeRepository.php index 012ea0c39..7d32aa655 100644 --- a/src/Bridge/AuthCodeRepository.php +++ b/src/Bridge/AuthCodeRepository.php @@ -2,8 +2,7 @@ namespace Laravel\Passport\Bridge; -use Laravel\Passport\Passport; -use Illuminate\Database\Connection; +use Laravel\Passport\AuthCodeRepository as CodeRepository; use League\OAuth2\Server\Entities\AuthCodeEntityInterface; use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; @@ -12,21 +11,21 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface use FormatsScopesForStorage; /** - * The database connection. + * The token repository instance. * - * @var \Illuminate\Database\Connection + * @var \Laravel\Passport\AuthCodeRepository */ - protected $database; + protected $codeRepository; /** * Create a new repository instance. * - * @param \Illuminate\Database\Connection $database + * @param \Laravel\Passport\AuthCodeRepository $codeRepository * @return void */ - public function __construct(Connection $database) + public function __construct(CodeRepository $codeRepository) { - $this->database = $database; + $this->codeRepository = $codeRepository; } /** @@ -42,16 +41,14 @@ public function getNewAuthCode() */ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity) { - $attributes = [ + $this->codeRepository->create([ 'id' => $authCodeEntity->getIdentifier(), 'user_id' => $authCodeEntity->getUserIdentifier(), 'client_id' => $authCodeEntity->getClient()->getIdentifier(), 'scopes' => $this->formatScopesForStorage($authCodeEntity->getScopes()), 'revoked' => false, 'expires_at' => $authCodeEntity->getExpiryDateTime(), - ]; - - Passport::authCode()->setRawAttributes($attributes)->save(); + ]); } /** @@ -59,8 +56,7 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity) */ public function revokeAuthCode($codeId) { - $this->database->table(Passport::authCode()->getTable()) - ->where('id', $codeId)->update(['revoked' => true]); + $this->codeRepository->revokeAuthCode($codeId); } /** @@ -68,7 +64,6 @@ public function revokeAuthCode($codeId) */ public function isAuthCodeRevoked($codeId) { - return $this->database->table(Passport::authCode()->getTable()) - ->where('id', $codeId)->where('revoked', 1)->exists(); + return $this->codeRepository->isAuthCodeRevoked($codeId); } } diff --git a/tests/BridgeAuthCodeRepositoryTest.php b/tests/BridgeAuthCodeRepositoryTest.php new file mode 100644 index 000000000..2530c5737 --- /dev/null +++ b/tests/BridgeAuthCodeRepositoryTest.php @@ -0,0 +1,46 @@ +shouldReceive('create')->once()->andReturnUsing(function ($array) use ($expiration) { + $this->assertEquals(1, $array['id']); + $this->assertEquals(2, $array['user_id']); + $this->assertEquals('client-id', $array['client_id']); + $this->assertEquals(['scopes'], json_decode($array['scopes'], true)); + $this->assertEquals(false, $array['revoked']); + $this->assertEquals($expiration, $array['expires_at']); + }); + + $authCode = new AuthCode(); + $authCode->setIdentifier(1); + $authCode->addScope(new Scope('scopes')); + $authCode->setExpiryDateTime($expiration); + $authCode->setUserIdentifier(2); + $authCode->setClient(new Client('client-id', 'name', 'redirect')); + + $repository = new AuthCodeRepository($codeRepository); + + $repository->persistNewAuthCode($authCode); + } +}