5 use BookStack\Auth\User;
9 protected string $apiTokenId = 'apitoken';
10 protected string $apiTokenSecret = 'password';
13 * Set the given user as the current logged-in user via the API driver.
14 * This does not ensure API access. The user may still lack required role permissions.
16 protected function actingAsForApi(User $user): static
18 parent::actingAs($user, 'api');
24 * Set the API editor role as the current user via the API driver.
26 protected function actingAsApiEditor(): static
28 $this->actingAs($this->users->editor(), 'api');
34 * Set the API admin role as the current user via the API driver.
36 protected function actingAsApiAdmin(): static
38 $this->actingAs($this->users->admin(), 'api');
44 * Format the given items into a standardised error format.
46 protected function errorResponse(string $message, int $code): array
48 return ['error' => ['code' => $code, 'message' => $message]];
52 * Get the structure that matches a permission error response.
54 protected function permissionErrorResponse(): array
56 return $this->errorResponse('You do not have permission to perform the requested action.', 403);
60 * Format the given (field_name => ["messages"]) array
61 * into a standard validation response format.
63 protected function validationResponse(array $messages): array
65 $err = $this->errorResponse('The given data was invalid.', 422);
66 $err['error']['validation'] = $messages;
72 * Get an approved API auth header.
74 protected function apiAuthHeader(): array
77 'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",