Documentation
¶
Overview ¶
Package auth provides authentication and authorization utilities.
Index ¶
- Variables
- func Anonymous[T any](handleError func(http.ResponseWriter, *http.Request, error)) func(http.Handler) http.Handler
- func Authenticate(identify func(context.Context, *sess.Session) (any, error), ...) func(http.Handler) http.Handler
- func Authenticated[T any](handleError func(http.ResponseWriter, *http.Request, error)) func(http.Handler) http.Handler
- func CSRF(handleError func(http.ResponseWriter, *http.Request, error)) func(http.Handler) http.Handler
- func GenerateCSRFToken() string
- func GenerateJWT(jwks JWKS, customClaims Claims, ttl time.Duration) (string, error)
- func GetCSRFToken(ctx context.Context) string
- func GetCSRFTokenOrCreate(ctx context.Context) string
- func GetFreshCSRFToken(ctx context.Context) string
- func GetIdentity[T any](ctx context.Context) (T, error)
- func MustGetIdentity[T any](ctx context.Context) T
- func RefreshCSRFToken(ctx context.Context)
- type Claims
- type JWK
- type JWKS
- type OAuth2AuthStrategy
- type OAuth2Controller
Constants ¶
This section is empty.
Variables ¶
var ( ErrCtxIdentityMissing = errors.New("missing identity context") ErrCtxSessionMissing = errors.New("missing session context") )
var ( ErrJWTActiveKeyMissing = errors.New("missing active key") ErrJWTClaimsParseFailure = errors.New("failed to parse claims") ErrJWTInvalid = errors.New("invalid") ErrJWTKidClaimMissing = errors.New("missing kid claim") ErrJWTKidClaimUnknown = errors.New("unknown kid claim") ErrJWTSignFailure = errors.New("failed to sign") )
var ( ErrOAuthAuthenticateFailure = errors.New("failed to authenticate") ErrOAuthCSRFTokenMismatch = errors.New("CSRF mismatch") ErrOAuthExchangeFailure = errors.New("failed to exchange authorization code") ErrOAuthInitiateFailure = errors.New("failed to initiate") )
var CSRFTokenKey = "csrf-token"
var ErrAuthIdentifyFailure = errors.New("failed to identify")
var ErrCSRFTokenMismatch = errors.New("CSRF mismatch")
var OAuthCSRFTokenKey = "oauth-csrf-token"
OAuthCSRFTokenKey is the session key for the OAuth CSRF token.
Functions ¶
func Authenticate ¶
func Authenticate( identify func(context.Context, *sess.Session) (any, error), handleError func(http.ResponseWriter, *http.Request, error), ) func(http.Handler) http.Handler
Authenticate authenticates the user and sets the identity in the context.
func Authenticated ¶
func Authenticated[T any]( handleError func(http.ResponseWriter, *http.Request, error), ) func(http.Handler) http.Handler
Authenticated ensures that the user is authenticated.
func CSRF ¶
func CSRF( handleError func(http.ResponseWriter, *http.Request, error), ) func(http.Handler) http.Handler
CSRF protects against cross-site request forgery attacks.
func GenerateCSRFToken ¶
func GenerateCSRFToken() string
GenerateCSRFToken generates a new CSRF token.
func GenerateJWT ¶
GenerateJWT generates a JWT.
func GetCSRFToken ¶
GetCSRFToken returns the CSRF token from the session.
func GetCSRFTokenOrCreate ¶
GetCSRFTokenOrCreate returns the CSRF token from the session or creates a new one.
func GetFreshCSRFToken ¶
GetFreshCSRFToken returns a fresh CSRF token and stores it in the session.
func GetIdentity ¶
GetIdentity returns the identity from the context.
func MustGetIdentity ¶
MustGetIdentity returns the identity from the context or panics if it is missing.
func RefreshCSRFToken ¶
RefreshCSRFToken refreshes the CSRF token in the session.
Types ¶
type JWKS ¶
JWKS represents a map of JSON Web Keys.
type OAuth2AuthStrategy ¶
type OAuth2AuthStrategy interface {
Initiate(*http.Request, *sess.Session) error
Authenticate(context.Context, *oauth2.Token, *sess.Session) (string, error)
HandleError(http.ResponseWriter, *http.Request, error)
}
OAuth2AuthStrategy is the interface for OAuth2 authentication strategies.
type OAuth2Controller ¶
type OAuth2Controller struct {
// contains filtered or unexported fields
}
OAuth2Controller is a controller for OAuth2 authentication.
func NewOAuth2Controller ¶
func NewOAuth2Controller( config *oauth2.Config, strategy OAuth2AuthStrategy, ) *OAuth2Controller
NewOAuth2Controller creates a new OAuth2Controller.
func (*OAuth2Controller) Callback ¶
func (p *OAuth2Controller) Callback(w http.ResponseWriter, r *http.Request)
Callback handles the OAuth2 callback.
func (*OAuth2Controller) Login ¶
func (p *OAuth2Controller) Login(w http.ResponseWriter, r *http.Request)
Login initiates the OAuth2 login flow.