auth

package
v0.0.0-...-43686f6 Latest Latest
Warning

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

Go to latest
Published: May 23, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const PermissionsClaim = "permissions"
View Source
const TypeClaim = "type"
View Source
const TypePasswordReset = "password_reset"
View Source
const TypeVerifyAccount = "verify_account"
View Source
const UserIDContextKey = "userID"

Variables

View Source
var (
	ErrNoAuthHeader      = errors.New("authorization header is missing")
	ErrInvalidAuthHeader = errors.New("authorization header is malformed")
	ErrClaimsInvalid     = errors.New("provided claims do not match expected scopes")
)

Functions

func Authenticate

Authenticate uses the specified validator to ensure a JWT is valid, then makes sure that the claims provided by the JWT match the scopes as required in the API.

func GetAuthMiddleware

func GetAuthMiddleware(swagger *openapi3.T, v JWSVerifier) func(next http.Handler) http.Handler

GetAuthMiddleware returns a middleware for validating requests against the OpenAPI spec.

func GetUserIDFromContext

func GetUserIDFromContext(ctx context.Context) (uuid.UUID, error)

GetUserIDFromContext retrieves the user ID from the context.

func GetUserIDFromToken

func GetUserIDFromToken(t jwt.Token) (uuid.UUID, error)

Get userID from token

Types

type JWSSigner

type JWSSigner interface {
	CreateAccessToken(userID uuid.UUID, claims []string) (string, time.Duration, error)
	CreateRefreshToken(userID uuid.UUID) (string, time.Duration, error)
	CreatePasswordResetToken(userID uuid.UUID) (string, time.Duration, error)
	CreateVerifyAccountToken(userID uuid.UUID) (string, time.Duration, error)
}

type JWSVerifier

type JWSVerifier interface {
	ValidateToken(jws string) (jwt.Token, error)
	ValidatePasswordResetToken(jws string) (jwt.Token, error)
}

type LocalJWSSigner

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

func NewLocalJWSSigner

func NewLocalJWSSigner(
	privateKeySource source.SourceProvider,
	issuer,
	audience string,
	accessTokenExpiresIn time.Duration,
	refreshTokenExpiresIn time.Duration,
) (*LocalJWSSigner, error)

func (*LocalJWSSigner) CreateAccessToken

func (s *LocalJWSSigner) CreateAccessToken(userID uuid.UUID, claims []string) (string, time.Duration, error)

CreateAccessToken creates a JWS with the given user ID and claims. The claims are added to the "permissions" claim in the JWS.

func (*LocalJWSSigner) CreatePasswordResetToken

func (s *LocalJWSSigner) CreatePasswordResetToken(userID uuid.UUID) (string, time.Duration, error)

CreatePasswordResetToken creates a refresh JWS

func (*LocalJWSSigner) CreateRefreshToken

func (s *LocalJWSSigner) CreateRefreshToken(userID uuid.UUID) (string, time.Duration, error)

CreateRefreshToken creates a refresh JWS

func (*LocalJWSSigner) CreateVerifyAccountToken

func (s *LocalJWSSigner) CreateVerifyAccountToken(userID uuid.UUID) (string, time.Duration, error)

CreateVerifyAccountToken creates a refresh JWS

type LocalJWSVerifier

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

func NewLocalJWSVerifier

func NewLocalJWSVerifier(publicKeySource source.SourceProvider, issuer, audience string) (*LocalJWSVerifier, error)

func (*LocalJWSVerifier) ValidatePasswordResetToken

func (v *LocalJWSVerifier) ValidatePasswordResetToken(jwsString string) (jwt.Token, error)

ValidatePasswordToken ensures that the critical JWT claims needed to ensure that we trust the JWT are present and with the correct values.

func (*LocalJWSVerifier) ValidateToken

func (v *LocalJWSVerifier) ValidateToken(jwsString string) (jwt.Token, error)

ValidateToken ensures that the critical JWT claims needed to ensure that we trust the JWT are present and with the correct values.

Jump to

Keyboard shortcuts

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