Allow auth-less

This commit is contained in:
Jan Zípek 2024-03-31 18:33:56 +02:00
parent 013886a2ff
commit aca2e8778c
Signed by: kamen
GPG Key ID: A17882625B33AC31
5 changed files with 26 additions and 5 deletions

View File

@ -13,6 +13,8 @@ COPY server/config/*.go ./config/
COPY server/routes/*.go ./routes/ COPY server/routes/*.go ./routes/
COPY server/services/*.go ./services/ COPY server/services/*.go ./services/
COPY server/middleware/*.go ./middleware/ COPY server/middleware/*.go ./middleware/
COPY server/integrations/*.go ./integrations/
COPY server/models/*.go ./models/
COPY server/database ./database/ COPY server/database ./database/
RUN go build -o /basic-sensor-receiver RUN go build -o /basic-sensor-receiver
@ -53,6 +55,7 @@ ENV BIND_IP 0.0.0.0
ENV AUTH_USERNAME admin ENV AUTH_USERNAME admin
ENV AUTH_PASSWORD password ENV AUTH_PASSWORD password
ENV AUTH_KEY password ENV AUTH_KEY password
ENV AUTH_ENABLED true
EXPOSE ${PORT} EXPOSE ${PORT}
VOLUME [ "/data" ] VOLUME [ "/data" ]

View File

@ -2,6 +2,7 @@ GIN_MODE=debug
DATABASE_URL=./sensors.sqlite3?_busy_timeout=500 DATABASE_URL=./sensors.sqlite3?_busy_timeout=500
PORT=8083 PORT=8083
BIND_IP=localhost BIND_IP=localhost
AUTH_ENABLED=true
AUTH_USERNAME=admin AUTH_USERNAME=admin
AUTH_PASSWORD=password AUTH_PASSWORD=password
AUTH_KEY=password AUTH_KEY=password

View File

@ -10,6 +10,7 @@ type Config struct {
DatabaseUrl string DatabaseUrl string
Port int Port int
Ip string Ip string
AuthEnabled bool
AuthUsername string AuthUsername string
AuthPassword string AuthPassword string
AuthKey string AuthKey string
@ -27,6 +28,7 @@ func LoadConfig() *Config {
DatabaseUrl: os.Getenv("DATABASE_URL"), DatabaseUrl: os.Getenv("DATABASE_URL"),
Port: port, Port: port,
Ip: os.Getenv("BIND_IP"), Ip: os.Getenv("BIND_IP"),
AuthEnabled: os.Getenv("AUTH_ENABLED") != "false",
AuthUsername: os.Getenv("AUTH_USERNAME"), AuthUsername: os.Getenv("AUTH_USERNAME"),
AuthPassword: os.Getenv("AUTH_PASSWORD"), AuthPassword: os.Getenv("AUTH_PASSWORD"),
AuthKey: os.Getenv("AUTH_KEY"), AuthKey: os.Getenv("AUTH_KEY"),

View File

@ -15,7 +15,7 @@ import (
func main() { func main() {
err := godotenv.Load() err := godotenv.Load()
if err != nil { if err != nil {
log.Println("Error loading .env file") log.Println("Error loading .env file: ", err)
} }
server := app.InitializeServer() server := app.InitializeServer()
@ -33,8 +33,10 @@ func main() {
router.Use(middleware.CorsMiddleware()) router.Use(middleware.CorsMiddleware())
} }
// User login route if server.Config.AuthEnabled {
router.POST("/api/login", routes.Login(server)) // User login route
router.POST("/api/login", routes.Login(server))
}
// Routes that are only accessible after logging in // Routes that are only accessible after logging in
loginProtected := router.Group("/", middleware.LoginAuthMiddleware(server)) loginProtected := router.Group("/", middleware.LoginAuthMiddleware(server))
@ -63,7 +65,10 @@ func main() {
loginProtected.PUT("/api/contact-points/:contactPointId", routes.PutContactPoint(server)) loginProtected.PUT("/api/contact-points/:contactPointId", routes.PutContactPoint(server))
loginProtected.DELETE("/api/contact-points/:contactPointId", routes.DeleteContactPoint(server)) loginProtected.DELETE("/api/contact-points/:contactPointId", routes.DeleteContactPoint(server))
loginProtected.POST("/api/contact-points/test", routes.TestContactPoint(server)) loginProtected.POST("/api/contact-points/test", routes.TestContactPoint(server))
loginProtected.POST("/api/logout", routes.Logout(server))
if server.Config.AuthEnabled {
loginProtected.POST("/api/logout", routes.Logout(server))
}
// Routes accessible using auth key // Routes accessible using auth key
keyProtected := router.Group("/", middleware.KeyAuthMiddleware(server)) keyProtected := router.Group("/", middleware.KeyAuthMiddleware(server))
@ -75,6 +80,10 @@ func main() {
// Starts alerts handling goroutine // Starts alerts handling goroutine
server.StartAlerts() server.StartAlerts()
address := fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port)
log.Println("Starting server on", address)
// Run the server // Run the server
router.Run(fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port)) router.Run(address)
} }

View File

@ -10,6 +10,12 @@ import (
func LoginAuthMiddleware(server *app.Server) gin.HandlerFunc { func LoginAuthMiddleware(server *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
if !server.Config.AuthEnabled {
c.Next()
return
}
_, err := server.Services.Auth.FromContext(c) _, err := server.Services.Auth.FromContext(c)
if err != nil { if err != nil {