-
Notifications
You must be signed in to change notification settings - Fork 402
/
apikey.go
53 lines (44 loc) · 1.12 KB
/
apikey.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"storj.io/storj/pkg/macaroon"
)
// APIKey represents an access credential to certain resources
type APIKey struct {
key *macaroon.APIKey
}
// Serialize serializes the API key to a string
func (a APIKey) Serialize() string {
return a.key.Serialize()
}
func (a APIKey) serializeRaw() []byte {
return a.key.SerializeRaw()
}
// IsZero returns if the api key is an uninitialized value
func (a *APIKey) IsZero() bool {
return a.key == nil
}
// ParseAPIKey parses an API key
func ParseAPIKey(val string) (APIKey, error) {
k, err := macaroon.ParseAPIKey(val)
if err != nil {
return APIKey{}, err
}
return APIKey{key: k}, nil
}
func parseRawAPIKey(data []byte) (APIKey, error) {
k, err := macaroon.ParseRawAPIKey(data)
if err != nil {
return APIKey{}, err
}
return APIKey{key: k}, nil
}
// Restrict generates a new APIKey with the provided Caveat attached.
func (a APIKey) Restrict(caveat macaroon.Caveat) (APIKey, error) {
k, err := a.key.Restrict(caveat)
if err != nil {
return APIKey{}, err
}
return APIKey{key: k}, nil
}