2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-26 14:00:53 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package console
|
2018-12-26 14:00:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2018-12-26 14:00:53 +00:00
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// APIKeys is interface for working with api keys store.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2018-12-26 14:00:53 +00:00
|
|
|
type APIKeys interface {
|
2019-09-12 15:19:30 +01:00
|
|
|
// GetPagedByProjectID is a method for querying API keys from the database by projectID and cursor
|
|
|
|
GetPagedByProjectID(ctx context.Context, projectID uuid.UUID, cursor APIKeyCursor) (akp *APIKeyPage, err error)
|
2018-12-27 15:30:15 +00:00
|
|
|
// Get retrieves APIKeyInfo with given ID
|
|
|
|
Get(ctx context.Context, id uuid.UUID) (*APIKeyInfo, error)
|
2019-05-24 17:51:27 +01:00
|
|
|
// GetByHead retrieves APIKeyInfo for given key head
|
|
|
|
GetByHead(ctx context.Context, head []byte) (*APIKeyInfo, error)
|
2019-10-10 14:28:35 +01:00
|
|
|
// GetByNameAndProjectID retrieves APIKeyInfo for given key name and projectID
|
|
|
|
GetByNameAndProjectID(ctx context.Context, name string, projectID uuid.UUID) (*APIKeyInfo, error)
|
2018-12-27 15:30:15 +00:00
|
|
|
// Create creates and stores new APIKeyInfo
|
2019-05-24 17:51:27 +01:00
|
|
|
Create(ctx context.Context, head []byte, info APIKeyInfo) (*APIKeyInfo, error)
|
2018-12-27 15:30:15 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
// RESTKeys is an interface for rest key operations.
|
|
|
|
type RESTKeys interface {
|
2022-02-11 22:48:35 +00:00
|
|
|
Create(ctx context.Context, userID uuid.UUID, expiration time.Duration) (apiKey string, expiresAt time.Time, err error)
|
2022-03-27 11:16:46 +01:00
|
|
|
GetUserAndExpirationFromKey(ctx context.Context, apiKey string) (userID uuid.UUID, exp time.Time, err error)
|
2022-02-11 22:48:35 +00:00
|
|
|
Revoke(ctx context.Context, apiKey string) (err error)
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
// CreateAPIKeyRequest holds create API key info.
|
|
|
|
type CreateAPIKeyRequest struct {
|
|
|
|
ProjectID string `json:"projectID"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAPIKeyResponse holds macaroon.APIKey and APIKeyInfo.
|
|
|
|
type CreateAPIKeyResponse struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
KeyInfo *APIKeyInfo `json:"keyInfo"`
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// APIKeyInfo describing api key model in the database.
|
2018-12-27 15:30:15 +00:00
|
|
|
type APIKeyInfo struct {
|
2019-05-24 17:51:27 +01:00
|
|
|
ID uuid.UUID `json:"id"`
|
2018-12-26 14:00:53 +00:00
|
|
|
ProjectID uuid.UUID `json:"projectId"`
|
2019-07-12 18:59:19 +01:00
|
|
|
PartnerID uuid.UUID `json:"partnerId"`
|
2021-09-23 00:38:18 +01:00
|
|
|
UserAgent []byte `json:"userAgent"`
|
2019-05-24 17:51:27 +01:00
|
|
|
Name string `json:"name"`
|
2022-02-14 18:20:12 +00:00
|
|
|
Head []byte `json:"-"`
|
2019-05-24 17:51:27 +01:00
|
|
|
Secret []byte `json:"-"`
|
2018-12-26 14:00:53 +00:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
2019-09-12 15:19:30 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// APIKeyCursor holds info for api keys cursor pagination.
|
2019-09-12 15:19:30 +01:00
|
|
|
type APIKeyCursor struct {
|
|
|
|
Search string
|
|
|
|
Limit uint
|
|
|
|
Page uint
|
|
|
|
Order APIKeyOrder
|
|
|
|
OrderDirection OrderDirection
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// APIKeyPage represent api key page result.
|
2019-09-12 15:19:30 +01:00
|
|
|
type APIKeyPage struct {
|
|
|
|
APIKeys []APIKeyInfo
|
|
|
|
|
|
|
|
Search string
|
|
|
|
Limit uint
|
|
|
|
Order APIKeyOrder
|
|
|
|
OrderDirection OrderDirection
|
|
|
|
Offset uint64
|
|
|
|
|
|
|
|
PageCount uint
|
|
|
|
CurrentPage uint
|
|
|
|
TotalCount uint64
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// APIKeyOrder is used for querying api keys in specified order.
|
2019-09-12 15:19:30 +01:00
|
|
|
type APIKeyOrder uint8
|
|
|
|
|
|
|
|
const (
|
2020-08-11 15:50:01 +01:00
|
|
|
// KeyName indicates that we should order by key name.
|
2019-09-12 15:19:30 +01:00
|
|
|
KeyName APIKeyOrder = 1
|
2020-08-11 15:50:01 +01:00
|
|
|
// CreationDate indicates that we should order by creation date.
|
2019-09-12 15:19:30 +01:00
|
|
|
CreationDate APIKeyOrder = 2
|
|
|
|
)
|