user(); $tokens = $user->tokens() ->where('client_id', '=', config('passport.personal_access_client.id')) ->get(); return new ApiTokenCollection($tokens); } /** * Create a new api token for the currently authenticated user * * The response will contain the access token that can be used to send authenticated API requests. * Please note that the access token is only shown in this response and cannot be retrieved later. * * @operationId createApiToken * * @throws AuthorizationException|PersonalAccessClientIsNotConfiguredException */ public function store(ApiTokenStoreRequest $request): ApiTokenWithAccessTokenResource { $user = $this->user(); if (config('passport.personal_access_client.id') === null || config('passport.personal_access_client.secret') === null) { throw new PersonalAccessClientIsNotConfiguredException; } $token = $user->createToken($request->getName(), ['*']); /** @var Token $tokenModel */ $tokenModel = $token->token; return new ApiTokenWithAccessTokenResource($tokenModel, $token->accessToken); } /** * Revoke an api token * * @operationId revokeApiToken * * @throws AuthorizationException * @throws PersonalAccessClientIsNotConfiguredException */ public function revoke(Token $apiToken): JsonResponse { $user = $this->user(); if (config('passport.personal_access_client.id') === null || config('passport.personal_access_client.secret') === null) { throw new PersonalAccessClientIsNotConfiguredException; } if ($apiToken->user_id !== $user->getKey()) { throw new AuthorizationException('API token does not belong to user'); } if ($apiToken->client_id !== config('passport.personal_access_client.id')) { throw new AuthorizationException('API token is not a personal access token'); } $apiToken->revoke(); return response()->json(null, 204); } /** * Delete an api token * * @operationId deleteApiToken * * @throws AuthorizationException|PersonalAccessClientIsNotConfiguredException */ public function destroy(Token $apiToken): JsonResponse { $user = $this->user(); if (config('passport.personal_access_client.id') === null || config('passport.personal_access_client.secret') === null) { throw new PersonalAccessClientIsNotConfiguredException; } if ($apiToken->user_id !== $user->getKey()) { throw new AuthorizationException('API token does not belong to user'); } if ($apiToken->client_id !== config('passport.personal_access_client.id')) { throw new AuthorizationException('API token is not a personal access token'); } $apiToken->delete(); return response()->json(null, 204); } }