extending httpUtil

This commit is contained in:
Roland Osborne 2022-01-17 13:42:17 -08:00
parent 491b86fba9
commit 6b0c268a14
9 changed files with 20 additions and 18 deletions

View File

@ -9,13 +9,13 @@ import (
func AddAccount(w http.ResponseWriter, r *http.Request) { func AddAccount(w http.ResponseWriter, r *http.Request) {
if _, err := bearerAccountToken(r); err != nil { if _, err := BearerAccountToken(r); err != nil {
LogMsg("authentication failed") LogMsg("authentication failed")
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
return return
} }
username, password, err := basicCredentials(r); username, password, err := BasicCredentials(r);
if err != nil { if err != nil {
LogMsg("invalid basic credentials") LogMsg("invalid basic credentials")
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)

View File

@ -8,7 +8,7 @@ import (
func AddNodeAccount(w http.ResponseWriter, r *http.Request) { func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
if !adminLogin(r) { if !AdminLogin(r) {
LogMsg("invalid admin credentials"); LogMsg("invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized); w.WriteHeader(http.StatusUnauthorized);
return return

View File

@ -9,7 +9,7 @@ import (
"databag/internal/store" "databag/internal/store"
) )
func adminLogin(r *http.Request) bool { func AdminLogin(r *http.Request) bool {
// extract request auth // extract request auth
username, password, ok := r.BasicAuth(); username, password, ok := r.BasicAuth();
@ -36,7 +36,7 @@ func adminLogin(r *http.Request) bool {
return true; return true;
} }
func bearerAccountToken(r *http.Request) (store.AccountToken, error) { func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
// parse bearer authentication // parse bearer authentication
auth := r.Header.Get("Authorization") auth := r.Header.Get("Authorization")
@ -48,7 +48,7 @@ func bearerAccountToken(r *http.Request) (store.AccountToken, error) {
return accountToken, err return accountToken, err
} }
func basicCredentials(r *http.Request) (string, []byte, error) { func BasicCredentials(r *http.Request) (string, []byte, error) {
var username string var username string
var password []byte var password []byte

View File

@ -6,7 +6,7 @@ import (
func GetAccountToken(w http.ResponseWriter, r *http.Request) { func GetAccountToken(w http.ResponseWriter, r *http.Request) {
accountToken, err := bearerAccountToken(r); accountToken, err := BearerAccountToken(r);
if err != nil { if err != nil {
LogMsg("token not found"); LogMsg("token not found");
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)

View File

@ -11,7 +11,7 @@ type accountUsername struct {
func GetAccountUsername(w http.ResponseWriter, r *http.Request) { func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
_, err := bearerAccountToken(r); _, err := BearerAccountToken(r);
if err != nil { if err != nil {
LogMsg("authentication failed") LogMsg("authentication failed")
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)

View File

@ -7,7 +7,7 @@ import (
func GetNodeConfig(w http.ResponseWriter, r *http.Request) { func GetNodeConfig(w http.ResponseWriter, r *http.Request) {
// validate login // validate login
if !adminLogin(r) { if !AdminLogin(r) {
LogMsg("SetNodeConfig - invalid admin credentials"); LogMsg("SetNodeConfig - invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized); w.WriteHeader(http.StatusUnauthorized);
return return

View File

@ -62,3 +62,10 @@ func SetCredentials(r *http.Request, login string) {
r.Header.Add("Credentials", "Basic " + auth) r.Header.Add("Credentials", "Basic " + auth)
} }
func ParseRequest(r *http.Request, w http.ResponseWriter, obj interface{}) error {
r.Body = http.MaxBytesReader(w, r.Body, APP_BODYLIMIT)
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
return dec.Decode(&obj)
}

View File

@ -20,7 +20,7 @@ func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
return return
} }
username, password, res := basicCredentials(r); username, password, res := BasicCredentials(r);
if res != nil { if res != nil {
LogMsg("invalid credenitals"); LogMsg("invalid credenitals");
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View File

@ -2,7 +2,6 @@ package databag
import ( import (
"log" "log"
"encoding/json"
"net/http" "net/http"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -12,19 +11,15 @@ import (
func SetNodeConfig(w http.ResponseWriter, r *http.Request) { func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
// validate login // validate login
if !adminLogin(r) { if !AdminLogin(r) {
log.Printf("SetNodeConfig - invalid admin credentials"); log.Printf("SetNodeConfig - invalid admin credentials");
w.WriteHeader(http.StatusUnauthorized); w.WriteHeader(http.StatusUnauthorized);
return return
} }
// parse node config // parse node config
r.Body = http.MaxBytesReader(w, r.Body, APP_BODYLIMIT) var config NodeConfig
dec := json.NewDecoder(r.Body) if ParseRequest(r, w, &config) != nil {
dec.DisallowUnknownFields()
var config NodeConfig;
res := dec.Decode(&config);
if res != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }