Skip to content

Commit 9e9bdaf

Browse files
Update InteractsWithInput.php (#38426)
* Update InteractsWithInput.php Recently I came across some conditions that I needed to send both `Basic` and `Bearer` token. As we can send `Authorization Basic X, Bearer Y` we need to change logic how to get token from the `Authorization` header. * Update InteractsWithInput.php * Update InteractsWithInput.php * formatting * use strpos method Co-authored-by: Taylor Otwell <[email protected]>
1 parent 997e2aa commit 9e9bdaf

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Illuminate/Http/Concerns/InteractsWithInput.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Http\UploadedFile;
66
use Illuminate\Support\Arr;
7-
use Illuminate\Support\Str;
87
use SplFileInfo;
98
use stdClass;
109
use Symfony\Component\VarDumper\VarDumper;
@@ -55,8 +54,12 @@ public function bearerToken()
5554
{
5655
$header = $this->header('Authorization', '');
5756

58-
if (Str::startsWith($header, 'Bearer ')) {
59-
return Str::substr($header, 7);
57+
$position = strrpos($header, 'Bearer');
58+
59+
if ($position !== false) {
60+
$header = substr($header, $position + 7);
61+
62+
return strpos($header, ',') !== false ? strstr(',', $header, true) : $header;
6063
}
6164
}
6265

tests/Http/HttpRequestTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,15 @@ public function testHeaderMethod()
695695
$this->assertSame('foo', $all['do-this'][0]);
696696
}
697697

698+
public function testBearerTokenMethod()
699+
{
700+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer foo']);
701+
$this->assertSame('foo', $request->bearerToken());
702+
703+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => 'Basic foo, Bearer bar']);
704+
$this->assertSame('bar', $request->bearerToken());
705+
}
706+
698707
public function testJSONMethod()
699708
{
700709
$payload = ['name' => 'taylor'];

0 commit comments

Comments
 (0)