lib/uplink: expose restrict on api keys (#2189)

This commit is contained in:
JT Olio 2019-06-12 15:35:57 -06:00 committed by Stefan Benten
parent 474d9e7492
commit ff7a9030eb
2 changed files with 21 additions and 8 deletions

View File

@ -79,11 +79,7 @@ func shareMain(cmd *cobra.Command, args []string) (err error) {
return err
}
// TODO(jeff): we have to have the server side of things expecting macaroons
// before we can change libuplink to use macaroons because of all the tests.
// For now, just use the raw macaroon library.
key, err := macaroon.ParseAPIKey(cfg.Client.APIKey)
key, err := libuplink.ParseAPIKey(cfg.Client.APIKey)
if err != nil {
return err
}

View File

@ -3,17 +3,34 @@
package uplink
import (
"storj.io/storj/pkg/macaroon"
)
// APIKey represents an access credential to certain resources
type APIKey struct {
key string
key *macaroon.APIKey
}
// Serialize serializes the API key to a string
func (a APIKey) Serialize() string {
return a.key
return a.key.Serialize()
}
// ParseAPIKey parses an API key
func ParseAPIKey(val string) (APIKey, error) {
return APIKey{key: val}, nil
k, err := macaroon.ParseAPIKey(val)
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
}