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.

108 lines
2.6 KiB

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