Skip to content

Commit

Permalink
CognitoApi単位でクラス化するのではなく、CognitoClientのメソッド単位でクラス化する
Browse files Browse the repository at this point in the history
  • Loading branch information
ucan-lab committed Jul 8, 2023
1 parent 23e0d48 commit 3c454b5
Show file tree
Hide file tree
Showing 67 changed files with 604 additions and 921 deletions.
23 changes: 11 additions & 12 deletions src/app/Console/Commands/DeleteAllCognitoUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace App\Console\Commands;

use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminDeleteUser\AdminDeleteUser;
use Acme\Application\Port\Aws\CognitoIdentityProvider\ListUsers\ListUsers;
use Acme\Domain\Aws\CognitoIdentityProvider\AdminDeleteUser\AdminDeleteUserPayload;
use Acme\Domain\Aws\CognitoIdentityProvider\ListUsers\ListUsersPayload;
use Acme\Application\Port\Aws\AdminDeleteUserPayload;
use Acme\Application\Port\Aws\CognitoClient;
use Acme\Application\Port\Aws\ListUsersPayload;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Arr;
Expand All @@ -32,24 +31,24 @@ final class DeleteAllCognitoUserCommand extends Command
*/
protected $description = 'Delete All Cognito User';

public function handle(ListUsers $listUsers, AdminDeleteUser $adminDeleteUser): int
public function handle(CognitoClient $cognitoClient): int
{
if (! $this->confirmToProceed()) {
return self::FAILURE;
}

$usernameList = $this->executeListUsers($listUsers);
$usernameList = $this->executeListUsers($cognitoClient);

$this->withProgressBar($usernameList, fn (string $username) => $this->executeAdminDeleteUser($adminDeleteUser, $username));
$this->withProgressBar($usernameList, fn (string $username) => $this->executeAdminDeleteUser($cognitoClient, $username));
$this->newLine();

return self::SUCCESS;
}

private function executeListUsers(ListUsers $listUsers): array
private function executeListUsers(CognitoClient $cognitoClient): array
{
$payload = ListUsersPayload::create();
$result = $listUsers->execute($payload);
$result = $cognitoClient->listUsers($payload);

if ($result->hasNext() === false) {
return $result->usernameList;
Expand All @@ -60,16 +59,16 @@ private function executeListUsers(ListUsers $listUsers): array

do {
$payload = ListUsersPayload::create($result->paginationToken);
$result = $listUsers->execute($payload);
$result = $cognitoClient->listUsers($payload);
$usernameList[] = $result->usernameList;
} while ($result->hasNext());

return Arr::flatten($usernameList);
}

private function executeAdminDeleteUser(AdminDeleteUser $adminDeleteUser, string $username): void
private function executeAdminDeleteUser(CognitoClient $cognitoClient, string $username): void
{
$payload = AdminDeleteUserPayload::create($username);
$adminDeleteUser->execute($payload);
$cognitoClient->adminDeleteUser($payload);
}
}
14 changes: 7 additions & 7 deletions src/app/Console/Commands/ListAllCognitoUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Console\Commands;

use Acme\Application\Port\Aws\CognitoIdentityProvider\ListUsers\ListUsers;
use Acme\Domain\Aws\CognitoIdentityProvider\ListUsers\ListUsersPayload;
use Acme\Application\Port\Aws\CognitoClient;
use Acme\Application\Port\Aws\ListUsersPayload;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;

Expand All @@ -25,19 +25,19 @@ final class ListAllCognitoUserCommand extends Command
*/
protected $description = 'Delete All Cognito User';

public function handle(ListUsers $listUsers): int
public function handle(CognitoClient $cognitoClient): int
{
$usernameList = $this->executeListUsers($listUsers);
$usernameList = $this->executeListUsers($cognitoClient);

array_map(fn (string $username) => $this->info($username), $usernameList);

return self::SUCCESS;
}

