auth

package
v0.0.0-...-0044c33 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package auth provides authentication and authorization support.

Index

Constants

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

These are the expected values for Claims.Roles.

Variables

View Source
var ErrForbidden = errors.New("attempted action is not allowed")

ErrForbidden is returned when a auth issue is identified.

Functions

func SetClaims

func SetClaims(ctx context.Context, claims Claims) context.Context

SetClaims stores the claims in the context.

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(activeKID string, keyLookup KeyLookup) (*Auth, error)

New creates an Auth to support authentication/authorization.

func (*Auth) GenerateToken

func (a *Auth) GenerateToken(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 {
	jwt.RegisteredClaims
	Roles []string `json:"roles"`
}

Claims represents the authorization claims transmitted via a JWT.

func GetClaims

func GetClaims(ctx context.Context) (Claims, error)

GetClaims returns the claims from the context.

func (Claims) Authorized

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

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

type KeyLookup

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.

Jump to

Keyboard shortcuts

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