/
recover.go
167 lines (144 loc) · 5.03 KB
/
recover.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
// Copyright 2022 E99p1ant. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package auth
import (
"os"
"time"
"github.com/flamego/cache"
"github.com/flamego/recaptcha"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thanhpk/randstr"
"github.com/NekoWheel/NekoBox/internal/context"
"github.com/NekoWheel/NekoBox/internal/db"
"github.com/NekoWheel/NekoBox/internal/form"
"github.com/NekoWheel/NekoBox/internal/mail"
)
func ForgotPassword(ctx context.Context) {
ctx.Success("auth/forgot-password")
}
func ForgotPasswordAction(ctx context.Context, f form.ForgotPassword, cache cache.Cache, recaptcha recaptcha.RecaptchaV3) {
if ctx.HasError() {
ctx.Success("auth/forgot-password")
return
}
// Check recaptcha code.
resp, err := recaptcha.Verify(f.Recaptcha, ctx.Request().Request.RemoteAddr)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to check recaptcha")
ctx.SetInternalErrorFlash()
ctx.Redirect("/forgot-password")
return
}
if !resp.Success {
ctx.SetErrorFlash("验证码错误")
ctx.Redirect("/forgot-password")
return
}
user, err := db.Users.GetByEmail(ctx.Request().Context(), f.Email)
if err != nil {
if errors.Is(err, db.ErrUserNotExists) {
ctx.SetErrorFlash("用户邮箱不存在")
} else {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to get user by email")
ctx.SetInternalErrorFlash()
}
ctx.Redirect("/forgot-password")
return
}
emailSentCacheKey := "forgot-password-email-sent:" + user.Email
_, err = cache.Get(ctx.Request().Context(), emailSentCacheKey)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to read password recovery email sent cache")
}
} else {
ctx.SetErrorFlash("邮件发送太频繁,请稍后再试")
ctx.Redirect("/forgot-password")
return
}
code := randstr.String(64)
recoveryCodeCacheKey := "forgot-password-recovery-code:" + code
if err := cache.Set(ctx.Request().Context(), recoveryCodeCacheKey, user.ID, 24*time.Hour); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to set password recovery code cache")
ctx.SetInternalErrorFlash()
ctx.Redirect("/forgot-password")
return
}
if err := mail.SendPasswordRecoveryMail(user.Email, code); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to send password recovery mail")
ctx.SetErrorFlash("邮件发送失败,请稍后再试")
ctx.Redirect("/forgot-password")
return
}
if err := cache.Set(ctx.Request().Context(), emailSentCacheKey, time.Now(), 2*time.Minute); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to set password recovery email cache")
}
ctx.Data["email"] = user.Email
ctx.Success("auth/forgot-password-sent")
}
func checkRecoverPasswordCode(ctx context.Context, cache cache.Cache) (*db.User, bool) {
code := ctx.Query("code")
recoveryCodeCacheKey := "forgot-password-recovery-code:" + code
userIDItf, err := cache.Get(ctx.Request().Context(), recoveryCodeCacheKey)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
ctx.SetErrorFlash("验证码已过期")
} else {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to read password recovery code cache")
ctx.SetInternalErrorFlash()
}
ctx.Redirect("/login")
return nil, false
}
userID, ok := userIDItf.(uint)
if !ok {
logrus.WithContext(ctx.Request().Context()).WithField("user_id_itf", userIDItf).Error("Failed to convert user id interface to uint")
ctx.SetInternalErrorFlash()
ctx.Redirect("/login")
return nil, false
}
user, err := db.Users.GetByID(ctx.Request().Context(), userID)
if err != nil {
ctx.SetErrorFlash("用户不存在")
ctx.Redirect("/login")
return nil, false
}
return user, true
}
func RecoverPassword(ctx context.Context, cache cache.Cache) {
user, ok := checkRecoverPasswordCode(ctx, cache)
if !ok {
return
}
ctx.Data["User"] = user
ctx.Success("auth/password-recovery")
}
func RecoverPasswordAction(ctx context.Context, cache cache.Cache, f form.RecoverPassword) {
user, ok := checkRecoverPasswordCode(ctx, cache)
if !ok {
return
}
if ctx.HasError() {
ctx.Data["User"] = user
ctx.Success("auth/password-recovery")
return
}
code := ctx.Query("code")
recoveryCodeCacheKey := "forgot-password-recovery-code:" + code
if err := cache.Delete(ctx.Request().Context(), recoveryCodeCacheKey); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to delete password recovery code cache")
ctx.SetInternalErrorFlash()
ctx.Redirect("/login")
return
}
if err := db.Users.UpdatePassword(ctx.Request().Context(), user.ID, f.NewPassword); err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to update user password")
ctx.SetInternalErrorFlash()
ctx.Refresh()
return
}
ctx.SetSuccessFlash("密码修改成功")
ctx.Redirect("/login")
}