Skip to content

Commit 45a1ffd

Browse files
committed
AdminGetUser API
1 parent 2a41820 commit 45a1ffd

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoIdentityProvider\AdminGetUser;
6+
7+
/**
8+
* ユーザープール内のユーザー名で指定されたユーザーを管理者として取得します。
9+
*
10+
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-cognito-idp-2016-04-18.html#admingetuser
11+
*/
12+
interface AdminGetUser
13+
{
14+
public function execute(AdminGetUserPayload $payload): AdminGetUserResult;
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoIdentityProvider\AdminGetUser;
6+
7+
final readonly class AdminGetUserPayload
8+
{
9+
/**
10+
* @param string $userPoolId
11+
* @param string $username
12+
*/
13+
private function __construct(
14+
public string $userPoolId,
15+
public string $username,
16+
) {
17+
}
18+
19+
/**
20+
* @param string $username
21+
* @return $this
22+
*/
23+
public static function create(string $username): self
24+
{
25+
return new self(
26+
config('services.cognito.user_pool_id'),
27+
$username,
28+
);
29+
}
30+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoIdentityProvider\AdminGetUser;
6+
7+
use App\Aws\CognitoType\AttributeType;
8+
use App\Aws\CognitoType\UserMFASetting;
9+
use App\Aws\CognitoType\UserMFASettingList;
10+
use App\Aws\CognitoType\UserStatus;
11+
use Aws\Result;
12+
use Carbon\Carbon;
13+
14+
final readonly class AdminGetUserResult
15+
{
16+
/**
17+
* @param bool $enabled
18+
* @param array $mFAOptions 非推奨オプション。userMFASettingListの方を参照する
19+
* @param UserMFASetting|null $preferredMfaSetting ユーザーの好みの優先MFA設定
20+
* @param AttributeType $userAttributes ユーザー属性
21+
* @param Carbon $userCreateDate ユーザー作成日
22+
* @param Carbon $userLastModifiedDate ユーザー最終更新日
23+
* @param UserMFASettingList $userMFASettingList ユーザーMFA設定リスト
24+
* @param UserStatus $userStatus ユーザーステータス
25+
* @param string $username
26+
*/
27+
private function __construct(
28+
public bool $enabled,
29+
public array $mFAOptions,
30+
public ?UserMFASetting $preferredMfaSetting,
31+
public AttributeType $userAttributes,
32+
public Carbon $userCreateDate,
33+
public Carbon $userLastModifiedDate,
34+
public UserMFASettingList $userMFASettingList,
35+
public UserStatus $userStatus,
36+
public string $username,
37+
) {
38+
}
39+
40+
/**
41+
* @param Result $result
42+
* @return static
43+
*/
44+
public static function createForAws(Result $result): self
45+
{
46+
return new self(
47+
$result->get('Enabled'),
48+
$result->get('MFAOptions') ?? [],
49+
$result->get('PreferredMfaSetting') ? UserMFASetting::from($result->get('PreferredMfaSetting')) : null,
50+
AttributeType::create($result->get('UserAttributes')),
51+
Carbon::instance($result->get('UserCreateDate')),
52+
Carbon::instance($result->get('UserLastModifiedDate')),
53+
UserMFASettingList::create($result->get('UserMFASettingList') ?? []),
54+
UserStatus::from($result->get('UserStatus')),
55+
$result->get('Username'),
56+
);
57+
}
58+
59+
/**
60+
* @param array $result
61+
* @return static
62+
*/
63+
public static function createForMock(array $result): self
64+
{
65+
return new self(
66+
$result['Enabled'],
67+
$result['MFAOptions'],
68+
$result['PreferredMfaSetting'] ? UserMFASetting::from($result['PreferredMfaSetting']) : null,
69+
AttributeType::create($result['UserAttributes']),
70+
$result['UserCreateDate'],
71+
$result['UserLastModifiedDate'],
72+
UserMFASettingList::create($result['UserMFASettingList'] ?? []),
73+
UserStatus::from($result['UserStatus']),
74+
$result['Username'],
75+
);
76+
}
77+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoIdentityProvider\AdminGetUser;
6+
7+
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
8+
use Illuminate\Support\Facades\Log;
9+
use JsonException;
10+
11+
final readonly class AwsAdminGetUser implements AdminGetUser
12+
{
13+
public function __construct(private CognitoIdentityProviderClient $client)
14+
{
15+
}
16+
17+
/**
18+
* @param AdminGetUserPayload $payload
19+
* @return AdminGetUserResult
20+
* @throws JsonException
21+
*/
22+
public function execute(AdminGetUserPayload $payload): AdminGetUserResult
23+
{
24+
Log::debug(get_class($payload), json_decode(json_encode($payload, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR));
25+
26+
$awsResult = $this->client->adminGetUser([
27+
'UserPoolId' => $payload->userPoolId,
28+
'Username' => $payload->username,
29+
]);
30+
31+
$result = AdminGetUserResult::createForAws($awsResult);
32+
33+
Log::debug(get_class($result), json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR));
34+
35+
return $result;
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoIdentityProvider\AdminGetUser;
6+
7+
use Carbon\Carbon;
8+
use Illuminate\Support\Str;
9+
10+
final class MockAdminGetUser implements AdminGetUser
11+
{
12+
/**
13+
* @param AdminGetUserPayload $payload
14+
* @return AdminGetUserResult
15+
*/
16+
public function execute(AdminGetUserPayload $payload): AdminGetUserResult
17+
{
18+
return AdminGetUserResult::createForMock([
19+
'Attributes' => [
20+
[
21+
'Name' => 'sub',
22+
'Value' => (string) Str::uuid(),
23+
],
24+
],
25+
'Enabled' => true,
26+
'MFAOptions' => [],
27+
'UserCreateDate' => Carbon::now(),
28+
'UserLastModifiedDate' => Carbon::now(),
29+
'UserStatus' => 'FORCE_CHANGE_PASSWORD',
30+
'Username' => 'mock-user',
31+
]);
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoType;
6+
7+
enum UserMFASetting: string
8+
{
9+
case SMS_MFA = 'SMS_MFA';
10+
case SOFTWARE_TOKEN_MFA = 'SOFTWARE_TOKEN_MFA'; // TOTP
11+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoType;
6+
7+
final readonly class UserMFASettingList
8+
{
9+
private function __construct(public array $attributes)
10+
{
11+
}
12+
13+
/**
14+
* @param array $attributes
15+
* @return static
16+
*/
17+
public static function create(array $attributes): self
18+
{
19+
return new self(array_map(static fn($attribute) => UserMFASetting::from($attribute), $attributes));
20+
}
21+
22+
/**
23+
* 多要素認証登録済みか
24+
*
25+
* @return bool
26+
*/
27+
public function enabledMfa(): bool
28+
{
29+
return $this->attributes === [];
30+
}
31+
32+
/**
33+
* TOTP認証が有効か
34+
*
35+
* @return bool
36+
*/
37+
public function enabledTotp(): bool
38+
{
39+
return in_array(UserMFASetting::SOFTWARE_TOKEN_MFA, $this->attributes, true);
40+
}
41+
42+
/**
43+
* SMS認証が有効か
44+
*
45+
* @return bool
46+
*/
47+
public function enabledSms(): bool
48+
{
49+
return in_array(UserMFASetting::SMS_MFA, $this->attributes, true);
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Aws\CognitoType;
6+
7+
enum UserStatus: string
8+
{
9+
case UNCONFIRMED = 'UNCONFIRMED'; // ユーザーは作成されましたが、確認されていません。
10+
case CONFIRMED = 'CONFIRMED'; // ユーザーが確認されました。
11+
case ARCHIVED = 'ARCHIVED'; // ユーザーはもうアクティブではありません。
12+
case UNKNOWN = 'UNKNOWN'; // ユーザーのステータスが不明です。
13+
case RESET_REQUIRED = 'RESET_REQUIRED'; // ユーザーは確認されていますが、ユーザーはサインインする前にコードを要求し、パスワードをリセットする必要があります。
14+
case FORCE_CHANGE_PASSWORD = 'FORCE_CHANGE_PASSWORD'; // ユーザーは確認され、一時パスワードを使用してサインインできますが、最初のサインインでは、他の操作を行う前にパスワードを新しい値に変更する必要があります。
15+
}

0 commit comments

Comments
 (0)