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 12, 2021
1 parent
efa2bf7
commit 6836747
Showing
11 changed files
with
196 additions
and
23 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,26 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
"github.com/gofor-little/xerror" | ||
) | ||
|
||
// ChangePassword changes a user's password. | ||
func ChangePassword(ctx context.Context, accessToken string, oldPassword string, newPassword string) error { | ||
if err := checkPackage(); err != nil { | ||
return xerror.Wrap("checkPackage call failed", err) | ||
} | ||
|
||
if _, err := CognitoClient.ChangePassword(ctx, &cognitoidentityprovider.ChangePasswordInput{ | ||
AccessToken: aws.String(accessToken), | ||
PreviousPassword: aws.String(oldPassword), | ||
ProposedPassword: aws.String(newPassword), | ||
}); err != nil { | ||
return xerror.Wrap("failed to change password", err) | ||
} | ||
|
||
return 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,44 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
"github.com/stretchr/testify/require" | ||
|
||
auth "github.com/gofor-little/aws-auth" | ||
) | ||
|
||
func TestChangePassword(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("TestChangePassword%d", i), func(t *testing.T) { | ||
_, err := auth.SignUp(context.Background(), tc.emailAddress, tc.password) | ||
require.NoError(t, err) | ||
|
||
// Confirm the user without the use of a confirmation code so we can test the sign in. | ||
_, err = auth.CognitoClient.AdminConfirmSignUp(context.Background(), &cognitoidentityprovider.AdminConfirmSignUpInput{ | ||
UserPoolId: aws.String(auth.CognitoUserPoolID), | ||
Username: aws.String(tc.emailAddress), | ||
}) | ||
require.NoError(t, err) | ||
|
||
output, err := auth.SignIn(context.Background(), tc.emailAddress, tc.password) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, auth.ChangePassword(context.Background(), *output.AuthenticationResult.AccessToken, tc.password, fmt.Sprintf("new-%s", tc.password))) | ||
}) | ||
} | ||
} |
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,36 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider" | ||
"github.com/gofor-little/xerror" | ||
) | ||
|
||
// ForgotPassword will initiate a forgot password request. | ||
func ForgotPassword(ctx context.Context, emailAddress string) (*cognitoidentityprovider.ForgotPasswordOutput, error) { | ||
output, err := CognitoClient.ForgotPassword(ctx, &cognitoidentityprovider.ForgotPasswordInput{ | ||
ClientId: aws.String(CognitoClientID), | ||
Username: aws.String(emailAddress), | ||
}) | ||
if err != nil { | ||
return nil, xerror.Wrap("failed to send forgot password request", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
// ForgotPasswordConfirm will confirm a forgot password request. | ||
func ForgotPasswordConfirm(ctx context.Context, confirmationCode string, emailAddress string, newPassword string) error { | ||
if _, err := CognitoClient.ConfirmForgotPassword(ctx, &cognitoidentityprovider.ConfirmForgotPasswordInput{ | ||
ClientId: aws.String(CognitoClientID), | ||
ConfirmationCode: aws.String(confirmationCode), | ||
Password: aws.String(newPassword), | ||
Username: aws.String(emailAddress), | ||
}); err != nil { | ||
return xerror.Wrap("failed to send forgot password confirmation request", err) | ||
} | ||
|
||
return 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,56 @@ | ||
package auth_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"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/stretchr/testify/require" | ||
|
||
auth "github.com/gofor-little/aws-auth" | ||
) | ||
|
||
func TestForgotPassword(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("TestForgotPassword%d", i), func(t *testing.T) { | ||
_, err := auth.SignUp(context.Background(), tc.emailAddress, tc.password) | ||
require.NoError(t, err) | ||
|
||
// Confirm the user without the use of a confirmation code so we can test the sign in. | ||
_, err = auth.CognitoClient.AdminConfirmSignUp(context.Background(), &cognitoidentityprovider.AdminConfirmSignUpInput{ | ||
UserPoolId: aws.String(auth.CognitoUserPoolID), | ||
Username: aws.String(tc.emailAddress), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Set the email verified to true so we can use it in the forgot password request. | ||
_, err = auth.CognitoClient.AdminUpdateUserAttributes(context.Background(), &cognitoidentityprovider.AdminUpdateUserAttributesInput{ | ||
UserAttributes: []types.AttributeType{ | ||
{ | ||
Name: aws.String("email_verified"), | ||
Value: aws.String("true"), | ||
}, | ||
}, | ||
UserPoolId: aws.String(auth.CognitoUserPoolID), | ||
Username: aws.String(tc.emailAddress), | ||
}) | ||
require.NoError(t, err) | ||
|
||
_, err = auth.ForgotPassword(context.Background(), tc.emailAddress) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
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