Skip to content

Commit

Permalink
Fix API authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Feb 15, 2024
1 parent b718044 commit d21b32e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
17 changes: 9 additions & 8 deletions src/Core/Security/OAuth2/TokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ public function start(Request $request, AuthenticationException $authException =

public function supports(Request $request): bool
{
$authorization = $request->headers->get('Authorization') ?? null;
if (null === $authorization) {
return false;
}
if (!str_starts_with(strtolower($authorization), 'bearer ')) {
return false;
}

// Every request to the API should be handled by this Authenticator
return true;
}
Expand All @@ -79,6 +71,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
// No response returned here, the request should keep running
// A filter is already present for requests linked to the API in security.yml
return null;
}

Expand All @@ -89,6 +82,14 @@ private function returnWWWAuthenticateResponse(): Response

public function authenticate(Request $request): Passport
{
$authorization = $request->headers->get('Authorization') ?? null;
if (null === $authorization) {
throw new CustomUserMessageAuthenticationException('No Authorization header provided');
}
if (!str_starts_with($authorization, 'Bearer ')) {
throw new CustomUserMessageAuthenticationException('Bearer token missing');
}

$authorization = $request->headers->get('Authorization');
if (null === $authorization) {
throw new CustomUserMessageAuthenticationException('No API token provided');
Expand Down
17 changes: 10 additions & 7 deletions tests/Unit/Core/Security/TokenAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ public function testOnAuthenticationFailure(): void

public function testSupports(): void
{
$this->assertFalse($this->tokenAuthenticator->supports($this->request));

$this->request->headers->add(['Authorization' => 'toto']);
$this->assertFalse($this->tokenAuthenticator->supports($this->request));

$this->request->headers->add(['Authorization' => 'bearer ' . $this->buildTestToken()]);
$this->assertTrue($this->tokenAuthenticator->supports($this->request));
}

Expand All @@ -107,11 +101,20 @@ private function buildTestToken(): string

public function testAuthenticate(): void
{
$this->expectException(CustomUserMessageAuthenticationException::class);
$this->expectExceptionMessage('No Authorization header provided');
$this->tokenAuthenticator->authenticate($this->request);

$this->expectException(CustomUserMessageAuthenticationException::class);
$this->expectExceptionMessage('Bearer token missing');
$this->request->headers->add(['Authorization' => 'toto']);
$this->tokenAuthenticator->authenticate($this->request);

$this->expectException(CustomUserMessageAuthenticationException::class);
$this->expectExceptionMessage('No API token provided');
$this->tokenAuthenticator->authenticate($this->request);

$this->request->headers->add(['Authorization' => 'bearer ' . $this->buildTestToken()]);
$this->request->headers->add(['Authorization' => 'Bearer ' . $this->buildTestToken()]);
$this->expectException(CustomUserMessageAuthenticationException::class);
$this->expectExceptionMessage('Invalid credentials');
$this->tokenAuthenticator->authenticate($this->request);
Expand Down

0 comments on commit d21b32e

Please sign in to comment.