This repository was archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
139 lines (114 loc) · 3.11 KB
/
token.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
package token
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"strconv"
"time"
)
const (
//CurrentVesion is the default version
CurrentVesion = 1
tokenSignLen = 11
)
// Token is generated based on version and key
type Token struct {
key []byte
}
// New return a token object
func New(key []byte) *Token {
return &Token{key: key}
}
// Sign used to generate signatures
func (t *Token) Sign(payload []byte) ([]byte, error) {
m := &message{version: CurrentVesion, createAt: int64(time.Now().Unix()), payload: payload}
data, err := m.MarshalBinary()
if err != nil {
return nil, err
}
mac := hmac.New(sha256.New, t.key)
mac.Write(data)
sign := mac.Sum(nil)
//truncate to 32 byte: https://tools.ietf.org/html/rfc2104#section-5
// we have 11 byte rigth of hmac,so the rest of data is token message
sign = sign[:tokenSignLen]
encodedSign := make([]byte, hex.EncodedLen(len(sign)))
hex.Encode(encodedSign, sign)
var token []byte
token = append(token, data...)
token = append(token, '-')
token = append(token, encodedSign...)
return token, nil
}
// Verify a token
func (t *Token) Verify(sign []byte) error {
encodedSignLen := hex.EncodedLen(tokenSignLen)
if len(sign) < encodedSignLen {
return errors.New("invalid size")
}
s := make([]byte, tokenSignLen)
hex.Decode(s, sign[len(sign)-encodedSignLen:])
meta := sign[:len(sign)-encodedSignLen-1] //counting in the "-"
mac := hmac.New(sha256.New, t.key)
mac.Write(meta)
if !hmac.Equal(mac.Sum(nil)[:tokenSignLen], s) {
return errors.New("token mismatch")
}
return nil
}
// Auth a token and renturn payload
func (t *Token) Auth(sign []byte) ([]byte, error) {
encodedSignLen := hex.EncodedLen(tokenSignLen)
if len(sign) < encodedSignLen {
return nil, errors.New("auth invalid size")
}
s := make([]byte, tokenSignLen)
hex.Decode(s, sign[len(sign)-encodedSignLen:])
meta := sign[:len(sign)-encodedSignLen-1] //counting in the ":"
mac := hmac.New(sha256.New, t.key)
mac.Write(meta)
if !hmac.Equal(mac.Sum(nil)[:tokenSignLen], s) {
return nil, errors.New("token mismatch")
}
var m message
if err := m.UnmarshalBinary(meta); err != nil {
return nil, err
}
return m.payload, nil
}
// message contains the necessary constituent fields for a signature
type message struct {
version int64
createAt int64
payload []byte
}
// MarshalBinary is used to binary code data
func (m *message) MarshalBinary() (data []byte, err error) {
data = append(data, m.payload...)
data = append(data, '-')
data = append(data, []byte(strconv.FormatInt(m.createAt, 10))...)
data = append(data, '-')
data = append(data, []byte(strconv.FormatInt(m.version, 10))...)
return data, nil
}
func (m *message) UnmarshalBinary(data []byte) error {
fields := bytes.Split(data, []byte{'-'})
l := len(fields)
if l < 3 {
return errors.New("invalid token")
}
version, err := strconv.ParseInt(string(fields[l-1]), 10, 64)
if err != nil {
return err
}
m.version = version
createAt, err := strconv.ParseInt(string(fields[l-2]), 10, 64)
if err != nil {
return err
}
m.createAt = createAt
m.payload = bytes.Join(fields[:l-2], []byte(""))
return nil
}