auth

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleAdmin = "ADMIN"
	RoleUser  = "USER"
)

These are the expected values for Claims.Roles.

View Source
const Key ctxKey = 1

Key is used to store/retrieve a Claims value from a context.Context.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

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

Auth is used to authenticate clients. It can generate a token for a set of user claims and recreate the claims by parsing the token.

func New

func New(algorithm string, keyLookup KeyLookup) (*Auth, error)

New creates an *Auth for use. It will error if: - The private key is nil. - The public key func is nil. - The key ID is blank. - The specified algorithm is unsupported.

func (*Auth) GenerateToken

func (a *Auth) GenerateToken(kid string, claims Claims) (string, error)

GenerateToken generates a signed JWT token string representing the user Claims.

func (*Auth) ValidateToken

func (a *Auth) ValidateToken(tokenStr string) (Claims, error)

ValidateToken recreates the Claims that were used to generate a token. It verifies that the token was signed using our key.

type Claims

type Claims struct {
	Roles []string `json:"roles"`
	jwt.StandardClaims
}

Claims represents the authorization claims transmitted via a JWT.

func (Claims) Authorized

func (c Claims) Authorized(roles ...string) bool

Authorized returns true if the claims has at least one of the provided roles.

func (Claims) Valid

func (c Claims) Valid(h *jwt.ValidationHelper) error

Valid is called during the parsing of a token.

type KeyLookup added in v0.3.3

type KeyLookup interface {
	PrivateKey(kid string) (*rsa.PrivateKey, error)
	PublicKey(kid string) (*rsa.PublicKey, error)
}

KeyLookup declares a method set of behavior for looking up private and public keys for JWT use.

type Keys

type Keys map[string]*rsa.PrivateKey

Keys represents an in memory store of keys.

Jump to

Keyboard shortcuts

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