4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでCognitoのFORCE_CHANGE_PASSWORDをどないかする

Posted at

マネコンからユーザー登録したら...

ステータスがFORCE_CHANGE_PASSWORDに。
Cognito01.PNG

とりあえず初回の認証や。

import boto3

client = boto3.client('cognito-idp')

first_auth_response = client.admin_initiate_auth(
    UserPoolId = "ユーザープールID",
    ClientId = "クライアントID",
    AuthFlow = "ADMIN_NO_SRP_AUTH",
    AuthParameters = {
        "USERNAME": "ユーザー名",
        "PASSWORD": "初回パスワード",
    }
)

レスポンスはだいたいこんな感じ↓(だいぶ省略)。アクセストークンやらIDトークンが返ってんくて困った。
NEW_PASSWORD_REQUIREDやから、どないかしてパスワード変更してやればええんか。

{
    'ChallengeName': 'NEW_PASSWORD_REQUIRED', 
    'Session': '長い文字列', 
    'ChallengeParameters': {
        'USER_ID_FOR_SRP': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 
        'requiredAttributes': '[]', 
        'userAttributes': '{
            "email_verified":"true",
            "email":"xxxx@xxxx.com"}'
    }, 
    'ResponseMetadata': {略}
}

#どないかする
パスワードを変更して認証。

session = first_auth_response["Session"]


auth_challenge_response = client.admin_respond_to_auth_challenge(
    UserPoolId = "ユーザープールID",
    ClientId = "クライアントID",
    ChallengeName="NEW_PASSWORD_REQUIRED",
    ChallengeResponses={
        "NEW_PASSWORD":"新しいパスワード",
        "USERNAME":"ユーザー名"
    },
    Session=session
)

レスポンスはだいたいこんな感じ↓(だいぶ省略)。無事、アクセストークンやらIDトークンが取れて安心やわ。

{
    'ChallengeParameters': {}, 
    'AuthenticationResult': {
        'AccessToken': '...', 
        'ExpiresIn': 3600, 
        'TokenType': 'Bearer', 
        'RefreshToken': '...', 
        'IdToken': '...'
    }, 
    'ResponseMetadata': {略}
}

ステータスもCONFIRMになってたわ。
Cognito02.PNG

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?