From 69704a38ae92b933e53e3e85d32176229db88449 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Wed, 17 Jul 2024 07:42:51 +0200 Subject: [PATCH] Add some very basic authentication --- README.md | 2 +- main.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 021b6d1..45b47d2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,6 @@ - [x] Use timeout for fetching departures - [ ] Write ESP8266 client -- [ ] Add basic auth +- [x] Add basic auth - [ ] Create Nix package - [ ] Create container diff --git a/main.go b/main.go index edb887c..6fe9c4f 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import "fmt" import "log" import "net/http" import "net/url" +import "os" import "slices" import "strconv" import "time" @@ -81,7 +82,18 @@ func FetchDepartures(stopId string) (*DMResponse, error) { } func main() { + password := os.Getenv("VRNP_PASSWORD") + if len(password) == 0 { + panic("Required environment variable VRNP_PASSWORD is not set"); + } + http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) { + user, pass, ok := r.BasicAuth() + if !(ok && user == "admin" && pass == password) { + http.Error(w, "You shall not pass", http.StatusUnauthorized) + return + } + query := r.URL.Query() if query["stop_id"] == nil || len(query["stop_id"]) < 1 {