-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
52 lines (49 loc) · 1.56 KB
/
Copy pathtoken.go
File metadata and controls
52 lines (49 loc) · 1.56 KB
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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"strconv"
"strings"
"time"
)
func issueAuthToken(secret []byte, userID string, ttl time.Duration) (string, error) {
if len(secret) == 0 || !validUserID(userID) {
return "", errors.New("invalid token input")
}
expiresAt := time.Now().Add(ttl).Unix()
payload := "v1\n" + userID + "\n" + strconv.FormatInt(expiresAt, 10) + "\n"
mac := hmac.New(sha256.New, secret)
_, _ = mac.Write([]byte(payload))
return base64.RawURLEncoding.EncodeToString([]byte(payload)) + "." +
base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), nil
}
func verifyAuthToken(secret []byte, token string) (string, error) {
parts := strings.Split(token, ".")
if len(parts) != 2 || len(secret) == 0 {
return "", errors.New("invalid token")
}
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return "", errors.New("invalid token payload")
}
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return "", errors.New("invalid token signature")
}
mac := hmac.New(sha256.New, secret)
_, _ = mac.Write(payloadBytes)
if !hmac.Equal(signature, mac.Sum(nil)) {
return "", errors.New("invalid token signature")
}
fields := strings.Split(string(payloadBytes), "\n")
if len(fields) < 4 || fields[0] != "v1" || !validUserID(fields[1]) {
return "", errors.New("invalid token payload")
}
expiresAt, err := strconv.ParseInt(fields[2], 10, 64)
if err != nil || time.Now().Unix() > expiresAt {
return "", errors.New("token expired")
}
return fields[1], nil
}