package Seeder import ( "encoding/base64" "fmt" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" "github.com/gofrs/uuid" ) func seedMessage( primaryUser Models.User, primaryUserThreadKey, secondaryUserThreadKey string, thread Models.ConversationDetail, i int, ) error { var ( message Models.Message messageData Models.MessageData key aesKey plaintext string dataCiphertext []byte senderIdCiphertext []byte err error ) key, err = generateAesKey() if err != nil { panic(err) } plaintext = "Test Message" dataCiphertext, err = key.aesEncrypt([]byte(plaintext)) if err != nil { panic(err) } senderIdCiphertext, err = key.aesEncrypt(primaryUser.ID.Bytes()) if err != nil { panic(err) } messageData = Models.MessageData{ Data: base64.StdEncoding.EncodeToString(dataCiphertext), SenderID: base64.StdEncoding.EncodeToString(senderIdCiphertext), } message = Models.Message{ MessageData: messageData, SymmetricKey: base64.StdEncoding.EncodeToString( encryptWithPublicKey(key.Key, decodedPublicKey), ), MessageThreadKey: primaryUserThreadKey, } err = Database.CreateMessage(&message) if err != nil { return err } // The symmetric key would be encrypted with secondary users public key in production // But due to using the same pub/priv key pair for all users, we will just duplicate it message = Models.Message{ MessageDataID: message.MessageDataID, SymmetricKey: base64.StdEncoding.EncodeToString( encryptWithPublicKey(key.Key, decodedPublicKey), ), MessageThreadKey: secondaryUserThreadKey, } err = Database.CreateMessage(&message) if err != nil { return err } return err } func seedConversationDetail(key aesKey) (Models.ConversationDetail, error) { var ( messageThread Models.ConversationDetail name string nameCiphertext []byte err error ) name = "Test Conversation" nameCiphertext, err = key.aesEncrypt([]byte(name)) if err != nil { panic(err) } messageThread = Models.ConversationDetail{ Name: base64.StdEncoding.EncodeToString(nameCiphertext), } err = Database.CreateConversationDetail(&messageThread) return messageThread, err } func seedUpdateUserConversation( userJson string, key aesKey, messageThread Models.ConversationDetail, ) (Models.ConversationDetail, error) { var ( usersCiphertext []byte err error ) usersCiphertext, err = key.aesEncrypt([]byte(userJson)) if err != nil { return messageThread, err } messageThread.Users = base64.StdEncoding.EncodeToString(usersCiphertext) err = Database.UpdateConversationDetail(&messageThread) return messageThread, err } func seedUserConversation( user Models.User, threadID uuid.UUID, messageThreadKey string, key aesKey, ) (Models.UserConversation, error) { var ( messageThreadUser Models.UserConversation threadIdCiphertext []byte keyCiphertext []byte adminCiphertext []byte err error ) threadIdCiphertext, err = key.aesEncrypt([]byte(threadID.String())) if err != nil { return messageThreadUser, err } keyCiphertext, err = key.aesEncrypt([]byte(messageThreadKey)) if err != nil { return messageThreadUser, err } adminCiphertext, err = key.aesEncrypt([]byte("true")) if err != nil { return messageThreadUser, err } messageThreadUser = Models.UserConversation{ UserID: user.ID, ConversationDetailID: base64.StdEncoding.EncodeToString(threadIdCiphertext), MessageThreadKey: base64.StdEncoding.EncodeToString(keyCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), SymmetricKey: base64.StdEncoding.EncodeToString( encryptWithPublicKey(key.Key, decodedPublicKey), ), } err = Database.CreateUserConversation(&messageThreadUser) return messageThreadUser, err } func SeedMessages() { var ( messageThread Models.ConversationDetail key aesKey primaryUser Models.User primaryUserThreadKey string secondaryUser Models.User secondaryUserThreadKey string userJson string thread Models.ConversationDetail i int err error ) key, err = generateAesKey() messageThread, err = seedConversationDetail(key) primaryUserThreadKey = Util.RandomString(32) secondaryUserThreadKey = Util.RandomString(32) primaryUser, err = Database.GetUserByUsername("testUser") if err != nil { panic(err) } _, err = seedUserConversation( primaryUser, messageThread.ID, primaryUserThreadKey, key, ) secondaryUser, err = Database.GetUserByUsername("testUser2") if err != nil { panic(err) } _, err = seedUserConversation( secondaryUser, messageThread.ID, secondaryUserThreadKey, key, ) userJson = fmt.Sprintf( ` [ { "id": "%s", "username": "%s", "admin": "true" }, { "id": "%s", "username": "%s", "admin": "true" } ] `, primaryUser.ID.String(), primaryUser.Username, secondaryUser.ID.String(), secondaryUser.Username, ) messageThread, err = seedUpdateUserConversation( userJson, key, messageThread, ) for i = 0; i <= 20; i++ { err = seedMessage( primaryUser, primaryUserThreadKey, secondaryUserThreadKey, thread, i, ) if err != nil { panic(err) } } }