/
cognitoauth.go
215 lines (180 loc) · 6.58 KB
/
cognitoauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Auth functionalities using AWS Cognito
package auth
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
cognitoidp "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
cognitoidpiface "github.com/aws/aws-sdk-go/service/cognitoidentityprovider/cognitoidentityprovideriface"
)
type CognitoAuth struct {
// Interface that provides AWS Cognito API methods (client)
Client cognitoidpiface.CognitoIdentityProviderAPI
// AWS Cognito User Pool ID
UserPoolId string
// AWS Cognito User Pool's App Client ID
AppClientId string
// Available methods:
//
// func (self *CognitoAuth) SignUp(username string, password string) (map[string]string, error)
// func (self *CognitoAuth) ResendConfirmationCode(username string) (string, error)
// func (self *CognitoAuth) ConfirmSignUp(username string, confirmationCode string) (string, error)
// func (self *CognitoAuth) SignIn(username string, password string) (map[string]string, error)
// func (self *CognitoAuth) RefreshTokenAuth(refreshToken string) (map[string]string, error)
// func (self *CognitoAuth) ForgetPassword(username string) (string, error)
// func (self *CognitoAuth) ConfirmForgetPassword(username string, confirmationCode string, password string) (string, error)
// func (self *CognitoAuth) ChangePassword(currentPassword string, newPassword string, accessToken string) (string, error)
}
func NewCognitoAuth() CognitoAuth {
return CognitoAuth {
Client: CognitoClient,
UserPoolId: os.Getenv("USER_POOL_ID"),
AppClientId: os.Getenv("APP_CLIENT_ID"),
}
}
// Users Signup by themselves
func (self *CognitoAuth) SignUp(username string, password string) (map[string]string, error) {
signUpInput := &cognitoidp.SignUpInput {
ClientId: aws.String(self.AppClientId),
Username: aws.String(username),
Password: aws.String(password),
UserAttributes: []*cognitoidp.AttributeType{
{
Name: aws.String("email"),
Value: aws.String(username),
},
},
}
response, err := self.Client.SignUp(signUpInput)
if err != nil {
return map[string]string {}, err
} else {
return map[string]string {
"message": fmt.Sprintf(
"User with email %s signed up successfully. Please check your email for confirmation code.",
username,
),
"cognitoUsername": *(response.UserSub),
}, err
}
}
// Resend a verification code to user's `email`
func (self *CognitoAuth) ResendConfirmationCode(username string) (string, error) {
resendConfirmationCodeInput := &cognitoidp.ResendConfirmationCodeInput {
ClientId: aws.String(self.AppClientId),
Username: aws.String(username),
}
_, err := self.Client.ResendConfirmationCode(resendConfirmationCodeInput)
if err != nil {
return "", err
} else {
return fmt.Sprintf(
"A verification code will be resent to email %s if a user exists with this email.", username,
), err
}
}
// Confirm Signup by providing a confirmation email sent to the user email
func (self *CognitoAuth) ConfirmSignUp(username string, confirmationCode string) (string, error) {
confirmSignupInput := &cognitoidp.ConfirmSignUpInput {
ClientId: aws.String(self.AppClientId),
Username: aws.String(username),
ConfirmationCode: aws.String(confirmationCode),
}
_, err := self.Client.ConfirmSignUp(confirmSignupInput)
if err != nil {
return "", err
} else {
return fmt.Sprintf("User with email %s confirmed successfully.", username), err
}
}
// User signin using `username` and `password`
func (self *CognitoAuth) SignIn(username string, password string) (map[string]string, error) {
signinInput := &cognitoidp.InitiateAuthInput {
ClientId : aws.String(self.AppClientId),
AuthFlow: aws.String("USER_PASSWORD_AUTH"),
AuthParameters: map[string]*string {
"USERNAME": aws.String(username),
"PASSWORD": aws.String(password),
},
}
response, err := self.Client.InitiateAuth(signinInput)
if err != nil {
return map[string]string {}, err
} else {
if response.AuthenticationResult != nil {
return map[string]string {
"accessToken": *(response.AuthenticationResult.AccessToken),
"idToken": *(response.AuthenticationResult.IdToken),
"refreshToken": *(response.AuthenticationResult.RefreshToken),
}, err
} else {
return map[string]string {
"session": *(response.Session),
}, err
}
}
}
// Get new `accessToken` and `idToken` using the `refreshToken`
func (self *CognitoAuth) RefreshTokenAuth(refreshToken string) (map[string]string, error) {
refreshTokenAuthInput := &cognitoidp.InitiateAuthInput {
ClientId : aws.String(self.AppClientId),
AuthFlow: aws.String("REFRESH_TOKEN_AUTH"),
AuthParameters: map[string]*string {
"REFRESH_TOKEN": aws.String(refreshToken),
},
}
response, err := self.Client.InitiateAuth(refreshTokenAuthInput)
if err != nil {
return map[string]string {}, err
} else {
return map[string]string {
"accessToken": *(response.AuthenticationResult.AccessToken),
"idToken": *(response.AuthenticationResult.IdToken),
"refreshToken": refreshToken,
}, err
}
}
// Send a verification code to users' `email` to reset password
func (self *CognitoAuth) ForgotPassword(username string) (string, error) {
forgetPasswordInput := &cognitoidp.ForgotPasswordInput {
ClientId: aws.String(self.AppClientId),
Username: aws.String(username),
}
_, err := self.Client.ForgotPassword(forgetPasswordInput)
if err != nil {
return "", err
} else {
return fmt.Sprintf(
"A verification code will be sent to email %s if a user exists with this email.", username,
), err
}
}
// Users can reset password using the verification code sent to their email
func (self *CognitoAuth) ConfirmForgotPassword(username string, confirmationCode string, password string) (string, error) {
confirmForgetPasswordInput := &cognitoidp.ConfirmForgotPasswordInput {
ClientId: aws.String(self.AppClientId),
Username: aws.String(username),
ConfirmationCode: aws.String(confirmationCode),
Password: aws.String(password),
}
_, err := self.Client.ConfirmForgotPassword(confirmForgetPasswordInput)
if err != nil {
return "", err
} else {
return fmt.Sprintf("New password has been set successfully."), err
}
}
// Change password staying signed in
func (self *CognitoAuth) ChangePassword(currentPassword string, newPassword string, accessToken string) (string, error) {
changePasswordInput := &cognitoidp.ChangePasswordInput {
PreviousPassword: aws.String(currentPassword),
ProposedPassword: aws.String(newPassword),
AccessToken: aws.String(accessToken),
}
_, err := self.Client.ChangePassword(changePasswordInput)
if err != nil {
return "", err
} else {
return fmt.Sprintf("Password has been changed successfully."), err
}
}