with('user')->where('key_type', ApiKey::TYPE_APPLICATION)) ->allowedFilters(['memo', 'identifier']) ->allowedSorts(['id', 'memo', 'created_at']) ->paginate($request->perPage()); return ApiKeyResource::collection($keys); } /** * Create API key. */ #[Endpoint('Create API key', 'Creates a new application API key and returns the plaintext secret token once.')] #[ResponseFromResource(ApiKeyResource::class, ApiKey::class, status: 201, description: 'API key created.', resourceKey: 'api_key', meta: ['secret_token' => 'ptla_1234567890abcdef'])] public function store(StoreApiKeyRequest $request, CreatesApiKeys $keyCreator): JsonResponse { $key = $keyCreator->create(ApiKey::TYPE_APPLICATION, [...$request->actionInput(), 'user_id' => $request->user()->id], $request->getKeyPermissions()); $key->load('user'); Activity::event('admin:api-key.create') ->subject($key) ->property('identifier', $key->identifier) ->log(); $token = Crypt::decrypt($key->token); throw_unless(is_string($token), UnexpectedValueException::class, 'The decrypted API key must be a string.'); return ApiKeyResource::make($key) ->withMeta([ 'secret_token' => $key->identifier.$token, ]) ->respond(Response::HTTP_CREATED); } /** * Show API key. */ #[Endpoint('Get API key', 'Returns a single application API key by identifier without exposing its token.')] #[ResponseFromResource(ApiKeyResource::class, ApiKey::class, description: 'API key returned.', resourceKey: 'api_key')] public function show(GetApiKeysRequest $request, string $identifier): ApiKeyResource { $key = ApiKey::query() ->with('user') ->where('key_type', ApiKey::TYPE_APPLICATION) ->where('identifier', $identifier) ->firstOrFail(); return ApiKeyResource::make($key); } /** * Update API key. */ #[Endpoint('Update API key', 'Updates an application API key memo and resource permissions without regenerating the token.')] #[ResponseFromResource(ApiKeyResource::class, ApiKey::class, description: 'API key updated.', resourceKey: 'api_key')] public function update(UpdateApiKeyRequest $request, UpdatesApiKeys $keyUpdater, string $identifier): ApiKeyResource { $key = ApiKey::query() ->where('key_type', ApiKey::TYPE_APPLICATION) ->where('identifier', $identifier) ->firstOrFail(); $key = $keyUpdater->update($key, $request->actionInput(), $request->getKeyPermissions()); $key->load('user'); Activity::event('admin:api-key.update') ->subject($key) ->property('identifier', $key->identifier) ->log(); return ApiKeyResource::make($key); } /** * Delete API key. */ #[Endpoint('Delete API key', 'Deletes an application API key by identifier.')] #[ScribeResponse(status: 204, description: 'API key deleted.')] public function destroy(DeleteApiKeyRequest $request, DeletesApiKeys $keyDeleter, string $identifier): Response { $key = ApiKey::query() ->where('key_type', ApiKey::TYPE_APPLICATION) ->where('identifier', $identifier) ->firstOrFail(); $keyDeleter->delete($key); Activity::event('admin:api-key.delete') ->subject($key) ->property('identifier', $key->identifier) ->log(); return $this->returnNoContent(); } }