forked from iromli/go-itsdangerous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
200 lines (173 loc) · 4.9 KB
/
signature.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
200
package itsdangerous
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"hash"
"strings"
)
// Signature can sign bytes and unsign it and validate the signature
// provided.
//
// Salt can be used to namespace the hash, so that a signed string is only
// valid for a given namespace. Leaving this at the default value or re-using
// a salt value across different parts of your application where the same
// signed value in one part can mean something different in another part
// is a security risk.
type Signature struct {
SecretKey string
Sep string
Salt string
KeyDerivation string
DigestMethod hash.Hash
Algorithm SigningAlgorithm
}
// DeriveKey generates a key derivation. Keep in mind that the key derivation in itsdangerous
// is not intended to be used as a security method to make a complex key out of a short password.
// Instead you should use large random secret keys.
func (s *Signature) DeriveKey() (string, error) {
var key string
var err error
s.DigestMethod.Reset()
switch s.KeyDerivation {
case "concat":
h := s.DigestMethod
h.Write([]byte(s.Salt + s.SecretKey))
key = string(h.Sum(nil))
case "django-concat":
h := s.DigestMethod
h.Write([]byte(s.Salt + "signer" + s.SecretKey))
key = string(h.Sum(nil))
case "hmac":
h := hmac.New(func() hash.Hash { return s.DigestMethod }, []byte(s.SecretKey))
h.Write([]byte(s.Salt))
key = string(h.Sum(nil))
case "none":
key = s.SecretKey
default:
key, err = "", errors.New("unknown key derivation method")
}
return key, err
}
// Get returns the signature for the given value.
func (s *Signature) Get(value string) (string, error) {
key, err := s.DeriveKey()
if err != nil {
return "", err
}
sig := s.Algorithm.GetSignature(key, value)
return base64Encode(sig), err
}
// Verify verifies the signature for the given value.
func (s *Signature) Verify(value, sig string) (bool, error) {
key, err := s.DeriveKey()
if err != nil {
return false, err
}
signed, err := base64Decode(sig)
if err != nil {
return false, err
}
return s.Algorithm.VerifySignature(key, value, signed), nil
}
// Sign the given string.
func (s *Signature) Sign(value string) (string, error) {
sig, err := s.Get(value)
if err != nil {
return "", err
}
return value + s.Sep + sig, nil
}
// Unsign the given string.
func (s *Signature) Unsign(signed string) (string, error) {
if !strings.Contains(signed, s.Sep) {
return "", fmt.Errorf("no %s found in value", s.Sep)
}
li := strings.LastIndex(signed, s.Sep)
value, sig := signed[:li], signed[li+len(s.Sep):]
if ok, _ := s.Verify(value, sig); ok == true {
return value, nil
}
return "", fmt.Errorf("signature %s does not match", sig)
}
// NewSignature creates a new Signature
func NewSignature(secret, salt, sep, derivation string, digest hash.Hash, algo SigningAlgorithm) *Signature {
if salt == "" {
salt = "itsdangerous.Signer"
}
if sep == "" {
sep = "."
}
if derivation == "" {
derivation = "django-concat"
}
if digest == nil {
digest = sha1.New()
}
if algo == nil {
algo = &HMACAlgorithm{DigestMethod: digest}
}
return &Signature{
SecretKey: secret,
Salt: salt,
Sep: sep,
KeyDerivation: derivation,
DigestMethod: digest,
Algorithm: algo,
}
}
// TimestampSignature works like the regular Signature but also records the time
// of the signing and can be used to expire signatures.
type TimestampSignature struct {
Signature
}
// Sign the given string.
func (s *TimestampSignature) Sign(value string) (string, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, getTimestamp()); err != nil {
return "", err
}
ts := base64Encode(buf.Bytes())
val := value + s.Sep + ts
sig, err := s.Get(val)
if err != nil {
return "", err
}
return val + s.Sep + sig, nil
}
// Unsign the given string.
func (s *TimestampSignature) Unsign(value string, maxAge uint32) (string, error) {
var timestamp uint32
result, err := s.Signature.Unsign(value)
if err != nil {
return "", err
}
// If there is no timestamp in the result there is something seriously wrong.
if !strings.Contains(result, s.Sep) {
return "", errors.New("timestamp missing")
}
li := strings.LastIndex(result, s.Sep)
val, ts := result[:li], result[li+len(s.Sep):]
sig, err := base64Decode(ts)
if err != nil {
return "", err
}
buf := bytes.NewReader([]byte(sig))
if err = binary.Read(buf, binary.BigEndian, ×tamp); err != nil {
return "", err
}
if maxAge > 0 {
if age := getTimestamp() - timestamp; age > maxAge {
return "", fmt.Errorf("signature age %d > %d seconds", age, maxAge)
}
}
return val, nil
}
// NewTimestampSignature creates a new TimestampSignature
func NewTimestampSignature(secret, salt, sep, derivation string, digest hash.Hash, algo SigningAlgorithm) *TimestampSignature {
s := NewSignature(secret, salt, sep, derivation, digest, algo)
return &TimestampSignature{Signature: *s}
}