-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_handler.go
186 lines (180 loc) · 6.97 KB
/
password_handler.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
package password
import (
"context"
"encoding/json"
//"io/ioutil"
"net/http"
"strings"
)
type PasswordActionConfig struct {
Resource string `mapstructure:"resource" json:"resource,omitempty" gorm:"column:resource" bson:"resource,omitempty" dynamodbav:"resource,omitempty" firestore:"resource,omitempty"`
Change string `mapstructure:"change" json:"change,omitempty" gorm:"column:change" bson:"change,omitempty" dynamodbav:"change,omitempty" firestore:"change,omitempty"`
Reset string `mapstructure:"reset" json:"reset,omitempty" gorm:"column:reset" bson:"reset,omitempty" dynamodbav:"reset,omitempty" firestore:"reset,omitempty"`
Forgot string `mapstructure:"forgot" json:"forgot,omitempty" gorm:"column:forgot" bson:"forgot,omitempty" dynamodbav:"forgot,omitempty" firestore:"forgot,omitempty"`
Contact string `mapstructure:"contact" json:"contact,omitempty" gorm:"column:contact" bson:"contact,omitempty" dynamodbav:"contact,omitempty" firestore:"contact,omitempty"`
}
type PasswordHandler struct {
PasswordService PasswordService
Error func(context.Context, string, ...map[string]interface{})
Decrypt func(string) (string, error)
Config PasswordActionConfig
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
}
func NewPasswordHandlerWithDecrypter(authenticationService PasswordService, logError func(context.Context, string, ...map[string]interface{}), decrypt func(string) (string, error), writeLog func(context.Context, string, string, bool, string) error, options...PasswordActionConfig) *PasswordHandler {
var c PasswordActionConfig
if len(options) >= 1 {
conf := options[0]
c.Resource = conf.Resource
c.Change = conf.Change
c.Reset = conf.Reset
c.Forgot = conf.Forgot
}
if len(c.Resource) == 0 {
c.Resource = "password"
}
if len(c.Change) == 0 {
c.Change = "change"
}
if len(c.Reset) == 0 {
c.Reset = "reset"
}
if len(c.Forgot) == 0 {
c.Forgot = "forgot"
}
if len(c.Contact) == 0 {
c.Forgot = "contact"
}
return &PasswordHandler{PasswordService: authenticationService, Config: c, Error: logError, Log: writeLog, Decrypt: decrypt}
}
func NewDefaultPasswordHandler(authenticationService PasswordService, logError func(context.Context, string, ...map[string]interface{}), options...func(context.Context, string, string, bool, string) error) *PasswordHandler {
var writeLog func(context.Context, string, string, bool, string) error
if len(options) >= 1 {
writeLog = options[0]
}
return NewPasswordHandlerWithDecrypter(authenticationService, logError, nil, writeLog)
}
func NewPasswordHandler(authenticationService PasswordService, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, options...PasswordActionConfig) *PasswordHandler {
return NewPasswordHandlerWithDecrypter(authenticationService, logError, nil, writeLog, options...)
}
func (h *PasswordHandler) ChangePassword(w http.ResponseWriter, r *http.Request) {
var passwordChange PasswordChange
er1 := json.NewDecoder(r.Body).Decode(&passwordChange)
if er1 != nil {
if h.Error != nil {
msg := "Cannot decode PasswordChange model: " + er1.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "Cannot decode PasswordChange model", http.StatusBadRequest)
return
}
if h.Decrypt != nil {
decodedCurrentPassword, er2 := h.Decrypt(passwordChange.CurrentPassword)
if er2 != nil {
if h.Error != nil {
msg := "cannot decode current password: " + er2.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "cannot decode current password", http.StatusBadRequest)
return
}
decodedNewPassword, er3 := h.Decrypt(passwordChange.Password)
if er3 != nil {
if h.Error != nil {
msg := "cannot decode new password: " + er3.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "cannot decode new password", http.StatusBadRequest)
return
}
passwordChange.CurrentPassword = decodedCurrentPassword
passwordChange.Password = decodedNewPassword
}
result, er4 := h.PasswordService.ChangePassword(r.Context(), passwordChange)
if er4 != nil {
msg := er4.Error()
if h.Error != nil {
h.Error(r.Context(), msg)
}
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Change, false, msg)
} else {
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Change, result > 0, "")
}
}
func (h *PasswordHandler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
email := ""
if r.Method == "GET" {
i := strings.LastIndex(r.RequestURI, "/")
if i >= 0 {
email = r.RequestURI[i+1:]
}
} else {
structMail := make(map[string]string)
structMail[h.Config.Contact] = ""
er1 := json.NewDecoder(r.Body).Decode(&structMail)
email = structMail["contact"]
//b, er1 := ioutil.ReadAll(r.Body)
if er1 != nil {
if h.Error != nil {
msg := "Cannot get the body of 'Forgot Password': " + er1.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "Cannot get the body of 'Forgot Password'", http.StatusBadRequest)
return
}
//email = strings.Trim(string(b), " ")
}
result, er2 := h.PasswordService.ForgotPassword(r.Context(), email)
if er2 != nil {
msg := er2.Error()
if h.Error != nil {
h.Error(r.Context(), msg)
}
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Forgot, false, msg)
} else {
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Forgot, result, "")
}
}
func (h *PasswordHandler) ResetPassword(w http.ResponseWriter, r *http.Request) {
var passwordReset PasswordReset
er1 := json.NewDecoder(r.Body).Decode(&passwordReset)
if er1 != nil {
if h.Error != nil {
msg := "Cannot decode PasswordReset model: " + er1.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "Cannot decode PasswordReset model", http.StatusBadRequest)
return
}
if h.Decrypt != nil {
decodedNewPassword, er2 := h.Decrypt(passwordReset.Password)
if er2 != nil {
if h.Error != nil {
msg := "cannot decode new password: " + er2.Error()
h.Error(r.Context(), msg)
}
http.Error(w, "cannot decode new password", http.StatusBadRequest)
return
}
passwordReset.Password = decodedNewPassword
}
result, er3 := h.PasswordService.ResetPassword(r.Context(), passwordReset)
if er3 != nil {
msg := er3.Error()
if h.Error != nil {
h.Error(r.Context(), msg)
}
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Reset, false, msg)
} else {
respond(w, r, http.StatusOK, result, h.Log, h.Config.Resource, h.Config.Reset, result == 1, "")
}
}
func respond(w http.ResponseWriter, r *http.Request, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
err := json.NewEncoder(w).Encode(result)
if writeLog != nil {
newCtx := context.WithValue(r.Context(), "request", r)
writeLog(newCtx, resource, action, success, desc)
}
return err
}