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.

111 lines
2.6 KiB

  1. package Auth
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "net/http"
  6. "time"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  8. )
  9. type signup struct {
  10. Username string `json:"username"`
  11. Password string `json:"password"`
  12. ConfirmPassword string `json:"confirm_password"`
  13. PublicKey string `json:"asymmetric_public_key"`
  14. PrivateKey string `json:"asymmetric_private_key"`
  15. }
  16. // Signup to the platform
  17. func Signup(w http.ResponseWriter, r *http.Request) {
  18. var (
  19. user Database.User
  20. expiresAt time.Time
  21. session Database.Session
  22. messageExpiryRaw driver.Value
  23. messageExpiry string
  24. imageLink string
  25. returnJSON []byte
  26. err error
  27. )
  28. err = json.NewDecoder(r.Body).Decode(&user)
  29. if err != nil {
  30. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  31. return
  32. }
  33. if user.Username == "" ||
  34. user.Password == "" ||
  35. user.ConfirmPassword == "" ||
  36. len(user.AsymmetricPrivateKey) == 0 ||
  37. len(user.AsymmetricPublicKey) == 0 {
  38. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  39. return
  40. }
  41. if user.Password != user.ConfirmPassword {
  42. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  43. return
  44. }
  45. err = Database.CheckUniqueUsername(user.Username)
  46. if err != nil {
  47. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  48. return
  49. }
  50. user.Password, err = HashPassword(user.Password)
  51. if err != nil {
  52. http.Error(w, "Error", http.StatusInternalServerError)
  53. return
  54. }
  55. err = (&user).CreateUser()
  56. if err != nil {
  57. http.Error(w, "Error", http.StatusInternalServerError)
  58. return
  59. }
  60. // TODO: Revisit before production
  61. expiresAt = time.Now().Add(12 * time.Hour)
  62. session = Database.Session{
  63. UserID: user.ID,
  64. Expiry: expiresAt,
  65. }
  66. err = (&session).CreateSession()
  67. if err != nil {
  68. http.Error(w, "Unauthorized", http.StatusUnauthorized)
  69. return
  70. }
  71. http.SetCookie(w, &http.Cookie{
  72. Name: "session_token",
  73. Value: session.ID.String(),
  74. Expires: expiresAt,
  75. })
  76. if user.AttachmentID != nil {
  77. imageLink = user.Attachment.FilePath
  78. }
  79. messageExpiryRaw, _ = user.MessageExpiryDefault.Value()
  80. messageExpiry, _ = messageExpiryRaw.(string)
  81. returnJSON, err = json.MarshalIndent(loginResponse{
  82. UserID: user.ID.String(),
  83. Username: user.Username,
  84. AsymmetricPublicKey: user.AsymmetricPublicKey,
  85. AsymmetricPrivateKey: user.AsymmetricPrivateKey,
  86. SymmetricKey: user.SymmetricKey,
  87. MessageExpiryDefault: messageExpiry,
  88. ImageLink: imageLink,
  89. }, "", " ")
  90. w.WriteHeader(http.StatusOK)
  91. w.Write(returnJSON)
  92. }