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"
|
|
|
|
)
|
|
|
|
|
feat(oidc): add additional config options, accurate token times, and refactoring (#1991)
* This gives admins more control over their OIDC installation exposing options that had defaults before. Things like lifespans for authorize codes, access tokens, id tokens, refresh tokens, a option to enable the debug client messages, minimum parameter entropy. It also allows admins to configure the response modes.
* Additionally this records specific values about a users session indicating when they performed a specific authz factor so this is represented in the token accurately.
* Lastly we also implemented a OIDC key manager which calculates the kid for jwk's using the SHA1 digest instead of being static, or more specifically the first 7 chars. As per https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-key#section-8.1.1 the kid should not exceed 8 chars. While it's allowed to exceed 8 chars, it must only be done so with a compelling reason, which we do not have.
2021-07-03 23:44:30 +00:00
|
|
|
// handleOIDCWorkflowResponse handle the redirection upon authentication in the OIDC workflow.
|
|
|
|
func handleOIDCWorkflowResponse(ctx *middlewares.AutheliaCtx) {
|
2021-05-04 22:06:05 +00:00
|
|
|
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)
|
2021-07-22 03:52:37 +00:00
|
|
|
handleAuthenticationUnauthorized(ctx, fmt.Errorf("Unable to get forward facing URI"), messageAuthenticationFailed)
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Handle1FAResponse handle the redirection upon 1FA authentication.
|
2021-03-05 04:18:31 +00:00
|
|
|
func Handle1FAResponse(ctx *middlewares.AutheliaCtx, targetURI, requestMethod string, username string, groups []string) {
|
2020-02-04 21:18:02 +00:00
|
|
|
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 {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to parse target URL %s: %s", targetURI, err), messageAuthenticationFailed)
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-05 04:18:31 +00:00
|
|
|
requiredLevel := ctx.Providers.Authorizer.GetRequiredLevel(
|
|
|
|
authorization.Subject{
|
|
|
|
Username: username,
|
|
|
|
Groups: groups,
|
|
|
|
IP: ctx.RemoteIP(),
|
|
|
|
},
|
|
|
|
authorization.NewObject(targetURL, requestMethod))
|
2020-02-04 21:18:02 +00:00
|
|
|
|
|
|
|
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)
|
2021-08-02 06:15:38 +00:00
|
|
|
err = ctx.SetJSONBody(redirectResponse{Redirect: targetURI})
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-12-16 01:47:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:15:38 +00:00
|
|
|
safe, err := utils.IsRedirectionURISafe(targetURI, ctx.Configuration.Session.Domain)
|
2020-02-04 21:18:02 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-02 06:15:38 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to check target URL: %s", err), messageMFAValidationFailed)
|
2020-02-04 21:18:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:15:38 +00:00
|
|
|
if safe {
|
|
|
|
ctx.Logger.Debugf("Redirection URL %s is safe", targetURI)
|
2020-12-16 01:47:31 +00:00
|
|
|
err := ctx.SetJSONBody(redirectResponse{Redirect: targetURI})
|
2021-08-02 06:15:38 +00:00
|
|
|
|
2020-12-16 01:47:31 +00:00
|
|
|
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)
|
|
|
|
}
|