Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
774d5615
Commit
774d5615
authored
2 years ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Update ConfirmPasswordReset to be able to handle both forgot password and user confirmation
parent
60a07523
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
cognito/cognito.go
+48
-1
48 additions, 1 deletion
cognito/cognito.go
errors/errors.go
+11
-0
11 additions, 0 deletions
errors/errors.go
with
59 additions
and
1 deletion
cognito/cognito.go
+
48
−
1
View file @
774d5615
...
@@ -2,6 +2,8 @@ package cognito
...
@@ -2,6 +2,8 @@ package cognito
import
(
import
(
"fmt"
"fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
"math/rand"
"math/rand"
"strings"
"strings"
...
@@ -90,7 +92,7 @@ func SetUserPassword(pool string, username string, password string) (*cognitoide
...
@@ -90,7 +92,7 @@ func SetUserPassword(pool string, username string, password string) (*cognitoide
return
output
,
err
return
output
,
err
}
}
func
C
onfirmPassword
Reset
(
appClientID
string
,
username
string
,
password
string
,
confirmationCode
string
)
(
*
cognitoidentityprovider
.
ConfirmForgotPasswordOutput
,
error
)
{
func
c
onfirm
Forgot
Password
(
appClientID
string
,
username
string
,
password
string
,
confirmationCode
string
)
(
*
cognitoidentityprovider
.
ConfirmForgotPasswordOutput
,
error
)
{
input
:=
cognitoidentityprovider
.
ConfirmForgotPasswordInput
{
input
:=
cognitoidentityprovider
.
ConfirmForgotPasswordInput
{
ClientId
:
&
appClientID
,
ClientId
:
&
appClientID
,
ConfirmationCode
:
&
confirmationCode
,
ConfirmationCode
:
&
confirmationCode
,
...
@@ -102,6 +104,51 @@ func ConfirmPasswordReset(appClientID string, username string, password string,
...
@@ -102,6 +104,51 @@ func ConfirmPasswordReset(appClientID string, username string, password string,
return
output
,
err
return
output
,
err
}
}
func
confirmPasswordReset
(
appClientID
string
,
username
string
,
password
string
,
initiateAuthOutput
*
cognitoidentityprovider
.
InitiateAuthOutput
)
(
*
cognitoidentityprovider
.
RespondToAuthChallengeOutput
,
error
)
{
// Respond to the Auth challenge to change the user's password
authChallengeParameters
:=
map
[
string
]
*
string
{
"USERNAME"
:
utils
.
PointerValue
(
username
),
"NEW_PASSWORD"
:
utils
.
PointerValue
(
password
),
}
respondToAuthChallengeInput
:=
cognitoidentityprovider
.
RespondToAuthChallengeInput
{
ChallengeName
:
initiateAuthOutput
.
ChallengeName
,
ChallengeResponses
:
authChallengeParameters
,
ClientId
:
&
appClientID
,
Session
:
initiateAuthOutput
.
Session
,
}
output
,
err
:=
CognitoService
.
RespondToAuthChallenge
(
&
respondToAuthChallengeInput
)
logs
.
Info
(
"output"
,
output
)
return
output
,
err
}
// ConfirmPasswordReset initiates a Cognito auth for the user, and based on the output either updates the user's password,
// or performs a forgot password confirmation.
func
ConfirmPasswordReset
(
appClientID
string
,
username
string
,
password
string
,
confirmationCode
string
)
(
interface
{},
error
)
{
// Initiate an auth for the user to see if a password reset or
authParameters
:=
map
[
string
]
*
string
{
"USERNAME"
:
utils
.
PointerValue
(
username
),
"PASSWORD"
:
utils
.
PointerValue
(
confirmationCode
),
}
initiateAuthInput
:=
cognitoidentityprovider
.
InitiateAuthInput
{
AuthFlow
:
utils
.
PointerValue
(
cognitoidentityprovider
.
ExplicitAuthFlowsTypeUserPasswordAuth
),
AuthParameters
:
authParameters
,
ClientId
:
&
appClientID
,
}
res
,
err
:=
CognitoService
.
InitiateAuth
(
&
initiateAuthInput
)
if
err
!=
nil
{
if
errors
.
AWSErrorExceptionCode
(
err
)
==
cognitoidentityprovider
.
ErrCodePasswordResetRequiredException
{
// Not a user verification - perform forgot password confirmation
return
confirmForgotPassword
(
appClientID
,
username
,
password
,
confirmationCode
)
}
return
nil
,
err
}
if
utils
.
Unwrap
(
res
.
ChallengeName
)
==
cognitoidentityprovider
.
ChallengeNameTypeNewPasswordRequired
{
return
confirmPasswordReset
(
appClientID
,
username
,
password
,
res
)
}
return
nil
,
errors
.
New
(
"User state not correct for confirmation. Please contact support."
)
}
// FOR API LOGS
// FOR API LOGS
func
DetermineAuthType
(
identity
events
.
APIGatewayRequestIdentity
)
*
string
{
func
DetermineAuthType
(
identity
events
.
APIGatewayRequestIdentity
)
*
string
{
...
...
This diff is collapsed.
Click to expand it.
errors/errors.go
+
11
−
0
View file @
774d5615
...
@@ -112,6 +112,17 @@ func HTTPWithError(code int, err error) error {
...
@@ -112,6 +112,17 @@ func HTTPWithError(code int, err error) error {
return
wrappedErr
return
wrappedErr
}
}
func
AWSErrorExceptionCode
(
err
error
)
string
{
if
err
==
nil
{
return
""
}
if
awsError
,
ok
:=
err
.
(
awserr
.
Error
);
ok
{
return
awsError
.
Code
()
}
return
""
}
func
AWSErrorWithoutExceptionCode
(
err
error
)
error
{
func
AWSErrorWithoutExceptionCode
(
err
error
)
error
{
if
err
==
nil
{
if
err
==
nil
{
return
nil
return
nil
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment