all repos — privychka @ 781f26ac38324da74a9fe59c0d37f603b7f8eca8

habit tracking service

Add basic auth
Anirudh Oppiliappan x@icyphox.sh
Wed, 29 Dec 2021 19:47:34 +0530
commit

781f26ac38324da74a9fe59c0d37f603b7f8eca8

parent

1a65ee6754b3c5732de4272177629e7f54240508

1 files changed, 14 insertions(+), 0 deletions(-)

jump to
M main.gomain.go

@@ -9,6 +9,7 @@ "fmt"

"log" "net/http" "os" + "strings" "time" )

@@ -55,8 +56,16 @@

return nil } +func getKey(r *http.Request) string { + var key string + header := r.Header.Get("Authorization") + key = strings.Split(header, " ")[1] + return key +} + func main() { hFile := *flag.String("f", "./habits.tsv", "csv file to store habit data") + secretKey := *flag.String("key", "secret", "auth key to be passed as bearer token") flag.Parse() if _, err := os.Stat(hFile); errors.Is(err, os.ErrNotExist) {

@@ -68,6 +77,11 @@ }

http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { h := Habit{} + key := getKey(r) + if secretKey != key { + log.Printf("incorrect key: %v\n", key) + w.WriteHeader(401) + } json.NewDecoder(r.Body).Decode(&h) log.Printf(h.String())