-
Notifications
You must be signed in to change notification settings - Fork 4
/
bewit.go
61 lines (51 loc) · 1.14 KB
/
bewit.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
package hawk
import (
"encoding/base64"
"strconv"
"time"
)
type BewitConfig struct {
Credential *Credential
Ttl time.Duration
Ext string
LocalTimeOffset time.Duration
}
func NewBewitConfig(c *Credential, ttl time.Duration) *BewitConfig {
return &BewitConfig{
Credential: c,
Ttl: ttl,
}
}
// TODO: Implement the SNTP for time sync management
// GetBewit builds a value of bewit parameter.
func (b *BewitConfig) GetBewit(url string, clock Clock) string {
if url == "" {
return ""
}
if b.Credential == nil {
return ""
}
if b.Credential.ID == "" || b.Credential.Key == "" || b.Credential.Alg == 0 {
return ""
}
if clock == nil {
clock = &LocalClock{}
}
now := clock.Now(b.LocalTimeOffset)
exp := time.Unix(now, 0).Add(b.Ttl).Unix()
opt := &Option{
TimeStamp: exp,
Nonce: "",
Ext: b.Ext,
}
m := &Mac{
Type: Bewit,
Credential: b.Credential,
Uri: url,
Method: "GET",
Option: opt,
}
mac, _ := m.String()
bewit := b.Credential.ID + "\\" + strconv.FormatInt(exp, 10) + "\\" + mac + "\\" + b.Ext
return base64.RawURLEncoding.EncodeToString([]byte(bewit))
}