From 0209ceb0c355cb9b9993d5b110a25f5d3e9187c8 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sat, 26 Oct 2024 12:11:44 -0400 Subject: [PATCH] Support json or forms --- pkg/gordio/auth/jwt.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/gordio/auth/jwt.go b/pkg/gordio/auth/jwt.go index 9c21718..74c0ee4 100644 --- a/pkg/gordio/auth/jwt.go +++ b/pkg/gordio/auth/jwt.go @@ -2,6 +2,7 @@ package auth import ( "context" + "encoding/json" "net/http" "strconv" "strings" @@ -168,18 +169,35 @@ func (a *Auth) routeRefresh(w http.ResponseWriter, r *http.Request) { } func (a *Auth) routeAuth(w http.ResponseWriter, r *http.Request) { - err := r.ParseForm() + var creds struct { + Username string `json:"username"` + Password string `json:"password"` + } + + var err error + + switch r.Header.Get("Content-Type") { + case "application/json": + err = json.NewDecoder(r.Body).Decode(&creds) + default: + err = r.ParseForm() + if err != nil { + break + } + creds.Username, creds.Password = r.PostFormValue("username"), r.PostFormValue("password") + } + if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } - username, password := r.PostFormValue("username"), r.PostFormValue("password") - if username == "" || password == "" { + + if creds.Username == "" || creds.Password == "" { http.Error(w, "blank credentials", http.StatusBadRequest) return } - tok, err := a.Login(r.Context(), username, password) + tok, err := a.Login(r.Context(), creds.Username, creds.Password) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return