Skip to content

Commit 3d77b07

Browse files
committed
Improve passport grants
1 parent 7c62a40 commit 3d77b07

6 files changed

+88
-158
lines changed

src/Lodash/Auth/Passport/Grants/EmulateUserGrant.php

+3-32
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
use DateInterval;
88
use Illuminate\Support\Arr;
99
use Laravel\Passport\Bridge\AccessToken;
10-
use League\OAuth2\Server\Entities\ClientEntityInterface;
1110
use League\OAuth2\Server\Entities\UserEntityInterface;
1211
use League\OAuth2\Server\Exception\OAuthServerException;
13-
use League\OAuth2\Server\Grant\AbstractGrant;
1412
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1513
use League\OAuth2\Server\RequestEvent;
1614
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
@@ -20,17 +18,15 @@
2018
use Psr\Http\Message\ServerRequestInterface;
2119

2220
use function event;
23-
use function is_null;
2421

25-
class EmulateUserGrant extends AbstractGrant
22+
class EmulateUserGrant extends Grant
2623
{
27-
protected readonly AuthServiceContract $authService;
24+
public const string IDENTIFIER = 'emulate';
2825

2926
public function __construct(
30-
AuthServiceContract $authService,
27+
protected readonly AuthServiceContract $authService,
3128
RefreshTokenRepositoryInterface $refreshTokenRepository,
3229
) {
33-
$this->authService = $authService;
3430
$this->setRefreshTokenRepository($refreshTokenRepository);
3531
$this->refreshTokenTTL = new DateInterval('P1M');
3632
}
@@ -79,31 +75,6 @@ public function getRefreshToken(AccessToken $token): void
7975
$this->issueRefreshToken($token);
8076
}
8177

