Skip to content

Commit 0025337

Browse files
author
Laurie Clark
committed
oauth2-server interfaces for Laravel 12
1 parent 2abc787 commit 0025337

File tree

8 files changed

+88
-141
lines changed

8 files changed

+88
-141
lines changed

src/Bridge/AccessTokenRepository.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace MoeenBasra\LaravelPassportMongoDB\Bridge;
44

5-
use DateTime;
6-
use MoeenBasra\LaravelPassportMongoDB\TokenRepository;
75
use Illuminate\Contracts\Events\Dispatcher;
8-
use MoeenBasra\LaravelPassportMongoDB\Events\AccessTokenCreated;
9-
use League\OAuth2\Server\Entities\ClientEntityInterface;
106
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
7+
use League\OAuth2\Server\Entities\ClientEntityInterface;
118
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
9+
use MoeenBasra\LaravelPassportMongoDB\Events\AccessTokenCreated;
10+
use MoeenBasra\LaravelPassportMongoDB\TokenRepository;
1211

1312
class AccessTokenRepository implements AccessTokenRepositoryInterface
1413
{
@@ -17,50 +16,44 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
1716
/**
1817
* The token repository instance.
1918
*
20-
* @var \MoeenBasra\LaravelPassportMongoDB\TokenRepository
19+
* @var TokenRepository
2120
*/
2221
protected $tokenRepository;
2322

2423
/**
2524
* The event dispatcher instance.
2625
*
27-
* @var \Illuminate\Contracts\Events\Dispatcher
26+
* @var Dispatcher
2827
*/
2928
private $events;
3029

3130
/**
3231
* Create a new repository instance.
33-
*
34-
* @param \MoeenBasra\LaravelPassportMongoDB\TokenRepository $tokenRepository
35-
* @param \Illuminate\Contracts\Events\Dispatcher $events
3632
*/
3733
public function __construct(TokenRepository $tokenRepository, Dispatcher $events)
3834
{
3935
$this->events = $events;
4036
$this->tokenRepository = $tokenRepository;
4137
}
4238

43-
/**
44-
* {@inheritdoc}
45-
*/
46-
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
47-
{
48-
return new AccessToken($userIdentifier, $scopes, $clientEntity);
39+
public function getNewToken(
40+
ClientEntityInterface $clientEntity,
41+
array $scopes,
42+
?string $userIdentifier = null
43+
): AccessTokenEntityInterface {
44+
return new AccessToken($userIdentifier, $clientEntity, $scopes);
4945
}
5046

51-
/**
52-
* {@inheritdoc}
53-
*/
54-
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
47+
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void
5548
{
5649
$this->tokenRepository->create([
5750
'_id' => $accessTokenEntity->getIdentifier(),
5851
'user_id' => $accessTokenEntity->getUserIdentifier(),
5952
'client_id' => $accessTokenEntity->getClient()->getIdentifier(),
6053
'scopes' => $this->scopesToArray($accessTokenEntity->getScopes()),
6154
'revoked' => false,
62-
'created_at' => new DateTime,
63-
'updated_at' => new DateTime,
55+
'created_at' => new \DateTime(),
56+
'updated_at' => new \DateTime(),
6457
'expires_at' => $accessTokenEntity->getExpiryDateTime(),
6558
]);
6659

@@ -71,18 +64,12 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
7164
));
7265
}
7366

74-
/**
75-
* {@inheritdoc}
76-
*/
77-
public function revokeAccessToken($tokenId)
67+
public function revokeAccessToken(string $tokenId): void
7868
{
7969
$this->tokenRepository->revokeAccessToken($tokenId);
8070
}
8171

82-
/**
83-
* {@inheritdoc}
84-
*/
85-
public function isAccessTokenRevoked($tokenId)
72+
public function isAccessTokenRevoked(string $tokenId): bool
8673
{
8774
return $this->tokenRepository->isAccessTokenRevoked($tokenId);
8875
}

src/Bridge/AuthCodeRepository.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,26 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
1313
/**
1414
* The database connection.
1515
*
16-
* @var \Illuminate\Database\Connection
16+
* @var Connection
1717
*/
1818
protected $database;
1919

2020
/**
2121
* Create a new repository instance.
2222
*
23-
* @param \Illuminate\Database\Connection $database
2423
* @return void
2524
*/
2625
public function __construct(Connection $database)
2726
{
2827
$this->database = $database;
2928
}
3029

