storj/uplink/apikey.go
Egon Elbre 6098f606a2 lib/uplink: move to uplink
Setup aliases in lib/uplink and set uplink as the default.

Change-Id: Ic06b5f3d59fe402faaed2143fdf4b2314e3d06b9
2020-01-02 17:56:16 +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
}