Commit 8e22ffcb authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Do routes for token manipulation

parent 466bcf16
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Tokens\GetToken\GetTokenUseCase;
use Source\UseCases\Tokens\CreateToken\CreateTokenUseCase;
use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCase;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCase;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCase;
use Source\UseCases\Tokens\GetToken\APIPresenter as GetTokenAPIPresenter;
use Source\UseCases\Tokens\GetAllTokens\APIPresenter as AllTokensAPIPresenter;
use Source\UseCases\Tokens\CreateToken\APIPresenter as CreateTokenAPIPresenter;
use Source\UseCases\Tokens\ExpireToken\APIPresenter as ExpireTokenAPIPresenter;
use Source\UseCases\Tokens\UpdateToken\APIPresenter as UpdateTokenAPIPresenter;
class TokensController extends ApiController
{
/**
* @param \Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCase $allTokens
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function index(GetAllTokensUseCase $allTokens): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new AllTokensAPIPresenter();
$allTokens->all($presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Tokens\GetToken\GetTokenUseCase $token
* @param string $tokenId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function get(GetTokenUseCase $token, string $tokenId): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new GetTokenAPIPresenter();
$token->getToken($tokenId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Tokens\CreateToken\CreateTokenUseCase $createToken
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function store(CreateTokenUseCase $createToken): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'name' => 'required|string|max:255',
'user_id' => 'required|numeric',
'expires_at' => 'date',
]);
$attributes = $this->request->all();
$attributes['salt'] = config('app.key');
$presenter = new CreateTokenAPIPresenter();
$createToken->create($attributes, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCase $updateToken
* @param string $tokenId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function update(UpdateTokenUseCase $updateToken, string $tokenId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$this->validate($this->request, [
'name' => 'string|max:255',
'expires_at' => 'date',
]);
$attributes = $this->request->all();
$attributes['salt'] = config('app.key');
$presenter = new UpdateTokenAPIPresenter();
$updateToken->update($tokenId, $attributes, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCase $expireToken
* @param string $tokenId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function expire(ExpireTokenUseCase $expireToken, string $tokenId): JsonResponse
{
$this->authorizer->protect(Permissions::ADMIN);
$presenter = new ExpireTokenAPIPresenter();
$expireToken->expire($tokenId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -6,6 +6,7 @@ use App\Http\Controllers\AuthController;
use App\Http\Controllers\DoorsController;
use App\Http\Controllers\UsersController;
use App\Http\Controllers\GroupsController;
use App\Http\Controllers\TokensController;
/*
|--------------------------------------------------------------------------
......@@ -64,6 +65,16 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::delete('{doorId}/group/{groupId}', [DoorsController::class, 'removeDoorFromGroup']);
});
Route::group([
'prefix' => 'tokens',
], static function () {
Route::get('/', [TokensController::class, 'index']);
Route::get('{tokenId}', [TokensController::class, 'get']);
Route::post('/', [TokensController::class, 'store']);
Route::put('{tokenId}', [TokensController::class, 'update']);
Route::post('{tokenId}/expire', [TokensController::class, 'expire']);
});
Route::group([
'prefix' => 'me',
], static function () {
......
......@@ -154,7 +154,7 @@ class DatabaseTokensRepository implements TokensRepository
*/
public function all(): array
{
$tokens = \App\Token::all();
$tokens = \App\Token::whereNotNull('name')->get();
return array_map(static function (\App\Token $token) {
return self::dbTokenToToken($token);
......
......@@ -2,6 +2,7 @@
namespace Source\UseCases\Tokens\CreateToken;
use Carbon\Carbon;
use Source\Entities\Token;
use Source\Sanitize\CastsTo;
use Source\Entities\HashedSearchable;
......@@ -31,13 +32,18 @@ class CreateToken implements CreateTokenUseCase
{
$tokenString = $this->tokens::generateTokenString();
$date = null;
if (isset($attributes['expires_at'])) {
$date = new Carbon($attributes['expires_at']);
}
$token = $this->tokens->create(
new Token(
0,
$this->castToInt($attributes['user_id']),
HashedSearchable::hash($attributes['salt'], $tokenString),
$attributes['name'] ?? null,
$this->castToDate($attributes['expires_at'] ?? null)
$date
)
);
......
......@@ -10,12 +10,13 @@ interface CreateTokenUseCase
* - user_id
* - salt
* Optional Attributes:
* - expires_at (Must be of format Y-m-d H:i:s)
* - expires_at
* - name
*
* @param array $attributes
* @param \Source\UseCases\Tokens\CreateToken\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function create(array $attributes, Presenter $presenter): void;
}
......@@ -3,10 +3,10 @@
namespace Source\UseCases\Tokens\CreateToken;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
......@@ -3,10 +3,10 @@
namespace Source\UseCases\Tokens\ExpireToken;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
......@@ -3,10 +3,10 @@
namespace Source\UseCases\Tokens\GetAllTokens;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
......@@ -3,10 +3,10 @@
namespace Source\UseCases\Tokens\GetToken;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
......@@ -2,6 +2,7 @@
namespace Source\UseCases\Tokens\UpdateToken;
use Carbon\Carbon;
use Source\Entities\Token;
use Source\Sanitize\CastsTo;
use Source\Gateways\Tokens\TokensRepository;
......@@ -32,12 +33,17 @@ class UpdateToken implements UpdateTokenUseCase
throw new EntityNotFoundException();
}
$date = null;
if (isset($attributes['expires_at'])) {
$date = new Carbon($attributes['expires_at']);
}
$token = new Token(
$token->getId(),
$token->getUserId(),
$token->getTokenString(),
$attributes['name'] ?? $token->getName(),
$this->castToDate($attributes['expired_at'] ?? null) ?? $token->getExpiresAt(),
$date,
$token->getCreatedAt(),
$token->getUpdatedAt()
);
......
......@@ -14,6 +14,7 @@ interface UpdateTokenUseCase
* @param array $attributes
* @param \Source\UseCases\Tokens\UpdateToken\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function update(string $tokenId, array $attributes, Presenter $presenter): void;
}
......@@ -3,10 +3,10 @@
namespace Source\UseCases\Tokens\UpdateToken;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment