mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
ddea31193b
OpenID connect has become a standard when it comes to authentication and in order to fix a security concern around forwarding authentication and authorization information it has been decided to add support for it. This feature is in beta version and only enabled when there is a configuration for it. Before enabling it in production, please consider that it's in beta with potential bugs and that there are several production critical features still missing such as all OIDC related data is stored in configuration or memory. This means you are potentially going to experience issues with HA deployments, or when restarting a single instance specifically related to OIDC. We are still working on adding the remaining set of features before making it GA as soon as possible. Related to #189 Co-authored-by: Clement Michaud <clement.michaud34@gmail.com>
150 lines
4.4 KiB
Go
150 lines
4.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
|
"github.com/authelia/authelia/internal/middlewares"
|
|
"github.com/authelia/authelia/internal/utils"
|
|
)
|
|
|
|
// HandleOIDCWorkflowResponse handle the redirection upon authentication in the OIDC workflow.
|
|
func HandleOIDCWorkflowResponse(ctx *middlewares.AutheliaCtx) {
|
|
userSession := ctx.GetSession()
|
|
|
|
if !authorization.IsAuthLevelSufficient(userSession.AuthenticationLevel, userSession.OIDCWorkflowSession.RequiredAuthorizationLevel) {
|
|
ctx.Logger.Warn("OIDC requires 2FA, cannot be redirected yet")
|
|
ctx.ReplyOK()
|
|
|
|
return
|
|
}
|
|
|
|
uri, err := ctx.ForwardedProtoHost()
|
|
if err != nil {
|
|
ctx.Logger.Errorf("%v", err)
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("Unable to get forward facing URI"), authenticationFailedMessage)
|
|
|
|
return
|
|
}
|
|
|
|
if isConsentMissing(
|
|
userSession.OIDCWorkflowSession,
|
|
userSession.OIDCWorkflowSession.RequestedScopes,
|
|
userSession.OIDCWorkflowSession.RequestedAudience) {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: fmt.Sprintf("%s/consent", uri)})
|
|
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
}
|
|
} else {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: userSession.OIDCWorkflowSession.AuthURI})
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle1FAResponse handle the redirection upon 1FA authentication.
|
|
func Handle1FAResponse(ctx *middlewares.AutheliaCtx, targetURI, requestMethod string, username string, groups []string) {
|
|
if targetURI == "" {
|
|
if !ctx.Providers.Authorizer.IsSecondFactorEnabled() && ctx.Configuration.DefaultRedirectionURL != "" {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
}
|
|
} else {
|
|
ctx.ReplyOK()
|
|
}
|
|
|
|
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(),
|
|
},
|
|
authorization.NewObject(targetURL, requestMethod))
|
|
|
|
ctx.Logger.Debugf("Required level for the URL %s is %d", targetURI, requiredLevel)
|
|
|
|
if requiredLevel == authorization.TwoFactor {
|
|
ctx.Logger.Warnf("%s requires 2FA, cannot be redirected yet", targetURI)
|
|
ctx.ReplyOK()
|
|
|
|
return
|
|
}
|
|
|
|
safeRedirection := utils.IsRedirectionSafe(*targetURL, ctx.Configuration.Session.Domain)
|
|
|
|
if !safeRedirection {
|
|
if !ctx.Providers.Authorizer.IsSecondFactorEnabled() && ctx.Configuration.DefaultRedirectionURL != "" {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
}
|
|
} else {
|
|
ctx.ReplyOK()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
ctx.Logger.Debugf("Redirection URL %s is safe", targetURI)
|
|
|
|
response := redirectResponse{Redirect: targetURI}
|
|
|
|
err = ctx.SetJSONBody(response)
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set redirection URL in body: %s", err)
|
|
}
|
|
}
|
|
|
|
// Handle2FAResponse handle the redirection upon 2FA authentication.
|
|
func Handle2FAResponse(ctx *middlewares.AutheliaCtx, targetURI string) {
|
|
if targetURI == "" {
|
|
if ctx.Configuration.DefaultRedirectionURL != "" {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: ctx.Configuration.DefaultRedirectionURL})
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set default redirection URL in body: %s", err)
|
|
}
|
|
} else {
|
|
ctx.ReplyOK()
|
|
}
|
|
|
|
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) {
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: targetURI})
|
|
if err != nil {
|
|
ctx.Logger.Errorf("Unable to set redirection URL in body: %s", err)
|
|
}
|
|
} else {
|
|
ctx.ReplyOK()
|
|
}
|
|
}
|
|
|
|
// handleAuthenticationUnauthorized provides harmonized response codes for 1FA.
|
|
func handleAuthenticationUnauthorized(ctx *middlewares.AutheliaCtx, err error, message string) {
|
|
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
|
ctx.Error(err, message)
|
|
}
|