31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getNewAuthCode()
30+
public function getNewAuthCode(): AuthCodeEntityInterface
3531
{
36-
return new AuthCode;
32+
return new AuthCode();
3733
}
3834

39-
/**
40-
* {@inheritdoc}
41-
*/
42-
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
35+
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity): void
4336
{
4437
$this->database->table('oauth_auth_codes')->insert([
4538
'_id' => $authCodeEntity->getIdentifier(),
@@ -51,19 +44,13 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
5144
]);
5245
}
5346

54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function revokeAuthCode($codeId)
47+
public function revokeAuthCode($codeId): void
5848
{
5949
$this->database->table('oauth_auth_codes')
6050
->where('_id', $codeId)->update(['revoked' => true]);
6151
}
6252

63-
/**
64-
* {@inheritdoc}
65-
*/
66-
public function isAuthCodeRevoked($codeId)
53+
public function isAuthCodeRevoked($codeId): bool
6754
{
6855
return $this->database->table('oauth_auth_codes')
6956
->where('_id', $codeId)->where('revoked', 1)->exists();

src/Bridge/ClientRepository.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,35 @@
22

33
namespace MoeenBasra\LaravelPassportMongoDB\Bridge;
44

5-
use MoeenBasra\LaravelPassportMongoDB\ClientRepository as ClientModelRepository;
5+
use League\OAuth2\Server\Entities\ClientEntityInterface;
66
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
7+
use MoeenBasra\LaravelPassportMongoDB\ClientRepository as ClientModelRepository;
78

89
class ClientRepository implements ClientRepositoryInterface
910
{
1011
/**
1112
* The client model repository.
1213
*
13-
* @var \MoeenBasra\LaravelPassportMongoDB\ClientRepository
14+
* @var ClientModelRepository
1415
*/
1516
protected $clients;
1617

1718
/**
1819
* Create a new repository instance.
1920
*
20-
* @param \MoeenBasra\LaravelPassportMongoDB\ClientRepository $clients
2121
* @return void
2222
*/
2323
public function __construct(ClientModelRepository $clients)
2424
{
2525
$this->clients = $clients;
2626
}
2727

28-
/**
29-
* {@inheritdoc}
30-
*/
31-
public function getClientEntity($clientIdentifier)
28+
public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface
3229
{
3330
$record = $this->clients->findActive($clientIdentifier);
3431

35-
if (! $record) {
36-
return;
32+
if (!$record) {
33+
return null;
3734
}
3835

3936
return new Client(
@@ -48,15 +45,13 @@ public function getClientEntity($clientIdentifier)
4845
/**
4946
* Determine if the given client can handle the given grant type.
5047
*
51-
* @param \MoeenBasra\LaravelPassportMongoDB\Client $record
52-
* @param string $grantType
53-
* @return bool
48+
* @param \MoeenBasra\LaravelPassportMongoDB\Client $record
5449
*/
55-
protected function handlesGrant($record, $grantType)
50+
protected function handlesGrant($record, string $grantType): bool
5651
{
5752
switch ($grantType) {
5853
case 'authorization_code':
59-
return ! $record->firstParty();
54+
return !$record->firstParty();
6055
case 'personal_access':
6156
return $record->personal_access_client;
6257
case 'password':
@@ -66,16 +61,21 @@ protected function handlesGrant($record, $grantType)
6661
}
6762
}
6863

69-
public function validateClient($clientIdentifier, $clientSecret, $grantType) {
70-
// First, we will verify that the client exists and is authorized to create personal
71-
// access tokens. Generally personal access tokens are only generated by the user
72-
// from the main interface. We'll only let certain clients generate the tokens.
64+
public function validateClient(
65+
string $clientIdentifier,
66+
?string $clientSecret,
67+
?string $grantType
68+
): bool {
7369
$record = $this->clients->findActive($clientIdentifier);
7470

75-
if (! $record || ! $this->handlesGrant($record, $grantType) || ! hash_equals($record->secret, (string) $clientSecret)) {
71+
if (!$record || !$this->handlesGrant($record, $grantType)) {
72+
return false;
73+
}
74+
75+
if (!$clientSecret) {
7676
return false;
7777
}
7878

79-
return true;
79+
return hash_equals($record->secret, (string) $clientSecret);
8080
}
8181
}

src/Bridge/PersonalAccessGrant.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
namespace MoeenBasra\LaravelPassportMongoDB\Bridge;
44

5-
use DateInterval;
6-
use Psr\Http\Message\ServerRequestInterface;
75
use League\OAuth2\Server\Grant\AbstractGrant;
86
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
98

109
class PersonalAccessGrant extends AbstractGrant
1110
{
12-
/**
13-
* {@inheritdoc}
14-
*/
1511
public function respondToAccessTokenRequest(
1612
ServerRequestInterface $request,
1713
ResponseTypeInterface $responseType,
18-
DateInterval $accessTokenTTL
19-
) {
14+
\DateInterval $accessTokenTTL
15+
): ResponseTypeInterface {
2016
// Validate request
2117
$client = $this->validateClient($request);
2218
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
@@ -36,10 +32,7 @@ public function respondToAccessTokenRequest(
3632
return $responseType;
3733
}
3834

39-
/**
40-
* {@inheritdoc}
41-
*/
42-
public function getIdentifier()
35+
public function getIdentifier(): string
4336
{
4437
return 'personal_access';
4538
}

src/Bridge/RefreshTokenRepository.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,55 @@
22

33
namespace MoeenBasra\LaravelPassportMongoDB\Bridge;
44

5-
use Illuminate\Database\Connection;
65
use Illuminate\Contracts\Events\Dispatcher;
7-
use MoeenBasra\LaravelPassportMongoDB\Events\RefreshTokenCreated;
6+
use Illuminate\Database\Connection;
87
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
98
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
9+
use MoeenBasra\LaravelPassportMongoDB\Events\RefreshTokenCreated;
1010

1111
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
1212
{
1313
/**
1414
* The access token repository instance.
1515
*
16-
* @var \MoeenBasra\LaravelPassportMongoDB\Bridge\AccessTokenRepository
16+
* @var AccessTokenRepository
1717
*/
1818
protected $tokens;
1919

2020
/**
2121
* The database connection.
2222
*
23-
* @var \Illuminate\Database\Connection
23+
* @var Connection
2424
*/
2525
protected $database;
2626

2727
/**
2828
* The event dispatcher instance.
2929
*
30-
* @var \Illuminate\Contracts\Events\Dispatcher
30+
* @var Dispatcher
3131
*/
3232
protected $events;
3333

3434
/**
3535
* Create a new repository instance.
3636
*
37-
* @param \MoeenBasra\LaravelPassportMongoDB\Bridge\AccessTokenRepository $tokens
38-
* @param \Illuminate\Database\Connection $database
39-
* @param \Illuminate\Contracts\Events\Dispatcher $events
4037
* @return void
4138
*/
4239
public function __construct(AccessTokenRepository $tokens,
43-
Connection $database,
44-
Dispatcher $events)
40+
Connection $database,
41+
Dispatcher $events)
4542
{
4643
$this->events = $events;
4744
$this->tokens = $tokens;
4845
$this->database = $database;
4946
}
5047

51-
/**
52-
* {@inheritdoc}
53-
*/
54-
public function getNewRefreshToken()
48+
public function getNewRefreshToken(): RefreshTokenEntityInterface|null
5549
{
56-
return new RefreshToken;
50+
return new RefreshToken();
5751
}
5852

59-
/**
60-
* {@inheritdoc}
61-
*/
62-
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
53+
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity): void
6354
{
6455
$this->database->table('oauth_refresh_tokens')->insert([
6556
'_id' => $id = $refreshTokenEntity->getIdentifier(),
@@ -71,24 +62,18 @@ public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshToken
7162
$this->events->fire(new RefreshTokenCreated($id, $accessTokenId));
7263
}
7364

74-
/**
75-
* {@inheritdoc}
76-
*/
77-
public function revokeRefreshToken($tokenId)
65+
public function revokeRefreshToken($tokenId): void
7866
{
7967
$this->database->table('oauth_refresh_tokens')
8068
->where('_id', $tokenId)->update(['revoked' => true]);
8169
}
8270

83-
/**
84-
* {@inheritdoc}
85-
*/
86-
public function isRefreshTokenRevoked($tokenId)
71+
public function isRefreshTokenRevoked($tokenId): bool
8772
{
8873
$refreshToken = $this->database->table('oauth_refresh_tokens')
8974
->where('_id', $tokenId)->first();
9075

91-
if ($refreshToken === null || $refreshToken->revoked) {
76+
if (null === $refreshToken || $refreshToken->revoked) {
9277
return true;
9378
}
9479

0 commit comments

Comments
 (0)