Repository navigation
Add read API and signed webhooks #390
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
Open
SebastiaanKloos
wants to merge
4
commits into
main
Choose a base branch
from
pol-353-fase-2-ploi-roadmap-api-en-ondertekende-webhooks
base: main
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
4 commits
Select commit
Hold shift + click to select a range
4e3fcc5
Add read API and signed webhooks (POL-353)
SebastiaanKloos a4a3223
Use admin-managed API tokens not tied to a user for the read API
SebastiaanKloos bc2ae11
Address review feedback on read API and webhooks
SebastiaanKloos 8387763
Hide API token and webhook navigation from non-admins
SebastiaanKloos 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 hidden or 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 hidden or 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 | ||
|
|
||
| namespace App\Enums; | ||
|
|
||
| enum WebhookEvent: string | ||
| { | ||
| case ItemCreated = 'item.created'; | ||
| case ItemUpdated = 'item.updated'; | ||
| case ItemStatusChanged = 'item.status_changed'; | ||
| case ItemDeleted = 'item.deleted'; | ||
| case CommentCreated = 'comment.created'; | ||
| case CommentUpdated = 'comment.updated'; | ||
| case CommentDeleted = 'comment.deleted'; | ||
| case ProjectCreated = 'project.created'; | ||
| case ProjectUpdated = 'project.updated'; | ||
| case ProjectDeleted = 'project.deleted'; | ||
|
|
||
| /** | ||
| * @return array<string, string> | ||
| */ | ||
| public static function options(): array | ||
| { | ||
| return collect(self::cases())->mapWithKeys(fn (self $event) => [$event->value => $event->value])->all(); | ||
| } | ||
| } |
This file contains hidden or 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,89 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\ApiTokens; | ||
|
|
||
| use App\Models\ApiToken; | ||
| use Filament\Tables\Table; | ||
| use Filament\Schemas\Schema; | ||
| use Filament\Resources\Resource; | ||
| use Filament\Actions\DeleteAction; | ||
| use Filament\Actions\DeleteBulkAction; | ||
| use Filament\Tables\Columns\TextColumn; | ||
| use Filament\Forms\Components\TextInput; | ||
| use App\Filament\Resources\ApiTokens\Pages\ListApiTokens; | ||
| use App\Filament\Resources\ApiTokens\Pages\CreateApiToken; | ||
|
|
||
| class ApiTokenResource extends Resource | ||
| { | ||
| protected static ?string $model = ApiToken::class; | ||
|
|
||
| protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-key'; | ||
|
|
||
| protected static ?int $navigationSort = 1400; | ||
|
|
||
| public static function shouldRegisterNavigation(): bool | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: now that the policy is in place, this override isn't needed. Filament v5's |
||
| { | ||
| return static::canAccess(); | ||
| } | ||
|
|
||
| public static function getNavigationGroup(): ?string | ||
| { | ||
| return trans('nav.manage'); | ||
| } | ||
|
|
||
| public static function getNavigationLabel(): string | ||
| { | ||
| return trans('nav.api-tokens'); | ||
| } | ||
|
|
||
| public static function getModelLabel(): string | ||
| { | ||
| return trans('resources.api-token.label'); | ||
| } | ||
|
|
||
| public static function getPluralModelLabel(): string | ||
| { | ||
| return trans('resources.api-token.label-plural'); | ||
| } | ||
|
|
||
| public static function form(Schema $schema): Schema | ||
| { | ||
| return $schema->components([ | ||
| TextInput::make('name') | ||
| ->label(trans('resources.api-token.name')) | ||
| ->maxLength(255) | ||
| ->required(), | ||
| ]); | ||
| } | ||
|
|
||
| public static function table(Table $table): Table | ||
| { | ||
| return $table | ||
| ->columns([ | ||
| TextColumn::make('name') | ||
| ->label(trans('resources.api-token.name')) | ||
| ->searchable(), | ||
| TextColumn::make('last_used_at') | ||
| ->label(trans('resources.api-token.last-used-at')) | ||
| ->since() | ||
| ->placeholder('-'), | ||
| TextColumn::make('created_at') | ||
| ->label(trans('resources.created-at')) | ||
| ->dateTime(), | ||
| ]) | ||
| ->recordActions([ | ||
| DeleteAction::make(), | ||
| ]) | ||
| ->toolbarActions([ | ||
| DeleteBulkAction::make(), | ||
| ]); | ||
| } | ||
|
|
||
| public static function getPages(): array | ||
| { | ||
| return [ | ||
| 'index' => ListApiTokens::route('/'), | ||
| 'create' => CreateApiToken::route('/create'), | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or 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,38 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\ApiTokens\Pages; | ||
|
|
||
| use App\Models\ApiToken; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Filament\Notifications\Notification; | ||
| use Filament\Resources\Pages\CreateRecord; | ||
| use App\Filament\Resources\ApiTokens\ApiTokenResource; | ||
|
|
||
| class CreateApiToken extends CreateRecord | ||
| { | ||
| protected static string $resource = ApiTokenResource::class; | ||
|
|
||
| protected function handleRecordCreation(array $data): Model | ||
| { | ||
| [$token, $plainTextToken] = ApiToken::generate($data['name']); | ||
|
|
||
| Notification::make() | ||
| ->title(trans('resources.api-token.created-title')) | ||
| ->body(trans('resources.api-token.created-body', ['token' => $plainTextToken])) | ||
| ->success() | ||
| ->persistent() | ||
| ->send(); | ||
|
|
||
| return $token; | ||
| } | ||
|
|
||
| protected function getCreatedNotification(): ?Notification | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| protected function getRedirectUrl(): string | ||
| { | ||
| return static::getResource()::getUrl('index'); | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\ApiTokens\Pages; | ||
|
|
||
| use Filament\Actions\CreateAction; | ||
| use Filament\Resources\Pages\ListRecords; | ||
| use App\Filament\Resources\ApiTokens\ApiTokenResource; | ||
|
|
||
| class ListApiTokens extends ListRecords | ||
| { | ||
| protected static string $resource = ApiTokenResource::class; | ||
|
|
||
| protected function getHeaderActions(): array | ||
| { | ||
| return [ | ||
| CreateAction::make(), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\Webhooks\Pages; | ||
|
|
||
| use Filament\Resources\Pages\CreateRecord; | ||
| use App\Filament\Resources\Webhooks\WebhookResource; | ||
|
|
||
| class CreateWebhook extends CreateRecord | ||
| { | ||
| protected static string $resource = WebhookResource::class; | ||
| } |
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\Webhooks\Pages; | ||
|
|
||
| use Filament\Actions\DeleteAction; | ||
| use Filament\Resources\Pages\EditRecord; | ||
| use App\Filament\Resources\Webhooks\WebhookResource; | ||
|
|
||
| class EditWebhook extends EditRecord | ||
| { | ||
| protected static string $resource = WebhookResource::class; | ||
|
|
||
| protected function mutateFormDataBeforeFill(array $data): array | ||
| { | ||
| // `secret` is hidden on the model, so Filament would otherwise leave the field empty. | ||
| $data['secret'] = $this->record->secret; | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| protected function getHeaderActions(): array | ||
| { | ||
| return [ | ||
| DeleteAction::make(), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace App\Filament\Resources\Webhooks\Pages; | ||
|
|
||
| use Filament\Actions\CreateAction; | ||
| use Filament\Resources\Pages\ListRecords; | ||
| use App\Filament\Resources\Webhooks\WebhookResource; | ||
|
|
||
| class ListWebhooks extends ListRecords | ||
| { | ||
| protected static string $resource = WebhookResource::class; | ||
|
|
||
| protected function getHeaderActions(): array | ||
| { | ||
| return [ | ||
| CreateAction::make(), | ||
| ]; | ||
| } | ||
| } |
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.
Blocking (security): this resource has no policy,
canAccess()orshouldRegisterNavigation()guard.User::canAccessPanel()lets both Admins and Employees in, so an Employee can open Manage > API tokens, create a token and read every private project, item and comment through/api/v1.The rest of the admin-only area restricts this explicitly.
SettingsandSystemcheckhasRole(UserRole::Admin), andUserPolicydoes the same. The README also says "Admins manage webhooks". The tests only log in as an Admin, so nothing covers this.Suggestion: add an
ApiTokenPolicy(and aWebhookEndpointPolicy) that only allowsUserRole::Admin, plus a test that an Employee gets a 403.