From 4082476d73add415153cf6a46e0d9300bd73c791 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sat, 22 Jan 2022 10:45:39 -0800 Subject: [PATCH] changing contact token format --- net/server/internal/authUtil.go | 14 +++++++++++++- net/server/internal/notify.go | 2 +- net/server/internal/store/schema.go | 12 ++++++------ net/server/internal/ucAddAccount_test.go | 2 ++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/net/server/internal/authUtil.go b/net/server/internal/authUtil.go index 90952d7f..c25444a9 100644 --- a/net/server/internal/authUtil.go +++ b/net/server/internal/authUtil.go @@ -115,15 +115,27 @@ func BearerAppToken(r *http.Request, detail bool) (*store.Account, int, error) { return &app.Account, http.StatusOK, nil } +func ParseToken(token string) (string, string, error) { + split := strings.Split(token, ":") + if len(split) != 2 { + return "", "", errors.New("invalid token format") + } + return split[0], split[1], nil +} + func BearerContactToken(r *http.Request) (*store.Card, int, error) { // parse bearer authentication auth := r.Header.Get("Authorization") token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer")) + target, access, err := ParseToken(token) + if err != nil { + return nil, http.StatusBadRequest, err + } // find token record var card store.Card - if err := store.DB.Preload("Account").Where("InToken = ?", token).First(&card).Error; err != nil { + if err := store.DB.Preload("Account").Where("account_id = ? AND InToken = ?", target, access).First(&card).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, http.StatusNotFound, err } else { diff --git a/net/server/internal/notify.go b/net/server/internal/notify.go index c2c0b28a..27610f2d 100644 --- a/net/server/internal/notify.go +++ b/net/server/internal/notify.go @@ -70,7 +70,7 @@ func SendLocalNotification(notification *store.Notification) { } func SendRemoteNotification(notification *store.Notification) { - PrintMsg(notification) + // TODO send remote notification } // notify all cards of profile change diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 1305a012..3bff6210 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -87,12 +87,12 @@ type AccountDetail struct { type App struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` - AccountID string `gorm:"index"` + AccountID string `gorm:"not null;index:appguid,unique"` Name string Description string Image string Url string - Token string `gorm:"not null;index"` + Token string `gorm:"not null;index:appguid,unique"` Created int64 `gorm:"autoCreateTime"` Account Account `gorm:"references:Guid"` } @@ -124,8 +124,8 @@ type Label struct { type Card struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` CardId string `gorm:"not null;index:card,unique"` - AccountID string `gorm:"not null;index:card,unique;index:guid,unqiue"` - Guid string `gorm:"not null;index:guid,unique"` + AccountID string `gorm:"not null;index:card,unique"` + Guid string `gorm:"not null;index:cardguid,unique"` Username string Name string Description string @@ -135,7 +135,7 @@ type Card struct { Node string `gorm:"not null"` ProfileRevision int64 `gorm:"not null"` Status string `gorm:"not null"` - InToken string + InToken string `gorm:"not null;index:cardguid,unique"` OutToken string Notes string DataRevision int64 `gorm:"not null"` @@ -144,8 +144,8 @@ type Card struct { ViewRevision int64 `gorm:"not null"` RemoteProfile int64 RemoteContent int64 - Account Account `gorm:"references:Guid"` Groups []Group `gorm:"many2many:card_groups;"` + Account Account `gorm:"references:Guid"` } type CardGroup struct { diff --git a/net/server/internal/ucAddAccount_test.go b/net/server/internal/ucAddAccount_test.go index b2c2192a..00a5af63 100644 --- a/net/server/internal/ucAddAccount_test.go +++ b/net/server/internal/ucAddAccount_test.go @@ -7,6 +7,8 @@ import ( func TestAddAccount(t *testing.T) { +PrintMsg("ADD") + // acquire new token for creating accounts r, w, _ := NewRequest("POST", "/admin/accounts", nil) SetBasicAuth(r, "admin:pass");