This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Taliesin Millhouse
committed
Aug 15, 2021
1 parent
6836747
commit 392cc95
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types" | ||
"github.com/gofor-little/xerror" | ||
) | ||
|
||
// SetPassword sets a password for a user that has a requirement for their password to be changed. The session parameter | ||
// can be obtained from the output.Session return value of auth.SignIn. | ||
// | ||
// - Use auth.ForgotPassword if the user doesn't know their password. | ||
// | ||
// - Use auth.ChangePassword and auth.ChangePasswordConfirm to update a user's password that doesn't require resetting. | ||
func SetPassword(ctx context.Context, session string, emailAddress string, password string) (*cognitoidentityprovider.RespondToAuthChallengeOutput, error) { | ||
output, err := CognitoClient.RespondToAuthChallenge(ctx, &cognitoidentityprovider.RespondToAuthChallengeInput{ | ||
ChallengeName: types.ChallengeNameTypeNewPasswordRequired, | ||
ClientId: aws.String(CognitoClientID), | ||
ChallengeResponses: map[string]string{ | ||
"NEW_PASSWORD": password, | ||
"USERNAME": emailAddress, | ||
}, | ||
Session: aws.String(session), | ||
}) | ||
if err != nil { | ||
return nil, xerror.Wrap("failed to set password", err) | ||
} | ||
|
||
return output, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
auth "github.com/gofor-little/aws-auth" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetPassword(t *testing.T) { | ||
setup(t) | ||
defer teardown(t) | ||
|
||
testCases := []struct { | ||
emailAddress string | ||
password string | ||
}{ | ||
{"john@example.com", "test-Password1234!!"}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("TestSignIn_%d", i), func(t *testing.T) { | ||
_, err := auth.CognitoClient.AdminCreateUser(context.Background(), &cognitoidentityprovider.AdminCreateUserInput{ | ||
UserPoolId: aws.String(auth.CognitoUserPoolID), | ||
Username: aws.String(tc.emailAddress), | ||
TemporaryPassword: aws.String(tc.password), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Sign in so we can get a session to set a new password. | ||
output, err := auth.SignIn(context.Background(), tc.emailAddress, tc.password) | ||
require.NoError(t, err) | ||
|
||
_, err = auth.SetPassword(context.Background(), *output.Session, tc.emailAddress, tc.password) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |