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

namespace Laravel\Passport;

use League\OAuth2\Server\Entities\AuthCodeEntityInterface;

class AuthCodeRepository
{
/**
* Get a auth code by the given ID.
*
* @param int $id
* @return AuthCode|null
*/
public function find($id)
{
return AuthCode::on(Passport::$connectionName)->find($id);
}

/**
* Store a new auth code.
*
* @param array $attributes
* @return AuthCode
*/
public function create(array $attributes)
{
$authCode = (new AuthCode)->on(Passport::$connectionName)
->forceFill($attributes)
->save();

return $authCode;
}

/**
* Revoke an auth code.
*
* @param string $id
* @return bool|int
*/
public function revoke($id)
{
return $this->find($id)->update(['revoked' => true]);
}

/**
* Determine if the given auth code is revoked.
*
* @param int $id
* @return bool
*/
public function revoked($id)
{
return AuthCode::on(Passport::$connectionName)
->where('id', $id)
->where('revoked', true)
->exists();
}
}
22 changes: 10 additions & 12 deletions src/Bridge/AuthCodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Passport\Bridge;

use Illuminate\Database\Connection;
use Laravel\Passport\AuthCodeRepository as AuthCodeModelRepository;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;

Expand All @@ -11,21 +11,21 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
use FormatsScopesForStorage;

/**
* The database connection.
* The auth code model repository.
*
* @var \Illuminate\Database\Connection
* @var \Laravel\Passport\AuthCodeRepository
*/
protected $database;
protected $authCodes;

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

/**
Expand All @@ -41,7 +41,7 @@ public function getNewAuthCode()
*/
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
{
$this->database->table('oauth_auth_codes')->insert([
$this->authCodes->create([
'id' => $authCodeEntity->getIdentifier(),
'user_id' => $authCodeEntity->getUserIdentifier(),
'client_id' => $authCodeEntity->getClient()->getIdentifier(),
Expand All @@ -56,16 +56,14 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
*/
public function revokeAuthCode($codeId)
{
$this->database->table('oauth_auth_codes')
->where('id', $codeId)->update(['revoked' => true]);
$this->authCodes->revoke($codeId);
}

/**
* {@inheritdoc}
*/
public function isAuthCodeRevoked($codeId)
{
return $this->database->table('oauth_auth_codes')
->where('id', $codeId)->where('revoked', 1)->exists();
return $this->authCodes->revoked($codeId);
}
}
22 changes: 10 additions & 12 deletions src/Bridge/RefreshTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace Laravel\Passport\Bridge;

use Illuminate\Database\Connection;
use Laravel\Passport\RefreshTokenRepository as RefreshTokenModelRepository;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;

class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{
/**
* The database connection.
* The refresh token model repository.
*
* @var \Illuminate\Database\Connection
* @var \Laravel\Passport\RefreshTokenRepository
*/
protected $database;
protected $refreshTokens;

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

/**
Expand All @@ -39,7 +39,7 @@ public function getNewRefreshToken()
*/
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
{
$this->database->table('oauth_refresh_tokens')->insert([
$this->refreshTokens->create([
'id' => $refreshTokenEntity->getIdentifier(),
'access_token_id' => $refreshTokenEntity->getAccessToken()->getIdentifier(),
'revoked' => false,
Expand All @@ -52,16 +52,14 @@ public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshToken
*/
public function revokeRefreshToken($tokenId)
{
$this->database->table('oauth_refresh_tokens')
->where('id', $tokenId)->update(['revoked' => true]);
$this->refreshTokens->revoke($tokenId);
}

/**
* {@inheritdoc}
*/
public function isRefreshTokenRevoked($tokenId)
{
return $this->database->table('oauth_refresh_tokens')
->where('id', $tokenId)->where('revoked', 1)->exists();
return $this->refreshTokens->revoked($tokenId);
}
}
20 changes: 13 additions & 7 deletions src/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class ClientRepository
*/
public function find($id)
{
return Client::find($id);
return Client::on(Passport::$connectionName)
->find($id);
}

/**
Expand All @@ -36,8 +37,10 @@ public function findActive($id)
*/
public function forUser($userId)
{
return Client::where('user_id', $userId)
->orderBy('name', 'desc')->get();
return Client::on(Passport::$connectionName)
->where('user_id', $userId)
->orderBy('name', 'desc')
->get();
}

/**
Expand All @@ -61,9 +64,11 @@ public function activeForUser($userId)
public function personalAccessClient()
{
if (Passport::$personalAccessClient) {
return Client::find(Passport::$personalAccessClient);
return Client::on(Passport::$connectionName)
->find(Passport::$personalAccessClient);
} else {
return PersonalAccessClient::orderBy('id', 'desc')->first()->client;
return PersonalAccessClient::on(Passport::$connectionName)
->orderBy('id', 'desc')->first()->client;
}
}

Expand All @@ -79,7 +84,7 @@ public function personalAccessClient()
*/
public function create($userId, $name, $redirect, $personalAccess = false, $password = false)
{
$client = (new Client)->forceFill([
$client = (new Client)->on(Passport::$connectionName)->forceFill([
'user_id' => $userId,
'name' => $name,
'secret' => str_random(40),
Expand Down Expand Up @@ -160,7 +165,8 @@ public function regenerateSecret(Client $client)
*/
public function revoked($id)
{
return Client::where('id', $id)
return Client::on(Passport::$connectionName)
->where('id', $id)
->where('revoked', true)->exists();
}

Expand Down
30 changes: 30 additions & 0 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Passport
*/
public static $refreshTokensExpireAt;

/**
* The connection name used during queries.
*
* @var string
*/
public static $connectionName;

/**
* Get a Passport route registrar.
*
Expand Down Expand Up @@ -190,4 +197,27 @@ public static function refreshTokensExpireIn(DateTimeInterface $date = null)

return new static;
}

/**
* Get the connection name used during queries.
*
* @return null|string
*/
public static function getConnectionName()
{
return static::$connectionName;
}

/**
* Set the connection name used during queries.
*
* @param null|string $connectionName
* @return static
*/
public static function setConnectionName($connectionName)
{
static::$connectionName = $connectionName;

return new static;
}
}
84 changes: 84 additions & 0 deletions src/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Laravel\Passport;

use Illuminate\Database\Eloquent\Model;

class RefreshToken extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'oauth_refresh_tokens';

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The guarded attributes on the model.
*
* @var array
*/
protected $guarded = [];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'revoked' => 'bool',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'expires_at',
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;

/**
* Get the access token that the refresh token belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function accessToken()
{
return $this->belongsTo(Token::class);
}

/**
* Revoke the token instance.
*
* @return void
*/
public function revoke()
{
$this->forceFill(['revoked' => true])->save();
}

/**
* Determine if the token is a transient JWT token.
*
* @return bool
*/
public function transient()
{
return false;
}
}
Loading