-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make API token optional for user api routes (#159)
* feat: make api token optional for user api routes * config: disable deprecations in production php.ini files dev should probably override this setting, but keeping it as-is for now
- Loading branch information
1 parent
64f8a63
commit 5933325
Showing
11 changed files
with
52 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; | ||
|
||
class AuthOptional | ||
{ | ||
public function handle(Request $request, Closure $next, string $gate): Response | ||
{ | ||
if ($request->bearerToken()) { | ||
$user = auth($gate)->user() or throw new UnauthorizedHttpException('Invalid authentication token'); | ||
auth($gate)->setUser($user); | ||
} | ||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
<?php | ||
|
||
it('should return 401 if the API authentication is enable', function () { | ||
it('should return 200 when no auth token is present', function () { | ||
$response = $this->getjson('/plugins/info/1.1?action=query_plugins'); | ||
$response->assertStatus(200); | ||
}); | ||
|
||
it('should return 401 when an invalid auth token is present', function () { | ||
$response = $this->getjson('/plugins/info/1.1?action=query_plugins', ['Authorization' => 'Bearer invalid-token']); | ||
$response->assertStatus(401); | ||
})->skip(fn() => !config('app.aspirecloud.api_authentication_enable'), 'API authentication is disabled'); | ||
}); | ||
|
||
// TODO: write test for real auth token | ||
|
||
it('should return 200 if the API authentication is disable', function () { | ||
$response = $this->getjson('/plugins/info/1.1?action=query_plugins'); | ||
$response->assertStatus(200); | ||
})->skip(fn() => config('app.aspirecloud.api_authentication_enable'), 'API authentication is enabled'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters