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.

136 lines
3.5 KiB

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