mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
log message cleanup
This commit is contained in:
parent
694b811a19
commit
da272f386b
@ -12,15 +12,13 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
token, res := BearerAccountToken(r);
|
token, res := BearerAccountToken(r);
|
||||||
if res != nil || token.TokenType != "create" {
|
if res != nil || token.TokenType != "create" {
|
||||||
LogMsg("authentication failed")
|
ErrResponse(w, http.StatusUnauthorized, res)
|
||||||
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")
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,8 +27,7 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
privatePem := ExportRsaPrivateKeyAsPemStr(privateKey)
|
privatePem := ExportRsaPrivateKeyAsPemStr(privateKey)
|
||||||
publicPem, err := ExportRsaPublicKeyAsPemStr(publicKey)
|
publicPem, err := ExportRsaPublicKeyAsPemStr(publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed generate key")
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,8 +63,7 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
return nil;
|
return nil;
|
||||||
});
|
});
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to create account");
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,15 +12,13 @@ func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
id, err := AccountLogin(r)
|
id, err := AccountLogin(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to login")
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
w.WriteHeader(http.StatusUnauthorized);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, res := securerandom.Bytes(4)
|
data, res := securerandom.Bytes(4)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
LogMsg("failed to generate token")
|
ErrResponse(w, http.StatusInternalServerError, res)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
token := hex.EncodeToString(data)
|
token := hex.EncodeToString(data)
|
||||||
@ -31,9 +29,8 @@ func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
|||||||
Token: token,
|
Token: token,
|
||||||
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
||||||
};
|
};
|
||||||
if store.DB.Create(&accountToken).Error != nil {
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
||||||
LogMsg("failed to store token")
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,16 +10,14 @@ import (
|
|||||||
|
|
||||||
func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
|
func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if !AdminLogin(r) {
|
if err := AdminLogin(r); err != nil {
|
||||||
LogMsg("invalid admin credentials");
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
w.WriteHeader(http.StatusUnauthorized);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := securerandom.Bytes(16)
|
data, err := securerandom.Bytes(16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to generate token");
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
token := hex.EncodeToString(data)
|
token := hex.EncodeToString(data)
|
||||||
@ -30,9 +28,8 @@ func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
Expires: time.Now().Unix() + APP_CREATEEXPIRE,
|
Expires: time.Now().Unix() + APP_CREATEEXPIRE,
|
||||||
};
|
};
|
||||||
|
|
||||||
if store.DB.Create(&accountToken).Error != nil {
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
||||||
LogMsg("failed to store token");
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,9 +7,8 @@ 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 err := AdminLogin(r); err != nil {
|
||||||
LogMsg("SetNodeConfig - invalid admin credentials");
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
w.WriteHeader(http.StatusUnauthorized);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,13 +6,13 @@ import (
|
|||||||
|
|
||||||
func GetProfile(w http.ResponseWriter, r *http.Request) {
|
func GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
account, res := BearerAppToken(r, true);
|
account, err := BearerAppToken(r, true);
|
||||||
if res != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if account.Disabled {
|
if account.Disabled {
|
||||||
w.WriteHeader(http.StatusGone);
|
ErrResponse(w, http.StatusGone, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
detail := account.AccountDetail
|
detail := account.AccountDetail
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package databag
|
package databag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
@ -11,16 +10,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 err := AdminLogin(r); err != nil {
|
||||||
log.Printf("SetNodeConfig - invalid admin credentials");
|
ErrResponse(w, http.StatusUnauthorized, err)
|
||||||
w.WriteHeader(http.StatusUnauthorized);
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse node config
|
// parse node config
|
||||||
var config NodeConfig
|
var config NodeConfig
|
||||||
if ParseRequest(r, w, &config) != nil {
|
if err := ParseRequest(r, w, &config); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
ErrResponse(w, http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,8 +52,7 @@ func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
return nil;
|
return nil;
|
||||||
})
|
})
|
||||||
if(err != nil) {
|
if(err != nil) {
|
||||||
LogMsg("failed to store config")
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,31 +15,31 @@ type accountLogin struct {
|
|||||||
Password []byte
|
Password []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdminLogin(r *http.Request) bool {
|
func AdminLogin(r *http.Request) error {
|
||||||
|
|
||||||
// extract request auth
|
// extract request auth
|
||||||
username, password, ok := r.BasicAuth();
|
username, password, ok := r.BasicAuth()
|
||||||
if !ok || username == "" || password == "" {
|
if !ok || username == "" || password == "" {
|
||||||
return false
|
return errors.New("invalid credentials")
|
||||||
}
|
}
|
||||||
|
|
||||||
// nothing to do if not configured
|
// nothing to do if not configured
|
||||||
if !getBoolConfigValue(CONFIG_CONFIGURED, false) {
|
if !getBoolConfigValue(CONFIG_CONFIGURED, false) {
|
||||||
return false;
|
return errors.New("node not configured")
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare username
|
// compare username
|
||||||
if getStrConfigValue(CONFIG_USERNAME, "") != username {
|
if getStrConfigValue(CONFIG_USERNAME, "") != username {
|
||||||
return false
|
return errors.New("admin username error")
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare password
|
// compare password
|
||||||
p := getBinConfigValue(CONFIG_PASSWORD, nil);
|
p := getBinConfigValue(CONFIG_PASSWORD, nil);
|
||||||
if bcrypt.CompareHashAndPassword(p, []byte(password)) != nil {
|
if bcrypt.CompareHashAndPassword(p, []byte(password)) != nil {
|
||||||
return false
|
return errors.New("admin password error")
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AccountLogin(r *http.Request) (uint, error) {
|
func AccountLogin(r *http.Request) (uint, error) {
|
||||||
@ -72,11 +72,13 @@ func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
|
|||||||
|
|
||||||
// find token record
|
// find token record
|
||||||
var accountToken store.AccountToken
|
var accountToken store.AccountToken
|
||||||
err := store.DB.Where("token = ?", token).First(&accountToken).Error
|
if err := store.DB.Where("token = ?", token).First(&accountToken).Error; err != nil {
|
||||||
|
return accountToken, err
|
||||||
|
}
|
||||||
if accountToken.Expires < time.Now().Unix() {
|
if accountToken.Expires < time.Now().Unix() {
|
||||||
return accountToken, errors.New("expired token")
|
return accountToken, errors.New("expired token")
|
||||||
}
|
}
|
||||||
return accountToken, err
|
return accountToken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func BearerAppToken(r *http.Request, detail bool) (store.Account, error) {
|
func BearerAppToken(r *http.Request, detail bool) (store.Account, error) {
|
||||||
@ -88,12 +90,12 @@ func BearerAppToken(r *http.Request, detail bool) (store.Account, error) {
|
|||||||
// find token record
|
// find token record
|
||||||
var app store.App
|
var app store.App
|
||||||
if detail {
|
if detail {
|
||||||
if store.DB.Preload("Account.AccountDetail").Where("token = ?", token).First(&app).Error != nil {
|
if err := store.DB.Preload("Account.AccountDetail").Where("token = ?", token).First(&app).Error; err != nil {
|
||||||
return app.Account, errors.New("failed to load account");
|
return app.Account, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if store.DB.Preload("Account").Where("token = ?", token).First(&app).Error != nil {
|
if err := store.DB.Preload("Account").Where("token = ?", token).First(&app).Error; err != nil {
|
||||||
return app.Account, errors.New("failed to load account");
|
return app.Account, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return app.Account, nil
|
return app.Account, nil
|
||||||
@ -111,14 +113,12 @@ func BasicCredentials(r *http.Request) (string, []byte, error) {
|
|||||||
// decode basic auth
|
// decode basic auth
|
||||||
credentials, err := base64.StdEncoding.DecodeString(token)
|
credentials, err := base64.StdEncoding.DecodeString(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("faield to decode basic credentials");
|
|
||||||
return username, password, err
|
return username, password, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse credentials
|
// parse credentials
|
||||||
login := strings.Split(string(credentials), ":");
|
login := strings.Split(string(credentials), ":");
|
||||||
if login[0] == "" || login[1] == "" {
|
if login[0] == "" || login[1] == "" {
|
||||||
LogMsg("failed to parse basic credentials");
|
|
||||||
return username, password, errors.New("invalid credentials")
|
return username, password, errors.New("invalid credentials")
|
||||||
}
|
}
|
||||||
username = login[0]
|
username = login[0]
|
||||||
@ -126,7 +126,6 @@ func BasicCredentials(r *http.Request) (string, []byte, error) {
|
|||||||
// hash password
|
// hash password
|
||||||
password, err = bcrypt.GenerateFromPassword([]byte(login[1]), bcrypt.DefaultCost)
|
password, err = bcrypt.GenerateFromPassword([]byte(login[1]), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMsg("failed to hash password")
|
|
||||||
return username, password, err
|
return username, password, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,11 @@ func Logger(inner http.Handler, name string) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ErrResponse(w http.ResponseWriter, code int, err error) {
|
func ErrResponse(w http.ResponseWriter, code int, err error) {
|
||||||
ErrMsg(err)
|
if !hideLog && err != nil {
|
||||||
|
_, file, line, _ := runtime.Caller(1)
|
||||||
|
p, _ := os.Getwd()
|
||||||
|
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, err.Error())
|
||||||
|
}
|
||||||
w.WriteHeader(code)
|
w.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +51,6 @@ func ErrMsg(err error) {
|
|||||||
_, file, line, _ := runtime.Caller(1)
|
_, file, line, _ := runtime.Caller(1)
|
||||||
p, _ := os.Getwd()
|
p, _ := os.Getwd()
|
||||||
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, err.Error())
|
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, err.Error())
|
||||||
pretty.Println(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StatusHandler struct {}
|
type StatusHandler struct {}
|
||||||
@ -12,10 +13,14 @@ func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
Status(w, r)
|
Status(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartTestWebsocketServer() string {
|
func getTestWebsocket() *websocket.Conn {
|
||||||
h := StatusHandler{}
|
h := StatusHandler{}
|
||||||
s := httptest.NewServer(&h)
|
s := httptest.NewServer(&h)
|
||||||
wsUrl, _ := url.Parse(s.URL)
|
wsUrl, _ := url.Parse(s.URL)
|
||||||
wsUrl.Scheme = "ws"
|
wsUrl.Scheme = "ws"
|
||||||
return wsUrl.String()
|
ws, _, err := websocket.DefaultDialer.Dial(wsUrl.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
PrintMsg(err.Error());
|
||||||
|
}
|
||||||
|
return ws
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,12 @@ import (
|
|||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto"
|
"crypto"
|
||||||
"time"
|
"time"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAttachAccount(t *testing.T) {
|
func TestAttachAccount(t *testing.T) {
|
||||||
|
|
||||||
// setup websocket server
|
|
||||||
wsUrl := StartTestWebsocketServer()
|
|
||||||
|
|
||||||
// get account token
|
// get account token
|
||||||
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
||||||
SetBasicAuth(r, "admin:pass")
|
SetBasicAuth(r, "admin:pass")
|
||||||
@ -84,7 +81,7 @@ func TestAttachAccount(t *testing.T) {
|
|||||||
assert.Less(t, cur - 60, auth.Timestamp)
|
assert.Less(t, cur - 60, auth.Timestamp)
|
||||||
|
|
||||||
// app connects websocket
|
// app connects websocket
|
||||||
ws, _, _ := websocket.DefaultDialer.Dial(wsUrl, nil)
|
ws := getTestWebsocket()
|
||||||
announce := Announce{ AppToken: access }
|
announce := Announce{ AppToken: access }
|
||||||
msg, _ := json.Marshal(&announce)
|
msg, _ := json.Marshal(&announce)
|
||||||
ws.WriteMessage(websocket.TextMessage, msg)
|
ws.WriteMessage(websocket.TextMessage, msg)
|
||||||
|
Loading…
Reference in New Issue
Block a user