2019-04-25 04:52:08 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-30 23:49:52 +07:00
|
|
|
"path"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-12-24 09:14:52 +07: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-25 04:52:08 +07:00
|
|
|
duoapi "github.com/duosecurity/duo_api_golang"
|
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StartServer start Authelia server with the given configuration and providers.
|
|
|
|
func StartServer(configuration schema.Configuration, providers middlewares.Providers) {
|
|
|
|
router := router.New()
|
|
|
|
|
|
|
|
autheliaMiddleware := middlewares.AutheliaMiddleware(configuration, providers)
|
|
|
|
|
|
|
|
publicDir := os.Getenv("PUBLIC_DIR")
|
|
|
|
if publicDir == "" {
|
|
|
|
publicDir = "./public_html"
|
|
|
|
}
|
2020-03-01 07:51:11 +07:00
|
|
|
logging.Logger().Infof("Selected public_html directory is %s", publicDir)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
router.GET("/", fasthttp.FSHandler(publicDir, 0))
|
|
|
|
router.ServeFiles("/static/*filepath", publicDir+"/static")
|
|
|
|
|
|
|
|
router.GET("/api/state", autheliaMiddleware(handlers.StateGet))
|
|
|
|
|
2019-12-07 23:40:42 +07:00
|
|
|
router.GET("/api/configuration", autheliaMiddleware(handlers.ConfigurationGet))
|
|
|
|
router.GET("/api/configuration/extended", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.ExtendedConfigurationGet)))
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
router.GET("/api/verify", autheliaMiddleware(handlers.VerifyGet))
|
|
|
|
|
|
|
|
router.POST("/api/firstfactor", autheliaMiddleware(handlers.FirstFactorPost))
|
|
|
|
router.POST("/api/logout", autheliaMiddleware(handlers.LogoutPost))
|
|
|
|
|
|
|
|
// 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-12-07 18:18:22 +07:00
|
|
|
// Information about the user
|
|
|
|
router.GET("/api/user/info", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.UserInfoGet)))
|
|
|
|
router.POST("/api/user/info/2fa_method", autheliaMiddleware(
|
|
|
|
middlewares.RequireFirstFactor(handlers.MethodPreferencePost)))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// TOTP related endpoints
|
|
|
|
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-02-01 19:54:50 +07:00
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorTOTPPost(&handlers.TOTPVerifierImpl{}))))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// U2F related endpoints
|
|
|
|
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 19:54:50 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
router.POST("/api/secondfactor/u2f/sign", autheliaMiddleware(
|
2020-02-01 19:54:50 +07:00
|
|
|
middlewares.RequireFirstFactor(handlers.SecondFactorU2FSignPost(&handlers.U2FVerifierImpl{}))))
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
// Configure DUO api endpoint only if configuration exists
|
|
|
|
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))))
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
router.NotFound = func(ctx *fasthttp.RequestCtx) {
|
|
|
|
ctx.SendFile(path.Join(publicDir, "index.html"))
|
|
|
|
}
|
|
|
|
|
2019-12-07 03:45:59 +07:00
|
|
|
portPattern := fmt.Sprintf("%s:%d", configuration.Host, configuration.Port)
|
2019-04-25 04:52:08 +07:00
|
|
|
logging.Logger().Infof("Authelia is listening on %s", portPattern)
|
|
|
|
|
|
|
|
logging.Logger().Fatal(fasthttp.ListenAndServe(portPattern,
|
|
|
|
middlewares.LogRequestMiddleware(router.Handler)))
|
|
|
|
}
|