2018-12-26 14:00:53 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellite
|
|
|
|
|
|
|
|
import (
|
2018-12-27 15:30:15 +00:00
|
|
|
"bytes"
|
2018-12-26 14:00:53 +00:00
|
|
|
"context"
|
2018-12-27 15:30:15 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
2018-12-26 14:00:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-01-08 14:05:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-26 14:00:53 +00:00
|
|
|
)
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
// APIKeys is interface for working with api keys store
|
2018-12-26 14:00:53 +00:00
|
|
|
type APIKeys interface {
|
|
|
|
// GetByProjectID retrieves list of APIKeys for given projectID
|
2018-12-27 15:30:15 +00:00
|
|
|
GetByProjectID(ctx context.Context, projectID uuid.UUID) ([]APIKeyInfo, error)
|
|
|
|
// Get retrieves APIKeyInfo with given ID
|
|
|
|
Get(ctx context.Context, id uuid.UUID) (*APIKeyInfo, error)
|
|
|
|
// Create creates and stores new APIKeyInfo
|
|
|
|
Create(ctx context.Context, key APIKey, info APIKeyInfo) (*APIKeyInfo, error)
|
|
|
|
// Update updates APIKeyInfo in store
|
|
|
|
Update(ctx context.Context, key APIKeyInfo) error
|
|
|
|
// Delete deletes APIKeyInfo from store
|
2018-12-26 14:00:53 +00:00
|
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
// APIKeyInfo describing api key model in the database
|
|
|
|
type APIKeyInfo struct {
|
2018-12-26 14:00:53 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
|
|
|
|
// Fk on project
|
|
|
|
ProjectID uuid.UUID `json:"projectId"`
|
|
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
2018-12-27 15:30:15 +00:00
|
|
|
|
|
|
|
// APIKey is an api key type
|
|
|
|
type APIKey [24]byte
|
|
|
|
|
|
|
|
// String implements Stringer
|
|
|
|
func (key APIKey) String() string {
|
|
|
|
emptyKey := APIKey{}
|
|
|
|
if bytes.Equal(key[:], emptyKey[:]) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.URLEncoding.EncodeToString(key[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIKeyFromBytes creates new key from byte slice
|
|
|
|
func APIKeyFromBytes(b []byte) *APIKey {
|
|
|
|
key := new(APIKey)
|
|
|
|
copy(key[:], b)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
// createAPIKey creates new api key
|
|
|
|
func createAPIKey() (*APIKey, error) {
|
|
|
|
key := new(APIKey)
|
|
|
|
|
|
|
|
n, err := io.ReadFull(rand.Reader, key[:])
|
|
|
|
if err != nil || n != 24 {
|
|
|
|
return nil, errs.New("error creating api key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|