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.

135 lines
3.5 KiB

  1. package gateway
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "git.chrishayward.xyz/x/users/proto"
  7. )
  8. func Register(client proto.UsersClient) http.HandlerFunc {
  9. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. password, passwordAgain :=
  11. r.URL.Query().Get("password"),
  12. r.URL.Query().Get("password_again")
  13. _, err := client.Register(r.Context(), &proto.RegisterRequest{
  14. Form: &proto.UserForm{
  15. Email: r.URL.Query().Get("email"),
  16. Password: &password,
  17. PasswordAgain: &passwordAgain,
  18. },
  19. })
  20. if err != nil {
  21. w.WriteHeader(http.StatusBadRequest)
  22. w.Write([]byte(err.Error()))
  23. }
  24. w.WriteHeader(http.StatusOK)
  25. })
  26. }
  27. func Login(client proto.UsersClient) http.HandlerFunc {
  28. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  29. password, passwordAgain :=
  30. r.URL.Query().Get("password"),
  31. r.URL.Query().Get("password_again")
  32. res, err := client.Login(r.Context(), &proto.LoginRequest{
  33. Form: &proto.UserForm{
  34. Email: r.URL.Query().Get("email"),
  35. Password: &password,
  36. PasswordAgain: &passwordAgain,
  37. },
  38. })
  39. if err != nil {
  40. w.WriteHeader(http.StatusBadRequest)
  41. w.Write([]byte(err.Error()))
  42. }
  43. w.WriteHeader(http.StatusOK)
  44. w.Write([]byte(res.Token.Token))
  45. })
  46. }
  47. func Logout(client proto.UsersClient) http.HandlerFunc {
  48. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  49. _, err := client.Logout(r.Context(), &proto.LogoutRequest{
  50. Token: r.Context().Value("token").(*proto.UserToken),
  51. })
  52. if err != nil {
  53. w.WriteHeader(http.StatusBadRequest)
  54. w.Write([]byte(err.Error()))
  55. }
  56. w.WriteHeader(http.StatusOK)
  57. })
  58. }
  59. func Authorize(client proto.UsersClient, serverSecret *string, next http.HandlerFunc) http.HandlerFunc {
  60. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  61. res, err := client.Authorize(r.Context(), &proto.AuthorizeRequest{
  62. Secret: *serverSecret,
  63. Token: &proto.UserToken{
  64. Token: r.URL.Query().Get("token"),
  65. },
  66. })
  67. if err != nil {
  68. w.WriteHeader(http.StatusBadRequest)
  69. w.Write([]byte(err.Error()))
  70. }
  71. ctx := context.WithValue(r.Context(), "user", res.User)
  72. ctx = context.WithValue(ctx, "roles", res.Roles)
  73. ctx = context.WithValue(ctx, "token", &proto.UserToken{
  74. Token: r.URL.Query().Get("token"),
  75. })
  76. next(w, r.WithContext(ctx))
  77. })
  78. }
  79. func ResetPassword(client proto.UsersClient, endpoint string) http.HandlerFunc {
  80. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  81. res, err := client.ResetPassword(r.Context(), &proto.ResetPasswordRequest{
  82. Form: &proto.UserForm{
  83. Email: r.URL.Query().Get("email"),
  84. },
  85. })
  86. if err != nil {
  87. w.WriteHeader(http.StatusBadRequest)
  88. w.Write([]byte(err.Error()))
  89. }
  90. w.WriteHeader(http.StatusOK)
  91. w.Write([]byte(fmt.Sprintf(
  92. "Please follow this link to update your password: %s/change_password?token=%s\n",
  93. endpoint, res.Token.Token)))
  94. })
  95. }
  96. func ChangePassword(client proto.UsersClient) http.HandlerFunc {
  97. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  98. password, passwordAgain :=
  99. r.URL.Query().Get("password"),
  100. r.URL.Query().Get("password_again")
  101. _, err := client.ChangePassword(r.Context(), &proto.ChangePasswordRequest{
  102. Form: &proto.UserForm{
  103. Email: r.URL.Query().Get("email"),
  104. Password: &password,
  105. PasswordAgain: &passwordAgain,
  106. },
  107. })
  108. if err != nil {
  109. w.WriteHeader(http.StatusBadRequest)
  110. w.Write([]byte(err.Error()))
  111. }
  112. w.WriteHeader(http.StatusOK)
  113. })
  114. }