auth

package
v0.0.0-...-2e936b0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

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

Variables

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

ErrForbidden is returned when auth issue is identified.

Functions

func GetUserID

func GetUserID(ctx context.Context) uuid.UUID

GetUserID returns user ID from the context.

func IsAuthError

func IsAuthError(err error) bool

func NewError

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

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 strores user ID from the request in the context.

Types

type Auth

type Auth interface {
	GenerateToken(ctx context.Context, claims Claims) (string, error)
	Authenticate(ctx context.Context, barerToeken string) (Claims, error)
	Authorize(ctx context.Context, claims Claims, userID uuid.UUID, rule string) error
}

type Claims

type Claims struct {
	jwt.RegisteredClaims
	User  User        `json:"user"`
	Roles []user.Role `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
	KeyLookup KeyLookup
	ActiveKID string
	DB        *sqlx.DB
}

Config represents information required to initialize auth.

type Error

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

func (*Error) Error

func (ae *Error) Error() string

type KeyLookup

type KeyLookup interface {
	PrivateKeyPEM(kid string) (pem string, err error)
	PublicKeyPEM(kid string) (pem string, err error)
}

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

type Mock

type Mock struct {
	mock.Mock
}

func NewMock

func NewMock() *Mock

func (*Mock) Authenticate

func (m *Mock) Authenticate(ctx context.Context, barerToken string) (Claims, error)

func (*Mock) Authorize

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

func (*Mock) GenerateToken

func (m *Mock) GenerateToken(ctx context.Context, claims Claims) (string, error)

type Usecase

type Usecase 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) *Usecase

func (*Usecase) Authenticate

func (a *Usecase) Authenticate(ctx context.Context, barerToken string) (Claims, error)

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

func (*Usecase) Authorize

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

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

func (*Usecase) GenerateToken

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

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

type User

type User struct {
	Username string    `json:"username"`
	ID       uuid.UUID `json:"id"`
}

Jump to

Keyboard shortcuts

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