2019-04-25 04:52:08 +07:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/session"
|
2020-01-18 05:48:48 +07:00
|
|
|
"github.com/authelia/authelia/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 {
|
|
|
|
return logrus.WithFields(logrus.Fields{
|
|
|
|
"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.
|
|
|
|
func AutheliaMiddleware(configuration schema.Configuration, providers Providers) func(next RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(next RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
autheliaCtx, err := NewAutheliaCtx(ctx, configuration, providers)
|
|
|
|
if err != nil {
|
|
|
|
autheliaCtx.Error(err, operationFailedMessage)
|
|
|
|
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.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) Error(err error, message string) {
|
2019-11-17 08:05:46 +07:00
|
|
|
b, marshalErr := json.Marshal(ErrorResponse{Status: "KO", Message: message})
|
|
|
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
c.Logger.Error(marshalErr)
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
c.SetContentType("application/json")
|
|
|
|
c.SetBody(b)
|
|
|
|
c.Logger.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyError reply with an error but does not display any stack trace in the logs.
|
2019-11-17 02:50:58 +07:00
|
|
|
func (c *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 {
|
|
|
|
c.Logger.Error(marshalErr)
|
|
|
|
}
|
|
|
|
|
2019-11-17 02:50:58 +07:00
|
|
|
c.SetContentType("application/json")
|
|
|
|
c.SetBody(b)
|
|
|
|
c.Logger.Debug(err)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyUnauthorized response sent when user is unauthorized.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) ReplyUnauthorized() {
|
|
|
|
c.RequestCtx.Error(fasthttp.StatusMessage(fasthttp.StatusUnauthorized), fasthttp.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyForbidden response sent when access is forbidden to user.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) ReplyForbidden() {
|
|
|
|
c.RequestCtx.Error(fasthttp.StatusMessage(fasthttp.StatusForbidden), fasthttp.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedProto return the content of the X-Forwarded-Proto header.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) XForwardedProto() []byte {
|
|
|
|
return c.RequestCtx.Request.Header.Peek(xForwardedProtoHeader)
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedMethod return the content of the X-Forwarded-Method header.
|
|
|
|
func (c *AutheliaCtx) XForwardedMethod() []byte {
|
|
|
|
return c.RequestCtx.Request.Header.Peek(xForwardedMethodHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XForwardedHost return the content of the X-Forwarded-Host header.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) XForwardedHost() []byte {
|
|
|
|
return c.RequestCtx.Request.Header.Peek(xForwardedHostHeader)
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XForwardedURI return the content of the X-Forwarded-URI header.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) XForwardedURI() []byte {
|
|
|
|
return c.RequestCtx.Request.Header.Peek(xForwardedURIHeader)
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
// XOriginalURL return the content of the X-Original-URL header.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) XOriginalURL() []byte {
|
|
|
|
return c.RequestCtx.Request.Header.Peek(xOriginalURLHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession return the user session. Any update will be saved in cache.
|
|
|
|
func (c *AutheliaCtx) GetSession() session.UserSession {
|
2020-01-18 05:48:48 +07:00
|
|
|
userSession, err := c.Providers.SessionProvider.GetSession(c.RequestCtx)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger.Error("Unable to retrieve user session")
|
|
|
|
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.
|
|
|
|
func (c *AutheliaCtx) SaveSession(userSession session.UserSession) error {
|
|
|
|
return c.Providers.SessionProvider.SaveSession(c.RequestCtx, userSession)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ReplyOK is a helper method to reply ok.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) ReplyOK() {
|
|
|
|
c.SetContentType(applicationJSONContentType)
|
|
|
|
c.SetBody(okMessageBytes)
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ParseBody parse the request body into the type of value.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) ParseBody(value interface{}) error {
|
|
|
|
err := json.Unmarshal(c.PostBody(), &value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to parse body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := govalidator.ValidateStruct(value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to validate body: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (c *AutheliaCtx) SetJSONBody(value interface{}) error {
|
|
|
|
b, err := json.Marshal(OKResponse{Status: "OK", Data: value})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to marshal JSON body")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetContentType("application/json")
|
|
|
|
c.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.
|
|
|
|
func (c *AutheliaCtx) RemoteIP() net.IP {
|
2019-12-11 14:52:02 +07:00
|
|
|
XForwardedFor := c.Request.Header.Peek("X-Forwarded-For")
|
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
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return c.RequestCtx.RemoteIP()
|
|
|
|
}
|