-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.go
195 lines (165 loc) · 4.21 KB
/
cookies.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
187
188
189
190
191
192
193
194
195
package main
import (
"net/http"
"os"
"strings"
"sync"
"time"
)
type ProviderCookie struct {
EmailListID string
ProviderName ProviderName
OutputIDs []string
RedirectUrl string
CreatedAt time.Time
}
func NewProviderCookie(emailListID string, providerName ProviderName, outputIDs []string, redirectUrl string) *ProviderCookie {
return &ProviderCookie{
EmailListID: emailListID,
ProviderName: providerName,
OutputIDs: outputIDs,
RedirectUrl: redirectUrl,
CreatedAt: time.Now(),
}
}
func ProviderCookieFrom(r *http.Request) (*ProviderCookie, error) {
emailListID, err := CookieNameEmailListID.DecryptFrom(r)
if err != nil {
return nil, err
}
s, err := CookieNameProviderName.DecryptFrom(r)
if err != nil {
return nil, err
}
providerName, err := ToProviderName(s)
if err != nil {
return nil, err
}
outputIDsStr, err := CookieNameOutputIDs.DecryptFrom(r)
if err != nil {
return nil, err
}
outputIDs := strings.Split(outputIDsStr, outputCookieDelim)
redirectUrl, err := CookieNameRedirectURL.DecryptFrom(r)
if err != nil {
return nil, err
}
createdAtStr, _ := CookieNameCreatedAt.DecryptFrom(r)
createdAt, _ := time.Parse(timestampLayout, createdAtStr)
return &ProviderCookie{
EmailListID: emailListID,
ProviderName: providerName,
OutputIDs: outputIDs,
RedirectUrl: redirectUrl,
CreatedAt: createdAt,
}, nil
}
func (pc ProviderCookie) Set(w http.ResponseWriter) error {
if err := CookieNameEmailListID.SetEncrypted(w, pc.EmailListID); err != nil {
return err
}
if err := CookieNameProviderName.SetEncrypted(w, string(pc.ProviderName)); err != nil {
return err
}
if err := CookieNameOutputIDs.SetEncrypted(w, strings.Join(pc.OutputIDs, outputCookieDelim)); err != nil {
return err
}
if err := CookieNameRedirectURL.SetEncrypted(w, pc.RedirectUrl); err != nil {
return err
}
if err := CookieNameCreatedAt.SetEncrypted(w, pc.CreatedAt.Format(timestampFormat)); err != nil {
return err
}
return nil
}
func (cn CookieName) encrypt() (string, error) {
cookieSecret := os.Getenv(EnvCookieSecret)
if cookieSecret == "" {
return "", missingEnv(EnvCookieSecret)
}
return Encrypt(cookieSecret, string(cn))
}
func (cn CookieName) DecryptFrom(r *http.Request) (string, error) {
cookieSecret := os.Getenv(EnvCookieSecret)
if cookieSecret == "" {
return "", missingEnv(EnvCookieSecret)
}
encryptedCookieName, err := cn.encrypt()
if err != nil {
return "", err
}
cookie, err := r.Cookie(encryptedCookieName)
if err != nil {
return "", err
}
return Decrypt(cookieSecret, cookie.Value)
}
func (cn CookieName) SetEncrypted(w http.ResponseWriter, value string) error {
cookieSecret := os.Getenv(EnvCookieSecret)
if cookieSecret == "" {
return missingEnv(EnvCookieSecret)
}
encryptedCookieName, err := cn.encrypt()
if err != nil {
return err
}
encryptedValue, err := Encrypt(cookieSecret, value)
if err != nil {
return err
}
setCookie(w, encryptedCookieName, encryptedValue)
return nil
}
func (pc ProviderCookie) Handle(pr ProviderResult) error {
emailList, err := storage.GetEmailListByID(pc.EmailListID)
if err != nil {
return err
}
userID := emailList.UserID
var wg sync.WaitGroup
wg.Add(len(pc.OutputIDs))
for _, outputID := range pc.OutputIDs {
go func() {
defer wg.Done()
output, err := storage.GetOutputByIDAndUserID(outputID, userID)
if err != nil {
return
}
output.Handle(pr.EmailAddr, pr.Name)
}()
}
cr := SubscriberCreationReq{
EmailListID: pc.EmailListID,
UserID: userID,
SourceProviderName: pc.ProviderName,
Name: pr.Name,
EmailAddr: pr.EmailAddr,
}
_, err = storage.InsertNewSubscriber(cr)
wg.Wait()
return err
}
func setCookie(w http.ResponseWriter, name string, value string) {
cookie := &http.Cookie{
Name: name,
Value: value,
Path: "/",
MaxAge: cookieMaxAge,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
http.SetCookie(w, cookie)
}
func clearCookie(w http.ResponseWriter, name string) {
cookie := &http.Cookie{
Name: name,
Value: "",
Path: "/",
MaxAge: -1, // Expires immediately
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
http.SetCookie(w, cookie)
}