-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathauth.go
110 lines (92 loc) · 2.35 KB
/
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
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
package rest
import (
"fmt"
"strconv"
"time"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"github.com/ably/ably-go/Godeps/_workspace/src/github.com/flynn/flynn/pkg/random"
"github.com/ably/ably-go/config"
)
type Capability map[string][]string
func (c Capability) MarshalJSON() ([]byte, error) {
p, err := json.Marshal((map[string][]string)(c))
if err != nil {
return nil, err
}
return []byte(strconv.Quote(string(p))), nil
}
func (c *Capability) UnmarshalJSON(p []byte) error {
s, err := strconv.Unquote(string(p))
if err != nil {
return err
}
return json.Unmarshal([]byte(s), (*map[string][]string)(c))
}
func (c Capability) String() string {
p, err := json.Marshal((map[string][]string)(c))
if err != nil {
panic(err)
}
return string(p)
}
type Token struct {
ID string `json:"id"`
Key string `json:"key"`
Capability Capability `json:"capability"`
}
type tokenResponse struct {
AccessToken *Token `json:"access_token"`
}
type TokenRequest struct {
ID string `json:"id"`
TTL int `json:"ttl"`
Capability Capability `json:"capability"`
ClientID string `json:"client_id"`
Timestamp int64 `json:"timestamp"`
Nonce string `json:"nonce"`
Mac string `json:"mac"`
}
func (t *TokenRequest) Sign(secret string) {
mac := hmac.New(sha256.New, []byte(secret))
fmt.Fprintln(mac, t.ID)
fmt.Fprintln(mac, t.TTL)
fmt.Fprintln(mac, t.Capability.String())
fmt.Fprintln(mac, t.ClientID)
fmt.Fprintln(mac, t.Timestamp)
fmt.Fprintln(mac, t.Nonce)
t.Mac = base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
type Auth struct {
config.Params
client *Client
}
func NewAuth(params config.Params, client *Client) *Auth {
return &Auth{
Params: params,
client: client,
}
}
func (a *Auth) CreateTokenRequest(ttl int, capability Capability) *TokenRequest {
req := &TokenRequest{
ID: a.AppID,
TTL: ttl,
Capability: capability,
ClientID: a.ClientID,
Timestamp: time.Now().Unix(),
Nonce: random.String(32),
}
req.Sign(a.AppSecret)
return req
}
func (a *Auth) RequestToken(ttl int, capability Capability) (*Token, error) {
req := a.CreateTokenRequest(ttl, capability)
res := &tokenResponse{}
_, err := a.client.Post("/keys/"+a.AppID+"/requestToken", req, res)
if err != nil {
return nil, err
}
return res.AccessToken, nil
}