-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API endpoints for user API tokens
- Loading branch information
Showing
21 changed files
with
654 additions
and
11 deletions.
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Requests\V1\ApiToken\ApiTokenStoreRequest; | ||
use App\Http\Resources\V1\ApiToken\ApiTokenCollection; | ||
use App\Http\Resources\V1\ApiToken\ApiTokenWithAccessTokenResource; | ||
use App\Models\Passport\Token; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ApiTokenController extends Controller | ||
{ | ||
/** | ||
* List all api token of the currently authenticated user | ||
* | ||
* This endpoint is independent of organization. | ||
* | ||
* @operationId getApiTokens | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function index(): ApiTokenCollection | ||
{ | ||
$user = $this->user(); | ||
|
||
$tokens = $user->tokens()->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. | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function store(ApiTokenStoreRequest $request): ApiTokenWithAccessTokenResource | ||
{ | ||
$user = $this->user(); | ||
|
||
$token = $user->createToken($request->getName(), ['*']); | ||
/** @var Token $tokenModel */ | ||
$tokenModel = $token->token; | ||
|
||
return new ApiTokenWithAccessTokenResource($tokenModel, $token->accessToken); | ||
} | ||
|
||
/** | ||
* Revoke an api token | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function revoke(string $apiTokenId): JsonResponse | ||
{ | ||
$user = $this->user(); | ||
|
||
$apiToken = $user->tokens()->where('id', $apiTokenId)->firstOrFail(); | ||
|
||
$apiToken->revoke(); | ||
|
||
return response()->json(null, 204); | ||
} | ||
|
||
/** | ||
* Delete an api token | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function destroy(string $apiTokenId): JsonResponse | ||
{ | ||
$user = $this->user(); | ||
|
||
$apiToken = $user->tokens()->where('id', $apiTokenId)->firstOrFail(); | ||
|
||
$apiToken->delete(); | ||
|
||
return response()->json(null, 204); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests\V1\ApiToken; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class ApiTokenStoreRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, array<string>> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'min:1', | ||
'max:255', | ||
], | ||
]; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->input('name'); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\ApiToken; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class ApiTokenCollection extends ResourceCollection | ||
{ | ||
/** | ||
* The resource that this resource collects. | ||
* | ||
* @var string | ||
*/ | ||
public $collects = ApiTokenResource::class; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\ApiToken; | ||
|
||
use App\Http\Resources\V1\BaseResource; | ||
use App\Models\Passport\Token; | ||
use Illuminate\Http\Request; | ||
|
||
/** | ||
* @property-read Token $resource | ||
*/ | ||
class ApiTokenResource extends BaseResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, string|bool|int|null|array<string>> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->resource->id, | ||
'name' => $this->resource->name, | ||
'revoked' => $this->resource->revoked, | ||
'scopes' => $this->resource->scopes, | ||
'created_at' => $this->formatDateTime($this->resource->created_at), | ||
'expires_at' => $this->formatDateTime($this->resource->expires_at), | ||
]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/Http/Resources/V1/ApiToken/ApiTokenWithAccessTokenResource.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\ApiToken; | ||
|
||
use App\Models\Passport\Token; | ||
use Illuminate\Http\Request; | ||
|
||
/** | ||
* @property-read Token $resource | ||
*/ | ||
class ApiTokenWithAccessTokenResource extends ApiTokenResource | ||
{ | ||
private string $accessToken; | ||
|
||
public function __construct(Token $resource, string $accessToken) | ||
{ | ||
$this->accessToken = $accessToken; | ||
parent::__construct($resource); | ||
} | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, string|bool|int|null|array<string>> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
$parent = parent::toArray($request); | ||
|
||
return $parent + [ | ||
'access_token' => $this->accessToken, | ||
]; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Passport; | ||
|
||
use Laravel\Passport\AuthCode as PassportAuthCode; | ||
|
||
class AuthCode extends PassportAuthCode {} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Passport; | ||
|
||
use Database\Factories\Passport\ClientFactory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Laravel\Passport\Client as PassportClient; | ||
|
||
/** | ||
* @property string $id | ||
* @property string|null $user_id | ||
* @property string $name | ||
* @property string|null $secret | ||
* @property string|null $provider | ||
* @property string $redirect | ||
* @property bool $personal_access_client | ||
* @property bool $password_client | ||
* @property bool $revoked | ||
*/ | ||
class Client extends PassportClient | ||
{ | ||
/** @use HasFactory<ClientFactory> */ | ||
use HasFactory; | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Passport; | ||
|
||
use Laravel\Passport\PersonalAccessClient as PassportPersonalAccessClient; | ||
|
||
class PersonalAccessClient extends PassportPersonalAccessClient {} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Passport; | ||
|
||
use Laravel\Passport\RefreshToken as PassportRefreshToken; | ||
|
||
class RefreshToken extends PassportRefreshToken {} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models\Passport; | ||
|
||
use Database\Factories\Passport\TokenFactory; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Support\Carbon; | ||
use Laravel\Passport\Token as PassportToken; | ||
|
||
/** | ||
* @property string $id | ||
* @property null|string $user_id | ||
* @property string $client_id | ||
* @property null|string $name | ||
* @property array<string> $scopes | ||
* @property bool $revoked | ||
* @property Carbon|null $created_at | ||
* @property Carbon|null $updated_at | ||
* @property Carbon|null $expires_at | ||
*/ | ||
class Token extends PassportToken | ||
{ | ||
/** @use HasFactory<TokenFactory> */ | ||
use HasFactory; | ||
} |
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
Oops, something went wrong.