2019-04-24 21:52:08 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-05-04 22:06:05 +00:00
|
|
|
"net/url"
|
2019-04-24 21:52:08 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
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/configuration/schema"
|
2022-01-20 23:46:13 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2022-03-28 01:26:30 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2019-12-11 07:52:02 +00:00
|
|
|
// NewRequestLogger create a new request logger for the given request.
|
|
|
|
func NewRequestLogger(ctx *AutheliaCtx) *logrus.Entry {
|
2022-01-20 23:46:13 +00:00
|
|
|
return logging.Logger().WithFields(logrus.Fields{
|
2019-12-11 07:52:02 +00:00
|
|
|
"method": string(ctx.Method()),
|
|
|
|
"path": string(ctx.Path()),
|
|
|
|
"remote_ip": ctx.RemoteIP().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// NewAutheliaCtx instantiate an AutheliaCtx out of a RequestCtx.
|
2022-06-10 01:34:43 +00:00
|
|
|
func NewAutheliaCtx(requestCTX *fasthttp.RequestCtx, configuration schema.Configuration, providers Providers) (ctx *AutheliaCtx) {
|
|
|
|
ctx = new(AutheliaCtx)
|
|
|
|
ctx.RequestCtx = requestCTX
|
|
|
|
ctx.Providers = providers
|
|
|
|
ctx.Configuration = configuration
|
|
|
|
ctx.Logger = NewRequestLogger(ctx)
|
|
|
|
ctx.Clock = utils.RealClock{}
|
|
|
|
|
|
|
|
return ctx
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 01:26:30 +00:00
|
|
|
// AvailableSecondFactorMethods returns the available 2FA methods.
|
|
|
|
func (ctx *AutheliaCtx) AvailableSecondFactorMethods() (methods []string) {
|
|
|
|
methods = make([]string, 0, 3)
|
|
|
|
|
|
|
|
if !ctx.Configuration.TOTP.Disable {
|
|
|
|
methods = append(methods, model.SecondFactorMethodTOTP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Configuration.Webauthn.Disable {
|
|
|
|
methods = append(methods, model.SecondFactorMethodWebauthn)
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:34:26 +00:00
|
|
|
if !ctx.Configuration.DuoAPI.Disable {
|
2022-03-28 01:26:30 +00:00
|
|
|
methods = append(methods, model.SecondFactorMethodDuo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
// Error reply with an error and display the stack trace in the logs.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) Error(err error, message string) {
|
|
|
|
ctx.SetJSONError(message)
|
2021-11-29 03:09:14 +00:00
|
|
|
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.Logger.Error(err)
|
2021-11-29 03:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetJSONError sets the body of the response to an JSON error KO message.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) SetJSONError(message string) {
|
2022-07-08 12:18:52 +00:00
|
|
|
if replyErr := ctx.ReplyJSON(ErrorResponse{Status: "KO", Message: message}, 0); replyErr != nil {
|
|
|
|
ctx.Logger.Error(replyErr)
|
2019-11-17 01:05:46 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ReplyError reply with an error but does not display any stack trace in the logs.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyError(err error, message string) {
|
2019-11-17 01:05:46 +00:00
|
|
|
b, marshalErr := json.Marshal(ErrorResponse{Status: "KO", Message: message})
|
|
|
|
|
|
|
|
if marshalErr != nil {
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.Logger.Error(marshalErr)
|
2019-11-17 01:05:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
ctx.SetContentTypeApplicationJSON()
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.SetBody(b)
|
|
|
|
ctx.Logger.Debug(err)
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 12:18:52 +00:00
|
|
|
// ReplyStatusCode resets a response and replies with the given status code and relevant message.
|
|
|
|
func (ctx *AutheliaCtx) ReplyStatusCode(statusCode int) {
|
|
|
|
ctx.Response.Reset()
|
|
|
|
ctx.SetStatusCode(statusCode)
|
2022-12-17 00:49:05 +00:00
|
|
|
ctx.SetContentTypeTextPlain()
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.SetBodyString(fmt.Sprintf("%d %s", statusCode, fasthttp.StatusMessage(statusCode)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplyJSON writes a JSON response.
|
2022-10-05 05:05:23 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyJSON(data any, statusCode int) (err error) {
|
2022-07-08 12:18:52 +00:00
|
|
|
var (
|
|
|
|
body []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if body, err = json.Marshal(data); err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal JSON body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode > 0 {
|
|
|
|
ctx.SetStatusCode(statusCode)
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
ctx.SetContentTypeApplicationJSON()
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.SetBody(body)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ReplyUnauthorized response sent when user is unauthorized.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyUnauthorized() {
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.ReplyStatusCode(fasthttp.StatusUnauthorized)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ReplyForbidden response sent when access is forbidden to user.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyForbidden() {
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.ReplyStatusCode(fasthttp.StatusForbidden)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// ReplyBadRequest response sent when bad request has been sent.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyBadRequest() {
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.ReplyStatusCode(fasthttp.StatusBadRequest)
|
2021-05-04 22:06:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
// XForwardedProto return the content of the X-Forwarded-Proto header.
|
2022-02-06 13:37:28 +00: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-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
// XForwardedMethod return the content of the X-Forwarded-Method header.
|
2022-12-17 00:49:05 +00:00
|
|
|
func (ctx *AutheliaCtx) XForwardedMethod() []byte {
|
2022-02-06 13:37:28 +00:00
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXForwardedMethod)
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// XForwardedHost return the content of the X-Forwarded-Host header.
|
2022-02-06 13:37:28 +00: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-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
// XForwardedURI return the content of the X-Forwarded-URI header.
|
2022-02-06 13:37:28 +00: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-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
// XOriginalURL returns the content of the X-Original-URL header.
|
|
|
|
func (ctx *AutheliaCtx) XOriginalURL() []byte {
|
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXOriginalURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XOriginalMethod return the content of the X-Original-Method header.
|
|
|
|
func (ctx *AutheliaCtx) XOriginalMethod() []byte {
|
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXOriginalMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XAutheliaURL return the content of the X-Authelia-URL header which is used to communicate the location of the
|
|
|
|
// portal when using proxies like Envoy.
|
|
|
|
func (ctx *AutheliaCtx) XAutheliaURL() []byte {
|
2022-10-01 11:47:09 +00:00
|
|
|
return ctx.RequestCtx.Request.Header.PeekBytes(headerXAutheliaURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryArgRedirect return the content of the rd query argument.
|
2022-12-17 00:49:05 +00:00
|
|
|
func (ctx *AutheliaCtx) QueryArgRedirect() []byte {
|
|
|
|
return ctx.RequestCtx.QueryArgs().PeekBytes(qryArgRedirect)
|
2022-10-01 11:47:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 00:31:08 +00:00
|
|
|
// BasePath returns the base_url as per the path visited by the client.
|
2022-12-17 00:49:05 +00:00
|
|
|
func (ctx *AutheliaCtx) BasePath() string {
|
2022-02-06 13:37:28 +00:00
|
|
|
if baseURL := ctx.UserValueBytes(UserValueKeyBaseURL); baseURL != nil {
|
2021-08-10 00:31:08 +00:00
|
|
|
return baseURL.(string)
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
return ""
|
2021-08-10 00:31:08 +00:00
|
|
|
}
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
// BasePathSlash is the same as BasePath but returns a final slash as well.
|
|
|
|
func (ctx *AutheliaCtx) BasePathSlash() string {
|
|
|
|
if baseURL := ctx.UserValueBytes(UserValueKeyBaseURL); baseURL != nil {
|
|
|
|
return baseURL.(string) + strSlash
|
2021-08-10 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
return strSlash
|
2021-05-04 22:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
// RootURL returns the Root URL.
|
|
|
|
func (ctx *AutheliaCtx) RootURL() (issuerURL *url.URL) {
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: string(ctx.XForwardedProto()),
|
|
|
|
Host: string(ctx.XForwardedHost()),
|
|
|
|
Path: ctx.BasePath(),
|
2022-10-20 02:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
// RootURLSlash is the same as RootURL but includes a final slash as well.
|
|
|
|
func (ctx *AutheliaCtx) RootURLSlash() (issuerURL *url.URL) {
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: string(ctx.XForwardedProto()),
|
|
|
|
Host: string(ctx.XForwardedHost()),
|
|
|
|
Path: ctx.BasePathSlash(),
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession return the user session. Any update will be saved in cache.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) GetSession() session.UserSession {
|
|
|
|
userSession, err := ctx.Providers.SessionProvider.GetSession(ctx.RequestCtx)
|
2020-01-17 22:48:48 +00:00
|
|
|
if err != nil {
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.Logger.Error("Unable to retrieve user session")
|
2020-01-17 22:48:48 +00:00
|
|
|
return session.NewDefaultUserSession()
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-01-17 22:48:48 +00:00
|
|
|
return userSession
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSession save the content of the session.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) SaveSession(userSession session.UserSession) error {
|
|
|
|
return ctx.Providers.SessionProvider.SaveSession(ctx.RequestCtx, userSession)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ReplyOK is a helper method to reply ok.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) ReplyOK() {
|
2022-12-17 00:49:05 +00:00
|
|
|
ctx.SetContentTypeApplicationJSON()
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.SetBody(okMessageBytes)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ParseBody parse the request body into the type of value.
|
2022-10-05 05:05:23 +00:00
|
|
|
func (ctx *AutheliaCtx) ParseBody(value any) error {
|
2022-02-06 13:37:28 +00:00
|
|
|
err := json.Unmarshal(ctx.PostBody(), &value)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-11-29 03:09:14 +00:00
|
|
|
return fmt.Errorf("unable to parse body: %w", err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := govalidator.ValidateStruct(value)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-29 03:09:14 +00:00
|
|
|
return fmt.Errorf("unable to validate body: %w", err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("Body is not valid")
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// SetJSONBody Set json body.
|
2022-10-05 05:05:23 +00:00
|
|
|
func (ctx *AutheliaCtx) SetJSONBody(value any) error {
|
2022-07-08 12:18:52 +00:00
|
|
|
return ctx.ReplyJSON(OKResponse{Status: "OK", Data: value}, 0)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteIP return the remote IP taking X-Forwarded-For header into account if provided.
|
2022-02-06 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) RemoteIP() net.IP {
|
|
|
|
XForwardedFor := ctx.Request.Header.PeekBytes(headerXForwardedFor)
|
2019-04-24 21:52:08 +00:00
|
|
|
if XForwardedFor != nil {
|
|
|
|
ips := strings.Split(string(XForwardedFor), ",")
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
2019-12-11 07:29:32 +00:00
|
|
|
return net.ParseIP(strings.Trim(ips[0], " "))
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-06 13:37:28 +00:00
|
|
|
return ctx.RequestCtx.RemoteIP()
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2022-02-06 13:37:28 +00: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-04 22:06:05 +00: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 13:37:28 +00:00
|
|
|
ctx.Logger.Trace("Using X-Original-URL header content as targeted site URL")
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
return parsedURL, nil
|
|
|
|
}
|
|
|
|
|
2022-02-06 13:37:28 +00:00
|
|
|
forwardedProto, forwardedHost, forwardedURI := ctx.XForwardedProto(), ctx.XForwardedHost(), ctx.XForwardedURI()
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
if forwardedProto == nil {
|
|
|
|
return nil, errMissingXForwardedProto
|
|
|
|
}
|
|
|
|
|
|
|
|
if forwardedHost == nil {
|
|
|
|
return nil, errMissingXForwardedHost
|
|
|
|
}
|
|
|
|
|
|
|
|
var requestURI string
|
|
|
|
|
2022-07-05 01:32:10 +00:00
|
|
|
forwardedProto = append(forwardedProto, protoHostSeparator...)
|
|
|
|
requestURI = string(append(forwardedProto,
|
2021-05-04 22:06:05 +00:00
|
|
|
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 13:37:28 +00:00
|
|
|
ctx.Logger.Tracef("Using X-Fowarded-Proto, X-Forwarded-Host and X-Forwarded-URI headers " +
|
2021-05-04 22:06:05 +00:00
|
|
|
"to construct targeted site URL")
|
|
|
|
|
|
|
|
return parsedURL, nil
|
|
|
|
}
|
2021-07-22 03:52:37 +00:00
|
|
|
|
|
|
|
// IsXHR returns true if the request is a XMLHttpRequest.
|
2022-09-03 01:51:02 +00:00
|
|
|
func (ctx *AutheliaCtx) IsXHR() (xhr bool) {
|
2022-02-06 13:37:28 +00:00
|
|
|
requestedWith := ctx.Request.Header.PeekBytes(headerXRequestedWith)
|
2021-07-22 03:52:37 +00:00
|
|
|
|
2022-02-06 13:37:28 +00:00
|
|
|
return requestedWith != nil && strings.EqualFold(string(requestedWith), headerValueXRequestedWithXHR)
|
2021-07-22 03:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptsMIME takes a mime type and returns true if the request accepts that type or the wildcard type.
|
2022-09-03 01:51:02 +00:00
|
|
|
func (ctx *AutheliaCtx) AcceptsMIME(mime string) (acceptsMime bool) {
|
2022-02-06 13:37:28 +00:00
|
|
|
accepts := strings.Split(string(ctx.Request.Header.PeekBytes(headerAccept)), ",")
|
2021-07-22 03:52:37 +00: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 13:37:28 +00:00
|
|
|
func (ctx *AutheliaCtx) SpecialRedirect(uri string, statusCode int) {
|
2021-07-22 03:52:37 +00:00
|
|
|
if statusCode < fasthttp.StatusMovedPermanently || (statusCode > fasthttp.StatusSeeOther && statusCode != fasthttp.StatusTemporaryRedirect && statusCode != fasthttp.StatusPermanentRedirect && statusCode != fasthttp.StatusUnauthorized) {
|
|
|
|
statusCode = fasthttp.StatusFound
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:49:05 +00:00
|
|
|
ctx.SetContentTypeTextHTML()
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.SetStatusCode(statusCode)
|
2021-07-22 03:52:37 +00:00
|
|
|
|
|
|
|
u := fasthttp.AcquireURI()
|
|
|
|
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.URI().CopyTo(u)
|
2021-07-22 03:52:37 +00:00
|
|
|
u.Update(uri)
|
|
|
|
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.Response.Header.SetBytesKV(headerLocation, u.FullURI())
|
2021-07-22 03:52:37 +00:00
|
|
|
|
2022-07-08 12:18:52 +00:00
|
|
|
ctx.SetBodyString(fmt.Sprintf("<a href=\"%s\">%d %s</a>", utils.StringHTMLEscape(string(u.FullURI())), statusCode, fasthttp.StatusMessage(statusCode)))
|
2021-07-22 03:52:37 +00:00
|
|
|
|
|
|
|
fasthttp.ReleaseURI(u)
|
|
|
|
}
|
2022-06-14 07:20:13 +00:00
|
|
|
|
|
|
|
// RecordAuthentication records authentication metrics.
|
|
|
|
func (ctx *AutheliaCtx) RecordAuthentication(success, regulated bool, method string) {
|
|
|
|
if ctx.Providers.Metrics == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Providers.Metrics.RecordAuthentication(success, regulated, method)
|
|
|
|
}
|
2022-12-17 00:49:05 +00:00
|
|
|
|
|
|
|
// SetContentTypeTextPlain efficiently sets the Content-Type header to 'text/plain; charset=utf-8'.
|
|
|
|
func (ctx *AutheliaCtx) SetContentTypeTextPlain() {
|
|
|
|
ctx.SetContentTypeBytes(contentTypeTextPlain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContentTypeTextHTML efficiently sets the Content-Type header to 'text/html; charset=utf-8'.
|
|
|
|
func (ctx *AutheliaCtx) SetContentTypeTextHTML() {
|
|
|
|
ctx.SetContentTypeBytes(contentTypeTextHTML)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContentTypeApplicationJSON efficiently sets the Content-Type header to 'application/json; charset=utf-8'.
|
|
|
|
func (ctx *AutheliaCtx) SetContentTypeApplicationJSON() {
|
|
|
|
ctx.SetContentTypeBytes(contentTypeApplicationJSON)
|
|
|
|
}
|