mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
separating account details for efficient loading
This commit is contained in:
parent
7cec67bbb4
commit
22d6c74f9a
@ -4387,10 +4387,12 @@ components:
|
|||||||
Authenticate:
|
Authenticate:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
- did
|
- guid
|
||||||
- token
|
- token
|
||||||
- timestamp
|
- timestamp
|
||||||
properties:
|
properties:
|
||||||
|
guid:
|
||||||
|
type: string
|
||||||
token:
|
token:
|
||||||
type: string
|
type: string
|
||||||
timestamp:
|
timestamp:
|
||||||
|
@ -46,16 +46,22 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// create new account
|
// create new account
|
||||||
account := store.Account{
|
account := store.Account{
|
||||||
PublicKey: publicPem,
|
|
||||||
PrivateKey: privatePem,
|
|
||||||
KeyType: "RSA4096",
|
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
Guid: fingerprint,
|
Guid: fingerprint,
|
||||||
};
|
}
|
||||||
|
detail := store.AccountDetail{
|
||||||
|
PublicKey: publicPem,
|
||||||
|
PrivateKey: privatePem,
|
||||||
|
KeyType: "RSA4096",
|
||||||
|
}
|
||||||
|
|
||||||
// save account and delete token
|
// save account and delete token
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
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 {
|
if res := store.DB.Create(&account).Error; res != nil {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -74,10 +80,10 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
profile := Profile{
|
profile := Profile{
|
||||||
Guid: account.Guid,
|
Guid: account.Guid,
|
||||||
Handle: account.Username,
|
Handle: account.Username,
|
||||||
Name: account.Name,
|
Name: detail.Name,
|
||||||
Description: account.Description,
|
Description: detail.Description,
|
||||||
Location: account.Location,
|
Location: detail.Location,
|
||||||
Image: account.Image,
|
Image: detail.Image,
|
||||||
Revision: account.ProfileRevision,
|
Revision: account.ProfileRevision,
|
||||||
Version: APP_VERSION,
|
Version: APP_VERSION,
|
||||||
Node: "https://" + getStrConfigValue(CONFIG_DOMAIN, ""),
|
Node: "https://" + getStrConfigValue(CONFIG_DOMAIN, ""),
|
||||||
|
@ -14,6 +14,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Authorize(w http.ResponseWriter, r *http.Request) {
|
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.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,18 @@ func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
|
|||||||
return accountToken, err
|
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) {
|
func BasicCredentials(r *http.Request) (string, []byte, error) {
|
||||||
|
|
||||||
var username string
|
var username string
|
||||||
|
@ -69,6 +69,7 @@ type Asset struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Authenticate struct {
|
type Authenticate struct {
|
||||||
|
Guid string `json:"guid"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Timestamp int32 `json:"timestamp"`
|
Timestamp int32 `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
@ -47,16 +47,10 @@ type AccountToken struct {
|
|||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||||
PublicKey string `gorm:"not null"`
|
AccountDetailID uint `gorm:"not null"`
|
||||||
PrivateKey string `gorm:"not null"`
|
|
||||||
KeyType string `gorm:"not null"`
|
|
||||||
Guid string `gorm:"not null;uniqueIndex"`
|
Guid string `gorm:"not null;uniqueIndex"`
|
||||||
Username string `gorm:"not null;uniqueIndex"`
|
Username string `gorm:"not null;uniqueIndex"`
|
||||||
Password []byte `gorm:"not null"`
|
Password []byte `gorm:"not null"`
|
||||||
Name string
|
|
||||||
Description string
|
|
||||||
Location string
|
|
||||||
Image string
|
|
||||||
ProfileRevision int64 `gorm:"not null;default:1"`
|
ProfileRevision int64 `gorm:"not null;default:1"`
|
||||||
ContentRevision int64 `gorm:"not null;default:1"`
|
ContentRevision int64 `gorm:"not null;default:1"`
|
||||||
ViewRevision 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"`
|
InsightRevision uint64 `gorm:"not null;default:1"`
|
||||||
Created int64 `gorm:"autoCreateTime"`
|
Created int64 `gorm:"autoCreateTime"`
|
||||||
Disabled bool `gorm:"not null;default:false"`
|
Disabled bool `gorm:"not null;default:false"`
|
||||||
|
AccountDetail AccountDetail
|
||||||
Apps []App
|
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 {
|
type App struct {
|
||||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||||
AccountID uint `gorm:"index"`
|
AccountID uint `gorm:"index"`
|
||||||
|
@ -26,8 +26,12 @@ func TestAttachAccount(t *testing.T) {
|
|||||||
var access string
|
var access string
|
||||||
assert.NoError(t, ReadResponse(w, &access))
|
assert.NoError(t, ReadResponse(w, &access))
|
||||||
|
|
||||||
PrintMsg(access)
|
|
||||||
// autorize app
|
// 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
|
// set profile
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user