LoginSignup
2
0

More than 5 years have passed since last update.

Amazon Cognito User Poolsの管理者APIをPowerShellから呼び出してユーザーを追加する

Last updated at Posted at 2017-05-11

リファレンス

開発者ガイド
API Reference
AWS Tools for PowerShell Cmdlet Reference(User PoolsはAmazon Cognito Identity Providerの項)

Admin Create User

管理者によるユーザー追加はAdmin Create User、ユーザー自身によるユーザー登録はSign-up。ユーザーがサインインできるようになるまでのフローが違う。ユーザーアカウントのサインアップと確認の図がわかりやすい。

AdminCreateUserではCognitoからユーザーへメッセージ(invitation)が送信される。メッセージにはユーザー名とパスワードが含まれる。指定しなかった場合、パスワードは自動で生成される。

$desiredDeliveryMediums = "EMAIL"
$username = "username"
$userAttributes = @(
        @{Name="email"; Value="example@example.com"})

# AdminCreateUser 
New-CGIPUserAdmin `
    -AccessKey $accesskey `
    -SecretKey $secretKey `
    -Region $region `
    `
    -UserPoolId $userPoolId `
    -ForceAliasCreation $FALSE`
    -MessageAction $NULL`
    `
    -DesiredDeliveryMediums $desiredDeliveryMediums `
    -Username $username `
    -UserAttributes $userAttributes

最初のサインイン

管理者が追加したユーザーは最初のサインインのときにパスワードの変更が必要。

# AdminInitiateAuth
$res1 =
Start-CGIPAuthAdmin `
    -AccessKey $accesskey `
    -SecretKey $secretKey `
    -Region $region `
    `
    -UserPoolId $userPoolId `
    -ClientId $clientId `
    `
    -AuthFlow ADMIN_NO_SRP_AUTH `
    -AuthParameters @{USERNAME=$username; PASSWORD=$password}

#$res1

$challengeResponses = @{ 
    USERNAME=$userName;
    NEW_PASSWORD=$password;
}

# AdminRespondToAuthChallenge
$res2 = 
Send-CGIPAuthChallengeResponseAdmin `
    -AccessKey $accesskey `
    -SecretKey $secretKey `
    -Region $region `
    `
    -UserPoolId $userPoolId `
    -ClientId $clientId `
    `
    -Session $res1.Session `
    -ChallengeName NEW_PASSWORD_REQUIRED `
    -ChallengeResponses $challengeResponses

$res2

※invitaionでのみユーザーにパスワードを知らせるのであれば、サインイン成功でverification成功とみなしてよいのでは。verificationの成功は属性email_verified1の値trueで表す。admin APIではverified属性を直接設定できる。

$challengeResponses = @{ 
    USERNAME=$userName;
    NEW_PASSWORD=$password;
    EMAIL_VERIFIED=$TRUE;
}

確認コードの送信による電話番号、メールアドレスの確認

admin APIではない。ユーザーのAccessTokenで実行する処理。

Get-CGIPUserAttributeVerificationCodeコマンドでCognitoからユーザーに確認コードが送信される。Test-CGIPUserAttributeコマンドが成功の場合、Cognitoによりユーザーの属性email_verified1trueに設定される。

# AdminInitiateAuth
$res1 =
Start-CGIPAuthAdmin `
    -AccessKey $accesskey `
    -SecretKey $secretKey `
    -Region $region `
    `
    -UserPoolId $userPoolId `
    -ClientId $clientId `
    `
    -AuthFlow ADMIN_NO_SRP_AUTH `
    -AuthParameters @{USERNAME=$username; PASSWORD=$password}

$res1

# GetUserAttributeVerificationCode
Get-CGIPUserAttributeVerificationCode `
    -Region $region `
    `
    -AccessToken $res1.AuthenticationResult.AccessToken `
    -AttributeName email

# VerifyUserAttribute
Test-CGIPUserAttribute `
    -Region $region `
    `
    -AccessToken $res1.AuthenticationResult.AccessToken `
    -AttributeName email `
    -code (Read-Host -Prompt 'Code')

Secret Hash

app clientclient secretがある場合には必要になる。

$clientId = ""
$appClientSecret = ""
$userName = ""

$hmac = new-object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::ASCII.GetBytes($appClientSecret)
$signature = $hmac.ComputeHash([Text.Encoding]::ASCII.GetBytes($userName + $clientId))
$secretHash = [Convert]::ToBase64String($signature)


  1. SMSの場合は属性phone_number_verifiedtrueに設定される 

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0