|
12 | 12 | use Phenix\Auth\AuthenticationManager; |
13 | 13 | use Phenix\Auth\Events\FailedTokenValidation; |
14 | 14 | use Phenix\Auth\Events\TokenValidated; |
| 15 | +use Phenix\Auth\Middlewares\Concerns\InteractsWithBearerTokens; |
15 | 16 | use Phenix\Auth\User; |
16 | 17 | use Phenix\Facades\Config; |
17 | 18 | use Phenix\Facades\Event; |
|
21 | 22 |
|
22 | 23 | class Authenticated implements Middleware |
23 | 24 | { |
| 25 | + use InteractsWithBearerTokens; |
| 26 | + |
24 | 27 | public function handleRequest(Request $request, RequestHandler $next): Response |
25 | 28 | { |
26 | 29 | $authorizationHeader = $request->getHeader('Authorization'); |
27 | 30 |
|
28 | | - if (! $this->hasToken($authorizationHeader)) { |
| 31 | + if (! $this->hasBearerScheme($authorizationHeader)) { |
29 | 32 | return $this->unauthorized(); |
30 | 33 | } |
31 | 34 |
|
32 | | - $token = $this->extractToken($authorizationHeader); |
| 35 | + $token = $this->extractBearerToken($authorizationHeader); |
33 | 36 |
|
34 | 37 | /** @var AuthenticationManager $auth */ |
35 | 38 | $auth = App::make(AuthenticationManager::class); |
@@ -63,20 +66,6 @@ public function handleRequest(Request $request, RequestHandler $next): Response |
63 | 66 | return $next->handleRequest($request); |
64 | 67 | } |
65 | 68 |
|
66 | | - protected function hasToken(string|null $token): bool |
67 | | - { |
68 | | - return $token !== null |
69 | | - && trim($token) !== '' |
70 | | - && str_starts_with($token, 'Bearer '); |
71 | | - } |
72 | | - |
73 | | - protected function extractToken(string $authorizationHeader): string|null |
74 | | - { |
75 | | - $parts = explode(' ', $authorizationHeader, 2); |
76 | | - |
77 | | - return isset($parts[1]) ? trim($parts[1]) : null; |
78 | | - } |
79 | | - |
80 | 69 | protected function unauthorized(): Response |
81 | 70 | { |
82 | 71 | return response()->json([ |
|
0 commit comments