forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforgot_password_handler.go
57 lines (49 loc) · 1.55 KB
/
forgot_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
package uadmin
import (
"fmt"
"net"
"net/http"
"strings"
)
// forgotPasswordHandler !
func forgotPasswordHandler(u *User, r *http.Request) error {
if u.Email == "" {
return fmt.Errorf("unable to reset password, the user does not have an email")
}
msg := `Dear {NAME},
Have you forgotten your password to access {WEBSITE}. Don't worry we got your back. Please follow the link below to reset your password.
If you want to reset your password, click this link:
<a href="{URL}">{URL}</a>
If you didn't request a password reset, you can ignore this message.
Regards,
{WEBSITE} Support
`
// Check if the host name is in the allowed hosts list
allowed := false
var host string
var allowedHost string
var err error
if host, _, err = net.SplitHostPort(r.Host); err != nil {
host = r.Host
}
for _, v := range strings.Split(AllowedHosts, ",") {
if allowedHost, _, err = net.SplitHostPort(v); err != nil {
allowedHost = v
}
if allowedHost == host {
allowed = true
}
}
if !allowed {
Trail(CRITICAL, "Reset password request for host: (%s) which is not in AllowedHosts settings", host)
return nil
}
urlParts := strings.Split(r.Header.Get("origin"), "://")
link := urlParts[0] + "://" + r.Host + RootURL + "resetpassword?u=" + fmt.Sprint(u.ID) + "&key=" + u.GetOTP()
msg = strings.Replace(msg, "{NAME}", u.String(), -1)
msg = strings.Replace(msg, "{WEBSITE}", SiteName, -1)
msg = strings.Replace(msg, "{URL}", link, -1)
subject := "Password reset for " + SiteName
err = SendEmail([]string{u.Email}, []string{}, []string{}, subject, msg)
return err
}