storj/lib/uplink/apikey.go
Egon Elbre 3f38a7ed1e lib/uplink: revert library move
Moving lib/uplink actually makes implementing the new library harder.
Move the package back where it was.

Change-Id: If758e897e3e56155a48ae82b23904cd8cdd72c13
2020-01-07 18:39:15 +00:00

54 lines
1.1 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"storj.io/common/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
}