-
Notifications
You must be signed in to change notification settings - Fork 25
/
auth.go
48 lines (40 loc) · 814 Bytes
/
auth.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
package documentdb
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
)
type Key struct {
Key string
salt []byte
err error
}
func NewKey(key string) *Key {
return &Key{Key: key}
}
func (k *Key) Salt() ([]byte, error) {
if len(k.salt) == 0 && k.err == nil {
k.salt, k.err = base64.StdEncoding.DecodeString(k.Key)
if k.err != nil {
if _, ok := k.err.(base64.CorruptInputError); ok {
k.err = errors.New("base64 input is corrupt, check CosmosDB key.")
}
}
}
return k.salt, k.err
}
func authorize(str []byte, key *Key) (ret string, err error) {
var (
salt []byte
)
salt, err = key.Salt()
if err != nil {
return ret, err
}
hmac := hmac.New(sha256.New, salt)
hmac.Write(str)
b := hmac.Sum(nil)
ret = base64.StdEncoding.EncodeToString(b)
return ret, nil
}