2020-02-01 12:54:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2020-05-05 21:27:38 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
2020-02-01 12:54:50 +00:00
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Handle1FAResponse handle the redirection upon 1FA authentication.
|
2020-02-04 21:18:02 +00:00
|
|
|
func Handle1FAResponse(ctx *middlewares.AutheliaCtx, targetURI string, username string, groups []string) {
|
|
|
|
if targetURI == "" {
|
2020-03-06 00:31:09 +00:00
|
|
|
if !ctx.Providers.Authorizer.IsSecondFactorEnabled() && ctx.Configuration.DefaultRedirectionURL != "" {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
|
|
}
|
2020-02-04 21:18:02 +00:00
|
|
|
} else {
|
|
|
|
ctx.ReplyOK()
|
2020-02-01 12:54:50 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
targetURL, err := url.ParseRequestURI(targetURI)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to parse target URL %s: %s", targetURI, err), authenticationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredLevel := ctx.Providers.Authorizer.GetRequiredLevel(authorization.Subject{
|
|
|
|
Username: username,
|
|
|
|
Groups: groups,
|
|
|
|
IP: ctx.RemoteIP(),
|
|
|
|
}, *targetURL)
|
|
|
|
|
|
|
|
ctx.Logger.Debugf("Required level for the URL %s is %d", targetURI, requiredLevel)
|
|
|
|
|
2020-04-19 11:45:46 +00:00
|
|
|
if requiredLevel == authorization.TwoFactor {
|
|
|
|
ctx.Logger.Warnf("%s requires 2FA, cannot be redirected yet", targetURI)
|
2020-02-04 21:18:02 +00:00
|
|
|
ctx.ReplyOK()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
safeRedirection := utils.IsRedirectionSafe(*targetURL, ctx.Configuration.Session.Domain)
|
2020-02-01 12:54:50 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
if !safeRedirection {
|
2020-03-06 00:31:09 +00:00
|
|
|
if !ctx.Providers.Authorizer.IsSecondFactorEnabled() && ctx.Configuration.DefaultRedirectionURL != "" {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
|
|
}
|
2020-02-01 12:54:50 +00:00
|
|
|
} else {
|
|
|
|
ctx.ReplyOK()
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Logger.Debugf("Redirection URL %s is safe", targetURI)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
response := redirectResponse{Redirect: targetURI}
|
2020-12-16 01:47:31 +00:00
|
|
|
|
|
|
|
err = ctx.SetJSONBody(response)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to set redirection URL in body: %s", err)
|
|
|
|
}
|
2020-02-04 21:18:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Handle2FAResponse handle the redirection upon 2FA authentication.
|
2020-02-04 21:18:02 +00:00
|
|
|
func Handle2FAResponse(ctx *middlewares.AutheliaCtx, targetURI string) {
|
|
|
|
if targetURI == "" {
|
2020-02-01 12:54:50 +00:00
|
|
|
if ctx.Configuration.DefaultRedirectionURL != "" {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
|
|
}
|
2020-02-01 12:54:50 +00:00
|
|
|
} else {
|
|
|
|
ctx.ReplyOK()
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
targetURL, err := url.ParseRequestURI(targetURI)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to parse target URL: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetURL != nil && utils.IsRedirectionSafe(*targetURL, ctx.Configuration.Session.Domain) {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: targetURI})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to set redirection URL in body: %s", err)
|
|
|
|
}
|
2020-02-04 21:18:02 +00:00
|
|
|
} else {
|
|
|
|
ctx.ReplyOK()
|
2020-02-01 12:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 21:27:38 +00:00
|
|
|
|
|
|
|
// handleAuthenticationUnauthorized provides harmonized response codes for 1FA.
|
|
|
|
func handleAuthenticationUnauthorized(ctx *middlewares.AutheliaCtx, err error, message string) {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
|
|
|
ctx.Error(err, message)
|
|
|
|
}
|