adding attach app use case

This commit is contained in:
Roland Osborne 2022-01-17 22:56:00 -08:00
parent 51c0864817
commit 7cec67bbb4
7 changed files with 96 additions and 13 deletions

View File

@ -68,11 +68,6 @@ func RemoveAccountApp(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func SetAccountApp(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetAccountAuthentication(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

View File

@ -23,19 +23,20 @@ func AddAccountApp(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}
token := hex.EncodeToString(data)
token := store.AccountToken{
accountToken := store.AccountToken{
AccountID: id,
TokenType: "attach",
Token: hex.EncodeToString(data),
Token: token,
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
};
if store.DB.Create(&token).Error != nil {
if store.DB.Create(&accountToken).Error != nil {
LogMsg("failed to store token")
w.WriteHeader(http.StatusInternalServerError)
return
}
WriteResponse(w, data);
WriteResponse(w, token);
}

View File

@ -0,0 +1,64 @@
package databag
import (
"net/http"
"encoding/hex"
"gorm.io/gorm"
"databag/internal/store"
"github.com/theckman/go-securerandom"
)
func SetAccountApp(w http.ResponseWriter, r *http.Request) {
token, res := BearerAccountToken(r);
if res != nil || token.TokenType != "attach" {
LogMsg("invalid bearer token")
w.WriteHeader(http.StatusUnauthorized)
return
}
// parse app data
var appData AppData
if ParseRequest(r, w, &appData) != nil {
LogMsg("invalid request data")
w.WriteHeader(http.StatusBadRequest)
return
}
// gernate app token
data, err := securerandom.Bytes(32)
if err != nil {
LogMsg("failed to generate token")
w.WriteHeader(http.StatusInternalServerError);
return
}
access := hex.EncodeToString(data)
// create app entry
app := store.App {
AccountID: token.AccountID,
Name: appData.Name,
Description: appData.Description,
Image: appData.Image,
Url: appData.Url,
Token: access,
};
// save app and delete token
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Create(&app).Error; res != nil {
return res;
}
if res := store.DB.Delete(token).Error; res != nil {
return res;
}
return nil;
});
if err != nil {
LogMsg("failed to save app")
w.WriteHeader(http.StatusInternalServerError)
return
}
WriteResponse(w, access)
}

View File

@ -54,7 +54,7 @@ func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
return nil;
})
if(err != nil) {
log.Printf("SetNodeConfig - failed to store config");
LogMsg("failed to store config")
w.WriteHeader(http.StatusInternalServerError)
return
}

View File

@ -13,7 +13,6 @@ import (
type accountLogin struct {
ID uint
Password []byte
Expires int64
}
func AdminLogin(r *http.Request) bool {
@ -53,7 +52,7 @@ func AccountLogin(r *http.Request) (uint, error) {
// find account
var account accountLogin
if store.DB.Model(&Account{}).Where("Username = ?", username).First(&account).Error != nil {
if store.DB.Model(&store.Account{}).Where("Username = ?", username).First(&account).Error != nil {
return 0, errors.New("username not found");
}

View File

@ -2,9 +2,33 @@ package databag
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAttachAccount(t *testing.T) {
// attach app to test:pass
// acquire new token for attaching app
r, w, _ := NewRequest("POST", "/account/apps", nil)
SetBasicAuth(r, "user:pass");
AddAccountApp(w, r);
var token string
assert.NoError(t, ReadResponse(w, &token))
// attach app with token
app := AppData{
Name: "Appy",
Description: "A test app",
Url: "http://app.example.com",
};
r, w, _ = NewRequest("PUT", "/account/apps", &app)
SetBearerAuth(r, token)
SetAccountApp(w, r)
var access string
assert.NoError(t, ReadResponse(w, &access))
PrintMsg(access)
// autorize app
// set profile
}