auth

package
v0.0.0-...-720b936 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

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")

Functions

func GetUserID

func GetUserID(ctx context.Context) uuid.UUID

GetUserID returns the claims from the context.

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.

func SetClaims

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

SetClaims stores the claims in the context.

func SetUserID

func SetUserID(ctx context.Context, userID uuid.UUID) context.Context

SetUserID stores the user id from the request in the context.

Types

type Auth

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

func New

func New(cfg Config) (*Auth, error)

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

func (*Auth) GenerateToken

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

func GetClaims

func GetClaims(ctx context.Context) Claims

GetClaims returns the claims from the context.

type Config

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

type KeyLookup

type KeyLookup interface {
	PrivateKey(kid string) (key string, err error)
	PublicKey(kid string) (key string, err error)
}

Jump to

Keyboard shortcuts

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