Skip to content

Commit

Permalink
feat: add methods to retrieve Authorization and Bearer token from hea…
Browse files Browse the repository at this point in the history
…ders (#416)
  • Loading branch information
SonyPradana authored Feb 8, 2025
1 parent 939bd1f commit ce28557
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/System/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,31 @@ public function json(): Collection
return $this->json;
}

/**
* Get Authorization header.
*/
public function getAuthorization(): ?string
{
return $this->getHeaders('Authorization');
}

/**
* Get Bearer token from Authorization header.
*/
public function getBearerToken(): ?string
{
$authorization = $this->getAuthorization();
if (null === $authorization) {
return null;
}

if (Str::startsWith($authorization, 'Bearer ')) {
return substr($authorization, 7);
}

return null;
}

/**
* Compine all request input.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,24 @@ public function itCanNotGetRequestFormat()

$this->assertNull($request->getRequestFormat());
}

/**
* @test
*/
public function itCanGetHeaderAuthorization(): void
{
$request = new Request('test.test', headers: ['Authorization' => '123']);

$this->assertEquals('123', $request->getAuthorization());
}

/**
* @test
*/
public function itCanGetHeaderBearerAuthorization(): void
{
$request = new Request('test.test', headers: ['Authorization' => 'Bearer 123']);

$this->assertEquals('123', $request->getBearerToken());
}
}

0 comments on commit ce28557

Please sign in to comment.