apitokens

package
v0.0.0-...-ccb06fb Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 10 Imported by: 15

README

Package cloudeng.io/webapi/operations/apitokens

import cloudeng.io/webapi/operations/apitokens

Functions

Func ContextWithTokens
func ContextWithTokens(ctx context.Context, name string, tokens []byte) context.Context

ContextWithTokens returns a new context that contains the provided named tokens in addition to any existing tokens. The tokens are typically encoded as JSON or YAML.

Func ParseTokensYAML
func ParseTokensYAML(ctx context.Context, name string, cfg any) (bool, error)

ParseTokensYAML parses the tokens stored in the context for the specified name as JSON. It will return false if there are no tockens stored, true otherwise and an error if the unmsrshal fails.

Func TokensFromContext
func TokensFromContext(ctx context.Context, name string) ([]byte, bool)

TokensFromContext returns the tokens for the specified name, if any, that are stored in the context.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultReaders = &Readers{}

DefaultReaders is the default registry of readers, it includes readers for local files, literal values and environment variables.

Functions

func ContextWithToken

func ContextWithToken(ctx context.Context, name string, token *T) context.Context

ContextWithToken returns a new context that contains the provided named token in addition to any existing tokens.

func ContextWithTokens deprecated

func ContextWithTokens(ctx context.Context, name string, tokens []byte) context.Context

ContextWithTokens returns a new context that contains the provided named tokens in addition to any existing tokens. The tokens are typically encoded as JSON or YAML.

Deprecated: use ContextWithToken instead.

func ParseTokensJSON deprecated

func ParseTokensJSON(ctx context.Context, name string, cfg any) (bool, error)

ParseTokensJSON parses the tokens stored in the context for the specified name as JSON. It will return false if there are no tokens stored, true otherwise and an error if the unmarshal fails.

Deprecated: use TokenFromContext instead.

func ParseTokensYAML deprecated

func ParseTokensYAML(ctx context.Context, name string, cfg any) (bool, error)

ParseTokensYAML parses the tokens stored in the context for the specified name as YAML. It will return false if there are no tokens stored, true otherwise and an error if the unmarshal fails.

Deprecated: use TokenFromContext instead.

func TokensFromContext deprecated

func TokensFromContext(ctx context.Context, name string) ([]byte, bool)

TokensFromContext returns the tokens for the specified name, if any, that are stored in the context.

Deprecated: use TokenFromContext instead.

Types

type EnvVarReader

type EnvVarReader struct{}

EnvVarReader implements a Reader for environment variables. The text should be 'env://<env-var-name>'.

func (*EnvVarReader) ReadFileCtx

func (r *EnvVarReader) ReadFileCtx(_ context.Context, name string) ([]byte, error)

type LiteralReader

type LiteralReader struct{}

LiteralReader implements a Reader for a literal token. The text should be 'literal://<token>'. ReadFile will return the text string it is called with.

func (*LiteralReader) ReadFileCtx

func (*LiteralReader) ReadFileCtx(_ context.Context, text string) ([]byte, error)

type Reader

type Reader interface {
	ReadFileCtx(ctx context.Context, path string) ([]byte, error)
}

Reader is the interface that must be implemented by any token reader. ReadFileCtx is called with the path of the token and should return the value of the token, that is, the scheme (eg. file://) should be stripped from the path before calling ReadFileCtx.

type Readers

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

Readers is a registry of token readers.

func CloneReaders

func CloneReaders(readers *Readers) *Readers

CloneReaders returns a copy of the supplied readers.

func (*Readers) Lookup

func (r *Readers) Lookup(scheme string) (Reader, bool)

Lookup returns the reader for the specified scheme, if any.

func (*Readers) Register

func (r *Readers) Register(scheme string, rd Reader)

Register registers a reader for the specified scheme.

func (*Readers) Schemes

func (r *Readers) Schemes() []string

Schemes returns the list of registered schemes.

type T

type T struct {
	Scheme string
	Path   string
	// contains filtered or unexported fields
}

T represents a token that can be used to authenticate with an API. Tokens are typically of the form scheme://value where scheme is used to identify the reader that should be used to read the value.

func New

func New(text string) *T

New creates a token from the supplied text. If the text does not contain a scheme, the entire text is used as the value of the token and the scheme is the empty string. It is left to the caller to determine the appropriate interpretation of the value.

func TokenFromContext

func TokenFromContext(ctx context.Context, name string) (T, bool)

TokenFromContext returns the token for the specified name, if any, that are stored in the context.

func (*T) Clone

func (t *T) Clone() T

Clone creates a copy of a Token that does not share any state with the original.

func (*T) Read

func (t *T) Read(ctx context.Context, registry *Readers) error

Read read's the token value using the supplied registry of Readers.

func (T) String

func (t T) String() string

String returns a string representation of the token with the value redacted.

func (*T) Token

func (t *T) Token() []byte

Token returns the value of the token.

Jump to

Keyboard shortcuts

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