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.

272 lines
5.6 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. primaryUserAssociationKey, secondaryUserAssociationKey string,
  13. i int,
  14. ) error {
  15. var (
  16. message Models.Message
  17. messageData Models.MessageData
  18. key, userKey aesKey
  19. keyCiphertext []byte
  20. plaintext string
  21. dataCiphertext []byte
  22. senderIdCiphertext []byte
  23. err error
  24. )
  25. plaintext = "Test Message"
  26. userKey, err = generateAesKey()
  27. if err != nil {
  28. panic(err)
  29. }
  30. key, err = generateAesKey()
  31. if err != nil {
  32. panic(err)
  33. }
  34. dataCiphertext, err = key.aesEncrypt([]byte(plaintext))
  35. if err != nil {
  36. panic(err)
  37. }
  38. senderIdCiphertext, err = key.aesEncrypt([]byte(primaryUser.ID.String()))
  39. if err != nil {
  40. panic(err)
  41. }
  42. if i%2 == 0 {
  43. senderIdCiphertext, err = key.aesEncrypt([]byte(secondaryUser.ID.String()))
  44. if err != nil {
  45. panic(err)
  46. }
  47. }
  48. keyCiphertext, err = userKey.aesEncrypt(
  49. []byte(base64.StdEncoding.EncodeToString(key.Key)),
  50. )
  51. if err != nil {
  52. panic(err)
  53. }
  54. messageData = Models.MessageData{
  55. Data: base64.StdEncoding.EncodeToString(dataCiphertext),
  56. SenderID: base64.StdEncoding.EncodeToString(senderIdCiphertext),
  57. SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
  58. }
  59. message = Models.Message{
  60. MessageData: messageData,
  61. SymmetricKey: base64.StdEncoding.EncodeToString(
  62. encryptWithPublicKey(userKey.Key, decodedPublicKey),
  63. ),
  64. AssociationKey: primaryUserAssociationKey,
  65. }
  66. err = Database.CreateMessage(&message)
  67. if err != nil {
  68. return err
  69. }
  70. message = Models.Message{
  71. MessageData: messageData,
  72. SymmetricKey: base64.StdEncoding.EncodeToString(
  73. encryptWithPublicKey(userKey.Key, decodedPublicKey),
  74. ),
  75. AssociationKey: secondaryUserAssociationKey,
  76. }
  77. return Database.CreateMessage(&message)
  78. }
  79. func seedConversationDetail(key aesKey) (Models.ConversationDetail, error) {
  80. var (
  81. messageThread Models.ConversationDetail
  82. name string
  83. nameCiphertext []byte
  84. err error
  85. )
  86. name = "Test Conversation"
  87. nameCiphertext, err = key.aesEncrypt([]byte(name))
  88. if err != nil {
  89. panic(err)
  90. }
  91. messageThread = Models.ConversationDetail{
  92. Name: base64.StdEncoding.EncodeToString(nameCiphertext),
  93. }
  94. err = Database.CreateConversationDetail(&messageThread)
  95. return messageThread, err
  96. }
  97. func seedUpdateUserConversation(
  98. userJson string,
  99. key aesKey,
  100. messageThread Models.ConversationDetail,
  101. ) (Models.ConversationDetail, error) {
  102. var (
  103. usersCiphertext []byte
  104. err error
  105. )
  106. usersCiphertext, err = key.aesEncrypt([]byte(userJson))
  107. if err != nil {
  108. return messageThread, err
  109. }
  110. messageThread.Users = base64.StdEncoding.EncodeToString(usersCiphertext)
  111. err = Database.UpdateConversationDetail(&messageThread)
  112. return messageThread, err
  113. }
  114. func seedUserConversation(
  115. user Models.User,
  116. threadID uuid.UUID,
  117. key aesKey,
  118. ) (Models.UserConversation, error) {
  119. var (
  120. messageThreadUser Models.UserConversation
  121. threadIdCiphertext []byte
  122. adminCiphertext []byte
  123. err error
  124. )
  125. threadIdCiphertext, err = key.aesEncrypt([]byte(threadID.String()))
  126. if err != nil {
  127. return messageThreadUser, err
  128. }
  129. adminCiphertext, err = key.aesEncrypt([]byte("true"))
  130. if err != nil {
  131. return messageThreadUser, err
  132. }
  133. messageThreadUser = Models.UserConversation{
  134. UserID: user.ID,
  135. ConversationDetailID: base64.StdEncoding.EncodeToString(threadIdCiphertext),
  136. Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
  137. SymmetricKey: base64.StdEncoding.EncodeToString(
  138. encryptWithPublicKey(key.Key, decodedPublicKey),
  139. ),
  140. }
  141. err = Database.CreateUserConversation(&messageThreadUser)
  142. return messageThreadUser, err
  143. }
  144. func SeedMessages() {
  145. var (
  146. messageThread Models.ConversationDetail
  147. key aesKey
  148. primaryUser Models.User
  149. primaryUserAssociationKey string
  150. secondaryUser Models.User
  151. secondaryUserAssociationKey string
  152. userJson string
  153. id1, id2 uuid.UUID
  154. i int
  155. err error
  156. )
  157. key, err = generateAesKey()
  158. messageThread, err = seedConversationDetail(key)
  159. primaryUserAssociationKey = Util.RandomString(32)
  160. secondaryUserAssociationKey = Util.RandomString(32)
  161. primaryUser, err = Database.GetUserByUsername("testUser")
  162. if err != nil {
  163. panic(err)
  164. }
  165. _, err = seedUserConversation(
  166. primaryUser,
  167. messageThread.ID,
  168. key,
  169. )
  170. secondaryUser, err = Database.GetUserByUsername("ATestUser2")
  171. if err != nil {
  172. panic(err)
  173. }
  174. _, err = seedUserConversation(
  175. secondaryUser,
  176. messageThread.ID,
  177. key,
  178. )
  179. id1, err = uuid.NewV4()
  180. if err != nil {
  181. panic(err)
  182. }
  183. id2, err = uuid.NewV4()
  184. if err != nil {
  185. panic(err)
  186. }
  187. userJson = fmt.Sprintf(
  188. `
  189. [
  190. {
  191. "id": "%s",
  192. "user_id": "%s",
  193. "username": "%s",
  194. "admin": "true",
  195. "association_key": "%s"
  196. },
  197. {
  198. "id": "%s",
  199. "user_id": "%s",
  200. "username": "%s",
  201. "admin": "false",
  202. "association_key": "%s"
  203. }
  204. ]
  205. `,
  206. id1.String(),
  207. primaryUser.ID.String(),
  208. primaryUser.Username,
  209. primaryUserAssociationKey,
  210. id2.String(),
  211. secondaryUser.ID.String(),
  212. secondaryUser.Username,
  213. secondaryUserAssociationKey,
  214. )
  215. messageThread, err = seedUpdateUserConversation(
  216. userJson,
  217. key,
  218. messageThread,
  219. )
  220. for i = 0; i <= 20; i++ {
  221. err = seedMessage(
  222. primaryUser,
  223. secondaryUser,
  224. primaryUserAssociationKey,
  225. secondaryUserAssociationKey,
  226. i,
  227. )
  228. if err != nil {
  229. panic(err)
  230. }
  231. }
  232. }