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
32 changes: 31 additions & 1 deletion src/AuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,43 @@ class AuthCode extends Model
'expires_at',
];

/**
* The client relation model.
*
* @var string
*/
protected static $clientModel = Client::class;

/**
* Get the client that owns the authentication code.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function client()
{
return $this->hasMany(Client::class);
return $this->hasMany(static::$clientModel);
}

/**
* Get the client model.
*
* @return mixed
*/
public static function getClientModel()
{
return static::$clientModel;
}

/**
* Set the client model.
*
* @param mixed $clientModel
* @return static
*/
public static function setClientModel($clientModel)
{
static::$clientModel = $clientModel;

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

namespace Laravel\Passport;

use League\OAuth2\Server\Entities\AuthCodeEntityInterface;

class AuthCodeRepository
{
use RepositoryHelper;

/**
* The auth code model.
*
* @var string
*/
protected static $model = AuthCode::class;

/**
* Get a auth code by the given ID.
*
* @param int $id
* @return AuthCode|null
*/
public function find($id)
{
return $this->createModel()->find($id);
}

/**
* Store a new auth code.
*
* @param array $attributes
* @return AuthCode
*/
public function create(array $attributes)
{
$authCode = $this->createModel()->forceFill($attributes);

$authCode->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 $this->createModel()
->where('id', $id)
->where('revoked', true)
->exists();
}

/**
* {@inheritdoc}
*/
public static function getModel()
{
return static::$model;
}

/**
* {@inheritdoc}
*/
public static function setModel($model)
{
static::$model = $model;

return new static;
}
}
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);
}
}
64 changes: 62 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ class Client extends Model
'revoked' => 'bool',
];

/**
* The auth code relation model.
*
* @var string
*/
protected static $authCodeModel = AuthCode::class;

/**
* The access token relation model.
*
* @var string
*/
protected static $tokenModel = Token::class;

/**
* Get all of the authentication codes for the client.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function authCodes()
{
return $this->hasMany(AuthCode::class);
return $this->hasMany(static::$authCodeModel);
}

/**
Expand All @@ -57,7 +71,7 @@ public function authCodes()
*/
public function tokens()
{
return $this->hasMany(Token::class);
return $this->hasMany(static::$tokenModel);
}

/**
Expand All @@ -69,4 +83,50 @@ public function firstParty()
{
return $this->personal_access_client || $this->password_client;
}

/**
* Get the auth code model.
*
* @return mixed
*/
public static function getAuthCodeModel()
{
return static::$authCodeModel;
}

/**
* Set the auth code model.
*
* @param mixed $authCodeModel
* @return static
*/
public static function setAuthCodeModel($authCodeModel)
{
static::$authCodeModel = $authCodeModel;

return new static;
}

/**
* Get the token model.
*
* @return mixed
*/
public static function getTokenModel()
{
return static::$tokenModel;
}

/**
* Set the token model.
*
* @param mixed $tokenModel
* @return static
*/
public static function setTokenModel($tokenModel)
{
static::$tokenModel = $tokenModel;

return new static;
}
}
Loading