2019-04-24 21:52:08 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
duoapi "github.com/duosecurity/duo_api_golang"
|
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-11 04:59:58 +00:00
|
|
|
"github.com/valyala/fasthttp/expvarhandler"
|
2020-04-28 14:07:20 +00:00
|
|
|
"github.com/valyala/fasthttp/fasthttpadaptor"
|
2020-04-11 04:59:58 +00:00
|
|
|
"github.com/valyala/fasthttp/pprofhandler"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/duo"
|
|
|
|
"github.com/authelia/authelia/internal/handlers"
|
|
|
|
"github.com/authelia/authelia/internal/logging"
|
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StartServer start Authelia server with the given configuration and providers.
|
|
|
|
func StartServer(configuration schema.Configuration, providers middlewares.Providers) {
|
|
|
|
autheliaMiddleware := middlewares.AutheliaMiddleware(configuration, providers)
|
2020-04-28 14:07:20 +00:00
|
|
|
embeddedAssets := "/public_html"
|
2020-05-07 11:29:12 +00:00
|
|
|
rootFiles := []string{"favicon.ico", "manifest.json", "robots.txt"}
|
2020-04-28 14:07:20 +00:00
|
|
|
// TODO: Remove in v4.18.0.
|
|
|
|
if os.Getenv("PUBLIC_DIR") != "" {
|
|
|
|
logging.Logger().Warn("PUBLIC_DIR environment variable has been deprecated, assets are now embedded.")
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 04:59:58 +00:00
|
|
|
router := router.New()
|
2020-04-21 00:23:28 +00:00
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
router.GET("/", ServeIndex(embeddedAssets))
|
2020-05-07 11:29:12 +00:00
|
|
|
|
|
|
|
for _, f := range rootFiles {
|
|
|
|
router.GET("/"+f, fasthttpadaptor.NewFastHTTPHandler(br.Serve(embeddedAssets)))
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
router.GET("/static/{filepath:*}", fasthttpadaptor.NewFastHTTPHandler(br.Serve(embeddedAssets)))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
router.GET("/api/state", autheliaMiddleware(handlers.StateGet))
|
|
|
|
|
2019-12-07 16:40:42 +00:00
|
|
|
router.GET("/api/configuration", autheliaMiddleware(handlers.ConfigurationGet))
|
|
|
|
router.GET("/api/configuration/extended", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.ExtendedConfigurationGet)))
|
|
|
|
|
2020-05-04 19:39:25 +00:00
|
|
|
router.GET("/api/verify", autheliaMiddleware(handlers.VerifyGet(configuration.AuthenticationBackend)))
|
|
|
|
router.HEAD("/api/verify", autheliaMiddleware(handlers.VerifyGet(configuration.AuthenticationBackend)))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-05-20 22:03:15 +00:00
|
|
|
router.POST("/api/firstfactor", autheliaMiddleware(handlers.FirstFactorPost(1000, true)))
|
2019-04-24 21:52:08 +00:00
|
|
|
router.POST("/api/logout", autheliaMiddleware(handlers.LogoutPost))
|
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// Only register endpoints if forgot password is not disabled.
|
2020-04-04 23:28:09 +00:00
|
|
|
if !configuration.AuthenticationBackend.DisableResetPassword {
|
|
|
|
// Password reset related endpoints.
|
|
|
|
router.POST("/api/reset-password/identity/start", autheliaMiddleware(
|
|
|
|
handlers.ResetPasswordIdentityStart))
|
|
|
|
router.POST("/api/reset-password/identity/finish", autheliaMiddleware(
|
|
|
|
handlers.ResetPasswordIdentityFinish))
|
|
|
|
router.POST("/api/reset-password", autheliaMiddleware(
|
|
|
|
handlers.ResetPasswordPost))
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// Information about the user.
|
2019-12-07 11:18:22 +00:00
|
|
|
router.GET("/api/user/info", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.UserInfoGet)))
|
|
|
|
router.POST("/api/user/info/2fa_method", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.MethodPreferencePost)))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// TOTP related endpoints.
|
2019-04-24 21:52:08 +00:00
|
|
|
router.POST("/api/secondfactor/totp/identity/start", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPIdentityStart)))
|
|
|
|
router.POST("/api/secondfactor/totp/identity/finish", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPIdentityFinish)))
|
|
|
|
router.POST("/api/secondfactor/totp", autheliaMiddleware(
|
2020-03-25 01:48:20 +00:00
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPPost(&handlers.TOTPVerifierImpl{
|
|
|
|
Period: uint(configuration.TOTP.Period),
|
|
|
|
Skew: uint(*configuration.TOTP.Skew),
|
|
|
|
}))))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// U2F related endpoints.
|
2019-04-24 21:52:08 +00:00
|
|
|
router.POST("/api/secondfactor/u2f/identity/start", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FIdentityStart)))
|
|
|
|
router.POST("/api/secondfactor/u2f/identity/finish", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FIdentityFinish)))
|
|
|
|
|
|
|
|
router.POST("/api/secondfactor/u2f/register", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FRegister)))
|
|
|
|
|
|
|
|
router.POST("/api/secondfactor/u2f/sign_request", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FSignGet)))
|
2020-02-01 12:54:50 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
router.POST("/api/secondfactor/u2f/sign", autheliaMiddleware(
|
2020-02-01 12:54:50 +00:00
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FSignPost(&handlers.U2FVerifierImpl{}))))
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// Configure DUO api endpoint only if configuration exists.
|
2019-04-24 21:52:08 +00:00
|
|
|
if configuration.DuoAPI != nil {
|
|
|
|
var duoAPI duo.API
|
|
|
|
if os.Getenv("ENVIRONMENT") == "dev" {
|
|
|
|
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, ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
router.POST("/api/secondfactor/duo", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorDuoPost(duoAPI))))
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
// If trace is set, enable pprofhandler and expvarhandler.
|
2020-04-11 04:59:58 +00:00
|
|
|
if configuration.LogLevel == "trace" {
|
|
|
|
router.GET("/debug/pprof/{name?}", pprofhandler.PprofHandler)
|
|
|
|
router.GET("/debug/vars", expvarhandler.ExpvarHandler)
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:07:20 +00:00
|
|
|
router.NotFound = ServeIndex(embeddedAssets)
|
2019-11-30 16:49:52 +00:00
|
|
|
|
2020-04-11 04:59:58 +00:00
|
|
|
server := &fasthttp.Server{
|
2020-04-30 03:16:41 +00:00
|
|
|
ErrorHandler: autheliaErrorHandler,
|
|
|
|
Handler: middlewares.LogRequestMiddleware(router.Handler),
|
|
|
|
NoDefaultServerHeader: true,
|
|
|
|
ReadBufferSize: configuration.Server.ReadBufferSize,
|
|
|
|
WriteBufferSize: configuration.Server.WriteBufferSize,
|
2020-04-11 04:59:58 +00:00
|
|
|
}
|
2020-04-30 02:03:05 +00:00
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
addrPattern := fmt.Sprintf("%s:%d", configuration.Host, configuration.Port)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
if configuration.TLSCert != "" && configuration.TLSKey != "" {
|
2020-03-10 07:14:28 +00:00
|
|
|
logging.Logger().Infof("Authelia is listening for TLS connections on %s", addrPattern)
|
2020-04-11 04:59:58 +00:00
|
|
|
logging.Logger().Fatal(server.ListenAndServeTLS(addrPattern, configuration.TLSCert, configuration.TLSKey))
|
2020-03-03 07:18:25 +00:00
|
|
|
} else {
|
2020-03-10 07:14:28 +00:00
|
|
|
logging.Logger().Infof("Authelia is listening for non-TLS connections on %s", addrPattern)
|
2020-04-11 04:59:58 +00:00
|
|
|
logging.Logger().Fatal(server.ListenAndServe(addrPattern))
|
2020-03-03 07:18:25 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|