2019-04-25 04:52:08 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-02-22 06:07:06 +07:00
|
|
|
"embed"
|
|
|
|
"io/fs"
|
2020-07-16 13:36:37 +07:00
|
|
|
"net"
|
2021-02-22 06:07:06 +07:00
|
|
|
"net/http"
|
2019-04-25 04:52:08 +07:00
|
|
|
"os"
|
2020-06-21 20:40:37 +07:00
|
|
|
"strconv"
|
2022-01-21 06:46:13 +07:00
|
|
|
"time"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
duoapi "github.com/duosecurity/duo_api_golang"
|
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-11 11:59:58 +07:00
|
|
|
"github.com/valyala/fasthttp/expvarhandler"
|
2020-04-28 21:07:20 +07:00
|
|
|
"github.com/valyala/fasthttp/fasthttpadaptor"
|
2020-04-11 11:59:58 +07:00
|
|
|
"github.com/valyala/fasthttp/pprofhandler"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/duo"
|
|
|
|
"github.com/authelia/authelia/v4/internal/handlers"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2021-02-22 06:07:06 +07:00
|
|
|
//go:embed public_html
|
|
|
|
var assets embed.FS
|
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
func registerRoutes(configuration schema.Configuration, providers middlewares.Providers) fasthttp.RequestHandler {
|
2019-04-25 04:52:08 +07:00
|
|
|
autheliaMiddleware := middlewares.AutheliaMiddleware(configuration, providers)
|
2022-03-13 09:51:23 +07:00
|
|
|
rememberMe := strconv.FormatBool(configuration.Session.RememberMeDuration != schema.RememberMeDisabled)
|
2020-06-21 20:40:37 +07:00
|
|
|
resetPassword := strconv.FormatBool(!configuration.AuthenticationBackend.DisableResetPassword)
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
duoSelfEnrollment := f
|
|
|
|
if configuration.DuoAPI != nil {
|
|
|
|
duoSelfEnrollment = strconv.FormatBool(configuration.DuoAPI.EnableSelfEnrollment)
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:07:06 +07:00
|
|
|
embeddedPath, _ := fs.Sub(assets, "public_html")
|
|
|
|
embeddedFS := fasthttpadaptor.NewFastHTTPHandler(http.FileServer(http.FS(embeddedPath)))
|
2020-05-21 09:20:55 +07:00
|
|
|
|
2021-10-10 17:19:47 +07:00
|
|
|
https := configuration.Server.TLS.Key != "" && configuration.Server.TLS.Certificate != ""
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
serveIndexHandler := ServeTemplatedFile(embeddedAssets, indexFile, configuration.Server.AssetPath, duoSelfEnrollment, rememberMe, resetPassword, configuration.Session.Name, configuration.Theme, https)
|
|
|
|
serveSwaggerHandler := ServeTemplatedFile(swaggerAssets, indexFile, configuration.Server.AssetPath, duoSelfEnrollment, rememberMe, resetPassword, configuration.Session.Name, configuration.Theme, https)
|
|
|
|
serveSwaggerAPIHandler := ServeTemplatedFile(swaggerAssets, apiFile, configuration.Server.AssetPath, duoSelfEnrollment, rememberMe, resetPassword, configuration.Session.Name, configuration.Theme, https)
|
2020-06-21 20:40:37 +07:00
|
|
|
|
2020-05-21 09:20:55 +07:00
|
|
|
r := router.New()
|
2022-02-06 20:37:28 +07:00
|
|
|
r.GET("/", autheliaMiddleware(serveIndexHandler))
|
2021-07-22 10:52:37 +07:00
|
|
|
r.OPTIONS("/", autheliaMiddleware(handleOPTIONS))
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
r.GET("/api/", autheliaMiddleware(serveSwaggerHandler))
|
|
|
|
r.GET("/api/"+apiFile, autheliaMiddleware(serveSwaggerAPIHandler))
|
2020-05-07 18:29:12 +07:00
|
|
|
|
|
|
|
for _, f := range rootFiles {
|
2021-11-15 15:37:58 +07:00
|
|
|
r.GET("/"+f, middlewares.AssetOverrideMiddleware(configuration.Server.AssetPath, embeddedFS))
|
2020-05-07 18:29:12 +07:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:37:58 +07:00
|
|
|
r.GET("/static/{filepath:*}", middlewares.AssetOverrideMiddleware(configuration.Server.AssetPath, embeddedFS))
|
2021-03-11 14:36:58 +07:00
|
|
|
r.ANY("/api/{filepath:*}", embeddedFS)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-11-25 06:20:52 +07:00
|
|
|
r.GET("/api/health", autheliaMiddleware(handlers.HealthGet))
|
2020-05-21 09:20:55 +07:00
|
|
|
r.GET("/api/state", autheliaMiddleware(handlers.StateGet))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-06-21 20:40:37 +07:00
|
|
|
r.GET("/api/configuration", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.ConfigurationGet)))
|
2019-12-07 23:40:42 +07:00
|
|
|
|
2022-04-03 18:58:27 +07:00
|
|
|
r.GET("/api/configuration/password-policy", autheliaMiddleware(handlers.PasswordPolicyConfigurationGet))
|
|
|
|
|
2020-05-21 09:20:55 +07:00
|
|
|
r.GET("/api/verify", autheliaMiddleware(handlers.VerifyGet(configuration.AuthenticationBackend)))
|
|
|
|
r.HEAD("/api/verify", autheliaMiddleware(handlers.VerifyGet(configuration.AuthenticationBackend)))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-08-02 13:15:38 +07:00
|
|
|
r.POST("/api/checks/safe-redirection", autheliaMiddleware(handlers.CheckSafeRedirection))
|
|
|
|
|
2022-01-21 06:46:13 +07:00
|
|
|
r.POST("/api/firstfactor", autheliaMiddleware(handlers.FirstFactorPost(middlewares.TimingAttackDelay(10, 250, 85, time.Second))))
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/logout", autheliaMiddleware(handlers.LogoutPost))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-28 21:07:20 +07:00
|
|
|
// Only register endpoints if forgot password is not disabled.
|
2020-04-05 06:28:09 +07:00
|
|
|
if !configuration.AuthenticationBackend.DisableResetPassword {
|
|
|
|
// Password reset related endpoints.
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/reset-password/identity/start", autheliaMiddleware(
|
2020-04-05 06:28:09 +07:00
|
|
|
handlers.ResetPasswordIdentityStart))
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/reset-password/identity/finish", autheliaMiddleware(
|
2020-04-05 06:28:09 +07:00
|
|
|
handlers.ResetPasswordIdentityFinish))
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/reset-password", autheliaMiddleware(
|
2020-04-05 06:28:09 +07:00
|
|
|
handlers.ResetPasswordPost))
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-28 21:07:20 +07:00
|
|
|
// Information about the user.
|
2020-05-21 09:20:55 +07:00
|
|
|
r.GET("/api/user/info", autheliaMiddleware(
|
2022-03-28 08:26:30 +07:00
|
|
|
middlewares.RequireFirstFactor(handlers.UserInfoGET)))
|
|
|
|
r.POST("/api/user/info", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.UserInfoPOST)))
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/user/info/2fa_method", autheliaMiddleware(
|
2019-12-07 18:18:22 +07:00
|
|
|
middlewares.RequireFirstFactor(handlers.MethodPreferencePost)))
|
2022-03-03 18:20:43 +07:00
|
|
|
|
|
|
|
if !configuration.TOTP.Disable {
|
|
|
|
// TOTP related endpoints.
|
|
|
|
r.GET("/api/user/info/totp", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.UserTOTPGet)))
|
|
|
|
|
|
|
|
r.POST("/api/secondfactor/totp/identity/start", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPIdentityStart)))
|
|
|
|
r.POST("/api/secondfactor/totp/identity/finish", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPIdentityFinish)))
|
|
|
|
r.POST("/api/secondfactor/totp", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPPost)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !configuration.Webauthn.Disable {
|
|
|
|
// Webauthn Endpoints.
|
|
|
|
r.POST("/api/secondfactor/webauthn/identity/start", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorWebauthnIdentityStart)))
|
|
|
|
r.POST("/api/secondfactor/webauthn/identity/finish", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorWebauthnIdentityFinish)))
|
|
|
|
r.POST("/api/secondfactor/webauthn/attestation", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorWebauthnAttestationPOST)))
|
|
|
|
|
|
|
|
r.GET("/api/secondfactor/webauthn/assertion", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorWebauthnAssertionGET)))
|
|
|
|
r.POST("/api/secondfactor/webauthn/assertion", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorWebauthnAssertionPOST)))
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-28 21:07:20 +07:00
|
|
|
// Configure DUO api endpoint only if configuration exists.
|
2019-04-25 04:52:08 +07:00
|
|
|
if configuration.DuoAPI != nil {
|
|
|
|
var duoAPI duo.API
|
2020-11-19 08:50:34 +07:00
|
|
|
if os.Getenv("ENVIRONMENT") == dev {
|
2019-04-25 04:52:08 +07:00
|
|
|
duoAPI = duo.NewDuoAPI(duoapi.NewDuoApi(
|
|
|
|
configuration.DuoAPI.IntegrationKey,
|
|
|
|
configuration.DuoAPI.SecretKey,
|
|
|
|
configuration.DuoAPI.Hostname, "", duoapi.SetInsecure()))
|
|
|
|
} else {
|
|
|
|
duoAPI = duo.NewDuoAPI(duoapi.NewDuoApi(
|
|
|
|
configuration.DuoAPI.IntegrationKey,
|
|
|
|
configuration.DuoAPI.SecretKey,
|
|
|
|
configuration.DuoAPI.Hostname, ""))
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
r.GET("/api/secondfactor/duo_devices", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorDuoDevicesGet(duoAPI))))
|
|
|
|
|
2020-05-21 09:20:55 +07:00
|
|
|
r.POST("/api/secondfactor/duo", autheliaMiddleware(
|
2019-04-25 04:52:08 +07:00
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorDuoPost(duoAPI))))
|
2021-12-01 10:32:58 +07:00
|
|
|
|
|
|
|
r.POST("/api/secondfactor/duo_device", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorDuoDevicePost)))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-06-01 11:09:50 +07:00
|
|
|
if configuration.Server.EnablePprof {
|
2020-05-21 09:20:55 +07:00
|
|
|
r.GET("/debug/pprof/{name?}", pprofhandler.PprofHandler)
|
2021-06-01 11:09:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Server.EnableExpvars {
|
2020-05-21 09:20:55 +07:00
|
|
|
r.GET("/debug/vars", expvarhandler.ExpvarHandler)
|
2020-04-11 11:59:58 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
r.NotFound = autheliaMiddleware(serveIndexHandler)
|
2020-05-21 09:20:55 +07:00
|
|
|
|
|
|
|
handler := middlewares.LogRequestMiddleware(r.Handler)
|
|
|
|
if configuration.Server.Path != "" {
|
2021-08-10 07:31:08 +07:00
|
|
|
handler = middlewares.StripPathMiddleware(configuration.Server.Path, handler)
|
2020-05-21 09:20:55 +07:00
|
|
|
}
|
2019-11-30 23:49:52 +07:00
|
|
|
|
2021-05-05 05:06:05 +07:00
|
|
|
if providers.OpenIDConnect.Fosite != nil {
|
|
|
|
handlers.RegisterOIDC(r, autheliaMiddleware)
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
// Start Authelia's internal webserver with the given configuration and providers.
|
|
|
|
func Start(configuration schema.Configuration, providers middlewares.Providers) {
|
2021-05-05 05:06:05 +07:00
|
|
|
logger := logging.Logger()
|
|
|
|
|
|
|
|
handler := registerRoutes(configuration, providers)
|
|
|
|
|
2020-04-11 11:59:58 +07:00
|
|
|
server := &fasthttp.Server{
|
2020-04-30 10:16:41 +07:00
|
|
|
ErrorHandler: autheliaErrorHandler,
|
2020-05-21 09:20:55 +07:00
|
|
|
Handler: handler,
|
2020-04-30 10:16:41 +07:00
|
|
|
NoDefaultServerHeader: true,
|
|
|
|
ReadBufferSize: configuration.Server.ReadBufferSize,
|
|
|
|
WriteBufferSize: configuration.Server.WriteBufferSize,
|
2020-04-11 11:59:58 +07:00
|
|
|
}
|
2020-04-30 09:03:05 +07:00
|
|
|
|
2022-03-25 07:56:23 +07:00
|
|
|
address := net.JoinHostPort(configuration.Server.Host, strconv.Itoa(configuration.Server.Port))
|
2020-07-16 13:36:37 +07:00
|
|
|
|
2022-03-25 07:56:23 +07:00
|
|
|
listener, err := net.Listen("tcp", address)
|
2020-07-16 13:36:37 +07:00
|
|
|
if err != nil {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Fatalf("Error initializing listener: %s", err)
|
2020-07-16 13:36:37 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-08-02 18:55:30 +07:00
|
|
|
if configuration.Server.TLS.Certificate != "" && configuration.Server.TLS.Key != "" {
|
2021-08-05 11:02:07 +07:00
|
|
|
if err = writeHealthCheckEnv(configuration.Server.DisableHealthcheck, "https", configuration.Server.Host, configuration.Server.Path, configuration.Server.Port); err != nil {
|
|
|
|
logger.Fatalf("Could not configure healthcheck: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:31:08 +07:00
|
|
|
if configuration.Server.Path == "" {
|
2022-03-25 07:56:23 +07:00
|
|
|
logger.Infof("Listening for TLS connections on '%s' path '/'", address)
|
2021-08-10 07:31:08 +07:00
|
|
|
} else {
|
2022-03-25 07:56:23 +07:00
|
|
|
logger.Infof("Listening for TLS connections on '%s' paths '/' and '%s'", address, configuration.Server.Path)
|
2021-08-10 07:31:08 +07:00
|
|
|
}
|
|
|
|
|
2021-08-02 18:55:30 +07:00
|
|
|
logger.Fatal(server.ServeTLS(listener, configuration.Server.TLS.Certificate, configuration.Server.TLS.Key))
|
2020-03-03 14:18:25 +07:00
|
|
|
} else {
|
2021-08-05 11:02:07 +07:00
|
|
|
if err = writeHealthCheckEnv(configuration.Server.DisableHealthcheck, "http", configuration.Server.Host, configuration.Server.Path, configuration.Server.Port); err != nil {
|
|
|
|
logger.Fatalf("Could not configure healthcheck: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:31:08 +07:00
|
|
|
if configuration.Server.Path == "" {
|
2022-03-25 07:56:23 +07:00
|
|
|
logger.Infof("Listening for non-TLS connections on '%s' path '/'", address)
|
2021-08-10 07:31:08 +07:00
|
|
|
} else {
|
2022-03-25 07:56:23 +07:00
|
|
|
logger.Infof("Listening for non-TLS connections on '%s' paths '/' and '%s'", address, configuration.Server.Path)
|
2021-08-10 07:31:08 +07:00
|
|
|
}
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Fatal(server.Serve(listener))
|
2020-03-03 14:18:25 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|