@ -7,60 +7,93 @@ import (
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
"github.com/gofrs/uuid"
"github.com/gofrs/uuid"
)
)
func seedMessage (
func seedMessage (
primaryUser , secondaryUser Models . User ,
primaryUser , secondaryUser Models . User ,
primaryUserThreadKey , secondaryUserThreadKey string ,
thread Models . ConversationDetail ,
primaryUserAssociationKey , secondaryUserAssociationKey string ,
i int ,
i int ,
) error {
) error {
var (
var (
message Models . Message
message Models . Message
messageData Models . MessageData
messageData Models . MessageData
key aesKey
key , userKey aesKey
keyCiphertext [ ] byte
plaintext string
plaintext string
dataCiphertext [ ] byte
dataCiphertext [ ] byte
senderIdCiphertext [ ] byte
senderIdCiphertext [ ] byte
friendId [ ] byte
err error
err error
)
)
key , err = generateAesKey ( )
plaintext = "Test Message"
userKey , err = generateAesKey ( )
if err != nil {
if err != nil {
panic ( err )
panic ( err )
}
}
plaintext = "Test Message"
key , err = generateAesKey ( )
if err != nil {
panic ( err )
}
dataCiphertext , err = key . aesEncrypt ( [ ] byte ( plaintext ) )
dataCiphertext , err = key . aesEncrypt ( [ ] byte ( plaintext ) )
if err != nil {
if err != nil {
panic ( err )
panic ( err )
}
}
senderIdCiphertext , err = key . aesEncrypt ( [ ] byte ( primaryUser . ID . String ( ) ) )
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 {
if err != nil {
panic ( err )
panic ( err )
}
}
if i % 2 == 0 {
if i % 2 == 0 {
senderIdCiphertext , err = key . aesEncrypt ( [ ] byte ( secondaryUser . ID . String ( ) ) )
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 {
if err != nil {
panic ( err )
panic ( err )
}
}
}
}
keyCiphertext , err = userKey . aesEncrypt (
[ ] byte ( base64 . StdEncoding . EncodeToString ( key . Key ) ) ,
)
if err != nil {
panic ( err )
}
messageData = Models . MessageData {
messageData = Models . MessageData {
Data : base64 . StdEncoding . EncodeToString ( dataCiphertext ) ,
SenderID : base64 . StdEncoding . EncodeToString ( senderIdCiphertext ) ,
Data : base64 . StdEncoding . EncodeToString ( dataCiphertext ) ,
SenderID : base64 . StdEncoding . EncodeToString ( senderIdCiphertext ) ,
SymmetricKey : base64 . StdEncoding . EncodeToString ( keyCiphertext ) ,
}
}
message = Models . Message {
message = Models . Message {
MessageData : messageData ,
MessageData : messageData ,
SymmetricKey : base64 . StdEncoding . EncodeToString (
SymmetricKey : base64 . StdEncoding . EncodeToString (
encryptWithPublicKey ( k ey. Key , decodedPublicKey ) ,
encryptWithPublicKey ( userK ey. Key , decodedPublicKey ) ,
) ,
) ,
MessageThreadKey : primaryUserThread Key,
AssociationKey : primaryUserAssociation Key,
}
}
err = Database . CreateMessage ( & message )
err = Database . CreateMessage ( & message )
@ -68,22 +101,15 @@ func seedMessage(
return err
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 {
message = Models . Message {
MessageDataID : message . Message DataID ,
MessageData : messageData ,
SymmetricKey : base64 . StdEncoding . EncodeToString (
SymmetricKey : base64 . StdEncoding . EncodeToString (
encryptWithPublicKey ( k ey. Key , decodedPublicKey ) ,
encryptWithPublicKey ( userK ey. Key , decodedPublicKey ) ,
) ,
) ,
MessageThreadKey : secondaryUserThreadKey ,
}
err = Database . CreateMessage ( & message )
if err != nil {
return err
AssociationKey : secondaryUserAssociationKey ,
}
}
return err
return Database . CreateMessage ( & message )
}
}
func seedConversationDetail ( key aesKey ) ( Models . ConversationDetail , error ) {
func seedConversationDetail ( key aesKey ) ( Models . ConversationDetail , error ) {
@ -132,13 +158,11 @@ func seedUpdateUserConversation(
func seedUserConversation (
func seedUserConversation (
user Models . User ,
user Models . User ,
threadID uuid . UUID ,
threadID uuid . UUID ,
messageThreadKey string ,
key aesKey ,
key aesKey ,
) ( Models . UserConversation , error ) {
) ( Models . UserConversation , error ) {
var (
var (
messageThreadUser Models . UserConversation
messageThreadUser Models . UserConversation
threadIdCiphertext [ ] byte
threadIdCiphertext [ ] byte
keyCiphertext [ ] byte
adminCiphertext [ ] byte
adminCiphertext [ ] byte
err error
err error
)
)
@ -148,11 +172,6 @@ func seedUserConversation(
return messageThreadUser , err
return messageThreadUser , err
}
}
keyCiphertext , err = key . aesEncrypt ( [ ] byte ( messageThreadKey ) )
if err != nil {
return messageThreadUser , err
}
adminCiphertext , err = key . aesEncrypt ( [ ] byte ( "true" ) )
adminCiphertext , err = key . aesEncrypt ( [ ] byte ( "true" ) )
if err != nil {
if err != nil {
return messageThreadUser , err
return messageThreadUser , err
@ -161,7 +180,6 @@ func seedUserConversation(
messageThreadUser = Models . UserConversation {
messageThreadUser = Models . UserConversation {
UserID : user . ID ,
UserID : user . ID ,
ConversationDetailID : base64 . StdEncoding . EncodeToString ( threadIdCiphertext ) ,
ConversationDetailID : base64 . StdEncoding . EncodeToString ( threadIdCiphertext ) ,
MessageThreadKey : base64 . StdEncoding . EncodeToString ( keyCiphertext ) ,
Admin : base64 . StdEncoding . EncodeToString ( adminCiphertext ) ,
Admin : base64 . StdEncoding . EncodeToString ( adminCiphertext ) ,
SymmetricKey : base64 . StdEncoding . EncodeToString (
SymmetricKey : base64 . StdEncoding . EncodeToString (
encryptWithPublicKey ( key . Key , decodedPublicKey ) ,
encryptWithPublicKey ( key . Key , decodedPublicKey ) ,
@ -174,24 +192,24 @@ func seedUserConversation(
func SeedMessages ( ) {
func SeedMessages ( ) {
var (
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
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 ( )
key , err = generateAesKey ( )
messageThread , err = seedConversationDetail ( key )
messageThread , err = seedConversationDetail ( key )
primaryUserThreadKey = Util . RandomString ( 32 )
secondaryUserThreadKey = Util . RandomString ( 32 )
primaryUserAssociationKey = Util . RandomString ( 32 )
secondaryUserAssociationKey = Util . RandomString ( 32 )
primaryUser , err = Database . GetUserByUsername ( "testUser" )
primaryUser , err = Database . GetUserByUsername ( "testUser" )
if err != nil {
if err != nil {
@ -201,7 +219,6 @@ func SeedMessages() {
_ , err = seedUserConversation (
_ , err = seedUserConversation (
primaryUser ,
primaryUser ,
messageThread . ID ,
messageThread . ID ,
primaryUserThreadKey ,
key ,
key ,
)
)
@ -213,29 +230,50 @@ func SeedMessages() {
_ , err = seedUserConversation (
_ , err = seedUserConversation (
secondaryUser ,
secondaryUser ,
messageThread . ID ,
messageThread . ID ,
secondaryUserThreadKey ,
key ,
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 (
userJson = fmt . Sprintf (
`
`
[
[
{
{
"id" : "%s" ,
"id" : "%s" ,
"username" : "%s" ,
"username" : "%s" ,
"admin" : "true"
"admin" : "true" ,
"association_key" : "%s"
} ,
} ,
{
{
"id" : "%s" ,
"id" : "%s" ,
"username" : "%s" ,
"username" : "%s" ,
"admin" : "true"
"admin" : "true" ,
"association_key" : "%s"
}
}
]
]
` ,
` ,
primaryUser . ID . String ( ) ,
string ( primaryUserFriendId ) ,
primaryUser . Username ,
primaryUser . Username ,
secondaryUser . ID . String ( ) ,
primaryUserAssociationKey ,
string ( secondaryUserFriendId ) ,
secondaryUser . Username ,
secondaryUser . Username ,
secondaryUserAssociationKey ,
)
)
messageThread , err = seedUpdateUserConversation (
messageThread , err = seedUpdateUserConversation (
@ -248,9 +286,8 @@ func SeedMessages() {
err = seedMessage (
err = seedMessage (
primaryUser ,
primaryUser ,
secondaryUser ,
secondaryUser ,
primaryUserThreadKey ,
secondaryUserThreadKey ,
thread ,
primaryUserAssociationKey ,
secondaryUserAssociationKey ,
i ,
i ,
)
)
if err != nil {
if err != nil {