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.

65 lines
1.4 KiB

  1. package Auth
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  6. )
  7. type signup struct {
  8. Username string `json:"username"`
  9. Password string `json:"password"`
  10. ConfirmPassword string `json:"confirm_password"`
  11. PublicKey string `json:"asymmetric_public_key"`
  12. PrivateKey string `json:"asymmetric_private_key"`
  13. }
  14. // Signup to the platform
  15. func Signup(w http.ResponseWriter, r *http.Request) {
  16. var (
  17. user Database.User
  18. err error
  19. )
  20. err = json.NewDecoder(r.Body).Decode(&user)
  21. if err != nil {
  22. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  23. return
  24. }
  25. if user.Username == "" ||
  26. user.Password == "" ||
  27. user.ConfirmPassword == "" ||
  28. len(user.AsymmetricPrivateKey) == 0 ||
  29. len(user.AsymmetricPublicKey) == 0 {
  30. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  31. return
  32. }
  33. if user.Password != user.ConfirmPassword {
  34. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  35. return
  36. }
  37. err = Database.CheckUniqueUsername(user.Username)
  38. if err != nil {
  39. http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
  40. return
  41. }
  42. user.Password, err = HashPassword(user.Password)
  43. if err != nil {
  44. http.Error(w, "Error", http.StatusInternalServerError)
  45. return
  46. }
  47. err = (&user).CreateUser()
  48. if err != nil {
  49. http.Error(w, "Error", http.StatusInternalServerError)
  50. return
  51. }
  52. w.WriteHeader(http.StatusNoContent)
  53. }