Save used keytoken in messages
This commit is contained in:
parent
a0c72f5b94
commit
8826cb0312
@ -56,12 +56,14 @@
|
|||||||
* [X] compat simply uses default-keys
|
* [X] compat simply uses default-keys
|
||||||
* [X] CRUD routes for keys
|
* [X] CRUD routes for keys
|
||||||
* [X] KeyToken.messagecounter
|
* [X] KeyToken.messagecounter
|
||||||
* [ ] update old-data migration to create token-keys
|
* [x] update old-data migration to create token-keys
|
||||||
* [ ] unit tests
|
* [ ] unit tests
|
||||||
|
|
||||||
- We no longer have a route to reshuffle all keys (previously in updateUser), add a /user/:uid/keys/reset ?
|
- We no longer have a route to reshuffle all keys (previously in updateUser), add a /user/:uid/keys/reset ?
|
||||||
Would delete all existing keys and create 3 new ones?
|
Would delete all existing keys and create 3 new ones?
|
||||||
|
|
||||||
|
- TODO comments
|
||||||
|
|
||||||
#### PERSONAL
|
#### PERSONAL
|
||||||
|
|
||||||
- in my script: use `srvname` for sendername
|
- in my script: use `srvname` for sendername
|
||||||
|
@ -242,7 +242,7 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
|||||||
|
|
||||||
clientIP := g.ClientIP()
|
clientIP := g.ClientIP()
|
||||||
|
|
||||||
msg, err := h.database.CreateMessage(ctx, *UserID, channel, sendTimestamp, *Title, Content, priority, UserMessageID, clientIP, SenderName)
|
msg, err := h.database.CreateMessage(ctx, *UserID, channel, sendTimestamp, *Title, Content, priority, UserMessageID, clientIP, SenderName, keytok.KeyTokenID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err))
|
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err))
|
||||||
}
|
}
|
||||||
|
@ -195,21 +195,17 @@ func migrateUser(ctx context.Context, dbnew sq.DB, dbold sq.DB, user OldUser, ap
|
|||||||
|
|
||||||
fmt.Printf("New UserID: %s\n", userid)
|
fmt.Printf("New UserID: %s\n", userid)
|
||||||
|
|
||||||
readKey := scn.RandomAuthKey()
|
tokKeyID := models.NewKeyTokenID()
|
||||||
sendKey := scn.RandomAuthKey()
|
tokKeySec := user.UserKey
|
||||||
adminKey := user.UserKey
|
|
||||||
|
|
||||||
protoken := user.ProToken
|
protoken := user.ProToken
|
||||||
if protoken != nil {
|
if protoken != nil {
|
||||||
protoken = langext.Ptr("ANDROID|v1|" + *protoken)
|
protoken = langext.Ptr("ANDROID|v1|" + *protoken)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = dbnew.Exec(ctx, "INSERT INTO users (user_id, username, read_key, send_key, admin_key, is_pro, pro_token, timestamp_created) VALUES (:uid, :un, :rk, :sk, :ak, :pro, :tok, :ts)", sq.PP{
|
_, err = dbnew.Exec(ctx, "INSERT INTO users (user_id, username, is_pro, pro_token, timestamp_created) VALUES (:uid, :un, :pro, :tok, :ts)", sq.PP{
|
||||||
"uid": userid,
|
"uid": userid,
|
||||||
"un": nil,
|
"un": nil,
|
||||||
"rk": readKey,
|
|
||||||
"sk": sendKey,
|
|
||||||
"ak": adminKey,
|
|
||||||
"pro": langext.Conditional(user.IsPro, 1, 0),
|
"pro": langext.Conditional(user.IsPro, 1, 0),
|
||||||
"tok": protoken,
|
"tok": protoken,
|
||||||
"ts": user.TimestampCreated.UnixMilli(),
|
"ts": user.TimestampCreated.UnixMilli(),
|
||||||
@ -227,6 +223,20 @@ func migrateUser(ctx context.Context, dbnew sq.DB, dbold sq.DB, user OldUser, ap
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = dbnew.Exec(ctx, "INSERT INTO keytokens (keytoken_id, name, timestamp_created, owner_user_id, all_channels, channels, token, permissions) VALUES (:tid, :nam, :tsc, :owr, :all, :cha, :tok, :prm)", sq.PP{
|
||||||
|
"tid": tokKeyID,
|
||||||
|
"nam": "AdminKey (migrated)",
|
||||||
|
"tsc": user.TimestampCreated.UnixMilli(),
|
||||||
|
"owr": userid,
|
||||||
|
"all": 1,
|
||||||
|
"cha": "",
|
||||||
|
"tok": tokKeySec,
|
||||||
|
"prm": "A",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
var clientid *models.ClientID = nil
|
var clientid *models.ClientID = nil
|
||||||
|
|
||||||
if user.FcmToken != nil && *user.FcmToken != "BLACKLISTED" {
|
if user.FcmToken != nil && *user.FcmToken != "BLACKLISTED" {
|
||||||
@ -260,15 +270,14 @@ func migrateUser(ctx context.Context, dbnew sq.DB, dbold sq.DB, user OldUser, ap
|
|||||||
}
|
}
|
||||||
|
|
||||||
mainChannelID := models.NewChannelID()
|
mainChannelID := models.NewChannelID()
|
||||||
_, err = dbnew.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, send_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :sendkey, :ts)", sq.PP{
|
_, err = dbnew.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :ts)", sq.PP{
|
||||||
"cid": mainChannelID,
|
"cid": mainChannelID,
|
||||||
"ouid": userid,
|
"ouid": userid,
|
||||||
"dnam": "main",
|
"dnam": "main",
|
||||||
"inam": "main",
|
"inam": "main",
|
||||||
"hnam": nil,
|
"hnam": nil,
|
||||||
"subkey": scn.RandomAuthKey(),
|
"subkey": scn.RandomAuthKey(),
|
||||||
"sendkey": scn.RandomAuthKey(),
|
"ts": user.TimestampCreated.UnixMilli(),
|
||||||
"ts": user.TimestampCreated.UnixMilli(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -334,15 +343,14 @@ func migrateUser(ctx context.Context, dbnew sq.DB, dbold sq.DB, user OldUser, ap
|
|||||||
channelID = models.NewChannelID()
|
channelID = models.NewChannelID()
|
||||||
channelInternalName = intName
|
channelInternalName = intName
|
||||||
|
|
||||||
_, err = dbnew.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, send_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :sendkey, :ts)", sq.PP{
|
_, err = dbnew.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :ts)", sq.PP{
|
||||||
"cid": channelID,
|
"cid": channelID,
|
||||||
"ouid": userid,
|
"ouid": userid,
|
||||||
"dnam": dispName,
|
"dnam": dispName,
|
||||||
"inam": intName,
|
"inam": intName,
|
||||||
"hnam": nil,
|
"hnam": nil,
|
||||||
"subkey": scn.RandomAuthKey(),
|
"subkey": scn.RandomAuthKey(),
|
||||||
"sendkey": scn.RandomAuthKey(),
|
"ts": oldmessage.TimestampReal.UnixMilli(),
|
||||||
"ts": oldmessage.TimestampReal.UnixMilli(),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -419,8 +427,9 @@ func migrateUser(ctx context.Context, dbnew sq.DB, dbold sq.DB, user OldUser, ap
|
|||||||
"umid": oldmessage.UsrMessageId,
|
"umid": oldmessage.UsrMessageId,
|
||||||
"ip": "",
|
"ip": "",
|
||||||
"snam": sendername,
|
"snam": sendername,
|
||||||
|
"ukid": tokKeyID,
|
||||||
}
|
}
|
||||||
_, err = dbnew.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam)", pp)
|
_, err = dbnew.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name, used_key_id) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam, :ukid)", pp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jv, _ := json.MarshalIndent(pp, "", " ")
|
jv, _ := json.MarshalIndent(pp, "", " ")
|
||||||
fmt.Printf("%s", string(jv))
|
fmt.Printf("%s", string(jv))
|
||||||
|
@ -56,7 +56,7 @@ func (db *Database) GetMessage(ctx TxContext, scnMessageID models.MessageID, all
|
|||||||
return msg, nil
|
return msg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, channel models.Channel, timestampSend *time.Time, title string, content *string, priority int, userMsgId *string, senderIP string, senderName *string) (models.Message, error) {
|
func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, channel models.Channel, timestampSend *time.Time, title string, content *string, priority int, userMsgId *string, senderIP string, senderName *string, usedKeyID models.KeyTokenID) (models.Message, error) {
|
||||||
tx, err := ctx.GetOrCreateTransaction(db)
|
tx, err := ctx.GetOrCreateTransaction(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.Message{}, err
|
return models.Message{}, err
|
||||||
@ -66,7 +66,7 @@ func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, cha
|
|||||||
|
|
||||||
messageid := models.NewMessageID()
|
messageid := models.NewMessageID()
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam)", sq.PP{
|
_, err = tx.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name, used_key_id) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam, :uk)", sq.PP{
|
||||||
"mid": messageid,
|
"mid": messageid,
|
||||||
"suid": senderUserID,
|
"suid": senderUserID,
|
||||||
"ouid": channel.OwnerUserID,
|
"ouid": channel.OwnerUserID,
|
||||||
@ -80,6 +80,7 @@ func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, cha
|
|||||||
"umid": userMsgId,
|
"umid": userMsgId,
|
||||||
"ip": senderIP,
|
"ip": senderIP,
|
||||||
"snam": senderName,
|
"snam": senderName,
|
||||||
|
"uk": usedKeyID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.Message{}, err
|
return models.Message{}, err
|
||||||
@ -99,6 +100,7 @@ func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, cha
|
|||||||
Content: content,
|
Content: content,
|
||||||
Priority: priority,
|
Priority: priority,
|
||||||
UserMessageID: userMsgId,
|
UserMessageID: userMsgId,
|
||||||
|
UsedKeyID: usedKeyID,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +125,8 @@ CREATE TABLE messages
|
|||||||
priority INTEGER CHECK(priority IN (0, 1, 2)) NOT NULL,
|
priority INTEGER CHECK(priority IN (0, 1, 2)) NOT NULL,
|
||||||
usr_message_id TEXT NULL,
|
usr_message_id TEXT NULL,
|
||||||
|
|
||||||
|
used_key_id TEXT NOT NULL,
|
||||||
|
|
||||||
deleted INTEGER CHECK(deleted IN (0, 1)) NOT NULL DEFAULT '0',
|
deleted INTEGER CHECK(deleted IN (0, 1)) NOT NULL DEFAULT '0',
|
||||||
|
|
||||||
PRIMARY KEY (message_id)
|
PRIMARY KEY (message_id)
|
||||||
@ -139,6 +141,7 @@ CREATE INDEX "idx_messages_sendername" ON messages (sender_name COL
|
|||||||
CREATE INDEX "idx_messages_sendername_nc" ON messages (sender_name COLLATE NOCASE);
|
CREATE INDEX "idx_messages_sendername_nc" ON messages (sender_name COLLATE NOCASE);
|
||||||
CREATE INDEX "idx_messages_title" ON messages (title COLLATE BINARY);
|
CREATE INDEX "idx_messages_title" ON messages (title COLLATE BINARY);
|
||||||
CREATE INDEX "idx_messages_title_nc" ON messages (title COLLATE NOCASE);
|
CREATE INDEX "idx_messages_title_nc" ON messages (title COLLATE NOCASE);
|
||||||
|
CREATE INDEX "idx_messages_usedkey" ON messages (owner_user_id, used_key_id);
|
||||||
CREATE INDEX "idx_messages_deleted" ON messages (deleted);
|
CREATE INDEX "idx_messages_deleted" ON messages (deleted);
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
type TokenPerm string //@enum:type
|
type TokenPerm string //@enum:type
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PermAdmin TokenPerm = "A" // Edit userdata (+ includes all othe permissions)
|
PermAdmin TokenPerm = "A" // Edit userdata (+ includes all other permissions)
|
||||||
PermChannelRead TokenPerm = "CR" // Read messages
|
PermChannelRead TokenPerm = "CR" // Read messages
|
||||||
PermChannelSend TokenPerm = "CS" // Send messages
|
PermChannelSend TokenPerm = "CS" // Send messages
|
||||||
PermUserRead TokenPerm = "UR" // Read userdata
|
PermUserRead TokenPerm = "UR" // Read userdata
|
||||||
|
@ -26,6 +26,7 @@ type Message struct {
|
|||||||
Content *string
|
Content *string
|
||||||
Priority int
|
Priority int
|
||||||
UserMessageID *string
|
UserMessageID *string
|
||||||
|
UsedKeyID KeyTokenID
|
||||||
Deleted bool
|
Deleted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +44,7 @@ func (m Message) FullJSON() MessageJSON {
|
|||||||
Content: m.Content,
|
Content: m.Content,
|
||||||
Priority: m.Priority,
|
Priority: m.Priority,
|
||||||
UserMessageID: m.UserMessageID,
|
UserMessageID: m.UserMessageID,
|
||||||
|
UsedKeyID: m.UsedKeyID,
|
||||||
Trimmed: false,
|
Trimmed: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,6 +63,7 @@ func (m Message) TrimmedJSON() MessageJSON {
|
|||||||
Content: m.TrimmedContent(),
|
Content: m.TrimmedContent(),
|
||||||
Priority: m.Priority,
|
Priority: m.Priority,
|
||||||
UserMessageID: m.UserMessageID,
|
UserMessageID: m.UserMessageID,
|
||||||
|
UsedKeyID: m.UsedKeyID,
|
||||||
Trimmed: m.NeedsTrim(),
|
Trimmed: m.NeedsTrim(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,36 +97,38 @@ func (m Message) ShortContent() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MessageJSON struct {
|
type MessageJSON struct {
|
||||||
MessageID MessageID `json:"message_id"`
|
MessageID MessageID `json:"message_id"`
|
||||||
SenderUserID UserID `json:"sender_user_id"`
|
SenderUserID UserID `json:"sender_user_id"`
|
||||||
OwnerUserID UserID `json:"owner_user_id"`
|
OwnerUserID UserID `json:"owner_user_id"`
|
||||||
ChannelInternalName string `json:"channel_internal_name"`
|
ChannelInternalName string `json:"channel_internal_name"`
|
||||||
ChannelID ChannelID `json:"channel_id"`
|
ChannelID ChannelID `json:"channel_id"`
|
||||||
SenderName *string `json:"sender_name"`
|
SenderName *string `json:"sender_name"`
|
||||||
SenderIP string `json:"sender_ip"`
|
SenderIP string `json:"sender_ip"`
|
||||||
Timestamp string `json:"timestamp"`
|
Timestamp string `json:"timestamp"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Content *string `json:"content"`
|
Content *string `json:"content"`
|
||||||
Priority int `json:"priority"`
|
Priority int `json:"priority"`
|
||||||
UserMessageID *string `json:"usr_message_id"`
|
UserMessageID *string `json:"usr_message_id"`
|
||||||
Trimmed bool `json:"trimmed"`
|
UsedKeyID KeyTokenID `json:"used_key_id"`
|
||||||
|
Trimmed bool `json:"trimmed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageDB struct {
|
type MessageDB struct {
|
||||||
MessageID MessageID `db:"message_id"`
|
MessageID MessageID `db:"message_id"`
|
||||||
SenderUserID UserID `db:"sender_user_id"`
|
SenderUserID UserID `db:"sender_user_id"`
|
||||||
OwnerUserID UserID `db:"owner_user_id"`
|
OwnerUserID UserID `db:"owner_user_id"`
|
||||||
ChannelInternalName string `db:"channel_internal_name"`
|
ChannelInternalName string `db:"channel_internal_name"`
|
||||||
ChannelID ChannelID `db:"channel_id"`
|
ChannelID ChannelID `db:"channel_id"`
|
||||||
SenderName *string `db:"sender_name"`
|
SenderName *string `db:"sender_name"`
|
||||||
SenderIP string `db:"sender_ip"`
|
SenderIP string `db:"sender_ip"`
|
||||||
TimestampReal int64 `db:"timestamp_real"`
|
TimestampReal int64 `db:"timestamp_real"`
|
||||||
TimestampClient *int64 `db:"timestamp_client"`
|
TimestampClient *int64 `db:"timestamp_client"`
|
||||||
Title string `db:"title"`
|
Title string `db:"title"`
|
||||||
Content *string `db:"content"`
|
Content *string `db:"content"`
|
||||||
Priority int `db:"priority"`
|
Priority int `db:"priority"`
|
||||||
UserMessageID *string `db:"usr_message_id"`
|
UserMessageID *string `db:"usr_message_id"`
|
||||||
Deleted int `db:"deleted"`
|
UsedKeyID KeyTokenID `db:"used_key_id"`
|
||||||
|
Deleted int `db:"deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MessageDB) Model() Message {
|
func (m MessageDB) Model() Message {
|
||||||
@ -141,6 +146,7 @@ func (m MessageDB) Model() Message {
|
|||||||
Content: m.Content,
|
Content: m.Content,
|
||||||
Priority: m.Priority,
|
Priority: m.Priority,
|
||||||
UserMessageID: m.UserMessageID,
|
UserMessageID: m.UserMessageID,
|
||||||
|
UsedKeyID: m.UsedKeyID,
|
||||||
Deleted: m.Deleted != 0,
|
Deleted: m.Deleted != 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ type MessageFilter struct {
|
|||||||
OnlyDeleted bool
|
OnlyDeleted bool
|
||||||
IncludeDeleted bool
|
IncludeDeleted bool
|
||||||
CompatAcknowledged *bool
|
CompatAcknowledged *bool
|
||||||
|
UsedKeyID *[]KeyTokenID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f MessageFilter) SQL() (string, string, sq.PP, error) {
|
func (f MessageFilter) SQL() (string, string, sq.PP, error) {
|
||||||
@ -220,6 +221,15 @@ func (f MessageFilter) SQL() (string, string, sq.PP, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.UsedKeyID != nil {
|
||||||
|
filter := make([]string, 0)
|
||||||
|
for i, v := range *f.UsedKeyID {
|
||||||
|
filter = append(filter, fmt.Sprintf("(used_key_id = :usedkeyid_%d)", i))
|
||||||
|
params[fmt.Sprintf("usedkeyid_%d", i)] = v
|
||||||
|
}
|
||||||
|
sqlClauses = append(sqlClauses, "("+strings.Join(filter, " OR ")+")")
|
||||||
|
}
|
||||||
|
|
||||||
if f.SearchString != nil {
|
if f.SearchString != nil {
|
||||||
filter := make([]string, 0)
|
filter := make([]string, 0)
|
||||||
for i, v := range *f.SearchString {
|
for i, v := range *f.SearchString {
|
||||||
|
Loading…
Reference in New Issue
Block a user