2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-20 18:29:05 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package certificates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-01-02 17:39:17 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2018-12-20 18:29:05 +00:00
|
|
|
"crypto/rand"
|
2019-02-07 20:39:20 +00:00
|
|
|
"crypto/rsa"
|
2018-12-20 18:29:05 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2019-01-02 17:39:17 +00:00
|
|
|
"strconv"
|
2018-12-31 15:45:43 +00:00
|
|
|
"strings"
|
2019-01-02 17:39:17 +00:00
|
|
|
"time"
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2019-02-01 17:28:40 +00:00
|
|
|
"google.golang.org/grpc"
|
2019-01-02 17:39:17 +00:00
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-12-20 18:29:05 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-12-20 18:29:05 +00:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AuthorizationsBucket is the bucket used with a bolt-backed authorizations DB.
|
|
|
|
AuthorizationsBucket = "authorizations"
|
2019-01-02 17:39:17 +00:00
|
|
|
// MaxClaimDelaySeconds is the max duration in seconds in the past or
|
|
|
|
// future that a claim timestamp is allowed to have and still be valid.
|
|
|
|
MaxClaimDelaySeconds = 15
|
2018-12-31 15:45:43 +00:00
|
|
|
tokenDataLength = 64 // 2^(64*8) =~ 1.34E+154
|
|
|
|
tokenDelimiter = ":"
|
|
|
|
tokenVersion = 0
|
2018-12-20 18:29:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
// ErrAuthorization is used when an error occurs involving an authorization.
|
|
|
|
ErrAuthorization = errs.Class("authorization error")
|
|
|
|
// ErrAuthorizationDB is used when an error occurs involving the authorization database.
|
|
|
|
ErrAuthorizationDB = errs.Class("authorization db error")
|
2018-12-31 15:45:43 +00:00
|
|
|
// ErrInvalidToken is used when a token is invalid
|
|
|
|
ErrInvalidToken = errs.Class("invalid token error")
|
2018-12-20 18:29:05 +00:00
|
|
|
// ErrAuthorizationCount is used when attempting to create an invalid number of authorizations.
|
|
|
|
ErrAuthorizationCount = ErrAuthorizationDB.New("cannot add less than one authorizations")
|
|
|
|
)
|
|
|
|
|
|
|
|
// CertificateSigner implements pb.CertificatesServer
|
|
|
|
type CertificateSigner struct {
|
2019-01-11 14:59:35 +00:00
|
|
|
log *zap.Logger
|
|
|
|
signer *identity.FullCertificateAuthority
|
|
|
|
authDB *AuthorizationDB
|
|
|
|
minDifficulty uint16
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizationDB stores authorizations which may be claimed in exchange for a
|
|
|
|
// certificate signature.
|
|
|
|
type AuthorizationDB struct {
|
|
|
|
DB storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorizations is a slice of authorizations for convenient de/serialization
|
|
|
|
// and grouping.
|
|
|
|
type Authorizations []*Authorization
|
|
|
|
|
|
|
|
// Authorization represents a single-use authorization token and its status
|
|
|
|
type Authorization struct {
|
|
|
|
Token Token
|
|
|
|
Claim *Claim
|
|
|
|
}
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
// Token is a userID and a random byte array, when serialized, can be used like
|
|
|
|
// a pre-shared key for claiming certificate signatures.
|
|
|
|
type Token struct {
|
|
|
|
// NB: currently email address for convenience
|
|
|
|
UserID string
|
|
|
|
Data [tokenDataLength]byte
|
|
|
|
}
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// ClaimOpts hold parameters for claiming an authorization
|
|
|
|
type ClaimOpts struct {
|
|
|
|
Req *pb.SigningRequest
|
|
|
|
Peer *peer.Peer
|
|
|
|
ChainBytes [][]byte
|
|
|
|
MinDifficulty uint16
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
// Claim holds information about the circumstances under which an authorization
|
|
|
|
// token was claimed.
|
|
|
|
type Claim struct {
|
2019-01-02 17:39:17 +00:00
|
|
|
Addr string
|
|
|
|
Timestamp int64
|
2019-01-04 17:23:23 +00:00
|
|
|
Identity *identity.PeerIdentity
|
2019-01-02 17:39:17 +00:00
|
|
|
SignedChainBytes [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client implements pb.CertificateClient
|
|
|
|
type Client struct {
|
2019-02-01 17:28:40 +00:00
|
|
|
conn *grpc.ClientConn
|
2019-01-02 17:39:17 +00:00
|
|
|
client pb.CertificatesClient
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
func init() {
|
|
|
|
gob.Register(&ecdsa.PublicKey{})
|
2019-02-07 20:39:20 +00:00
|
|
|
gob.Register(&rsa.PublicKey{})
|
2019-01-02 17:39:17 +00:00
|
|
|
gob.Register(elliptic.P256())
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:59:35 +00:00
|
|
|
// NewServer creates a new certificate signing grpc server
|
|
|
|
func NewServer(log *zap.Logger, signer *identity.FullCertificateAuthority, authDB *AuthorizationDB, minDifficulty uint16) *CertificateSigner {
|
|
|
|
return &CertificateSigner{
|
|
|
|
log: log,
|
|
|
|
signer: signer,
|
|
|
|
authDB: authDB,
|
|
|
|
minDifficulty: minDifficulty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// NewClient creates a new certificate signing grpc client
|
2019-02-11 11:17:32 +00:00
|
|
|
func NewClient(ctx context.Context, tc transport.Client, address string) (*Client, error) {
|
2019-01-02 17:39:17 +00:00
|
|
|
conn, err := tc.DialAddress(ctx, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
return &Client{
|
2019-02-01 17:28:40 +00:00
|
|
|
conn: conn,
|
2019-01-02 17:39:17 +00:00
|
|
|
client: pb.NewCertificatesClient(conn),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientFrom creates a new certificate signing grpc client from an existing
|
|
|
|
// grpc cert signing client
|
|
|
|
func NewClientFrom(client pb.CertificatesClient) (*Client, error) {
|
|
|
|
return &Client{
|
|
|
|
client: client,
|
|
|
|
}, nil
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAuthorization creates a new, unclaimed authorization with a random token value
|
2018-12-31 15:45:43 +00:00
|
|
|
func NewAuthorization(userID string) (*Authorization, error) {
|
|
|
|
token := Token{UserID: userID}
|
|
|
|
_, err := rand.Read(token.Data[:])
|
2018-12-20 18:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrAuthorization.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Authorization{
|
|
|
|
Token: token,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
// ParseToken splits the token string on the delimiter to get a userID and data
|
|
|
|
// for a token and base58 decodes the data.
|
|
|
|
func ParseToken(tokenString string) (*Token, error) {
|
|
|
|
splitAt := strings.LastIndex(tokenString, tokenDelimiter)
|
|
|
|
if splitAt == -1 {
|
|
|
|
return nil, ErrInvalidToken.New("delimiter missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
userID, b58Data := tokenString[:splitAt], tokenString[splitAt+1:]
|
|
|
|
if len(userID) == 0 {
|
|
|
|
return nil, ErrInvalidToken.New("user ID missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _, err := base58.CheckDecode(b58Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInvalidToken.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) != tokenDataLength {
|
|
|
|
return nil, ErrInvalidToken.New("data size mismatch")
|
|
|
|
}
|
|
|
|
t := &Token{
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
copy(t.Data[:], data)
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
// Close closes the client
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
if c.conn != nil {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// Sign claims an authorization using the token string and returns a signed
|
|
|
|
// copy of the client's CA certificate
|
2019-02-01 17:28:40 +00:00
|
|
|
func (c *Client) Sign(ctx context.Context, tokenStr string) ([][]byte, error) {
|
2019-01-02 17:39:17 +00:00
|
|
|
res, err := c.client.Sign(ctx, &pb.SigningRequest{
|
|
|
|
AuthToken: tokenStr,
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Chain, nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
// Sign signs the CA certificate of the remote peer's identity with the signer's certificate.
|
|
|
|
// Returns a certificate chain consisting of the remote peer's CA followed by the signer's chain.
|
2018-12-20 18:29:05 +00:00
|
|
|
func (c CertificateSigner) Sign(ctx context.Context, req *pb.SigningRequest) (*pb.SigningResponse, error) {
|
2019-01-02 17:39:17 +00:00
|
|
|
grpcPeer, ok := peer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
// TODO: better error
|
|
|
|
return nil, errs.New("unable to get peer from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
peerIdent, err := identity.PeerIdentityFromPeer(grpcPeer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:59:35 +00:00
|
|
|
signedPeerCA, err := c.signer.Sign(peerIdent.CA)
|
2019-01-02 17:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
signedChainBytes := [][]byte{signedPeerCA.Raw, c.signer.Cert.Raw}
|
2019-03-04 20:03:33 +00:00
|
|
|
signedChainBytes = append(signedChainBytes, c.signer.RawRestChain()...)
|
2019-01-11 14:59:35 +00:00
|
|
|
err = c.authDB.Claim(&ClaimOpts{
|
2019-01-02 17:39:17 +00:00
|
|
|
Req: req,
|
|
|
|
Peer: grpcPeer,
|
|
|
|
ChainBytes: signedChainBytes,
|
2019-01-11 14:59:35 +00:00
|
|
|
MinDifficulty: c.minDifficulty,
|
2019-01-02 17:39:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.SigningResponse{
|
|
|
|
Chain: signedChainBytes,
|
|
|
|
}, nil
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the authorization database's underlying store.
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) Close() error {
|
|
|
|
return ErrAuthorizationDB.Wrap(authDB.DB.Close())
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new authorization and adds it to the authorization database.
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) Create(userID string, count int) (Authorizations, error) {
|
2018-12-31 15:45:43 +00:00
|
|
|
if len(userID) == 0 {
|
|
|
|
return nil, ErrAuthorizationDB.New("userID cannot be empty")
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
if count < 1 {
|
|
|
|
return nil, ErrAuthorizationCount
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
newAuths Authorizations
|
2019-03-29 12:30:23 +00:00
|
|
|
authErrs errs.Group
|
2018-12-20 18:29:05 +00:00
|
|
|
)
|
|
|
|
for i := 0; i < count; i++ {
|
2018-12-31 15:45:43 +00:00
|
|
|
auth, err := NewAuthorization(userID)
|
2018-12-20 18:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
authErrs.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newAuths = append(newAuths, auth)
|
|
|
|
}
|
2019-03-29 12:30:23 +00:00
|
|
|
if err := authErrs.Err(); err != nil {
|
2018-12-20 18:29:05 +00:00
|
|
|
return nil, ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
if err := authDB.add(userID, newAuths); err != nil {
|
2019-01-02 17:39:17 +00:00
|
|
|
return nil, err
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newAuths, nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
// Get retrieves authorizations by user ID.
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) Get(userID string) (Authorizations, error) {
|
|
|
|
authsBytes, err := authDB.DB.Get(storage.Key(userID))
|
2018-12-20 18:29:05 +00:00
|
|
|
if err != nil && !storage.ErrKeyNotFound.Has(err) {
|
|
|
|
return nil, ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
if authsBytes == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var auths Authorizations
|
|
|
|
if err := auths.Unmarshal(authsBytes); err != nil {
|
|
|
|
return nil, ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
return auths, nil
|
|
|
|
}
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
// UserIDs returns a list of all userIDs present in the authorization database.
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) UserIDs() ([]string, error) {
|
|
|
|
keys, err := authDB.DB.List([]byte{}, 0)
|
2018-12-20 18:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
return keys.Strings(), nil
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
// List returns all authorizations in the database.
|
|
|
|
func (authDB *AuthorizationDB) List() (auths Authorizations, _ error) {
|
|
|
|
uids, err := authDB.UserIDs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uid := range uids {
|
|
|
|
idAuths, err := authDB.Get(uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
auths = append(auths, idAuths...)
|
|
|
|
}
|
|
|
|
return auths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Claim marks an authorization as claimed and records claim information.
|
|
|
|
func (authDB *AuthorizationDB) Claim(opts *ClaimOpts) error {
|
2019-01-02 17:39:17 +00:00
|
|
|
now := time.Now().Unix()
|
|
|
|
if !(now-MaxClaimDelaySeconds < opts.Req.Timestamp) ||
|
|
|
|
!(opts.Req.Timestamp < now+MaxClaimDelaySeconds) {
|
|
|
|
return ErrAuthorization.New("claim timestamp is outside of max delay window: %d", opts.Req.Timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, err := identity.PeerIdentityFromPeer(opts.Peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerDifficulty, err := ident.ID.Difficulty()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if peerDifficulty < opts.MinDifficulty {
|
|
|
|
return ErrAuthorization.New("difficulty must be greater than: %d", opts.MinDifficulty)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := ParseToken(opts.Req.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
auths, err := authDB.Get(token.UserID)
|
2019-01-02 17:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, auth := range auths {
|
|
|
|
if auth.Token.Equal(token) {
|
|
|
|
if auth.Claim != nil {
|
|
|
|
return ErrAuthorization.New("authorization has already been claimed: %s", auth.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
auths[i] = &Authorization{
|
|
|
|
Token: auth.Token,
|
|
|
|
Claim: &Claim{
|
|
|
|
Timestamp: now,
|
|
|
|
Addr: opts.Peer.Addr.String(),
|
|
|
|
Identity: ident,
|
|
|
|
SignedChainBytes: opts.ChainBytes,
|
|
|
|
},
|
|
|
|
}
|
2019-02-06 11:00:26 +00:00
|
|
|
if err := authDB.put(token.UserID, auths); err != nil {
|
2019-01-02 17:39:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:17:26 +00:00
|
|
|
// Unclaim removes a claim from an authorization.
|
|
|
|
func (authDB *AuthorizationDB) Unclaim(authToken string) error {
|
|
|
|
token, err := ParseToken(authToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
auths, err := authDB.Get(token.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, auth := range auths {
|
|
|
|
if auth.Token.Equal(token) {
|
|
|
|
auths[i].Claim = nil
|
|
|
|
return authDB.put(token.UserID, auths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.New("token not found in authorizations DB")
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) add(userID string, newAuths Authorizations) error {
|
|
|
|
auths, err := authDB.Get(userID)
|
2019-01-02 17:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
auths = append(auths, newAuths...)
|
2019-02-06 11:00:26 +00:00
|
|
|
return authDB.put(userID, auths)
|
2019-01-02 17:39:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
func (authDB *AuthorizationDB) put(userID string, auths Authorizations) error {
|
2019-01-02 17:39:17 +00:00
|
|
|
authsBytes, err := auths.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
if err := authDB.DB.Put(storage.Key(userID), authsBytes); err != nil {
|
2019-01-02 17:39:17 +00:00
|
|
|
return ErrAuthorizationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
// Unmarshal deserializes a set of authorizations
|
|
|
|
func (a *Authorizations) Unmarshal(data []byte) error {
|
|
|
|
decoder := gob.NewDecoder(bytes.NewBuffer(data))
|
|
|
|
if err := decoder.Decode(a); err != nil {
|
|
|
|
return ErrAuthorization.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal serializes a set of authorizations
|
|
|
|
func (a Authorizations) Marshal() ([]byte, error) {
|
|
|
|
data := new(bytes.Buffer)
|
|
|
|
encoder := gob.NewEncoder(data)
|
|
|
|
err := encoder.Encode(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrAuthorization.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group separates a set of authorizations into a set of claimed and a set of open authorizations.
|
|
|
|
func (a Authorizations) Group() (claimed, open Authorizations) {
|
|
|
|
for _, auth := range a {
|
|
|
|
if auth.Claim != nil {
|
|
|
|
// TODO: check if claim is valid? what if not?
|
|
|
|
claimed = append(claimed, auth)
|
|
|
|
} else {
|
|
|
|
open = append(open, auth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return claimed, open
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface and prevents authorization data
|
2019-01-02 17:39:17 +00:00
|
|
|
// from completely leaking into logs and errors.
|
2018-12-20 18:29:05 +00:00
|
|
|
func (a Authorization) String() string {
|
2019-01-02 17:39:17 +00:00
|
|
|
fmtLen := strconv.Itoa(len(a.Token.UserID) + 7)
|
|
|
|
return fmt.Sprintf("%."+fmtLen+"s..", a.Token.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal checks if two tokens have equal user IDs and data
|
|
|
|
func (t *Token) Equal(cmpToken *Token) bool {
|
|
|
|
return t.UserID == cmpToken.UserID && bytes.Equal(t.Data[:], cmpToken.Data[:])
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface. Base68 w/ version and checksum bytes
|
|
|
|
// are used for easy and reliable human transport.
|
|
|
|
func (t *Token) String() string {
|
2018-12-31 15:45:43 +00:00
|
|
|
return fmt.Sprintf("%s:%s", t.UserID, base58.CheckEncode(t.Data[:], tokenVersion))
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|