-
-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add API Key support (🚧) #350
Open
grablair
wants to merge
2
commits into
HiEventsDev:v1.0.0-alpha
Choose a base branch
from
grablair:feat/api-keys
base: v1.0.0-alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,31 @@ | ||
<?php | ||
|
||
namespace HiEvents\Http\Actions\Auth; | ||
|
||
use App\Models\Sanctum\PersonalAccessToken; | ||
use HiEvents\DomainObjects\Enums\Role; | ||
use HiEvents\Http\Actions\BaseAction; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use \DateTime; | ||
|
||
class CreateApiKeyAction extends BaseAction | ||
{ | ||
public function __invoke(Request $request): JsonResponse | ||
{ | ||
$this->minimumAllowedRole(Role::ADMIN); | ||
|
||
$abilities = ['*']; | ||
$expiryDateTime = null; | ||
if ($request->abilities && count($request->abilities) > 0) { | ||
$abilities = $request->abilities; | ||
} | ||
if ($request->expires_at) { | ||
$expiryDateTime = DateTime::createFromFormat("U", strtotime($request->expires_at)); | ||
} | ||
return $this->jsonResponse($request->user()->createToken( | ||
$request->token_name, | ||
$abilities, | ||
$expiryDateTime)); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace HiEvents\Http\Actions\Auth; | ||
|
||
use HiEvents\DomainObjects\Enums\Role; | ||
use HiEvents\Http\Actions\BaseAction; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Laravel\Sanctum\PersonalAccessToken; | ||
|
||
class GetApiKeysAction extends BaseAction | ||
{ | ||
public function __invoke(Request $request): JsonResponse | ||
{ | ||
$this->minimumAllowedRole(Role::ADMIN); | ||
|
||
$tokens = $request->user()->tokens; | ||
return $this->jsonResponse($tokens); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace HiEvents\Http\Actions\Auth; | ||
|
||
use App\Models\Sanctum\PersonalAccessToken; | ||
use HiEvents\DomainObjects\Enums\Role; | ||
use HiEvents\Http\Actions\BaseAction; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class RevokeApiKeyAction extends BaseAction | ||
{ | ||
public function __invoke(Request $request, int $apiKey): Response | ||
{ | ||
$this->minimumAllowedRole(Role::ADMIN); | ||
|
||
if ($request->user()->tokens()->where('id', $apiKey)->delete()) { | ||
return $this->deletedResponse(); | ||
} else { | ||
return $this->notFoundResponse(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace HiEvents\Models; | ||
|
||
use Laravel\Sanctum\PersonalAccessToken as SanctumAccessToken; | ||
|
||
class PersonalAccessToken extends SanctumAccessToken | ||
{ | ||
/** | ||
* Create a new Eloquent model instance. | ||
* | ||
* @param array $attributes | ||
* @return void | ||
*/ | ||
public function __construct(array $attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
|
||
$this->mergeFillable(['account_id']); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
backend/database/migrations/2025_01_16_161633_add_account_id_to_access_tokens.php
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,25 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('personal_access_tokens', static function (Blueprint $table) { | ||
$table->foreignId('account_id') | ||
->constrained() | ||
->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('personal_access_tokens', static function (Blueprint $table) { | ||
$table->dropColumn('account_id'); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious what the reason for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As soon as I added the traits required for Sanctum to the User class, I received an error about being unable to morph a User object (I unfortunately don't recall the specific error). Upon doing some google research, I discovered some other people suggesting this as a fix to the error, and it worked.
I'm sure if you remove this you'll get the same error, just in case there is a better solution I am unaware of. Or maybe you won't and I was just being gaslit. 😂