auth

package
v0.0.0-...-c2300e3 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package auth provides authentication and authorization support. Authentication: You are who you say you are. Authorization: You have permission to do what you are requesting to do.

Index

Constants

View Source
const (
	RuleAuthenticate   = "auth"
	RuleAny            = "ruleAny"
	RuleAdminOnly      = "ruleAdminOnly"
	RuleUserOnly       = "ruleUserOnly"
	RuleAdminOrSubject = "ruleAdminOrSubject"
)

These the current set of rules we have for auth.

Variables

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

ErrForbidden is returned when a auth issue is identified.

Functions

func IsAuthError

func IsAuthError(err error) bool

IsAuthError checks if an error of type authError exists.

func NewAuthError

func NewAuthError(format string, args ...any) error

NewAuthError creates an authError for the provided message.

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(cfg Config) (*Auth, error)

New creates an Auth to support authentication/authorization.

func (*Auth) Authenticate

func (a *Auth) Authenticate(ctx context.Context, bearerToken string) (Claims, error)

Authenticate processes the token to validate the sender's token is valid.

func (*Auth) Authorize

func (a *Auth) Authorize(ctx context.Context, claims Claims, userID uuid.UUID, rule string) error

Authorize attempts to authorize the user with the provided input roles, if none of the input roles are within the user's claims, we return an error otherwise the user is authorized.

func (*Auth) GenerateToken

func (a *Auth) GenerateToken(ctx context.Context, kid string, claims Claims) (string, error)

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

type Claims

type Claims struct {
	jwt.RegisteredClaims
	Roles []user.Role `json:"roles"`
}

Claims represents the authorization claims transmitted via a JWT.

func (Claims) HasRole

func (c Claims) HasRole(r user.Role) bool

HasRole checks if the specified role exists.

type Config

type Config struct {
	Log       *logger.Logger
	DB        *sqlx.DB
	KeyLookup KeyLookup
	Issuer    string
}

Config represents information required to initialize auth.

type KeyLookup

type KeyLookup interface {
	PrivateKey(ctx context.Context, kid string) (key string, err error)
	PublicKey(ctx context.Context, kid string) (key string, err error)
}

KeyLookup declares a method set of behavior for looking up private and public keys for JWT use. The return could be a PEM encoded string or a JWS based key.

Jump to

Keyboard shortcuts

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