tokens

package
v0.0.0-...-4c708ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorContextTokenNotFound error = errors.New("token not in context")
	ErrorContextTokenNotValid error = errors.New("token not not valid")
)

Functions

func ScopesMap

func ScopesMap(token Token) map[Scope]struct{}

func StringScopes

func StringScopes(scopes []Scope) []string

func ValidateScopes

func ValidateScopes(token Token, scopes []Scope) error

Types

type ContextProvisioner

type ContextProvisioner struct {
	Provisioner
	Scopes []Scope
}

func (*ContextProvisioner) GetToken

func (cp *ContextProvisioner) GetToken(ctx context.Context) (Token, error)

func (*ContextProvisioner) SetToken

func (cp *ContextProvisioner) SetToken(ctx context.Context, tokenString string) (context.Context, error)

type Controller

type Controller interface {
	Create(context.Context, *CreateOptions) (string, error)
	List(context.Context) ([]Token, error)
	Read(context.Context, string) (Token, error)
	Exchange(context.Context, string, string) (string, error)
	Refresh(context.Context) (string, error)
}

type CreateOptions

type CreateOptions struct {
	User     string
	Nonce    string
	Name     string
	Lifetime time.Duration
	Scopes   []string
	Comment  string
}

type ExchangeOptions

type ExchangeOptions struct {
	ID    string
	Nonce string
}

type LocalController

type LocalController struct {
	// contains filtered or unexported fields
}

Controller implements our core logic for managing token lifecycles

func NewController

func NewController(ctx context.Context, store Store, provisioner Provisioner) *LocalController

Create a NewController with the given store and provisioner

func (LocalController) Create

func (c LocalController) Create(ctx context.Context, options *CreateOptions) (string, error)

Create a token and return it's ID

func (LocalController) Exchange

func (c LocalController) Exchange(ctx context.Context, id string, nonce string) (string, error)

Exchange an id and nonce for a token

func (LocalController) List

func (c LocalController) List(ctx context.Context) ([]Token, error)

List tokens

func (LocalController) Read

func (c LocalController) Read(ctx context.Context, id string) (Token, error)

Read token details from the database

func (LocalController) Refresh

func (c LocalController) Refresh(ctx context.Context) (string, error)

Refresh a token if it has the required permissions

func (LocalController) Validate

func (c LocalController) Validate(ctx context.Context, tokenString string, scopes []Scope) (Token, error)

Validate a given token has all of the scopes required

type Operator

type Operator interface {
	Controller
	Validator
}

type Options

type Options struct {
	TokenID  string
	UserID   string
	Expiry   time.Time
	Lifetime time.Duration
	Scopes   []string
}

type Provisioner

type Provisioner interface {
	New(context.Context, Options) (string, error)
	Validate(context.Context, string, []Scope) (Token, error)
}

type ReadOptions

type ReadOptions struct {
	ID string
}

type RefreshOptions

type RefreshOptions struct {
	TokenID string
	UserID  string
}

type Scope

type Scope string
const (
	// User scopes
	ScopeUserEmail       Scope = "user_email"
	ScopeUserRead        Scope = "user_read"
	ScopeUserImpersonate Scope = "user_impersonate"
	// Token scopes
	ScopeTokenReadSelf Scope = "token_read_self"
	ScopeTokenRefresh  Scope = "token_refresh"
	ScopeTokenCreate   Scope = "token_create"
	ScopeTokenList     Scope = "token_list"
	// Chat scopes
	ScopeChatRead  Scope = "chat_read"
	ScopeChatWrite Scope = "chat_write"
	// Responses scopes
	ScopeResponsesCreate Scope = "responses_create"
	ScopeResponsesRead   Scope = "responses_read"
	ScopeResponsesUpdate Scope = "responses_update"
	ScopeResponsesDelete Scope = "responses_delete"
	ScopeResponsesList   Scope = "responses_list"
	// Webhooks scopes
	ScopeWebhooksCreate Scope = "webhooks_create"
	ScopeWebhooksRead   Scope = "webhooks_read"
	ScopeWebhooksUpdate Scope = "webhooks_update"
	ScopeWebhooksDelete Scope = "webhooks_delete"
	ScopeWebhooksList   Scope = "webhooks_list"
)

type Token

type Token interface {
	GetTokenID() string
	GetCreatedAt() time.Time
	GetLastUsedAt() time.Time
	GetNonce() string
	GetUserID() string

	// Time the token expires
	//
	// This is mainly for book-keeping and clean up rather than if
	// the token is actually valid
	GetExpiry() time.Time
	// The time that new versions of the token should live
	GetLifetime() time.Duration

	// Has the token been exchanged, we shouldn't be able to
	// exchange more than once
	GetExchanged() bool
	GetExchangeBy() time.Time

	// Token name
	GetName() string
	GetComment() string

	GetScopes() []string
}

type TokenKey

type TokenKey struct{}

type Transaction

type Transaction interface {
	Options() Options
	// Commit changes
	Complete() error
	// Rollback changes
	Error() error
}

type Validator

type Validator interface {
	Validate(context.Context, string, []Scope) (Token, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL