Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
feat: set password
Browse files Browse the repository at this point in the history
  • Loading branch information
Taliesin Millhouse committed Aug 15, 2021
1 parent 6836747 commit 392cc95
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.3.0 - 2021-08-15
### Added
* Added ```SetPassword``` function.

### Changed
* Improved comments on ```ChangePassword``` and ```ForgotPassword``` functions.

## v0.2.0 - 2021-08-12
### Added
* Added ```ChangePassword```, ```ForgotPassword``` and ```ForgotPasswordConfirmation``` functions.
Expand Down
4 changes: 4 additions & 0 deletions change_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

// ChangePassword changes a user's password.
//
// - Use auth.ForgotPassword if the user doesn't know their password.
//
// - Use auth.SetPassword if the user has a requirement for their password to be reset.
func ChangePassword(ctx context.Context, accessToken string, oldPassword string, newPassword string) error {
if err := checkPackage(); err != nil {
return xerror.Wrap("checkPackage call failed", err)
Expand Down
4 changes: 4 additions & 0 deletions forgot_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

// ForgotPassword will initiate a forgot password request.
//
// - Use auth.ChangePassword and auth.ChangePasswordConfirm to update a user's password that doesn't require resetting.
//
// - Use auth.SetPassword if the user has a requirement for their password to be reset.
func ForgotPassword(ctx context.Context, emailAddress string) (*cognitoidentityprovider.ForgotPasswordOutput, error) {
output, err := CognitoClient.ForgotPassword(ctx, &cognitoidentityprovider.ForgotPasswordInput{
ClientId: aws.String(CognitoClientID),
Expand Down
33 changes: 33 additions & 0 deletions set_password.go
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
}
42 changes: 42 additions & 0 deletions set_password_test.go
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)
})
}
}

0 comments on commit 392cc95

Please sign in to comment.