-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_verification.go
134 lines (118 loc) · 3.31 KB
/
email_verification.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
package main
import (
"context"
"crypto/subtle"
"encoding/hex"
"log"
"strings"
"time"
"git.maharshi.ninja/root/rss2email/structures"
"github.com/nicksnyder/go-i18n/v2/i18n"
"go.mongodb.org/mongo-driver/bson"
"golang.org/x/net/idna"
"golang.org/x/xerrors"
smtp "github.com/xhit/go-simple-mail/v2"
)
func (c *connection) handleEmailVerification(mi *MessageInfo, buf []byte) {
var req structures.VerifyEmailRequest
ok := c.decodeToInterface(buf, &req)
if !ok {
return
}
u := c.getUser(mi)
if u == nil {
return
}
if u.EmailVerified {
c.writeMessage(false, mi, structures.ErrorMessage{
Code: structures.ErrorInvalidInputs,
Message: c.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Errors.AlreadyVerified",
}),
})
return
}
if subtle.ConstantTimeCompare(u.EmailVerificationToken[:], req.Token[:]) != 1 {
c.writeMessage(false, mi, structures.ErrorMessage{
Code: structures.ErrorInvalidInputs,
Message: c.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Errors.InvalidVerificationToken",
}),
})
return
}
c.a.users.UpdateByID(context.TODO(), c.userID, bson.M{
"$set": bson.M{
"email_verification_last": time.Now(),
"email_verified": true,
},
})
c.writeMessage(true, mi, structures.GenericIDResponse{
OK: true,
ID: c.userID,
})
}
func setEmailToAddress(msg *smtp.Email, address string) error {
eParts := strings.Split(address, "@")
if len(eParts) != 2 {
log.Printf("Ignoring invalid e-mail address, address: %#v\n", address)
return xerrors.New("invalid e-mail address")
}
x, err := idna.Punycode.ToASCII(eParts[1])
if err != nil {
log.Printf("Caught error converting from Unicode to Punycode: %s\n", err.Error())
return xerrors.New("incovertible to Punycode")
}
eParts[1] = x
msg.AddTo(strings.Join(eParts, "@"))
return nil
}
func (c *connection) handleEmailRequest(mi *MessageInfo, _ []byte) {
u := c.getUser(mi)
c.sendVerificationEmail(u)
c.writeMessage(true, mi, true)
}
func (c *connection) sendVerificationEmail(user *structures.User) {
if time.Since(user.EmailVerificationLast) <= 6*time.Hour {
log.Printf("Ignoring email verification request since it was sent within the past 6 hours. User ID: %#v\n", user.ID)
return
}
emailContent := c.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Emails.Verification",
TemplateData: map[string]string{
"BaseURL": c.a.config.BaseURL,
"Code": hex.EncodeToString(user.EmailVerificationToken[:]),
},
})
emailSubject := c.localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "Emails.VerificationSubject",
})
msg := smtp.NewMSG()
msg.SetFrom(c.a.config.EmailConfig.FromAddr)
msg.SetSubject(emailSubject)
msg.SetBody(smtp.TextPlain, emailContent)
err := setEmailToAddress(msg, user.Email)
// Above is guaranteed to print, so it's not necessary to log again.
if err != nil {
return
}
conn, err := c.a.emailClient.Connect()
if err != nil {
log.Printf("Error while connecting to SMTP: %s\n", err.Error())
return
}
err = msg.Send(conn)
if err != nil {
log.Printf("Error while sending to SMTP: %s\n", err.Error())
return
}
_, err = c.a.users.UpdateByID(context.TODO(), user.ID, bson.M{
"$set": bson.M{
"email_verification_last": time.Now(),
},
})
if err != nil {
log.Printf("Updating EVL didn't work, user ID: %#v\n", user.ID)
return
}
}