Skip to content
Merged
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
16 changes: 7 additions & 9 deletions src/Bridge/AccessTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Laravel\Passport\Bridge;

use DateTime;
use Illuminate\Database\Connection;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
Expand All @@ -17,17 +17,17 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
*
* @var \Illuminate\Database\Connection
*/
protected $database;
protected $tokenRepository;

/**
* Create a new repository instance.
*
* @param \Illuminate\Database\Connection $database
* @return void
*/
public function __construct(Connection $database)
public function __construct(TokenRepository $tokenRepository)
{
$this->database = $database;
$this->tokenRepository = $tokenRepository;
}

/**
Expand All @@ -43,7 +43,7 @@ public function getNewToken(ClientEntityInterface $clientEntity, array $scopes,
*/
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
{
$this->database->table('oauth_access_tokens')->insert([
$this->tokenRepository->create([
'id' => $accessTokenEntity->getIdentifier(),
'user_id' => $accessTokenEntity->getUserIdentifier(),
'client_id' => $accessTokenEntity->getClient()->getIdentifier(),
Expand All @@ -60,16 +60,14 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
*/
public function revokeAccessToken($tokenId)
{
$this->database->table('oauth_access_tokens')
->where('id', $tokenId)->update(['revoked' => true]);
$this->tokenRepository->revokeAccessToken($tokenId);
}

/**
* {@inheritdoc}
*/
public function isAccessTokenRevoked($tokenId)
{
return $this->database->table('oauth_access_tokens')
->where('id', $tokenId)->where('revoked', 1)->exists();
return $this->tokenRepository->isAccessTokenRevoked($tokenId);
}
}
35 changes: 34 additions & 1 deletion src/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

class TokenRepository
{
/**
* Creates a new Access Token
*
* @param array $attributes
* @return Token
*/
public function create($attributes)
{
return Token::create($attributes);
}

/**
* Get a token by the given ID.
*
Expand All @@ -21,11 +32,33 @@ public function find($id)
* @param Token $token
* @return void
*/
public function save($token)
public function save(Token $token)
{
$token->save();
}

/**
* Revoke an access token.
*
* @param string $id
*/
public function revokeAccessToken($id)
{
return $this->find($id)->update(['revoked' => true]);
}

/**
* Check if the access token has been revoked.
*
* @param string $id
*
* @return bool Return true if this token has been revoked
*/
public function isAccessTokenRevoked($id)
{
return Token::where('id', $id)->where('revoked', 1)->exists();
}

/**
* Revoke all of the access tokens for a given user and client.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/BridgeAccessTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public function test_access_tokens_can_be_persisted()
{
$expiration = Carbon::now();

$database = Mockery::mock('Illuminate\Database\Connection');
$tokenRepository = Mockery::mock('Laravel\Passport\TokenRepository');

$database->shouldReceive('table->insert')->once()->andReturnUsing(function ($array) use ($expiration) {
$tokenRepository->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']);
Expand All @@ -31,7 +31,7 @@ public function test_access_tokens_can_be_persisted()
$accessToken->setExpiryDateTime($expiration);
$accessToken->setClient(new Laravel\Passport\Bridge\Client('client-id', 'name', 'redirect'));

$repository = new Laravel\Passport\Bridge\AccessTokenRepository($database);
$repository = new Laravel\Passport\Bridge\AccessTokenRepository($tokenRepository);

$repository->persistNewAccessToken($accessToken);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PersonalAccessTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PersonalAccessTokenFactoryTestClientStub
public $secret = 'something';
}

class PersonalAccessTokenFactoryTestModelStub extends Illuminate\Database\Eloquent\Model
class PersonalAccessTokenFactoryTestModelStub extends Laravel\Passport\Token
{
public $id = 1;
public $secret = 'something';
Expand Down