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.

260 lines
5.5 KiB

  1. package Seeder
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
  8. "github.com/gofrs/uuid"
  9. )
  10. func seedMessage(
  11. primaryUser, secondaryUser Models.User,
  12. primaryUserThreadKey, secondaryUserThreadKey string,
  13. thread Models.ConversationDetail,
  14. i int,
  15. ) error {
  16. var (
  17. message Models.Message
  18. messageData Models.MessageData
  19. key aesKey
  20. plaintext string
  21. dataCiphertext []byte
  22. senderIdCiphertext []byte
  23. err error
  24. )
  25. key, err = generateAesKey()
  26. if err != nil {
  27. panic(err)
  28. }
  29. plaintext = "Test Message"
  30. dataCiphertext, err = key.aesEncrypt([]byte(plaintext))
  31. if err != nil {
  32. panic(err)
  33. }
  34. senderIdCiphertext, err = key.aesEncrypt([]byte(primaryUser.ID.String()))
  35. if err != nil {
  36. panic(err)
  37. }
  38. if i%2 == 0 {
  39. senderIdCiphertext, err = key.aesEncrypt([]byte(secondaryUser.ID.String()))
  40. if err != nil {
  41. panic(err)
  42. }
  43. }
  44. messageData = Models.MessageData{
  45. Data: base64.StdEncoding.EncodeToString(dataCiphertext),
  46. SenderID: base64.StdEncoding.EncodeToString(senderIdCiphertext),
  47. }
  48. message = Models.Message{
  49. MessageData: messageData,
  50. SymmetricKey: base64.StdEncoding.EncodeToString(
  51. encryptWithPublicKey(key.Key, decodedPublicKey),
  52. ),
  53. MessageThreadKey: primaryUserThreadKey,
  54. }
  55. err = Database.CreateMessage(&message)
  56. if err != nil {
  57. return err
  58. }
  59. // The symmetric key would be encrypted with secondary users public key in production
  60. // But due to using the same pub/priv key pair for all users, we will just duplicate it
  61. message = Models.Message{
  62. MessageDataID: message.MessageDataID,
  63. SymmetricKey: base64.StdEncoding.EncodeToString(
  64. encryptWithPublicKey(key.Key, decodedPublicKey),
  65. ),
  66. MessageThreadKey: secondaryUserThreadKey,
  67. }
  68. err = Database.CreateMessage(&message)
  69. if err != nil {
  70. return err
  71. }
  72. return err
  73. }
  74. func seedConversationDetail(key aesKey) (Models.ConversationDetail, error) {
  75. var (
  76. messageThread Models.ConversationDetail
  77. name string
  78. nameCiphertext []byte
  79. err error
  80. )
  81. name = "Test Conversation"
  82. nameCiphertext, err = key.aesEncrypt([]byte(name))
  83. if err != nil {
  84. panic(err)
  85. }
  86. messageThread = Models.ConversationDetail{
  87. Name: base64.StdEncoding.EncodeToString(nameCiphertext),
  88. }
  89. err = Database.CreateConversationDetail(&messageThread)
  90. return messageThread, err
  91. }
  92. func seedUpdateUserConversation(
  93. userJson string,
  94. key aesKey,
  95. messageThread Models.ConversationDetail,
  96. ) (Models.ConversationDetail, error) {
  97. var (
  98. usersCiphertext []byte
  99. err error
  100. )
  101. usersCiphertext, err = key.aesEncrypt([]byte(userJson))
  102. if err != nil {
  103. return messageThread, err
  104. }
  105. messageThread.Users = base64.StdEncoding.EncodeToString(usersCiphertext)
  106. err = Database.UpdateConversationDetail(&messageThread)
  107. return messageThread, err
  108. }
  109. func seedUserConversation(
  110. user Models.User,
  111. threadID uuid.UUID,
  112. messageThreadKey string,
  113. key aesKey,
  114. ) (Models.UserConversation, error) {
  115. var (
  116. messageThreadUser Models.UserConversation
  117. threadIdCiphertext []byte
  118. keyCiphertext []byte
  119. adminCiphertext []byte
  120. err error
  121. )
  122. threadIdCiphertext, err = key.aesEncrypt([]byte(threadID.String()))
  123. if err != nil {
  124. return messageThreadUser, err
  125. }
  126. keyCiphertext, err = key.aesEncrypt([]byte(messageThreadKey))
  127. if err != nil {
  128. return messageThreadUser, err
  129. }
  130. adminCiphertext, err = key.aesEncrypt([]byte("true"))
  131. if err != nil {
  132. return messageThreadUser, err
  133. }
  134. messageThreadUser = Models.UserConversation{
  135. UserID: user.ID,
  136. ConversationDetailID: base64.StdEncoding.EncodeToString(threadIdCiphertext),
  137. MessageThreadKey: base64.StdEncoding.EncodeToString(keyCiphertext),
  138. Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
  139. SymmetricKey: base64.StdEncoding.EncodeToString(
  140. encryptWithPublicKey(key.Key, decodedPublicKey),
  141. ),
  142. }
  143. err = Database.CreateUserConversation(&messageThreadUser)
  144. return messageThreadUser, err
  145. }
  146. func SeedMessages() {
  147. var (
  148. messageThread Models.ConversationDetail
  149. key aesKey
  150. primaryUser Models.User
  151. primaryUserThreadKey string
  152. secondaryUser Models.User
  153. secondaryUserThreadKey string
  154. userJson string
  155. thread Models.ConversationDetail
  156. i int
  157. err error
  158. )
  159. key, err = generateAesKey()
  160. messageThread, err = seedConversationDetail(key)
  161. primaryUserThreadKey = Util.RandomString(32)
  162. secondaryUserThreadKey = Util.RandomString(32)
  163. primaryUser, err = Database.GetUserByUsername("testUser")
  164. if err != nil {
  165. panic(err)
  166. }
  167. _, err = seedUserConversation(
  168. primaryUser,
  169. messageThread.ID,
  170. primaryUserThreadKey,
  171. key,
  172. )
  173. secondaryUser, err = Database.GetUserByUsername("testUser2")
  174. if err != nil {
  175. panic(err)
  176. }
  177. _, err = seedUserConversation(
  178. secondaryUser,
  179. messageThread.ID,
  180. secondaryUserThreadKey,
  181. key,
  182. )
  183. userJson = fmt.Sprintf(
  184. `
  185. [
  186. {
  187. "id": "%s",
  188. "username": "%s",
  189. "admin": "true"
  190. },
  191. {
  192. "id": "%s",
  193. "username": "%s",
  194. "admin": "true"
  195. }
  196. ]
  197. `,
  198. primaryUser.ID.String(),
  199. primaryUser.Username,
  200. secondaryUser.ID.String(),
  201. secondaryUser.Username,
  202. )
  203. messageThread, err = seedUpdateUserConversation(
  204. userJson,
  205. key,
  206. messageThread,
  207. )
  208. for i = 0; i <= 20; i++ {
  209. err = seedMessage(
  210. primaryUser,
  211. secondaryUser,
  212. primaryUserThreadKey,
  213. secondaryUserThreadKey,
  214. thread,
  215. i,
  216. )
  217. if err != nil {
  218. panic(err)
  219. }
  220. }
  221. }