Skip to content
Closed
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
40 changes: 40 additions & 0 deletions src/AuthCodeRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Laravel\Passport;

class AuthCodeRepository
{
/**
* Creates a new auth code
*
* @param array $attributes
* @return \Laravel\Passport\AuthCode
*/
public function create($attributes)
{
return Passport::authCode()->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();
}
}
27 changes: 11 additions & 16 deletions src/Bridge/AuthCodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -42,33 +41,29 @@ 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();
]);
}

/**
* {@inheritdoc}
*/
public function revokeAuthCode($codeId)
{
$this->database->table(Passport::authCode()->getTable())
->where('id', $codeId)->update(['revoked' => true]);
$this->codeRepository->revokeAuthCode($codeId);
}

/**
* {@inheritdoc}
*/
public function isAuthCodeRevoked($codeId)
{
return $this->database->table(Passport::authCode()->getTable())
->where('id', $codeId)->where('revoked', 1)->exists();
return $this->codeRepository->isAuthCodeRevoked($codeId);
}
}
46 changes: 46 additions & 0 deletions tests/BridgeAuthCodeRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Laravel\Passport\Tests;

use Mockery as m;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Bridge\Scope;
use Laravel\Passport\Bridge\Client;
use Laravel\Passport\Bridge\AuthCode;
use Laravel\Passport\Bridge\AuthCodeRepository;

class BridgeAuthCodeRepositoryTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function test_auth_codes_can_be_persisted()
{
$expiration = Carbon::now();

$codeRepository = m::mock('Laravel\Passport\AuthCodeRepository');

$codeRepository->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);
}
}