Support json or forms
This commit is contained in:
parent
0a786bc445
commit
0209ceb0c3
1 changed files with 22 additions and 4 deletions
|
@ -2,6 +2,7 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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) {
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
username, password := r.PostFormValue("username"), r.PostFormValue("password")
|
|
||||||
if username == "" || password == "" {
|
if creds.Username == "" || creds.Password == "" {
|
||||||
http.Error(w, "blank credentials", http.StatusBadRequest)
|
http.Error(w, "blank credentials", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tok, err := a.Login(r.Context(), username, password)
|
tok, err := a.Login(r.Context(), creds.Username, creds.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue