2019-04-24 21:52:08 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-06-14 07:20:13 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/metrics"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/notification"
|
2021-09-17 04:44:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/ntp"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/oidc"
|
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
2022-07-18 00:56:09 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2021-12-01 12:11:29 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/totp"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AutheliaCtx contains all server variables related to Authelia.
|
|
|
|
type AutheliaCtx struct {
|
|
|
|
*fasthttp.RequestCtx
|
|
|
|
|
|
|
|
Logger *logrus.Entry
|
|
|
|
Providers Providers
|
|
|
|
Configuration schema.Configuration
|
2020-01-17 22:48:48 +00:00
|
|
|
|
|
|
|
Clock utils.Clock
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Providers contain all provider provided to Authelia.
|
|
|
|
type Providers struct {
|
|
|
|
Authorizer *authorization.Authorizer
|
|
|
|
SessionProvider *session.Provider
|
|
|
|
Regulator *regulation.Regulator
|
2022-10-20 02:16:36 +00:00
|
|
|
OpenIDConnect *oidc.OpenIDConnectProvider
|
2022-06-14 07:20:13 +00:00
|
|
|
Metrics metrics.Provider
|
2021-09-17 04:44:35 +00:00
|
|
|
NTP *ntp.Provider
|
2019-04-24 21:52:08 +00:00
|
|
|
UserProvider authentication.UserProvider
|
|
|
|
StorageProvider storage.Provider
|
|
|
|
Notifier notification.Notifier
|
2022-07-18 00:56:09 +00:00
|
|
|
Templates *templates.Provider
|
2021-12-01 12:11:29 +00:00
|
|
|
TOTP totp.Provider
|
2022-04-03 00:48:26 +00:00
|
|
|
PasswordPolicy PasswordPolicyProvider
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestHandler represents an Authelia request handler.
|
|
|
|
type RequestHandler = func(*AutheliaCtx)
|
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
// AutheliaMiddleware represent an Authelia middleware.
|
|
|
|
type AutheliaMiddleware = func(next RequestHandler) RequestHandler
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
// Middleware represents a fasthttp middleware.
|
|
|
|
type Middleware = func(next fasthttp.RequestHandler) (handler fasthttp.RequestHandler)
|
2022-05-04 04:47:23 +00:00
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
// Bridge represents the func signature that returns a fasthttp.RequestHandler given a RequestHandler allowing it to
|
|
|
|
// bridge between the two handlers.
|
|
|
|
type Bridge = func(RequestHandler) fasthttp.RequestHandler
|
|
|
|
|
|
|
|
// BridgeBuilder is used to build a Bridge.
|
|
|
|
type BridgeBuilder struct {
|
|
|
|
config schema.Configuration
|
|
|
|
providers Providers
|
|
|
|
preMiddlewares []Middleware
|
|
|
|
postMiddlewares []AutheliaMiddleware
|
|
|
|
}
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
// Basic represents a middleware applied to a fasthttp.RequestHandler.
|
|
|
|
type Basic func(next fasthttp.RequestHandler) (handler fasthttp.RequestHandler)
|
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
// IdentityVerificationCommonArgs contains shared options for both verification start and finish steps.
|
|
|
|
type IdentityVerificationCommonArgs struct {
|
|
|
|
// If true, skip identity verification if the user's AuthenticationLevel is TwoFactor. Otherwise, always perform identity verification.
|
|
|
|
SkipIfAuthLevelTwoFactor bool
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// IdentityVerificationStartArgs represent the arguments used to customize the starting phase
|
|
|
|
// of the identity verification process.
|
|
|
|
type IdentityVerificationStartArgs struct {
|
2022-11-19 05:48:47 +00:00
|
|
|
IdentityVerificationCommonArgs
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Email template needs a subject, a title and the content of the button.
|
|
|
|
MailTitle string
|
|
|
|
MailButtonContent string
|
|
|
|
|
|
|
|
// The target endpoint where to redirect the user when verification process
|
|
|
|
// is completed successfully.
|
|
|
|
TargetEndpoint string
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// The action claim that will be stored in the JWT token.
|
2019-04-24 21:52:08 +00:00
|
|
|
ActionClaim string
|
|
|
|
|
|
|
|
// The function retrieving the identity to who the email will be sent.
|
|
|
|
IdentityRetrieverFunc func(ctx *AutheliaCtx) (*session.Identity, error)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// The function for checking the user in the token is valid for the current action.
|
2019-04-24 21:52:08 +00:00
|
|
|
IsTokenUserValidFunc func(ctx *AutheliaCtx, username string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityVerificationFinishArgs represent the arguments used to customize the finishing phase
|
|
|
|
// of the identity verification process.
|
|
|
|
type IdentityVerificationFinishArgs struct {
|
2022-11-19 05:48:47 +00:00
|
|
|
IdentityVerificationCommonArgs
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// The action claim that should be in the token to consider the action legitimate.
|
2019-04-24 21:52:08 +00:00
|
|
|
ActionClaim string
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// The function for checking the user in the token is valid for the current action.
|
2019-04-24 21:52:08 +00:00
|
|
|
IsTokenUserValidFunc func(ctx *AutheliaCtx, username string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityVerificationFinishBody type of the body received by the finish endpoint.
|
|
|
|
type IdentityVerificationFinishBody struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// OKResponse model of a status OK response.
|
2019-04-24 21:52:08 +00:00
|
|
|
type OKResponse struct {
|
2022-10-05 05:05:23 +00:00
|
|
|
Status string `json:"status"`
|
|
|
|
Data any `json:"data,omitempty"`
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ErrorResponse model of an error response.
|
2019-04-24 21:52:08 +00:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|