changing contact token format

This commit is contained in:
Roland Osborne 2022-01-22 10:45:39 -08:00
parent bbdc4ed722
commit 4082476d73
4 changed files with 22 additions and 8 deletions

View File

@ -115,15 +115,27 @@ func BearerAppToken(r *http.Request, detail bool) (*store.Account, int, error) {
return &app.Account, http.StatusOK, nil 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) { func BearerContactToken(r *http.Request) (*store.Card, int, error) {
// parse bearer authentication // parse bearer authentication
auth := r.Header.Get("Authorization") auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer")) token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
target, access, err := ParseToken(token)
if err != nil {
return nil, http.StatusBadRequest, err
}
// find token record // find token record
var card store.Card 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) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err return nil, http.StatusNotFound, err
} else { } else {

View File

@ -70,7 +70,7 @@ func SendLocalNotification(notification *store.Notification) {
} }
func SendRemoteNotification(notification *store.Notification) { func SendRemoteNotification(notification *store.Notification) {
PrintMsg(notification) // TODO send remote notification
} }
// notify all cards of profile change // notify all cards of profile change

View File

@ -87,12 +87,12 @@ type AccountDetail struct {
type App struct { type App struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
AccountID string `gorm:"index"` AccountID string `gorm:"not null;index:appguid,unique"`
Name string Name string
Description string Description string
Image string Image string
Url string Url string
Token string `gorm:"not null;index"` Token string `gorm:"not null;index:appguid,unique"`
Created int64 `gorm:"autoCreateTime"` Created int64 `gorm:"autoCreateTime"`
Account Account `gorm:"references:Guid"` Account Account `gorm:"references:Guid"`
} }
@ -124,8 +124,8 @@ type Label struct {
type Card struct { type Card struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
CardId string `gorm:"not null;index:card,unique"` CardId string `gorm:"not null;index:card,unique"`
AccountID string `gorm:"not null;index:card,unique;index:guid,unqiue"` AccountID string `gorm:"not null;index:card,unique"`
Guid string `gorm:"not null;index:guid,unique"` Guid string `gorm:"not null;index:cardguid,unique"`
Username string Username string
Name string Name string
Description string Description string
@ -135,7 +135,7 @@ type Card struct {
Node string `gorm:"not null"` Node string `gorm:"not null"`
ProfileRevision int64 `gorm:"not null"` ProfileRevision int64 `gorm:"not null"`
Status string `gorm:"not null"` Status string `gorm:"not null"`
InToken string InToken string `gorm:"not null;index:cardguid,unique"`
OutToken string OutToken string
Notes string Notes string
DataRevision int64 `gorm:"not null"` DataRevision int64 `gorm:"not null"`
@ -144,8 +144,8 @@ type Card struct {
ViewRevision int64 `gorm:"not null"` ViewRevision int64 `gorm:"not null"`
RemoteProfile int64 RemoteProfile int64
RemoteContent int64 RemoteContent int64
Account Account `gorm:"references:Guid"`
Groups []Group `gorm:"many2many:card_groups;"` Groups []Group `gorm:"many2many:card_groups;"`
Account Account `gorm:"references:Guid"`
} }
type CardGroup struct { type CardGroup struct {

View File

@ -7,6 +7,8 @@ import (
func TestAddAccount(t *testing.T) { func TestAddAccount(t *testing.T) {
PrintMsg("ADD")
// acquire new token for creating accounts // acquire new token for creating accounts
r, w, _ := NewRequest("POST", "/admin/accounts", nil) r, w, _ := NewRequest("POST", "/admin/accounts", nil)
SetBasicAuth(r, "admin:pass"); SetBasicAuth(r, "admin:pass");