-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignedcookie.go
203 lines (163 loc) · 4.82 KB
/
signedcookie.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
196
197
198
199
200
201
202
203
/*
This implements signed cookies in the same way as [gleam](https://github.com/gleam-lang/crypto/blob/v1.3.0/src/gleam/crypto.gleam#L75)
and elixir-plug in order to be easily compatible with those libraries.
Additionally, the payload of the cookie is JSON, allowing for multiple values to
be stored in a single cookie in a format easily read by other languages.
*/
package signedcookie
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type CookieValues = map[string]interface{}
type CookieOptions struct {
MaxAge int
Domain string
Path string
HttpOnly bool
Secure bool
SameSite http.SameSite
}
type SignedCookie struct {
secrets []string
CookieOptions CookieOptions
}
type CookieModifier func(cookie *http.Cookie)
var defaultCookieOptions = CookieOptions{
MaxAge: 86400,
Domain: "",
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
// Returns a new SignedCookie with the given secrets. The first secret should be the
// current key, and any others should be old keys for key rotation.
func New(secrets ...string) SignedCookie {
return SignedCookie{
secrets: secrets,
CookieOptions: CookieOptions{
MaxAge: defaultCookieOptions.MaxAge,
Domain: defaultCookieOptions.Domain,
Path: defaultCookieOptions.Path,
HttpOnly: defaultCookieOptions.HttpOnly,
Secure: defaultCookieOptions.Secure,
SameSite: defaultCookieOptions.SameSite,
},
}
}
func (sc *SignedCookie) GetValues(req *http.Request, writer http.ResponseWriter, name string, modifiers ...CookieModifier) (CookieValues, error) {
values := make(CookieValues)
cookie, err := req.Cookie(name)
if err != nil {
// no cookie found, so return empty values
return values, nil
}
for i, secret := range sc.secrets {
value, err := verifySignedMessage(cookie.Value, secret)
if err != nil {
continue
}
err = json.Unmarshal(value, &values)
if err != nil {
return values, err
}
if i > 0 {
sc.SetValues(writer, name, values, modifiers...)
}
return values, nil
}
return values, fmt.Errorf("No secret could verify the cookie")
}
func (sc *SignedCookie) SetValues(writer http.ResponseWriter, name string, values CookieValues, modifiers ...CookieModifier) error {
payload, err := json.Marshal(values)
if err != nil {
return err
}
cookie := &http.Cookie{
Name: name,
Value: signMessage(payload, sc.secrets[0]),
MaxAge: sc.CookieOptions.MaxAge,
Domain: sc.CookieOptions.Domain,
Path: sc.CookieOptions.Path,
HttpOnly: sc.CookieOptions.HttpOnly,
Secure: sc.CookieOptions.Secure,
SameSite: sc.CookieOptions.SameSite,
}
for _, modifier := range modifiers {
modifier(cookie)
}
http.SetCookie(writer, cookie)
return nil
}
func (sc *SignedCookie) RemoveValues(writer http.ResponseWriter, name string) error {
return sc.SetValues(writer, name, nil, removeModifier)
}
func removeModifier(cookie *http.Cookie) {
cookie.MaxAge = -1
}
func verifySignedMessage(signedMessage, secret string) ([]byte, error) {
parts := strings.Split(signedMessage, ".")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid signed message")
}
text := parts[0] + "." + parts[1]
for i := range parts {
parts[i] = fromFileSafeAlphabet(parts[i])
}
digestType, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return nil, err
}
payload, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
signature, err := base64.StdEncoding.DecodeString(parts[2])
if err != nil {
return nil, err
}
digestStr := string(digestType)
if digestStr != "HS256" {
return nil, fmt.Errorf("Unsupported digest type: %s", digestStr)
}
challenge := hmac.New(sha256.New, []byte(secret))
challenge.Write([]byte(text))
if !hmac.Equal(challenge.Sum(nil), signature) {
return nil, fmt.Errorf("Invalid signature")
}
return []byte(fromFileSafeAlphabet(string(payload))), nil
}
func signMessage(message []byte, secret string) string {
digestType := "HS256"
protected := base64.StdEncoding.EncodeToString([]byte(digestType))
protected = toFileSafeAlphabet(protected)
payload := base64.StdEncoding.EncodeToString(message)
payload = toFileSafeAlphabet(payload) // TODO: this is probably not necessary
text := protected + "." + payload
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(text))
signature := mac.Sum(nil)
signatureStr := base64.StdEncoding.EncodeToString(signature)
signatureStr = toFileSafeAlphabet(signatureStr)
return fmt.Sprintf("%s.%s.%s",
protected,
payload,
signatureStr,
)
}
func toFileSafeAlphabet(str string) string {
str = strings.ReplaceAll(str, "+", "-")
str = strings.ReplaceAll(str, "/", "_")
return str
}
func fromFileSafeAlphabet(str string) string {
str = strings.ReplaceAll(str, "-", "+")
str = strings.ReplaceAll(str, "_", "/")
return str
}