/
completeregistration.go
211 lines (184 loc) · 5.83 KB
/
completeregistration.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
package commands
import (
"bytes"
"crypto/sha256"
"fmt"
"time"
"github.com/Goldwin/ies-pik-cms/pkg/auth/dto"
"github.com/Goldwin/ies-pik-cms/pkg/auth/entities"
. "github.com/Goldwin/ies-pik-cms/pkg/common/commands"
)
const (
CompleteRegistrationErrorAlreadyRegistered CommandErrorCode = 20201
CompleteRegistrationErrorUpdateFailure CommandErrorCode = 20202
CompleteRegistrationErrorAccountIsNotRegistered CommandErrorCode = 20203
CompleteRegistrationErrorFailedToGetAccount CommandErrorCode = 20203
CompleteRegistrationErrorInvalidInput CommandErrorCode = 20204
CompleteRegistrationErrorPasswordMismatch CommandErrorCode = 20205
)
type CompleteRegistrationCommand struct {
Input dto.CompleteRegistrationInput
}
func (cmd CompleteRegistrationCommand) Execute(ctx CommandContext) CommandExecutionResult[dto.AuthData] {
accountResult := cmd.verifyOTPAndCreateAccount(ctx)
if accountResult.Status != ExecutionStatusSuccess {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: accountResult.Error,
}
}
account, err := ctx.AccountRepository().GetAccount(entities.EmailAddress(cmd.Input.Email))
if err != nil {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: CompleteRegistrationErrorFailedToGetAccount,
Message: fmt.Sprintf("Failed to Get Account: %s", err.Error()),
},
}
}
if account == nil {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: CompleteRegistrationErrorAccountIsNotRegistered,
Message: fmt.Sprintf("Account Is Not Registered"),
},
}
}
if len(account.Roles) > 0 {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: CompleteRegistrationErrorAlreadyRegistered,
Message: fmt.Sprintf("Account Already Completed Registration"),
},
}
}
if cmd.Input.FirstName == "" || cmd.Input.LastName == "" {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: CompleteRegistrationErrorInvalidInput,
Message: fmt.Sprintf("First Name and Last Name must be filled"),
},
}
}
account.Roles = []entities.Role{
entities.ChurchMember,
}
account, err = ctx.AccountRepository().UpdateAccount(*account)
if err != nil {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: CompleteRegistrationErrorUpdateFailure,
Message: fmt.Sprintf("Failed to Update Account: %s", err.Error()),
},
}
}
result := SavePasswordCommand{
Email: entities.EmailAddress(cmd.Input.Email),
Password: []byte(cmd.Input.Password),
}.Execute(ctx)
if result.Status != ExecutionStatusSuccess {
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusFailed,
Error: result.Error,
}
}
scopes := make([]string, 0)
for _, role := range account.Roles {
for _, scope := range role.Scopes {
scopes = append(scopes, string(scope))
}
}
return CommandExecutionResult[dto.AuthData]{
Status: ExecutionStatusSuccess,
Result: dto.AuthData{
Email: string(account.Email),
Scopes: scopes,
},
}
}
func (cmd CompleteRegistrationCommand) verifyOTPAndCreateAccount(ctx CommandContext) CommandExecutionResult[*entities.Account] {
otpRepository := ctx.OtpRepository()
accountRepository := ctx.AccountRepository()
otp, err := otpRepository.GetOtp(entities.EmailAddress(cmd.Input.Email))
if err != nil {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorFailedToGetOtp,
Message: fmt.Sprintf("Failed to Get OTP: %s", err.Error()),
},
}
}
if otp == nil {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorOTPNotFound,
Message: "OTP Not Found. Please try again.",
},
}
}
if otp.ExpiredTime.Before(time.Now()) {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorOtpExired,
Message: "OTP Expired. Please try again.",
},
}
}
passwordAndSalt := append([]byte(cmd.Input.OTP), otp.Salt...)
passwordHash := sha256.Sum256(passwordAndSalt)
isMatching := bytes.Equal(passwordHash[:], otp.PasswordHash[:])
if !isMatching {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorWrongOtp,
Message: "Wrong OTP. Please try again.",
},
}
}
err = otpRepository.RemoveOtp(*otp)
if err != nil {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorOTPFailedToInvalidateOTP,
Message: fmt.Sprintf("Failed to invalidate OTP: %s", err.Error()),
},
}
}
err = otpRepository.RemoveOtp(*otp)
account, err := accountRepository.GetAccount(entities.EmailAddress(cmd.Input.Email))
if err != nil {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SigninErrorFailedToGetOtp,
Message: fmt.Sprintf("Error When Requesting Access Token: %s", err.Error()),
},
}
}
if account == nil {
account, err = accountRepository.AddAccount(entities.Account{Email: entities.EmailAddress(cmd.Input.Email)})
if err != nil {
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusFailed,
Error: CommandErrorDetail{
Code: SignInErrorFailedToCreateNewAccount,
Message: fmt.Sprintf("Failed to create new account: %s", err.Error()),
},
}
}
}
return CommandExecutionResult[*entities.Account]{
Status: ExecutionStatusSuccess,
Result: account,
}
}