Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
6.4 KiB

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, secondaryUser Models.User,
primaryUserAssociationKey, secondaryUserAssociationKey string,
i int,
) error {
var (
message Models.Message
messageData Models.MessageData
key, userKey aesKey
keyCiphertext []byte
plaintext string
dataCiphertext []byte
senderIdCiphertext []byte
friendId []byte
err error
)
plaintext = "Test Message"
userKey, err = generateAesKey()
if err != nil {
panic(err)
}
key, err = generateAesKey()
if err != nil {
panic(err)
}
dataCiphertext, err = key.aesEncrypt([]byte(plaintext))
if err != nil {
panic(err)
}
friendId, err = base64.StdEncoding.DecodeString(primaryUser.FriendID)
if err != nil {
panic(err)
}
friendId, err = decryptWithPrivateKey(friendId, decodedPrivateKey)
if err != nil {
panic(err)
}
senderIdCiphertext, err = key.aesEncrypt(friendId)
if err != nil {
panic(err)
}
if i%2 == 0 {
friendId, err = base64.StdEncoding.DecodeString(secondaryUser.FriendID)
if err != nil {
panic(err)
}
friendId, err = decryptWithPrivateKey(friendId, decodedPrivateKey)
if err != nil {
panic(err)
}
senderIdCiphertext, err = key.aesEncrypt(friendId)
if err != nil {
panic(err)
}
}
keyCiphertext, err = userKey.aesEncrypt(
[]byte(base64.StdEncoding.EncodeToString(key.Key)),
)
if err != nil {
panic(err)
}
messageData = Models.MessageData{
Data: base64.StdEncoding.EncodeToString(dataCiphertext),
SenderID: base64.StdEncoding.EncodeToString(senderIdCiphertext),
SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
}
message = Models.Message{
MessageData: messageData,
SymmetricKey: base64.StdEncoding.EncodeToString(
encryptWithPublicKey(userKey.Key, decodedPublicKey),
),
AssociationKey: primaryUserAssociationKey,
}
err = Database.CreateMessage(&message)
if err != nil {
return err
}
message = Models.Message{
MessageData: messageData,
SymmetricKey: base64.StdEncoding.EncodeToString(
encryptWithPublicKey(userKey.Key, decodedPublicKey),
),
AssociationKey: secondaryUserAssociationKey,
}
return Database.CreateMessage(&message)
}
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,
key aesKey,
) (Models.UserConversation, error) {
var (
messageThreadUser Models.UserConversation
threadIdCiphertext []byte
adminCiphertext []byte
err error
)
threadIdCiphertext, err = key.aesEncrypt([]byte(threadID.String()))
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),
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
primaryUserAssociationKey string
secondaryUser Models.User
secondaryUserAssociationKey string
primaryUserFriendId []byte
secondaryUserFriendId []byte
userJson string
i int
err error
)
key, err = generateAesKey()
messageThread, err = seedConversationDetail(key)
primaryUserAssociationKey = Util.RandomString(32)
secondaryUserAssociationKey = Util.RandomString(32)
primaryUser, err = Database.GetUserByUsername("testUser")
if err != nil {
panic(err)
}
_, err = seedUserConversation(
primaryUser,
messageThread.ID,
key,
)
secondaryUser, err = Database.GetUserByUsername("ATestUser2")
if err != nil {
panic(err)
}
_, err = seedUserConversation(
secondaryUser,
messageThread.ID,
key,
)
primaryUserFriendId, err = base64.StdEncoding.DecodeString(primaryUser.FriendID)
if err != nil {
panic(err)
}
primaryUserFriendId, err = decryptWithPrivateKey(primaryUserFriendId, decodedPrivateKey)
if err != nil {
panic(err)
}
secondaryUserFriendId, err = base64.StdEncoding.DecodeString(secondaryUser.FriendID)
if err != nil {
panic(err)
}
secondaryUserFriendId, err = decryptWithPrivateKey(secondaryUserFriendId, decodedPrivateKey)
if err != nil {
panic(err)
}
userJson = fmt.Sprintf(
`
[
{
"id": "%s",
"username": "%s",
"admin": "true",
"association_key": "%s"
},
{
"id": "%s",
"username": "%s",
"admin": "false",
"association_key": "%s"
}
]
`,
string(primaryUserFriendId),
primaryUser.Username,
primaryUserAssociationKey,
string(secondaryUserFriendId),
secondaryUser.Username,
secondaryUserAssociationKey,
)
messageThread, err = seedUpdateUserConversation(
userJson,
key,
messageThread,
)
for i = 0; i <= 20; i++ {
err = seedMessage(
primaryUser,
secondaryUser,
primaryUserAssociationKey,
secondaryUserAssociationKey,
i,
)
if err != nil {
panic(err)
}
}
}