user.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // AuthCheckResponse is the user model response that is returned if the
  12. // user is logged in
  13. type AuthCheckResponse models.UserExternal
  14. // AuthCheck performs a check to ensure that the user is logged in
  15. func (c *Client) AuthCheck(ctx context.Context) (*AuthCheckResponse, error) {
  16. req, err := http.NewRequest(
  17. "GET",
  18. fmt.Sprintf("%s/users/current", c.BaseURL),
  19. nil,
  20. )
  21. if err != nil {
  22. return nil, err
  23. }
  24. req = req.WithContext(ctx)
  25. bodyResp := &AuthCheckResponse{}
  26. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  27. if httpErr != nil {
  28. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  29. }
  30. return nil, err
  31. }
  32. return bodyResp, nil
  33. }
  34. // LoginRequest is the email/password associated with a login request
  35. type LoginRequest struct {
  36. Email string `json:"email"`
  37. Password string `json:"password"`
  38. }
  39. // LoginResponse is the user model response that is returned after successfully
  40. // logging in
  41. type LoginResponse models.UserExternal
  42. // Login authorizes the user and grants them a cookie-based session
  43. func (c *Client) Login(ctx context.Context, loginRequest *LoginRequest) (*LoginResponse, error) {
  44. data, err := json.Marshal(loginRequest)
  45. if err != nil {
  46. return nil, err
  47. }
  48. req, err := http.NewRequest(
  49. "POST",
  50. fmt.Sprintf("%s/login", c.BaseURL),
  51. strings.NewReader(string(data)),
  52. )
  53. if err != nil {
  54. return nil, err
  55. }
  56. req = req.WithContext(ctx)
  57. bodyResp := &LoginResponse{}
  58. if httpErr, err := c.sendRequest(req, bodyResp, false); httpErr != nil || err != nil {
  59. if httpErr != nil {
  60. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  61. }
  62. return nil, err
  63. }
  64. return bodyResp, nil
  65. }
  66. // Logout logs the user out and deauthorizes the cookie-based session
  67. func (c *Client) Logout(ctx context.Context) error {
  68. req, err := http.NewRequest(
  69. "POST",
  70. fmt.Sprintf("%s/logout", c.BaseURL),
  71. nil,
  72. )
  73. if err != nil {
  74. return err
  75. }
  76. req = req.WithContext(ctx)
  77. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  78. if httpErr != nil {
  79. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  80. }
  81. return err
  82. }
  83. return nil
  84. }
  85. // CreateUserRequest is the email/password associated with creating a user
  86. type CreateUserRequest struct {
  87. Email string `json:"email"`
  88. Password string `json:"password"`
  89. }
  90. // CreateUserResponse is the user model response that is returned after successfully
  91. // creating a user
  92. type CreateUserResponse models.UserExternal
  93. // CreateUser will create the user, authorize the user and grant them a cookie-based session
  94. func (c *Client) CreateUser(
  95. ctx context.Context,
  96. createUserRequest *CreateUserRequest,
  97. ) (*CreateUserResponse, error) {
  98. data, err := json.Marshal(createUserRequest)
  99. if err != nil {
  100. return nil, err
  101. }
  102. req, err := http.NewRequest(
  103. "POST",
  104. fmt.Sprintf("%s/users", c.BaseURL),
  105. strings.NewReader(string(data)),
  106. )
  107. if err != nil {
  108. return nil, err
  109. }
  110. req = req.WithContext(ctx)
  111. bodyResp := &CreateUserResponse{}
  112. if httpErr, err := c.sendRequest(req, bodyResp, false); httpErr != nil || err != nil {
  113. if httpErr != nil {
  114. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  115. }
  116. return nil, err
  117. }
  118. return bodyResp, nil
  119. }
  120. // ListUserProjectsResponse is the list of projects returned
  121. type ListUserProjectsResponse []*types.Project
  122. // ListUserProjects returns a list of projects associated with a user
  123. func (c *Client) ListUserProjects(ctx context.Context, userID uint) (ListUserProjectsResponse, error) {
  124. req, err := http.NewRequest(
  125. "GET",
  126. fmt.Sprintf("%s/projects", c.BaseURL),
  127. nil,
  128. )
  129. if err != nil {
  130. return nil, err
  131. }
  132. req = req.WithContext(ctx)
  133. bodyResp := ListUserProjectsResponse{}
  134. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  135. if httpErr != nil {
  136. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  137. }
  138. return nil, err
  139. }
  140. return bodyResp, nil
  141. }
  142. // DeleteUserRequest is the password needed to verify a user should be deleted
  143. type DeleteUserRequest struct {
  144. Password string `json:"password"`
  145. }
  146. // DeleteUser deletes a user of a given user id
  147. func (c *Client) DeleteUser(
  148. ctx context.Context,
  149. userID uint,
  150. deleteUserRequest *DeleteUserRequest,
  151. ) error {
  152. data, err := json.Marshal(deleteUserRequest)
  153. if err != nil {
  154. return err
  155. }
  156. req, err := http.NewRequest(
  157. "DELETE",
  158. fmt.Sprintf("%s/users/%d", c.BaseURL, userID),
  159. strings.NewReader(string(data)),
  160. )
  161. if err != nil {
  162. return err
  163. }
  164. req = req.WithContext(ctx)
  165. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  166. if httpErr != nil {
  167. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  168. }
  169. return err
  170. }
  171. return nil
  172. }