Repository navigation
Expand file tree
/
Copy pathindex.go
More file actions
194 lines (164 loc) · 4.5 KB
/
Copy pathindex.go
File metadata and controls
194 lines (164 loc) · 4.5 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
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
package safetoken
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
var (
errInvalidToken = errors.New("Invalid token")
errTokenExpired = errors.New("Token expired")
errInvalidWindow = errors.New("Invalid time window")
)
type SafeToken struct {
timeWindow map[string]int64
key []byte
}
type Config struct {
TimeWindows map[string]int64
Secret string
}
func New(init Config) (*SafeToken, error) {
if len(init.Secret) < 12 {
return nil, errors.New("Please provide safetoken secret and time window")
}
// Defensive copy so external mutation of init.TimeWindows can't change rules.
timeWindow := make(map[string]int64, len(init.TimeWindows))
for k, v := range init.TimeWindows {
if v <= 0 {
return nil, errors.New("Please provide safetoken secret and time window")
}
timeWindow[k] = v
}
if len(timeWindow) == 0 {
timeWindow["access"] = 3600000 // 1 hour (ms)
timeWindow["refresh"] = 2592000000 // 30 days (ms)
}
return &SafeToken{
timeWindow: timeWindow,
key: []byte(init.Secret),
}, nil
}
func (s *SafeToken) Create(data map[string]any) (string, error) {
if data == nil {
data = make(map[string]any)
}
return createHmacSha256Signature(data, s.key, timestamp())
}
func (s *SafeToken) Verify(token string, timeWindowKeys ...string) (map[string]any, error) {
timeWindowKey := "access"
if len(timeWindowKeys) > 0 && timeWindowKeys[0] != "" {
timeWindowKey = timeWindowKeys[0]
}
window, ok := s.timeWindow[timeWindowKey]
if !ok {
return nil, errInvalidWindow
}
return verifyToken(token, s.key, window)
}
func (s *SafeToken) Decode(token string) (map[string]any, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 || parts[2] == "" {
return nil, errInvalidToken
}
return decodePayload(parts[2])
}
func decodePayload(data string) (map[string]any, error) {
decodedData, err := base64UrlDecode(data)
if err != nil {
return nil, errInvalidToken
}
var payload map[string]any
if err := json.Unmarshal(decodedData, &payload); err != nil {
return nil, errInvalidToken
}
return payload, nil
}
func createHmacSha256Signature(payload map[string]any, key []byte, t string) (string, error) {
payloadBytes, err := json.Marshal(payload)
if err != nil {
return "", err
}
tbuf := base64UrlEncode([]byte(t))
dataToSign := base64UrlEncode(payloadBytes)
h := hmac.New(sha256.New, key)
h.Write([]byte(dataToSign + tbuf))
signature := base64UrlEncode(h.Sum(nil))
return t + "." + signature + "." + dataToSign, nil
}
func verifyToken(token string, key []byte, timeWindow int64) (map[string]any, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return nil, errInvalidToken
}
t, signature, data := parts[0], parts[1], parts[2]
if signature == "" || data == "" || !isHex8(t) {
return nil, errInvalidToken
}
if !isInTime(timeWindow, t) {
return nil, errTokenExpired
}
h := hmac.New(sha256.New, key)
h.Write([]byte(data + base64UrlEncode([]byte(t))))
expectedSignature := base64UrlEncode(h.Sum(nil))
if !timingSafeEqual(signature, expectedSignature) {
return nil, errInvalidToken
}
return decodePayload(data)
}
// isHex8 matches /^[0-9a-fA-F]{8}$/ without regexp overhead.
func isHex8(s string) bool {
if len(s) != 8 {
return false
}
for i := 0; i < 8; i++ {
c := s[i]
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
return false
}
}
return true
}
func isInTime(timeWindow int64, timeCreated string) bool {
// timeWindow > 0 is guaranteed by New().
timeCreatedParsed, err := strconv.ParseInt(timeCreated, 16, 64)
if err != nil || timeCreatedParsed <= 0 {
return false
}
diff := time.Now().UnixMilli() - timeCreatedParsed*1000
// Protect against future-dated token timestamp exploit (allow up to 5s clock skew)
if diff < -5000 {
return false
}
return diff <= timeWindow
}
func timestamp() string {
return fmt.Sprintf("%08x", uint32(time.Now().Unix()))
}
func timingSafeEqual(a, b string) bool {
if len(a) != len(b) {
return false
}
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
}
func base64UrlEncode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
func base64UrlDecode(str string) ([]byte, error) {
data, err := base64.RawURLEncoding.DecodeString(str)
if err != nil {
s := strings.ReplaceAll(str, "-", "+")
s = strings.ReplaceAll(s, "_", "/")
for len(s)%4 != 0 {
s += "="
}
return base64.StdEncoding.DecodeString(s)
}
return data, nil
}