2022-04-07 00:58:51 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-04-08 04:13:47 +00:00
|
|
|
"os"
|
2023-01-25 09:36:40 +00:00
|
|
|
"path"
|
2022-04-07 00:58:51 +00:00
|
|
|
"strings"
|
2022-04-08 04:13:47 +00:00
|
|
|
"time"
|
2022-04-07 00:58:51 +00:00
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
duoapi "github.com/duosecurity/duo_api_golang"
|
|
|
|
"github.com/fasthttp/router"
|
2022-06-14 07:20:13 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-06-11 23:26:28 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-07 00:58:51 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2022-04-08 04:13:47 +00:00
|
|
|
"github.com/valyala/fasthttp/expvarhandler"
|
2022-06-14 07:20:13 +00:00
|
|
|
"github.com/valyala/fasthttp/fasthttpadaptor"
|
2022-04-08 04:13:47 +00:00
|
|
|
"github.com/valyala/fasthttp/pprofhandler"
|
2022-04-07 00:58:51 +00:00
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/duo"
|
2022-04-07 00:58:51 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/handlers"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2022-04-08 04:13:47 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/v4/internal/oidc"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2022-04-07 00:58:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Replacement for the default error handler in fasthttp.
|
2022-06-11 23:26:28 +00:00
|
|
|
func handleError() func(ctx *fasthttp.RequestCtx, err error) {
|
2022-04-08 04:13:47 +00:00
|
|
|
headerXForwardedFor := []byte(fasthttp.HeaderXForwardedFor)
|
|
|
|
|
|
|
|
getRemoteIP := func(ctx *fasthttp.RequestCtx) string {
|
|
|
|
if hdr := ctx.Request.Header.PeekBytes(headerXForwardedFor); hdr != nil {
|
|
|
|
ips := strings.Split(string(hdr), ",")
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
|
|
|
return strings.Trim(ips[0], " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.RemoteIP().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx, err error) {
|
2022-06-11 23:26:28 +00:00
|
|
|
var (
|
|
|
|
statusCode int
|
|
|
|
message string
|
|
|
|
)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
switch e := err.(type) {
|
|
|
|
case *fasthttp.ErrSmallBuffer:
|
2022-06-11 23:26:28 +00:00
|
|
|
statusCode = fasthttp.StatusRequestHeaderFieldsTooLarge
|
|
|
|
message = "Request from client exceeded the server buffer sizes."
|
2022-04-08 04:13:47 +00:00
|
|
|
case *net.OpError:
|
|
|
|
if e.Timeout() {
|
2022-06-11 23:26:28 +00:00
|
|
|
statusCode = fasthttp.StatusRequestTimeout
|
|
|
|
message = "Request timeout occurred while handling request from client."
|
2022-04-08 04:13:47 +00:00
|
|
|
} else {
|
2022-06-11 23:26:28 +00:00
|
|
|
statusCode = fasthttp.StatusBadRequest
|
|
|
|
message = "An unknown network error occurred while handling a request from client."
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
default:
|
2022-06-11 23:26:28 +00:00
|
|
|
statusCode = fasthttp.StatusBadRequest
|
|
|
|
message = "An unknown error occurred while handling a request from client."
|
2022-04-07 00:58:51 +00:00
|
|
|
}
|
2022-06-11 23:26:28 +00:00
|
|
|
|
|
|
|
logging.Logger().WithFields(logrus.Fields{
|
|
|
|
"method": string(ctx.Method()),
|
|
|
|
"path": string(ctx.Path()),
|
|
|
|
"remote_ip": getRemoteIP(ctx),
|
|
|
|
"status_code": statusCode,
|
|
|
|
}).WithError(err).Error(message)
|
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
handlers.SetStatusCodeResponse(ctx, statusCode)
|
2022-04-07 00:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:26:28 +00:00
|
|
|
func handleNotFound(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
2022-04-07 00:58:51 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
path := strings.ToLower(string(ctx.Path()))
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
for i := 0; i < len(dirsHTTPServer); i++ {
|
|
|
|
if path == dirsHTTPServer[i].name || strings.HasPrefix(path, dirsHTTPServer[i].prefix) {
|
2022-04-07 00:58:51 +00:00
|
|
|
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusNotFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
//nolint:gocyclo
|
2023-02-11 10:45:26 +00:00
|
|
|
func handleRouter(config *schema.Configuration, providers middlewares.Providers) fasthttp.RequestHandler {
|
2023-01-25 09:36:40 +00:00
|
|
|
log := logging.Logger()
|
|
|
|
|
2023-02-11 10:45:26 +00:00
|
|
|
optsTemplatedFile := NewTemplatedFileOptions(config)
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2023-01-03 03:49:02 +00:00
|
|
|
serveIndexHandler := ServeTemplatedFile(providers.Templates.GetAssetIndexTemplate(), optsTemplatedFile)
|
|
|
|
serveOpenAPIHandler := ServeTemplatedOpenAPI(providers.Templates.GetAssetOpenAPIIndexTemplate(), optsTemplatedFile)
|
|
|
|
serveOpenAPISpecHandler := ETagRootURL(ServeTemplatedOpenAPI(providers.Templates.GetAssetOpenAPISpecTemplate(), optsTemplatedFile))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
handlerPublicHTML := newPublicHTMLEmbeddedHandler()
|
|
|
|
handlerLocales := newLocalesEmbeddedHandler()
|
|
|
|
|
2023-02-11 10:45:26 +00:00
|
|
|
bridge := middlewares.NewBridgeBuilder(*config, providers).
|
2022-06-10 01:34:43 +00:00
|
|
|
WithPreMiddlewares(middlewares.SecurityHeaders).Build()
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSPublicGET := middlewares.NewCORSPolicyBuilder().
|
2022-12-17 00:49:05 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodGet).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins("*").
|
|
|
|
Build()
|
|
|
|
|
|
|
|
r := router.New()
|
|
|
|
|
|
|
|
// Static Assets.
|
2023-01-25 09:36:40 +00:00
|
|
|
r.GET("/", bridge(serveIndexHandler))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
for _, f := range filesRoot {
|
2022-04-08 04:13:47 +00:00
|
|
|
r.GET("/"+f, handlerPublicHTML)
|
|
|
|
}
|
|
|
|
|
2022-05-03 02:19:30 +00:00
|
|
|
r.GET("/favicon.ico", middlewares.AssetOverride(config.Server.AssetPath, 0, handlerPublicHTML))
|
|
|
|
r.GET("/static/media/logo.png", middlewares.AssetOverride(config.Server.AssetPath, 2, handlerPublicHTML))
|
2022-04-08 04:13:47 +00:00
|
|
|
r.GET("/static/{filepath:*}", handlerPublicHTML)
|
|
|
|
|
|
|
|
// Locales.
|
2022-05-03 02:19:30 +00:00
|
|
|
r.GET("/locales/{language:[a-z]{1,3}}-{variant:[a-zA-Z0-9-]+}/{namespace:[a-z]+}.json", middlewares.AssetOverride(config.Server.AssetPath, 0, handlerLocales))
|
|
|
|
r.GET("/locales/{language:[a-z]{1,3}}/{namespace:[a-z]+}.json", middlewares.AssetOverride(config.Server.AssetPath, 0, handlerLocales))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// Swagger.
|
2023-01-25 09:36:40 +00:00
|
|
|
r.GET("/api/", bridge(serveOpenAPIHandler))
|
2022-04-08 04:13:47 +00:00
|
|
|
r.OPTIONS("/api/", policyCORSPublicGET.HandleOPTIONS)
|
2023-01-25 09:36:40 +00:00
|
|
|
r.GET("/api/index.html", bridge(serveOpenAPIHandler))
|
2023-01-03 03:49:02 +00:00
|
|
|
r.OPTIONS("/api/index.html", policyCORSPublicGET.HandleOPTIONS)
|
2023-01-25 09:36:40 +00:00
|
|
|
r.GET("/api/openapi.yml", policyCORSPublicGET.Middleware(bridge(serveOpenAPISpecHandler)))
|
2023-01-03 03:49:02 +00:00
|
|
|
r.OPTIONS("/api/openapi.yml", policyCORSPublicGET.HandleOPTIONS)
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
for _, file := range filesSwagger {
|
2022-04-08 04:13:47 +00:00
|
|
|
r.GET("/api/"+file, handlerPublicHTML)
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:45:26 +00:00
|
|
|
middlewareAPI := middlewares.NewBridgeBuilder(*config, providers).
|
2022-06-10 01:34:43 +00:00
|
|
|
WithPreMiddlewares(middlewares.SecurityHeaders, middlewares.SecurityHeadersNoStore, middlewares.SecurityHeadersCSPNone).
|
|
|
|
Build()
|
|
|
|
|
2023-02-11 10:45:26 +00:00
|
|
|
middleware1FA := middlewares.NewBridgeBuilder(*config, providers).
|
2022-06-10 01:34:43 +00:00
|
|
|
WithPreMiddlewares(middlewares.SecurityHeaders, middlewares.SecurityHeadersNoStore, middlewares.SecurityHeadersCSPNone).
|
|
|
|
WithPostMiddlewares(middlewares.Require1FA).
|
|
|
|
Build()
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2023-02-11 10:49:42 +00:00
|
|
|
middleware2FA := middlewares.NewBridgeBuilder(*config, providers).
|
2022-11-19 05:48:47 +00:00
|
|
|
WithPreMiddlewares(middlewares.SecurityHeaders, middlewares.SecurityHeadersNoStore, middlewares.SecurityHeadersCSPNone).
|
2022-12-31 07:48:43 +00:00
|
|
|
WithPostMiddlewares(middlewares.Require2FAWithAPIResponse).
|
2022-11-19 05:48:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
r.GET("/api/health", middlewareAPI(handlers.HealthGET))
|
|
|
|
r.GET("/api/state", middlewareAPI(handlers.StateGET))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
r.GET("/api/configuration", middleware1FA(handlers.ConfigurationGET))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
r.GET("/api/configuration/password-policy", middlewareAPI(handlers.PasswordPolicyConfigurationGET))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
metricsVRMW := middlewares.NewMetricsAuthzRequest(providers.Metrics)
|
|
|
|
|
|
|
|
for name, endpoint := range config.Server.Endpoints.Authz {
|
|
|
|
uri := path.Join(pathAuthz, name)
|
|
|
|
|
2023-02-11 10:45:26 +00:00
|
|
|
authz := handlers.NewAuthzBuilder().WithConfig(config).WithEndpointConfig(endpoint).Build()
|
2023-01-25 09:36:40 +00:00
|
|
|
|
|
|
|
handler := middlewares.Wrap(metricsVRMW, bridge(authz.Handler))
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "legacy":
|
|
|
|
log.
|
|
|
|
WithField("path_prefix", pathAuthzLegacy).
|
|
|
|
WithField("impl", endpoint.Implementation).
|
|
|
|
WithField("methods", []string{"*"}).
|
|
|
|
Trace("Registering Authz Endpoint")
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
r.ANY(pathAuthzLegacy, handler)
|
|
|
|
r.ANY(path.Join(pathAuthzLegacy, pathParamAuthzEnvoy), handler)
|
|
|
|
default:
|
|
|
|
switch endpoint.Implementation {
|
|
|
|
case handlers.AuthzImplLegacy.String(), handlers.AuthzImplExtAuthz.String():
|
|
|
|
log.
|
|
|
|
WithField("path_prefix", uri).
|
|
|
|
WithField("impl", endpoint.Implementation).
|
|
|
|
WithField("methods", []string{"*"}).
|
|
|
|
Trace("Registering Authz Endpoint")
|
|
|
|
|
|
|
|
r.ANY(uri, handler)
|
|
|
|
r.ANY(path.Join(uri, pathParamAuthzEnvoy), handler)
|
|
|
|
default:
|
|
|
|
log.
|
|
|
|
WithField("path", uri).
|
|
|
|
WithField("impl", endpoint.Implementation).
|
|
|
|
WithField("methods", []string{fasthttp.MethodGet, fasthttp.MethodHead}).
|
|
|
|
Trace("Registering Authz Endpoint")
|
|
|
|
|
|
|
|
r.GET(uri, handler)
|
|
|
|
r.HEAD(uri, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 11:47:09 +00:00
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
r.POST("/api/checks/safe-redirection", middlewareAPI(handlers.CheckSafeRedirectionPOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
delayFunc := middlewares.TimingAttackDelay(10, 250, 85, time.Second, true)
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
r.POST("/api/firstfactor", middlewareAPI(handlers.FirstFactorPOST(delayFunc)))
|
|
|
|
r.POST("/api/logout", middlewareAPI(handlers.LogoutPOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// Only register endpoints if forgot password is not disabled.
|
2022-06-28 03:15:50 +00:00
|
|
|
if !config.AuthenticationBackend.PasswordReset.Disable &&
|
2022-04-08 04:13:47 +00:00
|
|
|
config.AuthenticationBackend.PasswordReset.CustomURL.String() == "" {
|
|
|
|
// Password reset related endpoints.
|
2022-05-04 04:47:23 +00:00
|
|
|
r.POST("/api/reset-password/identity/start", middlewareAPI(handlers.ResetPasswordIdentityStart))
|
|
|
|
r.POST("/api/reset-password/identity/finish", middlewareAPI(handlers.ResetPasswordIdentityFinish))
|
|
|
|
r.POST("/api/reset-password", middlewareAPI(handlers.ResetPasswordPOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Information about the user.
|
2022-06-10 01:34:43 +00:00
|
|
|
r.GET("/api/user/info", middleware1FA(handlers.UserInfoGET))
|
|
|
|
r.POST("/api/user/info", middleware1FA(handlers.UserInfoPOST))
|
|
|
|
r.POST("/api/user/info/2fa_method", middleware1FA(handlers.MethodPreferencePOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
if !config.TOTP.Disable {
|
|
|
|
// TOTP related endpoints.
|
2022-06-10 01:34:43 +00:00
|
|
|
r.GET("/api/user/info/totp", middleware1FA(handlers.UserTOTPInfoGET))
|
|
|
|
r.POST("/api/secondfactor/totp/identity/start", middleware1FA(handlers.TOTPIdentityStart))
|
|
|
|
r.POST("/api/secondfactor/totp/identity/finish", middleware1FA(handlers.TOTPIdentityFinish))
|
|
|
|
r.POST("/api/secondfactor/totp", middleware1FA(handlers.TimeBasedOneTimePasswordPOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !config.Webauthn.Disable {
|
2023-02-01 11:10:38 +00:00
|
|
|
r.GET("/api/secondfactor/webauthn", middleware1FA(handlers.WebauthnAssertionGET))
|
|
|
|
r.POST("/api/secondfactor/webauthn", middleware1FA(handlers.WebauthnAssertionPOST))
|
2022-11-14 02:19:18 +00:00
|
|
|
|
|
|
|
// Management of the webauthn devices.
|
2023-02-01 11:10:38 +00:00
|
|
|
r.GET("/api/secondfactor/webauthn/credentials", middleware1FA(handlers.WebauthnDevicesGET))
|
2023-02-14 02:53:57 +00:00
|
|
|
r.PUT("/api/secondfactor/webauthn/credential/register", middleware1FA(handlers.WebauthnRegistrationPUT))
|
2023-02-01 11:10:38 +00:00
|
|
|
r.POST("/api/secondfactor/webauthn/credential/register", middleware1FA(handlers.WebauthnRegistrationPOST))
|
|
|
|
r.PUT("/api/secondfactor/webauthn/credential/{deviceID}", middleware2FA(handlers.WebauthnDevicePUT))
|
|
|
|
r.DELETE("/api/secondfactor/webauthn/credential/{deviceID}", middleware2FA(handlers.WebauthnDeviceDELETE))
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure DUO api endpoint only if configuration exists.
|
2022-04-15 23:34:26 +00:00
|
|
|
if !config.DuoAPI.Disable {
|
2022-04-08 04:13:47 +00:00
|
|
|
var duoAPI duo.API
|
|
|
|
if os.Getenv("ENVIRONMENT") == dev {
|
|
|
|
duoAPI = duo.NewDuoAPI(duoapi.NewDuoApi(
|
|
|
|
config.DuoAPI.IntegrationKey,
|
|
|
|
config.DuoAPI.SecretKey,
|
|
|
|
config.DuoAPI.Hostname, "", duoapi.SetInsecure()))
|
|
|
|
} else {
|
|
|
|
duoAPI = duo.NewDuoAPI(duoapi.NewDuoApi(
|
|
|
|
config.DuoAPI.IntegrationKey,
|
|
|
|
config.DuoAPI.SecretKey,
|
|
|
|
config.DuoAPI.Hostname, ""))
|
|
|
|
}
|
|
|
|
|
2022-06-10 01:34:43 +00:00
|
|
|
r.GET("/api/secondfactor/duo_devices", middleware1FA(handlers.DuoDevicesGET(duoAPI)))
|
|
|
|
r.POST("/api/secondfactor/duo", middleware1FA(handlers.DuoPOST(duoAPI)))
|
|
|
|
r.POST("/api/secondfactor/duo_device", middleware1FA(handlers.DuoDevicePOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if config.Server.Endpoints.EnablePprof {
|
2022-04-08 04:13:47 +00:00
|
|
|
r.GET("/debug/pprof/{name?}", pprofhandler.PprofHandler)
|
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if config.Server.Endpoints.EnableExpvars {
|
2022-04-08 04:13:47 +00:00
|
|
|
r.GET("/debug/vars", expvarhandler.ExpvarHandler)
|
|
|
|
}
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
if providers.OpenIDConnect != nil {
|
2023-02-11 10:45:26 +00:00
|
|
|
bridgeOIDC := middlewares.NewBridgeBuilder(*config, providers).WithPreMiddlewares(
|
2023-01-07 20:04:06 +00:00
|
|
|
middlewares.SecurityHeaders, middlewares.SecurityHeadersCSPNoneOpenIDConnect, middlewares.SecurityHeadersNoStore,
|
2022-06-10 01:34:43 +00:00
|
|
|
).Build()
|
|
|
|
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET("/api/oidc/consent", bridgeOIDC(handlers.OpenIDConnectConsentGET))
|
|
|
|
r.POST("/api/oidc/consent", bridgeOIDC(handlers.OpenIDConnectConsentPOST))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
allowedOrigins := utils.StringSliceFromURLs(config.IdentityProviders.OIDC.CORS.AllowedOrigins)
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathWellKnownOpenIDConfiguration, policyCORSPublicGET.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET(oidc.EndpointPathWellKnownOpenIDConfiguration, policyCORSPublicGET.Middleware(bridgeOIDC(handlers.OpenIDConnectConfigurationWellKnownGET)))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathWellKnownOAuthAuthorizationServer, policyCORSPublicGET.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET(oidc.EndpointPathWellKnownOAuthAuthorizationServer, policyCORSPublicGET.Middleware(bridgeOIDC(handlers.OAuthAuthorizationServerWellKnownGET)))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathJWKs, policyCORSPublicGET.HandleOPTIONS)
|
|
|
|
r.GET(oidc.EndpointPathJWKs, policyCORSPublicGET.Middleware(middlewareAPI(handlers.JSONWebKeySetGET)))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// TODO (james-d-elliott): Remove in GA. This is a legacy implementation of the above endpoint.
|
|
|
|
r.OPTIONS("/api/oidc/jwks", policyCORSPublicGET.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET("/api/oidc/jwks", policyCORSPublicGET.Middleware(bridgeOIDC(handlers.JSONWebKeySetGET)))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSAuthorization := middlewares.NewCORSPolicyBuilder().
|
2022-10-26 08:14:43 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodGet, fasthttp.MethodPost).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins(allowedOrigins...).
|
2022-10-20 02:16:36 +00:00
|
|
|
WithEnabled(utils.IsStringInSlice(oidc.EndpointAuthorization, config.IdentityProviders.OIDC.CORS.Endpoints)).
|
2022-04-08 04:13:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathAuthorization, policyCORSAuthorization.HandleOnlyOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET(oidc.EndpointPathAuthorization, policyCORSAuthorization.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectAuthorization))))
|
|
|
|
r.POST(oidc.EndpointPathAuthorization, policyCORSAuthorization.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectAuthorization))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// TODO (james-d-elliott): Remove in GA. This is a legacy endpoint.
|
|
|
|
r.OPTIONS("/api/oidc/authorize", policyCORSAuthorization.HandleOnlyOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET("/api/oidc/authorize", policyCORSAuthorization.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectAuthorization))))
|
|
|
|
r.POST("/api/oidc/authorize", policyCORSAuthorization.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectAuthorization))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSToken := middlewares.NewCORSPolicyBuilder().
|
|
|
|
WithAllowCredentials(true).
|
2022-10-26 08:14:43 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodPost).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins(allowedOrigins...).
|
2022-10-20 02:16:36 +00:00
|
|
|
WithEnabled(utils.IsStringInSlice(oidc.EndpointToken, config.IdentityProviders.OIDC.CORS.Endpoints)).
|
2022-04-08 04:13:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathToken, policyCORSToken.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.POST(oidc.EndpointPathToken, policyCORSToken.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectTokenPOST))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSUserinfo := middlewares.NewCORSPolicyBuilder().
|
|
|
|
WithAllowCredentials(true).
|
2022-10-26 08:14:43 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodGet, fasthttp.MethodPost).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins(allowedOrigins...).
|
2022-10-20 02:16:36 +00:00
|
|
|
WithEnabled(utils.IsStringInSlice(oidc.EndpointUserinfo, config.IdentityProviders.OIDC.CORS.Endpoints)).
|
2022-04-08 04:13:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathUserinfo, policyCORSUserinfo.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.GET(oidc.EndpointPathUserinfo, policyCORSUserinfo.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectUserinfo))))
|
|
|
|
r.POST(oidc.EndpointPathUserinfo, policyCORSUserinfo.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OpenIDConnectUserinfo))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSIntrospection := middlewares.NewCORSPolicyBuilder().
|
|
|
|
WithAllowCredentials(true).
|
2022-10-26 08:14:43 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodPost).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins(allowedOrigins...).
|
2022-10-20 02:16:36 +00:00
|
|
|
WithEnabled(utils.IsStringInSlice(oidc.EndpointIntrospection, config.IdentityProviders.OIDC.CORS.Endpoints)).
|
2022-04-08 04:13:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathIntrospection, policyCORSIntrospection.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.POST(oidc.EndpointPathIntrospection, policyCORSIntrospection.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OAuthIntrospectionPOST))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// TODO (james-d-elliott): Remove in GA. This is a legacy implementation of the above endpoint.
|
|
|
|
r.OPTIONS("/api/oidc/introspect", policyCORSIntrospection.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.POST("/api/oidc/introspect", policyCORSIntrospection.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OAuthIntrospectionPOST))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
policyCORSRevocation := middlewares.NewCORSPolicyBuilder().
|
|
|
|
WithAllowCredentials(true).
|
2022-10-26 08:14:43 +00:00
|
|
|
WithAllowedMethods(fasthttp.MethodOptions, fasthttp.MethodPost).
|
2022-04-08 04:13:47 +00:00
|
|
|
WithAllowedOrigins(allowedOrigins...).
|
2022-10-20 02:16:36 +00:00
|
|
|
WithEnabled(utils.IsStringInSlice(oidc.EndpointRevocation, config.IdentityProviders.OIDC.CORS.Endpoints)).
|
2022-04-08 04:13:47 +00:00
|
|
|
Build()
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
r.OPTIONS(oidc.EndpointPathRevocation, policyCORSRevocation.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.POST(oidc.EndpointPathRevocation, policyCORSRevocation.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OAuthRevocationPOST))))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
|
|
|
// TODO (james-d-elliott): Remove in GA. This is a legacy implementation of the above endpoint.
|
|
|
|
r.OPTIONS("/api/oidc/revoke", policyCORSRevocation.HandleOPTIONS)
|
2023-01-07 20:04:06 +00:00
|
|
|
r.POST("/api/oidc/revoke", policyCORSRevocation.Middleware(bridgeOIDC(middlewares.NewHTTPToAutheliaHandlerAdaptor(handlers.OAuthRevocationPOST))))
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.HandleMethodNotAllowed = true
|
2022-06-14 07:20:13 +00:00
|
|
|
r.MethodNotAllowed = handlers.Status(fasthttp.StatusMethodNotAllowed)
|
2023-01-25 09:36:40 +00:00
|
|
|
r.NotFound = handleNotFound(bridge(serveIndexHandler))
|
2022-04-08 04:13:47 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
handler := middlewares.LogRequest(r.Handler)
|
2022-04-08 04:13:47 +00:00
|
|
|
if config.Server.Path != "" {
|
2022-06-14 07:20:13 +00:00
|
|
|
handler = middlewares.StripPath(config.Server.Path)(handler)
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
handler = middlewares.Wrap(middlewares.NewMetricsRequest(providers.Metrics), handler)
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMetrics() fasthttp.RequestHandler {
|
|
|
|
r := router.New()
|
|
|
|
|
|
|
|
r.GET("/metrics", fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler()))
|
|
|
|
|
|
|
|
r.HandleMethodNotAllowed = true
|
|
|
|
r.MethodNotAllowed = handlers.Status(fasthttp.StatusMethodNotAllowed)
|
|
|
|
r.NotFound = handlers.Status(fasthttp.StatusNotFound)
|
|
|
|
|
|
|
|
return r.Handler
|
2022-04-08 04:13:47 +00:00
|
|
|
}
|