-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeys.go
64 lines (58 loc) · 2.72 KB
/
keys.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
package control
// A struct representing an Ably Key.
type Key struct {
// The key ID.
ID string `json:"id,omitempty"`
// The Ably application ID which this key is associated with.
AppID string `json:"appId,omitempty"`
// The name for your API key. This is a friendly name for your reference.
Name string `json:"name,omitempty"`
// The status of the key. 0 is enabled, 1 is revoked.
Status int `json:"status"`
// The complete API key including API secret.
Key string `json:"key,omitempty"`
// The capabilities that this key has. More information on capabilities
// can be found in the Ably documentation https://ably.com/documentation/core-features/authentication#capabilities-explained.
Capability map[string][]string `json:"capability"`
// Unix timestamp representing the date and time of creation of the key.
Created int `json:"created"`
// Unix timestamp representing the date and time of the last modification of the key.
Modified int `json:"modified"`
// Token revocation is a security mechanism allowing an app to invalidate authentication tokens,
// primarily used against malicious clients. Implementation sets tokens' maximum time-to-live (TTL) to one hour.
RevocableTokens bool `json:"revocableTokens"`
}
// A struct representing the settable fields of an Ably key.
type NewKey struct {
// The name for your API key. This is a friendly name for your reference.
Name string `json:"name,omitempty"`
// The capabilities that this key has. More information on capabilities
// can be found in the Ably documentation https://ably.com/documentation/core-features/authentication#capabilities-explained.
Capability map[string][]string `json:"capability"`
// Enable Revocable Tokens. More information on Token Revocation can be
// found in the Ably documentation https://ably.com/docs/auth/revocation
RevocableTokens bool `json:"revocableTokens"`
}
// Keys lists the API keys associated with the application ID.
func (c *Client) Keys(appID string) ([]Key, error) {
var keys []Key
err := c.request("GET", "/apps/"+appID+"/keys", nil, &keys)
return keys, err
}
// CreateKey creates an application with the specified properties.
func (c *Client) CreateKey(appID string, key *NewKey) (Key, error) {
var out Key
err := c.request("POST", "/apps/"+appID+"/keys", &key, &out)
return out, err
}
// UpdateKey updates the API key with the specified key ID.
func (c *Client) UpdateKey(appID, keyID string, key *NewKey) (Key, error) {
var out Key
err := c.request("PATCH", "/apps/"+appID+"/keys/"+keyID, &key, &out)
return out, err
}
// RevokeKey revokes the API key with the specified ID. This deletes the key.
func (c *Client) RevokeKey(appID, keyID string) error {
err := c.request("POST", "/apps/"+appID+"/keys/"+keyID+"/revoke", nil, nil)
return err
}