82-
public function getIdentifier(): string
83-
{
84-
return 'emulate';
85-
}
86-
87-
protected function validateClient(ServerRequestInterface $request): ClientEntityInterface
88-
{
89-
[$basicAuthUser,] = $this->getBasicAuthCredentials($request);
90-
91-
$clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser);
92-
if (is_null($clientId)) {
93-
throw OAuthServerException::invalidRequest('client_id');
94-
}
95-
96-
// Get client without validating secret
97-
$client = $this->clientRepository->getClientEntity($clientId);
98-
99-
if (! $client instanceof ClientEntityInterface) {
100-
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
101-
throw OAuthServerException::invalidClient($request);
102-
}
103-
104-
return $client;
105-
}
106-
10778
protected function validateUser(
10879
UserEntityInterface $user,
10980
ServerRequestInterface $request,

src/Lodash/Auth/Passport/Grants/GoogleAccessTokenGrant.php

+4-32
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace Longman\LaravelLodash\Auth\Passport\Grants;
66

77
use DateInterval;
8-
use League\OAuth2\Server\Entities\ClientEntityInterface;
98
use League\OAuth2\Server\Entities\UserEntityInterface;
109
use League\OAuth2\Server\Exception\OAuthServerException;
11-
use League\OAuth2\Server\Grant\AbstractGrant;
1210
use League\OAuth2\Server\RequestEvent;
1311
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
1412
use Longman\LaravelLodash\Auth\Contracts\AuthServiceContract;
@@ -18,15 +16,14 @@
1816

1917
use function is_null;
2018

21-
class GoogleAccessTokenGrant extends AbstractGrant
19+
class GoogleAccessTokenGrant extends Grant
2220
{
23-
protected readonly AuthServiceContract $authService;
21+
public const string IDENTIFIER = 'google_access_token';
2422

2523
public function __construct(
26-
AuthServiceContract $authService,
24+
protected readonly AuthServiceContract $authService,
2725
RefreshTokenBridgeRepositoryContract $refreshTokenRepository,
2826
) {
29-
$this->authService = $authService;
3027
$this->setRefreshTokenRepository($refreshTokenRepository);
3128
$this->refreshTokenTTL = new DateInterval('P1M');
3229
}
@@ -35,7 +32,7 @@ public function respondToAccessTokenRequest(
3532
ServerRequestInterface $request,
3633
ResponseTypeInterface $responseType,
3734
DateInterval $accessTokenTtl,
38-
) {
35+
): ResponseTypeInterface {
3936
// Validate request
4037
$client = $this->validateClient($request);
4138
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
@@ -58,31 +55,6 @@ public function respondToAccessTokenRequest(
5855
return $responseType;
5956
}
6057

61-
public function getIdentifier(): string
62-
{
63-
return 'google_access_token';
64-
}
65-
66-
protected function validateClient(ServerRequestInterface $request): ClientEntityInterface
67-
{
68-
[$basicAuthUser,] = $this->getBasicAuthCredentials($request);
69-
70-
$clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser);
71-
if (is_null($clientId)) {
72-
throw OAuthServerException::invalidRequest('client_id');
73-
}
74-
75-
// Get client without validating secret
76-
$client = $this->clientRepository->getClientEntity($clientId);
77-
78-
if ($client instanceof ClientEntityInterface === false) {
79-
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
80-
throw OAuthServerException::invalidClient($request);
81-
}
82-
83-
return $client;
84-
}
85-
8658
protected function validateUser(ServerRequestInterface $request): UserEntityInterface
8759
{
8860
$googleToken = $this->getRequestParameter('token', $request);

src/Lodash/Auth/Passport/Grants/GoogleIdTokenGrant.php

+4-32
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
namespace Longman\LaravelLodash\Auth\Passport\Grants;
66

77
use DateInterval;
8-
use League\OAuth2\Server\Entities\ClientEntityInterface;
98
use League\OAuth2\Server\Entities\UserEntityInterface;
109
use League\OAuth2\Server\Exception\OAuthServerException;
11-
use League\OAuth2\Server\Grant\AbstractGrant;
1210
use League\OAuth2\Server\RequestEvent;
1311
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
1412
use Longman\LaravelLodash\Auth\Contracts\AuthServiceContract;
@@ -18,15 +16,14 @@
1816

1917
use function is_null;
2018

21-
class GoogleIdTokenGrant extends AbstractGrant
19+
class GoogleIdTokenGrant extends Grant
2220
{
23-
protected readonly AuthServiceContract $authService;
21+
public const string IDENTIFIER = 'google_id_token';
2422

2523
public function __construct(
26-
AuthServiceContract $authService,
24+
protected readonly AuthServiceContract $authService,
2725
RefreshTokenBridgeRepositoryContract $refreshTokenRepository,
2826
) {
29-
$this->authService = $authService;
3027
$this->setRefreshTokenRepository($refreshTokenRepository);
3128
$this->refreshTokenTTL = new DateInterval('P1M');
3229
}
@@ -35,7 +32,7 @@ public function respondToAccessTokenRequest(
3532
ServerRequestInterface $request,
3633
ResponseTypeInterface $responseType,
3734
DateInterval $accessTokenTtl,
38-
) {
35+
): ResponseTypeInterface {
3936
// Validate request
4037
$client = $this->validateClient($request);
4138
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
@@ -58,31 +55,6 @@ public function respondToAccessTokenRequest(
5855
return $responseType;
5956
}
6057

61-
public function getIdentifier(): string
62-
{
63-
return 'google_id_token';
64-
}
65-
66-
protected function validateClient(ServerRequestInterface $request): ClientEntityInterface
67-
{
68-
[$basicAuthUser,] = $this->getBasicAuthCredentials($request);
69-
70-
$clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser);
71-
if (is_null($clientId)) {
72-
throw OAuthServerException::invalidRequest('client_id');
73-
}
74-
75-
// Get client without validating secret
76-
$client = $this->clientRepository->getClientEntity($clientId);
77-
78-
if ($client instanceof ClientEntityInterface === false) {
79-
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
80-
throw OAuthServerException::invalidClient($request);
81-
}
82-
83-
return $client;
84-
}
85-
8658
protected function validateUser(ServerRequestInterface $request): UserEntityInterface
8759
{
8860
$googleToken = $this->getRequestParameter('token', $request);
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Longman\LaravelLodash\Auth\Passport\Grants;
6+
7+
use League\OAuth2\Server\Entities\ClientEntityInterface;
8+
use League\OAuth2\Server\Exception\OAuthServerException;
9+
use League\OAuth2\Server\Grant\AbstractGrant;
10+
use League\OAuth2\Server\RequestEvent;
11+
use Override;
12+
use Psr\Http\Message\ServerRequestInterface;
13+
14+
use function is_null;
15+
16+
abstract class Grant extends AbstractGrant
17+
{
18+
public function getIdentifier(): string
19+
{
20+
return static::IDENTIFIER;
21+
}
22+
23+
#[Override]
24+
protected function validateClient(ServerRequestInterface $request): ClientEntityInterface
25+
{
26+
$clientId = $this->getRequestParameter('client_id', $request);
27+
if (is_null($clientId)) {
28+
throw OAuthServerException::invalidRequest('client_id');
29+
}
30+
31+
// Get client without validating secret
32+
$client = $this->clientRepository->getClientEntity($clientId);
33+
34+
if (! $client instanceof ClientEntityInterface) {
35+
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
36+
throw OAuthServerException::invalidClient($request);
37+
}
38+
39+
return $client;
40+
}
41+
}

src/Lodash/Auth/Passport/Grants/InternalGrant.php

+3-31
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
use DateInterval;
88
use Laravel\Passport\Bridge\AccessToken;
9-
use League\OAuth2\Server\Entities\ClientEntityInterface;
109
use League\OAuth2\Server\Entities\UserEntityInterface;
1110
use League\OAuth2\Server\Exception\OAuthServerException;
12-
use League\OAuth2\Server\Grant\AbstractGrant;
1311
use League\OAuth2\Server\RequestEvent;
1412
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
1513
use Longman\LaravelLodash\Auth\Contracts\AuthServiceContract;
@@ -19,15 +17,14 @@
1917

2018
use function is_null;
2119

22-
class InternalGrant extends AbstractGrant
20+
class InternalGrant extends Grant
2321
{
24-
protected readonly AuthServiceContract $authService;
22+
public const string IDENTIFIER = 'internal';
2523

2624
public function __construct(
27-
AuthServiceContract $authService,
25+
protected readonly AuthServiceContract $authService,
2826
RefreshTokenBridgeRepositoryContract $refreshTokenRepository,
2927
) {
30-
$this->authService = $authService;
3128
$this->setRefreshTokenRepository($refreshTokenRepository);
3229
$this->refreshTokenTTL = new DateInterval('P1M');
3330
}
@@ -64,31 +61,6 @@ public function getRefreshToken(AccessToken $token): void
6461
$this->issueRefreshToken($token);
6562
}
6663

67-
public function getIdentifier(): string
68-
{
69-
return 'internal';
70-
}
71-
72-
protected function validateClient(ServerRequestInterface $request): ClientEntityInterface
73-
{
74-
[$basicAuthUser,] = $this->getBasicAuthCredentials($request);
75-
76-
$clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser);
77-
if (is_null($clientId)) {
78-
throw OAuthServerException::invalidRequest('client_id');
79-
}
80-
81-
// Get client without validating secret
82-
$client = $this->clientRepository->getClientEntity($clientId);
83-
84-
if ($client instanceof ClientEntityInterface === false) {
85-
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
86-
throw OAuthServerException::invalidClient($request);
87-
}
88-
89-
return $client;
90-
}
91-
9264
protected function validateUser(ServerRequestInterface $request): UserEntityInterface
9365
{
9466
$login = $this->getRequestParameter('login', $request);

0 commit comments

Comments
 (0)