separating account details for efficient loading

This commit is contained in:
Roland Osborne 2022-01-18 00:30:27 -08:00
parent 7cec67bbb4
commit 22d6c74f9a
7 changed files with 63 additions and 18 deletions

View File

@ -4387,10 +4387,12 @@ components:
Authenticate:
type: object
required:
- did
- guid
- token
- timestamp
properties:
guid:
type: string
token:
type: string
timestamp:

View File

@ -46,16 +46,22 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
// create new account
account := store.Account{
PublicKey: publicPem,
PrivateKey: privatePem,
KeyType: "RSA4096",
Username: username,
Password: password,
Guid: fingerprint,
};
}
detail := store.AccountDetail{
PublicKey: publicPem,
PrivateKey: privatePem,
KeyType: "RSA4096",
}
// save account and delete token
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Create(&detail).Error; res != nil {
return res;
}
account.AccountDetailID = detail.ID
if res := store.DB.Create(&account).Error; res != nil {
return res;
}
@ -74,10 +80,10 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
profile := Profile{
Guid: account.Guid,
Handle: account.Username,
Name: account.Name,
Description: account.Description,
Location: account.Location,
Image: account.Image,
Name: detail.Name,
Description: detail.Description,
Location: detail.Location,
Image: detail.Image,
Revision: account.ProfileRevision,
Version: APP_VERSION,
Node: "https://" + getStrConfigValue(CONFIG_DOMAIN, ""),

View File

@ -14,6 +14,20 @@ import (
)
func Authorize(w http.ResponseWriter, r *http.Request) {
account, res := BearerAppToken(r);
PrintMsg(res);
PrintMsg(account);
if res != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
if account.Disabled {
w.WriteHeader(http.StatusGone);
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -79,6 +79,18 @@ func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
return accountToken, err
}
func BearerAppToken(r *http.Request) (store.Account, error) {
// parse bearer authentication
auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
// find token record
var app store.App
err := store.DB.Preload("Account").Where("token = ?", token).First(&app).Error
return app.Account, err
}
func BasicCredentials(r *http.Request) (string, []byte, error) {
var username string

View File

@ -69,6 +69,7 @@ type Asset struct {
}
type Authenticate struct {
Guid string `json:"guid"`
Token string `json:"token"`
Timestamp int32 `json:"timestamp"`
}

View File

@ -38,7 +38,7 @@ type Config struct {
type AccountToken struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
AccountID uint `gorm:"index"`
TokenType string `gorm:"not null; `
TokenType string `gorm:"not null;`
Token string `gorm:"not null;uniqueIndex"`
Expires int64 `gorm:"not null"`
Created int64 `gorm:"autoCreateTime"`
@ -47,16 +47,10 @@ type AccountToken struct {
type Account struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
PublicKey string `gorm:"not null"`
PrivateKey string `gorm:"not null"`
KeyType string `gorm:"not null"`
AccountDetailID uint `gorm:"not null"`
Guid string `gorm:"not null;uniqueIndex"`
Username string `gorm:"not null;uniqueIndex"`
Password []byte `gorm:"not null"`
Name string
Description string
Location string
Image string
ProfileRevision int64 `gorm:"not null;default:1"`
ContentRevision int64 `gorm:"not null;default:1"`
ViewRevision int64 `gorm:"not null;default:1"`
@ -67,9 +61,21 @@ type Account struct {
InsightRevision uint64 `gorm:"not null;default:1"`
Created int64 `gorm:"autoCreateTime"`
Disabled bool `gorm:"not null;default:false"`
AccountDetail AccountDetail
Apps []App
}
type AccountDetail struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
PublicKey string `gorm:"not null"`
PrivateKey string `gorm:"not null"`
KeyType string `gorm:"not null"`
Name string
Description string
Location string
Image string
}
type App struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
AccountID uint `gorm:"index"`

View File

@ -26,8 +26,12 @@ func TestAttachAccount(t *testing.T) {
var access string
assert.NoError(t, ReadResponse(w, &access))
PrintMsg(access)
// autorize app
r, w, _ = NewRequest("PUT", "/authorize", "aabbccdd")
SetBearerAuth(r, access);
Authorize(w, r);
var message DataMessage
assert.NoError(t, ReadResponse(w, &message))
// set profile
}