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

Add door token regeneration

parent c0c5c4f0
Pipeline #5453 failed with stages
in 1 minute and 59 seconds
......@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Doors\GetDoor\GetDoorUseCase;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
use Source\UseCases\Doors\UpdateDoor\APIPresenter as UpdateDoorAPIPresenter;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase;
use Source\UseCases\Doors\CreateDoor\APIPresenter as CreateDoorAPIPresenter;
......@@ -93,4 +94,19 @@ class DoorsController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase $tokenGenerator
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function regenerateToken(GenerateDoorTokenUseCase $tokenGenerator, string $doorId): JsonResponse
{
$presenter = new CreateDoorAPIPresenter();
$tokenGenerator->generate($doorId, config('app.key'), $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -9,6 +9,7 @@ use Source\Gateways\Doors\DoorsRepositoryServiceProvider;
use Source\Gateways\Users\UsersRepositoryServiceProvider;
use Source\Gateways\Groups\GroupsRepositoryServiceProvider;
use Source\Gateways\Tokens\TokensRepositoryServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorToken;
use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
use Source\UseCases\Doors\GetDoor\GetDoorUseCaseServiceProvider;
use Source\Gateways\GroupUser\GroupUserRepositoryServiceProvider;
......@@ -28,6 +29,7 @@ use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
use Source\UseCases\Users\Authenticate\AuthenticateUseCaseServiceProvider as UserAuthenticateUseCaseServiceProvider;
......@@ -79,6 +81,7 @@ class AppServiceProvider extends ServiceProvider
AuthenticateUseCaseServiceProvider::class,
DoorAuthenticateUseCaseServiceProvider::class,
UserAuthenticateUseCaseServiceProvider::class,
GenerateDoorTokenUseCaseServiceProvider::class,
];
/**
......
......@@ -56,7 +56,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::post('/', [DoorsController::class, 'store']);
Route::get('{doorId}', [DoorsController::class, 'get']);
Route::put('{doorId}', [DoorsController::class, 'update']);
// Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
// Route::delete('{doorId}', [DoorsController::class, 'delete']);
});
......
......@@ -108,6 +108,7 @@ class DatabaseDoorsRepository implements DoorsRepository
$dbDoor->name = $door->getName();
$dbDoor->location = $door->getLocation();
$dbDoor->api_token = $door->getToken()->getHash();
$dbDoor->save();
......
<?php
namespace Source\UseCases\Doors\GenerateDoorToken;
use Source\Entities\Door;
use Source\Entities\HashedSearchable;
use Source\Gateways\Doors\DoorsRepository;
use Source\Gateways\Tokens\TokensRepository;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Doors\CreateDoor\Presenter;
use Source\UseCases\Doors\CreateDoor\ResponseModel;
class GenerateDoorToken implements GenerateDoorTokenUseCase
{
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
/**
* @var \Source\Gateways\Tokens\TokensRepository
*/
protected TokensRepository $tokens;
public function __construct(DoorsRepository $doors, TokensRepository $tokens)
{
$this->doors = $doors;
$this->tokens = $tokens;
}
/**
* @inheritDoc
*/
public function generate(string $doorId, string $salt, Presenter $presenter): void
{
if (!($door = $this->doors->get($doorId))) {
throw new EntityNotFoundException();
}
$token = $this->tokens::generateTokenString();
$door = new Door(
$door->getId(),
$door->getLocation(),
$door->getName(),
HashedSearchable::hash($salt, $token),
$door->getCreatedAt(),
$door->getUpdatedAt()
);
$returnedDoor = $this->doors->update($doorId, $door);
$response = new ResponseModel($returnedDoor, $token);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Doors\GenerateDoorToken;
use Source\UseCases\Doors\CreateDoor\Presenter;
interface GenerateDoorTokenUseCase
{
/**
* Regenerate a doors api_token
*
* @param string $doorId
* @param string $salt Salt to has the token with, app key is usually a good
* choice
* @param \Source\UseCases\Doors\CreateDoor\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function generate(string $doorId, string $salt, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Doors\GenerateDoorToken;
use Source\Gateways\Doors\DoorsRepository;
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
*/
class GenerateDoorTokenUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GenerateDoorTokenUseCase::class, static function (Application $app) {
return new GenerateDoorToken($app->make(DoorsRepository::class), $app->make(TokensRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GenerateDoorTokenUseCase::class];
}
}
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