storj/satellite/console/apikeys.go
Cameron 84b522bc06 satellite/console: create account management api keys service
We are in the process of creating an api to allow users to manage their
accounts programmatically. We would like to use api keys for
authorization. We were originally going to create an entirely new table
for these api keys, but seeing as we already have 2 other tables for
keys/tokens, api_keys and oauth_tokens, we thought it might be better to
use one of these. We're using oauth_tokens.

We create a new oidc.OAuthTokenKind for account management api keys:
KindAccountManagementTokenV0. We made the key versioned because we
likely want to improve the implementation in the future, but we want to
get something functional out the door ASAP because the account management
api feature is highly desired.

Add a new method to oidc.OAuthTokens interface for revoking v0 account
management api keys, RevokeAccountManagementTokenV0. Add update method
to dbx implementation to allow updating the expiration. We will revoke
these keys by setting the expiration to 0 so they are expired.

Change-Id: Ideb8ae04b23aa55d5825b064b5e43e32eadc1fba
2022-03-23 17:02:20 +00:00

85 lines
2.7 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package console
import (
"context"
"time"
"storj.io/common/uuid"
)
// APIKeys is interface for working with api keys store.
//
// architecture: Database
type APIKeys interface {
// 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)
// Get retrieves APIKeyInfo with given ID
Get(ctx context.Context, id uuid.UUID) (*APIKeyInfo, error)
// GetByHead retrieves APIKeyInfo for given key head
GetByHead(ctx context.Context, head []byte) (*APIKeyInfo, error)
// GetByNameAndProjectID retrieves APIKeyInfo for given key name and projectID
GetByNameAndProjectID(ctx context.Context, name string, projectID uuid.UUID) (*APIKeyInfo, error)
// Create creates and stores new APIKeyInfo
Create(ctx context.Context, head []byte, info APIKeyInfo) (*APIKeyInfo, error)
// Update updates APIKeyInfo in store
Update(ctx context.Context, key APIKeyInfo) error
// Delete deletes APIKeyInfo from store
Delete(ctx context.Context, id uuid.UUID) error
}
// AccountManagementAPIKeys is an interface for account management api key operations.
type AccountManagementAPIKeys interface {
Create(ctx context.Context, userID uuid.UUID, expiration time.Duration) (apiKey string, expiresAt time.Time, err error)
GetUserFromKey(ctx context.Context, apiKey string) (userID uuid.UUID, err error)
Revoke(ctx context.Context, apiKey string) (err error)
}
// APIKeyInfo describing api key model in the database.
type APIKeyInfo struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"projectId"`
PartnerID uuid.UUID `json:"partnerId"`
UserAgent []byte `json:"userAgent"`
Name string `json:"name"`
Head []byte `json:"-"`
Secret []byte `json:"-"`
CreatedAt time.Time `json:"createdAt"`
}
// APIKeyCursor holds info for api keys cursor pagination.
type APIKeyCursor struct {
Search string
Limit uint
Page uint
Order APIKeyOrder
OrderDirection OrderDirection
}
// APIKeyPage represent api key page result.
type APIKeyPage struct {
APIKeys []APIKeyInfo
Search string
Limit uint
Order APIKeyOrder
OrderDirection OrderDirection
Offset uint64
PageCount uint
CurrentPage uint
TotalCount uint64
}
// APIKeyOrder is used for querying api keys in specified order.
type APIKeyOrder uint8
const (
// KeyName indicates that we should order by key name.
KeyName APIKeyOrder = 1
// CreationDate indicates that we should order by creation date.
CreationDate APIKeyOrder = 2
)