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.

54 lines
1021 B

  1. package Users
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. )
  8. // SearchUsers searches a for a user by username
  9. func SearchUsers(w http.ResponseWriter, r *http.Request) {
  10. var (
  11. user Database.User
  12. query url.Values
  13. rawUsername []string
  14. username string
  15. returnJSON []byte
  16. ok bool
  17. err error
  18. )
  19. query = r.URL.Query()
  20. rawUsername, ok = query["username"]
  21. if !ok {
  22. http.Error(w, "Not Found", http.StatusNotFound)
  23. return
  24. }
  25. if len(rawUsername) != 1 {
  26. http.Error(w, "Not Found", http.StatusNotFound)
  27. return
  28. }
  29. username = rawUsername[0]
  30. user, err = Database.GetUserByUsername(username)
  31. if err != nil {
  32. http.Error(w, "Not Found", http.StatusNotFound)
  33. return
  34. }
  35. user.Password = ""
  36. user.AsymmetricPrivateKey = ""
  37. returnJSON, err = json.MarshalIndent(user, "", " ")
  38. if err != nil {
  39. http.Error(w, "Not Found", http.StatusNotFound)
  40. return
  41. }
  42. w.WriteHeader(http.StatusOK)
  43. w.Write(returnJSON)
  44. }