token

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Timestamp is a replacable function for getting the current time,
	// can be overridden for tests
	Timestamp = func() time.Time { return time.Now() }
	// ErrTokenNotFound is returned by stores that cannot find an access token
	// for a given key
	ErrTokenNotFound = errors.New("access token not found")
	// ErrInvalidToken indicates an access token is invalid
	ErrInvalidToken = errors.New("invalid access token")
	// DefaultTokenTTL is the default
	DefaultTokenTTL = time.Hour * 24 * 14
)

Functions

func AddContextTokenToRequest

func AddContextTokenToRequest(ctx context.Context, r *http.Request) (*http.Request, bool)

AddContextTokenToRequest checks the supplied context for an auth token and adds it to an http request, returns true if a token is added

func AddToContext

func AddToContext(ctx context.Context, s string) context.Context

AddToContext adds a token string to a context

func FromCtx

func FromCtx(ctx context.Context) string

FromCtx extracts the JWT from a given context if one is set, returning nil otherwise

func NewPrivKeyAuthToken

func NewPrivKeyAuthToken(pk crypto.PrivKey, profileID string, ttl time.Duration) (string, error)

NewPrivKeyAuthToken creates a JWT token string suitable for making requests authenticated as the given private key

func OAuthTokenMiddleware

func OAuthTokenMiddleware(next http.Handler) http.Handler

OAuthTokenMiddleware parses any "authorization" header containing a Bearer token & adds it to the request context

Types

type Claims

type Claims struct {
	*jwt.StandardClaims
	ProfileID string `json:"profileID"`
}

Claims is a JWT Claims object

type CtxKey

type CtxKey string

CtxKey defines a distinct type for context keys used by the access package

type RawToken

type RawToken struct {
	Key string
	Raw string
}

RawToken is a struct that binds a key to a raw token string

type RawTokens

type RawTokens []RawToken

RawTokens is a list of tokens that implements sorting by keys

func (RawTokens) Len

func (rts RawTokens) Len() int

func (RawTokens) Less

func (rts RawTokens) Less(a, b int) bool

func (RawTokens) Swap

func (rts RawTokens) Swap(i, j int)

type Source

type Source interface {
	CreateToken(pro *profile.Profile, ttl time.Duration) (string, error)
	CreateTokenWithClaims(claims jwt.MapClaims, ttl time.Duration) (string, error)
	// VerifyKey returns the verification key for a given token
	VerificationKey(t *Token) (interface{}, error)
}

Source creates tokens, and provides a verification key for all tokens it creates

implementations of Source must conform to the assertion test defined in the spec subpackage

func NewPrivKeySource

func NewPrivKeySource(privKey crypto.PrivKey) (Source, error)

NewPrivKeySource creates an authentication interface backed by a single private key. Intended for a node running as remote, or providing a public API

type Store

type Store interface {
	PutToken(ctx context.Context, key, rawToken string) error
	RawToken(ctx context.Context, key string) (rawToken string, err error)
	DeleteToken(ctx context.Context, key string) (err error)
	ListTokens(ctx context.Context, offset, limit int) (results []RawToken, err error)
}

Store is a store intended for clients, who need to persist secret jwts given to them by other remotes for API access. It deals in raw, string-formatted json web tokens, which are more useful when working with APIs, but validates the tokens are well-formed when placed in the store

implementations of Store must conform to the assertion test defined in the spec subpackage

func NewStore

func NewStore(filepath string, fs qfs.Filesystem) (Store, error)

NewStore creates a token store with a qfs.Filesystem

type Token

type Token = jwt.Token

Token abstracts a json web token

func Parse

func Parse(tokenString string, tokens Source) (*Token, error)

Parse will parse, validate and return a token

func ParseAuthToken

func ParseAuthToken(tokenString string, keystore key.Store) (*Token, error)

ParseAuthToken will parse, validate and return a token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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