-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_session.go
199 lines (170 loc) · 4.35 KB
/
user_session.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
package model
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"strconv"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/sha3"
)
// Envelope with signed user's data.
type Envelope struct {
uuid []byte
expires time.Time
payload []byte
data []byte
signature []byte
}
// NewEnvelope constructor.
func NewEnvelope(
secret string,
useruuid string,
payload string,
expires time.Time,
) (*Envelope, error) {
// validate input
if len(secret) < 1 {
return nil, errors.New("secret shouldn't be empty")
}
if len(payload) > 255 {
return nil, errors.New("payload should be shorter then 256")
}
if _, err := uuid.Parse(useruuid); err != nil {
return nil, fmt.Errorf("bad user uuid: %q", useruuid)
}
uuidLen := 36
expiresLen := 12
payloadLen := len([]byte(payload))
b := make([]byte, uuidLen+expiresLen+1+payloadLen) // 1 byte for payloadLen
// copy uuid bytes to b
idx := copy(b, []byte(useruuid))
// copy expires bytes to b
seconds := []byte(fmt.Sprintf("%012d", expires.Unix()))
idx += copy(b[idx:], seconds)
// copy payload bytes to b
b[idx] = byte(payloadLen)
idx++
idx += copy(b[idx:], []byte(payload))
// sign with secret
signature := sign(b, []byte(secret))
signatureLen := len(signature)
bSigned := make([]byte, len(b)+1+signatureLen)
idx = copy(bSigned, b)
bSigned[idx] = byte(signatureLen)
idx++
copy(bSigned[idx:], signature)
envelope := &Envelope{
uuid: []byte(useruuid),
expires: expires,
payload: []byte(payload),
data: b,
signature: signature,
}
return envelope, nil
}
// ParseEnvelope Envelope from string.
func ParseEnvelope(s string) (*Envelope, error) {
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return nil, err
}
// parse uuid
idx := 0
uuidLen := 36
if len(b) < uuidLen {
return nil, errors.New("bad input string")
}
useruuid := b[idx : idx+uuidLen]
if _, err := uuid.Parse(string(useruuid)); err != nil {
return nil, fmt.Errorf("bad uuid: %q", string(useruuid))
}
idx += uuidLen
// parse expires
expiresLen := 12
if len(b) < (idx + expiresLen) {
return nil, errors.New("bad input string")
}
seconds, err := strconv.ParseInt(string(b[idx:idx+expiresLen]), 10, 64)
if err != nil {
return nil, errors.New("bad input string")
}
expires := time.Unix(int64(seconds), 0)
if expires.Before(time.Now().UTC()) {
return nil, errors.New("expired")
}
idx += expiresLen
// parse payload
if len(b) < (idx + 1) {
return nil, errors.New("bad input string")
}
payloadLen := int(b[idx])
idx++
if len(b) < (idx + payloadLen) {
return nil, errors.New("bad input string")
}
payload := b[idx : idx+payloadLen]
idx += payloadLen
// parse signature
if len(b) < (idx + 1) {
return nil, errors.New("bad input string")
}
signatureLen := int(b[idx])
idx++
if len(b) < (idx + signatureLen) {
return nil, errors.New("bad input string")
}
signature := b[idx : idx+signatureLen]
idx += signatureLen
b = make([]byte, uuidLen+expiresLen+1+payloadLen) // 1 byte for payloadLen
idx = copy(b, useruuid)
secondsBytes := []byte(fmt.Sprintf("%012d", expires.Unix()))
idx += copy(b[idx:], secondsBytes)
b[idx] = byte(payloadLen)
idx++
copy(b[idx:], payload)
envelope := &Envelope{
uuid: useruuid,
expires: expires,
payload: payload,
data: b,
signature: signature,
}
return envelope, nil
}
// String to apply Stringer interface.
func (envelope *Envelope) String() string {
b := make([]byte, len(envelope.data)+1+len(envelope.signature))
idx := copy(b, envelope.data)
b[idx] = byte(len(envelope.signature))
idx++
copy(b[idx:], envelope.signature)
return base64.URLEncoding.EncodeToString(b)
}
// UUID value.
func (envelope *Envelope) UUID() string {
return string(envelope.uuid)
}
// Payload value.
func (envelope *Envelope) Payload() string {
return string(envelope.payload)
}
// Expires time.
func (envelope *Envelope) Expires() time.Time {
return envelope.expires
}
// CheckSignature validates Envelope signature.
func (envelope *Envelope) CheckSignature(secret string) bool {
signature := sign(envelope.data, []byte(secret))
return bytes.Compare(signature, envelope.signature) == 0
}
// sign signs data using key by calculating sha3-512 hash
// of their concatenation. Returns base64 url encoded string.
func sign(data, key []byte) []byte {
b := make([]byte, len(data)+len(key))
idx := copy(b, data)
copy(b[idx:], key)
h := sha3.Sum512(b)
return h[:]
}