From bf7d0a7005263dafc2188d08ed48c76acadbf2ae Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sun, 3 May 2026 17:49:31 +0200 Subject: [PATCH] feat(db): add basic auth Signed-off-by: Klagarge --- db/get-db.http | 5 +++++ db/src/docs/docs.go | 20 ++++++++++++++++++++ db/src/docs/swagger.json | 20 ++++++++++++++++++++ db/src/docs/swagger.yaml | 9 +++++++++ db/src/main.go | 12 +++++++++++- db/src/rest/rest.go | 15 ++++++++++++++- 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/db/get-db.http b/db/get-db.http index 681a77f..64d98d0 100644 --- a/db/get-db.http +++ b/db/get-db.http @@ -1,11 +1,16 @@ @room-id = B3 +@username = user +@password = password ### GET last value of temp, co2, humidity, windows states GET http://localhost:8080/api/v1/rooms/{{room-id}}/current +Authorization: Basic {{username}} {{password}} ### GET history of a room @window = 1 day GET http://localhost:8080/api/v1/rooms/{{room-id}}/history?window={{window}} +Authorization: Basic {{username}} {{password}} ### GET all rooms GET http://localhost:8080/api/v1/rooms +Authorization: Basic {{username}} {{password}} diff --git a/db/src/docs/docs.go b/db/src/docs/docs.go index cd7b455..c31d1ae 100644 --- a/db/src/docs/docs.go +++ b/db/src/docs/docs.go @@ -17,6 +17,11 @@ const docTemplate = `{ "paths": { "/rooms": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get a list of all unique rooms from the measurement", "produces": [ "application/json" @@ -49,6 +54,11 @@ const docTemplate = `{ }, "/rooms/{room-id}/current": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get the latest record for a specific room", "produces": [ "application/json" @@ -97,6 +107,11 @@ const docTemplate = `{ }, "/rooms/{room-id}/history": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get history for a specific room", "produces": [ "application/json" @@ -144,6 +159,11 @@ const docTemplate = `{ } } } + }, + "securityDefinitions": { + "BasicAuth": { + "type": "basic" + } } }` diff --git a/db/src/docs/swagger.json b/db/src/docs/swagger.json index eb85b24..32a16ae 100644 --- a/db/src/docs/swagger.json +++ b/db/src/docs/swagger.json @@ -11,6 +11,11 @@ "paths": { "/rooms": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get a list of all unique rooms from the measurement", "produces": [ "application/json" @@ -43,6 +48,11 @@ }, "/rooms/{room-id}/current": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get the latest record for a specific room", "produces": [ "application/json" @@ -91,6 +101,11 @@ }, "/rooms/{room-id}/history": { "get": { + "security": [ + { + "BasicAuth": [] + } + ], "description": "Get history for a specific room", "produces": [ "application/json" @@ -138,5 +153,10 @@ } } } + }, + "securityDefinitions": { + "BasicAuth": { + "type": "basic" + } } } \ No newline at end of file diff --git a/db/src/docs/swagger.yaml b/db/src/docs/swagger.yaml index e79e279..32e961e 100644 --- a/db/src/docs/swagger.yaml +++ b/db/src/docs/swagger.yaml @@ -24,6 +24,8 @@ paths: additionalProperties: type: string type: object + security: + - BasicAuth: [] summary: Get all unique rooms tags: - rooms @@ -56,6 +58,8 @@ paths: additionalProperties: type: string type: object + security: + - BasicAuth: [] summary: Get current data for a room tags: - rooms @@ -89,7 +93,12 @@ paths: additionalProperties: type: string type: object + security: + - BasicAuth: [] summary: Get history for a room tags: - rooms +securityDefinitions: + BasicAuth: + type: basic swagger: "2.0" diff --git a/db/src/main.go b/db/src/main.go index b87e8b8..1863386 100644 --- a/db/src/main.go +++ b/db/src/main.go @@ -12,6 +12,13 @@ import ( "time" ) +func getEnv(key, fallback string) string { + if value := os.Getenv(key); value != "" { + return value + } + return fallback +} + type ProvenceData struct { CO2PPM int `json:"co2_ppm"` Temp float64 `json:"temp"` @@ -98,6 +105,7 @@ func influxConnection() *influx.InfluxGateway { // @description This is a gateway API for IoT data. // @host doc.db.e.kb28.ch // @BasePath /api/v1 +// @securityDefinitions.basic BasicAuth func main() { // Load mapping configuration mappingPath := os.Getenv("MAPPING_CONFIG_PATH") @@ -165,7 +173,9 @@ func main() { } // Initialize and start REST Gateway - restGateway := rest.NewRestGateway(influxGateway, measurementName) + restUsername := getEnv("REST_USERNAME", "user") + restPassword := getEnv("REST_PASSWORD", "password") + restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword) port, ok := os.LookupEnv("REST_PORT") if !ok { diff --git a/db/src/rest/rest.go b/db/src/rest/rest.go index 02bbb04..8524e2a 100644 --- a/db/src/rest/rest.go +++ b/db/src/rest/rest.go @@ -17,13 +17,17 @@ type RestGateway struct { influxGateway *influx.InfluxGateway engine *gin.Engine measurementName string + username string + password string } -func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string) *RestGateway { +func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string, username, password string) *RestGateway { g := &RestGateway{ influxGateway: influxGateway, engine: gin.Default(), measurementName: measurementName, + username: username, + password: password, } g.setupRoutes() @@ -32,6 +36,12 @@ func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string) func (g *RestGateway) setupRoutes() { v1 := g.engine.Group("/api/v1") + if g.username != "" && g.password != "" { + v1.Use(gin.BasicAuth(gin.Accounts{ + g.username: g.password, + })) + } + { v1.GET("/rooms", g.getRooms) v1.GET("/rooms/:room-id/current", g.getRoomCurrent) @@ -53,6 +63,7 @@ func (g *RestGateway) Run(addr string) error { // @Produce json // @Success 200 {array} string // @Failure 500 {object} map[string]string +// @Security BasicAuth // @Router /rooms [get] func (g *RestGateway) getRooms(c *gin.Context) { // Query unique rooms from the measurement @@ -91,6 +102,7 @@ func (g *RestGateway) getRooms(c *gin.Context) { // @Success 200 {object} map[string]any // @Failure 404 {object} map[string]string // @Failure 500 {object} map[string]string +// @Security BasicAuth // @Router /rooms/{room-id}/current [get] func (g *RestGateway) getRoomCurrent(c *gin.Context) { roomID := c.Param("room-id") @@ -128,6 +140,7 @@ func (g *RestGateway) getRoomCurrent(c *gin.Context) { // @Param window query string false "Time window (e.g., 1 day, 1 hour, 30 min)" default(1 day) // @Success 200 {array} map[string]any // @Failure 500 {object} map[string]string +// @Security BasicAuth // @Router /rooms/{room-id}/history [get] func (g *RestGateway) getRoomHistory(c *gin.Context) { roomID := c.Param("room-id")