-
Notifications
You must be signed in to change notification settings - Fork 42
/
cookies.go
53 lines (48 loc) · 1.48 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
package serve
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"strconv"
"github.com/flimzy/kivik/authdb"
"github.com/pkg/errors"
)
// CreateAuthToken hashes a user name, salt, timestamp, and the server secret
// into an authentication token.
func (s *Service) CreateAuthToken(ctx context.Context, name, salt string, time int64) (string, error) {
secret := s.getAuthSecret(ctx)
sessionData := fmt.Sprintf("%s:%X", name, time)
h := hmac.New(sha1.New, []byte(secret+salt))
h.Write([]byte(sessionData))
hashData := string(h.Sum(nil))
return base64.RawURLEncoding.EncodeToString([]byte(sessionData + ":" + hashData)), nil
}
// ValidateCookie validates a cookie against a user context.
func (s *Service) ValidateCookie(ctx context.Context, user *authdb.UserContext, cookie string) (bool, error) {
name, t, err := DecodeCookie(cookie)
if err != nil {
return false, err
}
token, err := s.CreateAuthToken(ctx, name, user.Salt, t)
if err != nil {
return false, err
}
return token == cookie, nil
}
// DecodeCookie decodes a Base64-encoded cookie, and returns its component
// parts.
func DecodeCookie(cookie string) (name string, created int64, err error) {
data, err := base64.RawURLEncoding.DecodeString(cookie)
if err != nil {
return "", 0, err
}
parts := bytes.SplitN(data, []byte(":"), 3)
t, err := strconv.ParseInt(string(parts[1]), 16, 64)
if err != nil {
return "", 0, errors.Wrap(err, "invalid timestamp")
}
return string(parts[0]), t, nil
}