2019-04-25 04:52:08 +07:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-05-05 05:06:05 +07:00
|
|
|
"net/url"
|
2021-08-10 07:31:08 +07:00
|
|
|
"path"
|
2019-04-25 04:52:08 +07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-01-21 06:46:13 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2019-12-11 14:52:02 +07:00
|
|
|
// NewRequestLogger create a new request logger for the given request.
|
|
|
|
func NewRequestLogger(ctx *AutheliaCtx) *logrus.Entry {
|
2022-01-21 06:46:13 +07:00
|
|
|
return logging.Logger().WithFields(logrus.Fields{
|
2019-12-11 14:52:02 +07:00
|
|
|
"method": string(ctx.Method()),
|
|
|
|
"path": string(ctx.Path()),
|
|
|
|
"remote_ip": ctx.RemoteIP().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
// NewAutheliaCtx instantiate an AutheliaCtx out of a RequestCtx.
|
|
|
|
func NewAutheliaCtx(ctx *fasthttp.RequestCtx, configuration schema.Configuration, providers Providers) (*AutheliaCtx, error) {
|
|
|
|
autheliaCtx := new(AutheliaCtx)
|
|
|
|
autheliaCtx.RequestCtx = ctx
|
|
|
|
autheliaCtx.Providers = providers
|
|
|
|
autheliaCtx.Configuration = configuration
|
2019-12-11 14:52:02 +07:00
|
|
|
autheliaCtx.Logger = NewRequestLogger(autheliaCtx)
|
2020-01-18 05:48:48 +07:00
|
|
|
autheliaCtx.Clock = utils.RealClock{}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return autheliaCtx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutheliaMiddleware is wrapping the RequestCtx into an AutheliaCtx providing Authelia related objects.
|
2021-05-05 05:06:05 +07:00
|
|
|
func AutheliaMiddleware(configuration schema.Configuration, providers Providers) RequestHandlerBridge {
|
2019-04-25 04:52:08 +07:00
|
|
|
return func(next RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
autheliaCtx, err := NewAutheliaCtx(ctx, configuration, providers)
|
|
|
|
if err != nil {
|
2021-07-22 10:52:37 +07:00
|
|
|
autheliaCtx.Error(err, messageOperationFailed)
|
2019-04-25 04:52:08 +07:00
|
|
|
return
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
next(autheliaCtx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 02:50:58 +07:00
|
|
|
// Error reply with an error and display the stack trace in the logs.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) Error(err error, message string) {
|
|
|
|
ctx.SetJSONError(message)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Error(err)
|
2021-11-29 10:09:14 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetJSONError sets the body of the response to an JSON error KO message.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) SetJSONError(message string) {
|
2019-11-17 08:05:46 +07:00
|
|
|
b, marshalErr := json.Marshal(ErrorResponse{Status: "KO", Message: message})
|
|
|
|
|
|
|
|
if marshalErr != nil {
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Error(marshalErr)
|
2019-11-17 08:05:46 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.SetContentType(contentTypeApplicationJSON)
|
|
|
|
ctx.SetBody(b)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyError reply with an error but does not display any stack trace in the logs.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ReplyError(err error, message string) {
|
2019-11-17 08:05:46 +07:00
|
|
|
b, marshalErr := json.Marshal(ErrorResponse{Status: "KO", Message: message})
|
|
|
|
|
|
|
|
if marshalErr != nil {
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Error(marshalErr)
|
2019-11-17 08:05:46 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.SetContentType(contentTypeApplicationJSON)
|
|
|
|
ctx.SetBody(b)
|
|
|
|
ctx.Logger.Debug(err)
|
2019-11-17 02:50:58 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyUnauthorized response sent when user is unauthorized.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ReplyUnauthorized() {
|
|
|
|
ctx.RequestCtx.Error(fasthttp.StatusMessage(fasthttp.StatusUnauthorized), fasthttp.StatusUnauthorized)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyForbidden response sent when access is forbidden to user.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ReplyForbidden() {
|
|
|
|
ctx.RequestCtx.Error(fasthttp.StatusMessage(fasthttp.StatusForbidden), fasthttp.StatusForbidden)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
// ReplyBadRequest response sent when bad request has been sent.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ReplyBadRequest() {
|
|
|
|
ctx.RequestCtx.Error(fasthttp.StatusMessage(fasthttp.StatusBadRequest), fasthttp.StatusBadRequest)
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedProto return the content of the X-Forwarded-Proto header.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) XForwardedProto() (proto []byte) {
|
|
|
|
proto = ctx.RequestCtx.Request.Header.PeekBytes(headerXForwardedProto)
|
|
|
|
|
|
|
|
if proto == nil {
|
|
|
|
if ctx.RequestCtx.IsTLS() {
|
|
|
|
return protoHTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoHTTP
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedMethod return the content of the X-Forwarded-Method header.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) XForwardedMethod() (method []byte) {
|
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXForwardedMethod)
|
2021-03-05 11:18:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XForwardedHost return the content of the X-Forwarded-Host header.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) XForwardedHost() (host []byte) {
|
|
|
|
host = ctx.RequestCtx.Request.Header.PeekBytes(headerXForwardedHost)
|
|
|
|
|
|
|
|
if host == nil {
|
|
|
|
return ctx.RequestCtx.Host()
|
|
|
|
}
|
|
|
|
|
|
|
|
return host
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedURI return the content of the X-Forwarded-URI header.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) XForwardedURI() (uri []byte) {
|
|
|
|
uri = ctx.RequestCtx.Request.Header.PeekBytes(headerXForwardedURI)
|
|
|
|
|
|
|
|
if len(uri) == 0 {
|
|
|
|
return ctx.RequestCtx.RequestURI()
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-08-10 07:31:08 +07:00
|
|
|
// BasePath returns the base_url as per the path visited by the client.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) BasePath() (base string) {
|
|
|
|
if baseURL := ctx.UserValueBytes(UserValueKeyBaseURL); baseURL != nil {
|
2021-08-10 07:31:08 +07:00
|
|
|
return baseURL.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return base
|
|
|
|
}
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-08-10 07:31:08 +07:00
|
|
|
// ExternalRootURL gets the X-Forwarded-Proto, X-Forwarded-Host headers and the BasePath and forms them into a URL.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ExternalRootURL() (string, error) {
|
|
|
|
protocol := ctx.XForwardedProto()
|
2021-08-10 07:31:08 +07:00
|
|
|
if protocol == nil {
|
2021-05-05 05:06:05 +07:00
|
|
|
return "", errMissingXForwardedProto
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
host := ctx.XForwardedHost()
|
2021-08-10 07:31:08 +07:00
|
|
|
if host == nil {
|
2021-05-05 05:06:05 +07:00
|
|
|
return "", errMissingXForwardedHost
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:31:08 +07:00
|
|
|
externalRootURL := fmt.Sprintf("%s://%s", protocol, host)
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
if base := ctx.BasePath(); base != "" {
|
2021-08-10 07:31:08 +07:00
|
|
|
externalBaseURL, err := url.Parse(externalRootURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
externalBaseURL.Path = path.Join(externalBaseURL.Path, base)
|
|
|
|
|
|
|
|
return externalBaseURL.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalRootURL, nil
|
2021-05-05 05:06:05 +07:00
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XOriginalURL return the content of the X-Original-URL header.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) XOriginalURL() []byte {
|
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXOriginalURL)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession return the user session. Any update will be saved in cache.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) GetSession() session.UserSession {
|
|
|
|
userSession, err := ctx.Providers.SessionProvider.GetSession(ctx.RequestCtx)
|
2020-01-18 05:48:48 +07:00
|
|
|
if err != nil {
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Error("Unable to retrieve user session")
|
2020-01-18 05:48:48 +07:00
|
|
|
return session.NewDefaultUserSession()
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
return userSession
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSession save the content of the session.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) SaveSession(userSession session.UserSession) error {
|
|
|
|
return ctx.Providers.SessionProvider.SaveSession(ctx.RequestCtx, userSession)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyOK is a helper method to reply ok.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ReplyOK() {
|
|
|
|
ctx.SetContentType(contentTypeApplicationJSON)
|
|
|
|
ctx.SetBody(okMessageBytes)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ParseBody parse the request body into the type of value.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) ParseBody(value interface{}) error {
|
|
|
|
err := json.Unmarshal(ctx.PostBody(), &value)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2021-11-29 10:09:14 +07:00
|
|
|
return fmt.Errorf("unable to parse body: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := govalidator.ValidateStruct(value)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-29 10:09:14 +07:00
|
|
|
return fmt.Errorf("unable to validate body: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("Body is not valid")
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// SetJSONBody Set json body.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) SetJSONBody(value interface{}) error {
|
2019-04-25 04:52:08 +07:00
|
|
|
b, err := json.Marshal(OKResponse{Status: "OK", Data: value})
|
|
|
|
if err != nil {
|
2021-11-29 10:09:14 +07:00
|
|
|
return fmt.Errorf("unable to marshal JSON body: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.SetContentType(contentTypeApplicationJSON)
|
|
|
|
ctx.SetBody(b)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteIP return the remote IP taking X-Forwarded-For header into account if provided.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) RemoteIP() net.IP {
|
|
|
|
XForwardedFor := ctx.Request.Header.PeekBytes(headerXForwardedFor)
|
2019-04-25 04:52:08 +07:00
|
|
|
if XForwardedFor != nil {
|
|
|
|
ips := strings.Split(string(XForwardedFor), ",")
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
2019-12-11 14:29:32 +07:00
|
|
|
return net.ParseIP(strings.Trim(ips[0], " "))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
return ctx.RequestCtx.RemoteIP()
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
// GetOriginalURL extract the URL from the request headers (X-Original-URL or X-Forwarded-* headers).
|
|
|
|
func (ctx *AutheliaCtx) GetOriginalURL() (*url.URL, error) {
|
|
|
|
originalURL := ctx.XOriginalURL()
|
2021-05-05 05:06:05 +07:00
|
|
|
if originalURL != nil {
|
|
|
|
parsedURL, err := url.ParseRequestURI(string(originalURL))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse URL extracted from X-Original-URL header: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Trace("Using X-Original-URL header content as targeted site URL")
|
2021-05-05 05:06:05 +07:00
|
|
|
|
|
|
|
return parsedURL, nil
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
forwardedProto, forwardedHost, forwardedURI := ctx.XForwardedProto(), ctx.XForwardedHost(), ctx.XForwardedURI()
|
2021-05-05 05:06:05 +07:00
|
|
|
|
|
|
|
if forwardedProto == nil {
|
|
|
|
return nil, errMissingXForwardedProto
|
|
|
|
}
|
|
|
|
|
|
|
|
if forwardedHost == nil {
|
|
|
|
return nil, errMissingXForwardedHost
|
|
|
|
}
|
|
|
|
|
|
|
|
var requestURI string
|
|
|
|
|
2021-06-11 07:30:53 +07:00
|
|
|
scheme := forwardedProto
|
|
|
|
scheme = append(scheme, protoHostSeparator...)
|
2021-05-05 05:06:05 +07:00
|
|
|
requestURI = string(append(scheme,
|
|
|
|
append(forwardedHost, forwardedURI...)...))
|
|
|
|
|
|
|
|
parsedURL, err := url.ParseRequestURI(requestURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse URL %s: %v", requestURI, err)
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Logger.Tracef("Using X-Fowarded-Proto, X-Forwarded-Host and X-Forwarded-URI headers " +
|
2021-05-05 05:06:05 +07:00
|
|
|
"to construct targeted site URL")
|
|
|
|
|
|
|
|
return parsedURL, nil
|
|
|
|
}
|
2021-07-22 10:52:37 +07:00
|
|
|
|
|
|
|
// IsXHR returns true if the request is a XMLHttpRequest.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx AutheliaCtx) IsXHR() (xhr bool) {
|
|
|
|
requestedWith := ctx.Request.Header.PeekBytes(headerXRequestedWith)
|
2021-07-22 10:52:37 +07:00
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
return requestedWith != nil && strings.EqualFold(string(requestedWith), headerValueXRequestedWithXHR)
|
2021-07-22 10:52:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptsMIME takes a mime type and returns true if the request accepts that type or the wildcard type.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx AutheliaCtx) AcceptsMIME(mime string) (acceptsMime bool) {
|
|
|
|
accepts := strings.Split(string(ctx.Request.Header.PeekBytes(headerAccept)), ",")
|
2021-07-22 10:52:37 +07:00
|
|
|
|
|
|
|
for i, accept := range accepts {
|
|
|
|
mimeType := strings.Trim(strings.SplitN(accept, ";", 2)[0], " ")
|
|
|
|
if mimeType == mime || (i == 0 && mimeType == "*/*") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpecialRedirect performs a redirect similar to fasthttp.RequestCtx except it allows statusCode 401 and includes body
|
|
|
|
// content in the form of a link to the location.
|
2022-02-06 20:37:28 +07:00
|
|
|
func (ctx *AutheliaCtx) SpecialRedirect(uri string, statusCode int) {
|
2021-07-22 10:52:37 +07:00
|
|
|
if statusCode < fasthttp.StatusMovedPermanently || (statusCode > fasthttp.StatusSeeOther && statusCode != fasthttp.StatusTemporaryRedirect && statusCode != fasthttp.StatusPermanentRedirect && statusCode != fasthttp.StatusUnauthorized) {
|
|
|
|
statusCode = fasthttp.StatusFound
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.SetContentType(contentTypeTextHTML)
|
|
|
|
ctx.SetStatusCode(statusCode)
|
2021-07-22 10:52:37 +07:00
|
|
|
|
|
|
|
u := fasthttp.AcquireURI()
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.URI().CopyTo(u)
|
2021-07-22 10:52:37 +07:00
|
|
|
u.Update(uri)
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.Response.Header.SetBytesV("Location", u.FullURI())
|
2021-07-22 10:52:37 +07:00
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.SetBodyString(fmt.Sprintf("<a href=\"%s\">%s</a>", utils.StringHTMLEscape(string(u.FullURI())), fasthttp.StatusMessage(statusCode)))
|
2021-07-22 10:52:37 +07:00
|
|
|
|
|
|
|
fasthttp.ReleaseURI(u)
|
|
|
|
}
|