Skip to content

Commit 286e5af

Browse files
committed
Fix ability to use the api_token query parameter for authenticated API requests (#1034)
1 parent 004eec2 commit 286e5af

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

app/Http/Middleware/Authenticate.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class Authenticate extends IlluminateAuthenticate
99
{
1010
public function handle($request, Closure $next, ...$guards)
1111
{
12+
if ($request->has('api_token') && !$request->headers->has('Authorization')) {
13+
$request->headers->set('Authorization', 'Bearer ' . $request->api_token);
14+
}
15+
1216
$this->authenticate($request, $guards);
1317

1418
if (!$request->is('api/*') && $request->user()->isSystemUser()) {

tests/Controller/App/FeedControllerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ public function test_links_feed(): void
4444
$response->assertOk()->assertSee($link->url)->assertDontSee($otherLink->url);
4545
}
4646

47+
public function test_links_feed_with_auth_as_query_param(): void
48+
{
49+
$link = Link::factory()->create();
50+
51+
$otherUser = User::factory()->create();
52+
$otherLink = Link::factory()->for($otherUser)->create(['visibility' => ModelAttribute::VISIBILITY_PRIVATE]);
53+
54+
$token = $this->user->createToken('test', [ApiToken::ABILITY_USER_ACCESS])->plainTextToken;
55+
$response = $this->get('links/feed?api_token=' . $token);
56+
57+
$response->assertOk()->assertSee($link->url)->assertDontSee($otherLink->url);
58+
}
59+
4760
public function test_lists_feed(): void
4861
{
4962
$list = LinkList::factory()->create();

0 commit comments

Comments
 (0)