How to change User Status FORCE_CHANGE_PASSWORD?

前端 未结 12 1930
我在风中等你
我在风中等你 2020-12-07 08:15

Using AWS Cognito, I want to create dummy users for testing purposes.

I then use the AWS Console to create such user, but the user has its status s

12条回答
  •  温柔的废话
    2020-12-07 08:29

    I know It is the same answer, but thought it might help Go developer community. basically it is initiating auth request, get the session and respond to the challenge NEW_PASSWORD_REQUIRED

    func sessionWithDefaultRegion(region string) *session.Session {
        sess := Session.Copy()
        if v := aws.StringValue(sess.Config.Region); len(v) == 0 {
            sess.Config.Region = aws.String(region)
        }
    
        return sess
    }
    
    
    
    func (c *CognitoAppClient) ChangePassword(userName, currentPassword, newPassword string)   error {
    
        sess := sessionWithDefaultRegion(c.Region)
        svc := cognitoidentityprovider.New(sess)
    
        auth, err := svc.AdminInitiateAuth(&cognitoidentityprovider.AdminInitiateAuthInput{
            UserPoolId:aws.String(c.UserPoolID),
            ClientId:aws.String(c.ClientID),
            AuthFlow:aws.String("ADMIN_NO_SRP_AUTH"),
            AuthParameters: map[string]*string{
                "USERNAME": aws.String(userName),
                "PASSWORD": aws.String(currentPassword),
            },
    
        })
    
    
    
        if err != nil {
            return err
        }
    
        request := &cognitoidentityprovider.AdminRespondToAuthChallengeInput{
            ChallengeName: aws.String("NEW_PASSWORD_REQUIRED"),
            ClientId:aws.String(c.ClientID),
            UserPoolId: aws.String(c.UserPoolID),
            ChallengeResponses:map[string]*string{
                "USERNAME":aws.String(userName),
                "NEW_PASSWORD": aws.String(newPassword),
            },
            Session:auth.Session,
        }
    
    
        _, err = svc.AdminRespondToAuthChallenge(request)
    
        return err 
    }
    

    Here's a unit test:

    import (
        "fmt"
        "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
        . "github.com/smartystreets/goconvey/convey"
        "testing"
    )
    
    
    func TestCognitoAppClient_ChangePassword(t *testing.T) {
    
    
        Convey("Testing ChangePassword!", t, func() {
            err := client.ChangePassword("user_name_here", "current_pass", "new_pass")
    
    
    
            Convey("Testing ChangePassword Results!", func() {
                So(err, ShouldBeNil)
    
            })
    
        })
    }
    

提交回复
热议问题