private function executeListUsers(ListUsers $listUsers): array
private function executeListUsers(CognitoClient $cognitoClient): array
{
$payload = ListUsersPayload::create();
$result = $listUsers->execute($payload);
$result = $cognitoClient->listUsers($payload);

if ($result->hasNext() === false) {
return $result->usernameList;
Expand All @@ -48,7 +48,7 @@ private function executeListUsers(ListUsers $listUsers): array

do {
$payload = ListUsersPayload::create($result->paginationToken);
$result = $listUsers->execute($payload);
$result = $cognitoClient->listUsers($payload);
$usernameList[] = $result->usernameList;
} while ($result->hasNext());

Expand Down
13 changes: 6 additions & 7 deletions src/app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace App\Http\Controllers;

use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminInitiateAuth\AdminInitiateAuth;
use Acme\Domain\Aws\CognitoIdentityProvider\AdminInitiateAuth\AdminInitiateAuthPayload;
use Acme\Domain\User\Username;
use Acme\Application\Port\Aws\AdminInitiateAuthPayload;
use Acme\Application\Port\Aws\CognitoClient;
use Acme\Domain\User\UserRepository;
use App\Http\Requests\LoginRequest;
use Illuminate\Http\RedirectResponse;
Expand All @@ -17,16 +16,16 @@
{
public function __construct(
private UserRepository $userRepository,
private AdminInitiateAuth $adminInitiateAuth,
private CognitoClient $cognitoClient,
) {
}

public function __invoke(LoginRequest $request, AdminInitiateAuth $adminInitiateAuth): RedirectResponse
public function __invoke(LoginRequest $request): RedirectResponse
{
$user = $this->userRepository->findByUsername(new Username($request->username()));
$user = $this->userRepository->findByUsername($request->username());

$payload = AdminInitiateAuthPayload::createForAdminUserPasswordAuthFlow($user->username(), $request->password());
$this->adminInitiateAuth->execute($payload);
$this->cognitoClient->adminInitiateAuth($payload);

Auth::loginUsingId($user->userId());
Session::regenerate();
Expand Down
8 changes: 4 additions & 4 deletions src/app/Http/Controllers/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace App\Http\Controllers;

use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminUserGlobalSignOut\AdminUserGlobalSignOut;
use Acme\Domain\Aws\CognitoIdentityProvider\AdminUserGlobalSignOut\AdminUserGlobalSignOutPayload;
use Acme\Application\Port\Aws\AdminUserGlobalSignOutPayload;
use Acme\Application\Port\Aws\CognitoClient;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

final readonly class LogoutController
{
public function __construct(private AdminUserGlobalSignOut $adminUserGlobalSignOut)
public function __construct(private CognitoClient $cognitoClient)
{
}

Expand All @@ -24,7 +24,7 @@ public function __invoke(): RedirectResponse
$user = Auth::user();

$payload = AdminUserGlobalSignOutPayload::create($user->username);
$this->adminUserGlobalSignOut->execute($payload);
$this->cognitoClient->adminUserGlobalSignOut($payload);

Auth::logout();
Session::invalidate();
Expand Down
54 changes: 6 additions & 48 deletions src/app/Providers/AwsCognitoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,9 @@

namespace App\Providers;

use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminCreateUser\AdminCreateUser;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminDeleteUser\AdminDeleteUser;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminGetUser\AdminGetUser;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminInitiateAuth\AdminInitiateAuth;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminRespondToAuthChallenge\AdminRespondToAuthChallenge;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminSetUserPassword\AdminSetUserPassword;
use Acme\Application\Port\Aws\CognitoIdentityProvider\AdminUserGlobalSignOut\AdminUserGlobalSignOut;
use Acme\Application\Port\Aws\CognitoIdentityProvider\ListUsers\ListUsers;
use Acme\Infra\Adapter\Aws\AdminCreateUser\AwsAdminCreateUser;
use Acme\Infra\Adapter\Aws\AdminCreateUser\MockAdminCreateUser;
use Acme\Infra\Adapter\Aws\AdminDeleteUser\AwsAdminDeleteUser;
use Acme\Infra\Adapter\Aws\AdminDeleteUser\MockAdminDeleteUser;
use Acme\Infra\Adapter\Aws\AdminGetUser\AwsAdminGetUser;
use Acme\Infra\Adapter\Aws\AdminGetUser\MockAdminGetUser;
use Acme\Infra\Adapter\Aws\AdminInitiateAuth\AwsAdminInitiateAuth;
use Acme\Infra\Adapter\Aws\AdminInitiateAuth\MockAdminInitiateAuth;
use Acme\Infra\Adapter\Aws\AdminRespondToAuthChallenge\AwsAdminRespondToAuthChallenge;
use Acme\Infra\Adapter\Aws\AdminRespondToAuthChallenge\MockAdminRespondToAuthChallenge;
use Acme\Infra\Adapter\Aws\AdminSetUserPassword\AwsAdminSetUserPassword;
use Acme\Infra\Adapter\Aws\AdminSetUserPassword\MockAdminSetUserPassword;
use Acme\Infra\Adapter\Aws\AdminUserGlobalSignOut\AwsAdminUserGlobalSignOut;
use Acme\Infra\Adapter\Aws\AdminUserGlobalSignOut\MockAdminUserGlobalSignOut;
use Acme\Infra\Adapter\Aws\ListUsers\AwsListUsers;
use Acme\Infra\Adapter\Aws\ListUsers\MockListUsers;
use Acme\Application\Port\Aws\CognitoClient;
use Acme\Infra\Adapter\Aws\AwsCognitoClient;
use Acme\Infra\Adapter\Aws\MockCognitoClient;
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
Expand All @@ -46,23 +25,9 @@ public function register(): void
}

if (config('services.cognito.enabled')) {
$this->app->bind(AdminCreateUser::class, AwsAdminCreateUser::class);
$this->app->bind(AdminDeleteUser::class, AwsAdminDeleteUser::class);
$this->app->bind(AdminGetUser::class, AwsAdminGetUser::class);
$this->app->bind(AdminInitiateAuth::class, AwsAdminInitiateAuth::class);
$this->app->bind(AdminRespondToAuthChallenge::class, AwsAdminRespondToAuthChallenge::class);
$this->app->bind(AdminSetUserPassword::class, AwsAdminSetUserPassword::class);
$this->app->bind(AdminUserGlobalSignOut::class, AwsAdminUserGlobalSignOut::class);
$this->app->bind(ListUsers::class, AwsListUsers::class);
$this->app->singleton(CognitoClient::class, AwsCognitoClient::class);
} else {
$this->app->bind(AdminCreateUser::class, MockAdminCreateUser::class);
$this->app->bind(AdminDeleteUser::class, MockAdminDeleteUser::class);
$this->app->bind(AdminGetUser::class, MockAdminGetUser::class);
$this->app->bind(AdminInitiateAuth::class, MockAdminInitiateAuth::class);
$this->app->bind(AdminRespondToAuthChallenge::class, MockAdminRespondToAuthChallenge::class);
$this->app->bind(AdminSetUserPassword::class, MockAdminSetUserPassword::class);
$this->app->bind(AdminUserGlobalSignOut::class, MockAdminUserGlobalSignOut::class);
$this->app->bind(ListUsers::class, MockListUsers::class);
$this->app->singleton(CognitoClient::class, MockCognitoClient::class);
}
}

Expand All @@ -73,14 +38,7 @@ public function provides(): array
{
return [
CognitoIdentityProviderClient::class,
AdminCreateUser::class,
AdminDeleteUser::class,
AdminGetUser::class,
AdminInitiateAuth::class,
AdminRespondToAuthChallenge::class,
AdminSetUserPassword::class,
AdminUserGlobalSignOut::class,
ListUsers::class,
CognitoClient::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminCreateUser;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\AttributeType;
use Acme\Application\Port\Aws\CognitoType\AttributeType;

final readonly class AdminCreateUserPayload
final readonly class AdminCreateUserPayload implements CognitoPayload
{
/**
* @param string $userPoolId 必須
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminCreateUser;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\AttributeType;
use Acme\Application\Port\Aws\CognitoType\AttributeType;
use Aws\Result;
use Carbon\Carbon;

final readonly class AdminCreateUserResult
final readonly class AdminCreateUserResult implements CognitoResult
{
private function __construct(
public AttributeType $attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminDeleteUser;
namespace Acme\Application\Port\Aws;

final readonly class AdminDeleteUserPayload
final readonly class AdminDeleteUserPayload implements CognitoPayload
{
/**
* @param string $userPoolId 必須
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Acme\Application\Port\Aws;

final readonly class AdminDeleteUserResult implements CognitoResult
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminGetUser;
namespace Acme\Application\Port\Aws;

final readonly class AdminGetUserPayload
final readonly class AdminGetUserPayload implements CognitoPayload
{
private function __construct(
public string $userPoolId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminGetUser;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\AttributeType;
use Acme\Domain\Aws\CognitoType\UserMFASetting;
use Acme\Domain\Aws\CognitoType\UserMFASettingList;
use Acme\Domain\Aws\CognitoType\UserStatus;
use Acme\Application\Port\Aws\CognitoType\AttributeType;
use Acme\Application\Port\Aws\CognitoType\UserMFASetting;
use Acme\Application\Port\Aws\CognitoType\UserMFASettingList;
use Acme\Application\Port\Aws\CognitoType\UserStatus;
use Aws\Result;
use Carbon\Carbon;

final readonly class AdminGetUserResult
final readonly class AdminGetUserResult implements CognitoResult
{
/**
* @param array $mFAOptions 非推奨オプション。userMFASettingListの方を参照する
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminInitiateAuth;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\AuthFlow;
use Acme\Application\Port\Aws\CognitoType\AuthFlow;

final readonly class AdminInitiateAuthPayload
final readonly class AdminInitiateAuthPayload implements CognitoPayload
{
private function __construct(
public string $clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminInitiateAuth;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\ChallengeName;
use Acme\Application\Port\Aws\CognitoType\ChallengeName;
use Aws\Result;

final readonly class AdminInitiateAuthResult
final readonly class AdminInitiateAuthResult implements CognitoResult
{
private function __construct(
public string $session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminRespondToAuthChallenge;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoIdentityProvider\SecretHashGenerator;
use Acme\Domain\Aws\CognitoType\ChallengeName;
use Acme\Application\Port\Aws\CognitoType\ChallengeName;
use Acme\Infra\Adapter\Aws\SecretHashGenerator;

final readonly class AdminRespondToAuthChallengePayload
final readonly class AdminRespondToAuthChallengePayload implements CognitoPayload
{
private function __construct(
public string $userPoolId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Acme\Domain\Aws\CognitoIdentityProvider\AdminRespondToAuthChallenge;
namespace Acme\Application\Port\Aws;

use Acme\Domain\Aws\CognitoType\ChallengeName;
use Acme\Application\Port\Aws\CognitoType\ChallengeName;
use Aws\Result;

final readonly class AdminRespondToAuthChallengeResult
final readonly class AdminRespondToAuthChallengeResult implements CognitoResult
{
private function __construct(
public string $session,
Expand Down
Loading

0 comments on commit 3c454b5

Please sign in to comment.