forked from piotrkowalczuk/charon
/
password_recovery.go
160 lines (132 loc) · 5.35 KB
/
password_recovery.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
package web
import (
"database/sql"
"net/http"
"strconv"
"github.com/Sirupsen/logrus"
"github.com/go-soa/charon/controller/web/request"
"github.com/go-soa/charon/lib"
"github.com/go-soa/charon/lib/routing"
"golang.org/x/net/context"
)
// PasswordRecoveryIndex ...
func (h *Handler) PasswordRecoveryIndex(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
return h.renderTemplate(rw, ctx)
}
// PasswordRecoverySuccess ...
func (h *Handler) PasswordRecoverySuccess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"message": "web.password_recovery.success_please_check_email",
})
}
// PasswordRecoveryProcess ...
func (h *Handler) PasswordRecoveryProcess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
r.ParseForm()
validationErrorBuilder := lib.NewValidationErrorBuilder()
passwordRecoveryRequest := request.NewPasswordRecoveryRequestFromForm(r.Form)
passwordRecoveryRequest.Validate(validationErrorBuilder)
logger := h.Container.Logger.WithFields(logrus.Fields{
"email": passwordRecoveryRequest.Email,
})
if validationErrorBuilder.HasErrors() {
rw.WriteHeader(http.StatusBadRequest)
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"validationErrors": validationErrorBuilder.Errors(),
"request": passwordRecoveryRequest,
})
}
err := h.Container.PasswordRecoverer.Start(passwordRecoveryRequest.Email)
if err != nil {
switch err {
case lib.ErrUserNotFound, sql.ErrNoRows:
logger.Debug("User does not exists. Password cannot be recovered, user will get fake response")
case lib.ErrPasswordRecovererUserIsNotActive:
logger.Debug("User is not active. Password cannot be recovered, user will get fake response")
case lib.ErrPasswordRecovererUserIsNotConfirmed:
logger.Debug("User is not confirmed. Password cannot be recovered, user will get fake response")
default:
return h.renderTemplate500(rw, ctx, err)
}
}
h.redirect(rw, r, "password_recovery_success", http.StatusFound)
return ctx
}
// PasswordRecoveryConfirmationIndex ...
func (h *Handler) PasswordRecoveryConfirmationIndex(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
var ok bool
var confirmationToken string
var userID string
if confirmationToken, ok = routing.ParamFromContext(ctx, "confirmationToken"); !ok {
h.Container.Logger.Debug("confirmation token param is missing")
return h.renderTemplate400(rw, ctx)
}
if userID, ok = routing.ParamFromContext(ctx, "userId"); !ok {
h.Container.Logger.Debug("user id param is missing")
return h.renderTemplate400(rw, ctx)
}
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"confirmation_token": confirmationToken,
"user_id": userID,
})
}
// PasswordRecoveryConfirmationProcess ...
func (h *Handler) PasswordRecoveryConfirmationProcess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
r.ParseForm()
var ok bool
var confirmationToken string
var userIDParam string
if confirmationToken, ok = routing.ParamFromContext(ctx, "confirmationToken"); !ok {
h.Container.Logger.Debug("Confirmation token param is missing.")
return h.renderTemplate400(rw, ctx)
}
if userIDParam, ok = routing.ParamFromContext(ctx, "userId"); !ok {
h.Container.Logger.Debug("User id param is missing.")
return h.renderTemplate400(rw, ctx)
}
userID, err := strconv.ParseInt(userIDParam, 10, 64)
if err != nil {
h.Container.Logger.Debug("User ID param wrong type.")
return h.renderTemplate400(rw, ctx)
}
logger := h.Container.Logger.WithFields(logrus.Fields{
"user_id": userID,
"confirmation_token": confirmationToken,
})
validationErrorBuilder := lib.NewValidationErrorBuilder()
passwordRecoveryConfirmationRequest := request.NewPasswordRecoveryConfirmationRequestFromForm(r.Form)
passwordRecoveryConfirmationRequest.Validate(validationErrorBuilder)
if validationErrorBuilder.HasErrors() {
rw.WriteHeader(http.StatusBadRequest)
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"validationErrors": validationErrorBuilder.Errors(),
"request": passwordRecoveryConfirmationRequest,
"user_id": userID,
"confirmation_token": confirmationToken,
})
}
err = h.Container.PasswordRecoverer.Finalize(
userID,
confirmationToken,
passwordRecoveryConfirmationRequest.Password,
)
if err != nil {
switch err {
case lib.ErrUserNotFound, sql.ErrNoRows:
logger.Debug("User or PasswordRecovery does not exists. Password cannot be changed, user will get fake response.")
case lib.ErrPasswordRecovererUserIsNotActive:
logger.Debug("User is not active. Password cannot be recovered, user will get fake response")
case lib.ErrPasswordRecovererUserIsNotConfirmed:
logger.Debug("User is not confirmed. Password cannot be recovered, user will get fake response")
default:
return h.renderTemplate500(rw, ctx, err)
}
}
h.redirect(rw, r, "password_recovery_confirmation_success", http.StatusFound)
return ctx
}
// PasswordRecoveryConfirmationSuccess ...
func (h *Handler) PasswordRecoveryConfirmationSuccess(ctx context.Context, rw http.ResponseWriter, r *http.Request) context.Context {
return h.renderTemplateWithData(rw, ctx, map[string]interface{}{
"message": "web.password_recovery.password_changed",
})
}