users.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package api
  5. import (
  6. "context"
  7. "encoding/base64"
  8. "fmt"
  9. nethttp "net/http"
  10. "net/http/cookiejar"
  11. "sync"
  12. "github.com/influxdata/influxdb-client-go/v2/api/http"
  13. "github.com/influxdata/influxdb-client-go/v2/domain"
  14. "golang.org/x/net/publicsuffix"
  15. )
  16. // UsersAPI provides methods for managing users in a InfluxDB server
  17. type UsersAPI interface {
  18. // GetUsers returns all users
  19. GetUsers(ctx context.Context) (*[]domain.User, error)
  20. // FindUserByID returns user with userID
  21. FindUserByID(ctx context.Context, userID string) (*domain.User, error)
  22. // FindUserByName returns user with name userName
  23. FindUserByName(ctx context.Context, userName string) (*domain.User, error)
  24. // CreateUser creates new user
  25. CreateUser(ctx context.Context, user *domain.User) (*domain.User, error)
  26. // CreateUserWithName creates new user with userName
  27. CreateUserWithName(ctx context.Context, userName string) (*domain.User, error)
  28. // UpdateUser updates user
  29. UpdateUser(ctx context.Context, user *domain.User) (*domain.User, error)
  30. // UpdateUserPassword sets password for a user
  31. UpdateUserPassword(ctx context.Context, user *domain.User, password string) error
  32. // UpdateUserPasswordWithID sets password for a user with userID
  33. UpdateUserPasswordWithID(ctx context.Context, userID string, password string) error
  34. // DeleteUserWithID deletes an user with userID
  35. DeleteUserWithID(ctx context.Context, userID string) error
  36. // DeleteUser deletes an user
  37. DeleteUser(ctx context.Context, user *domain.User) error
  38. // Me returns actual user
  39. Me(ctx context.Context) (*domain.User, error)
  40. // MeUpdatePassword set password of actual user
  41. MeUpdatePassword(ctx context.Context, oldPassword, newPassword string) error
  42. // SignIn exchanges username and password credentials to establish an authenticated session with the InfluxDB server. The Client's authentication token is then ignored, it can be empty.
  43. SignIn(ctx context.Context, username, password string) error
  44. // SignOut signs out previously signed-in user
  45. SignOut(ctx context.Context) error
  46. }
  47. // usersAPI implements UsersAPI
  48. type usersAPI struct {
  49. apiClient *domain.Client
  50. httpService http.Service
  51. httpClient *nethttp.Client
  52. deleteCookieJar bool
  53. lock sync.Mutex
  54. }
  55. // NewUsersAPI creates new instance of UsersAPI
  56. func NewUsersAPI(apiClient *domain.Client, httpService http.Service, httpClient *nethttp.Client) UsersAPI {
  57. return &usersAPI{
  58. apiClient: apiClient,
  59. httpService: httpService,
  60. httpClient: httpClient,
  61. }
  62. }
  63. func (u *usersAPI) GetUsers(ctx context.Context) (*[]domain.User, error) {
  64. params := &domain.GetUsersParams{}
  65. response, err := u.apiClient.GetUsers(ctx, params)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return userResponsesToUsers(response.Users), nil
  70. }
  71. func (u *usersAPI) FindUserByID(ctx context.Context, userID string) (*domain.User, error) {
  72. params := &domain.GetUsersIDAllParams{
  73. UserID: userID,
  74. }
  75. response, err := u.apiClient.GetUsersID(ctx, params)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return userResponseToUser(response), nil
  80. }
  81. func (u *usersAPI) FindUserByName(ctx context.Context, userName string) (*domain.User, error) {
  82. users, err := u.GetUsers(ctx)
  83. if err != nil {
  84. return nil, err
  85. }
  86. var user *domain.User
  87. for _, u := range *users {
  88. if u.Name == userName {
  89. user = &u
  90. break
  91. }
  92. }
  93. if user == nil {
  94. return nil, fmt.Errorf("user '%s' not found", userName)
  95. }
  96. return user, nil
  97. }
  98. func (u *usersAPI) CreateUserWithName(ctx context.Context, userName string) (*domain.User, error) {
  99. user := &domain.User{Name: userName}
  100. return u.CreateUser(ctx, user)
  101. }
  102. func (u *usersAPI) CreateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
  103. params := &domain.PostUsersAllParams{
  104. Body: domain.PostUsersJSONRequestBody(*user),
  105. }
  106. response, err := u.apiClient.PostUsers(ctx, params)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return userResponseToUser(response), nil
  111. }
  112. func (u *usersAPI) UpdateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
  113. params := &domain.PatchUsersIDAllParams{
  114. Body: domain.PatchUsersIDJSONRequestBody(*user),
  115. UserID: *user.Id,
  116. }
  117. response, err := u.apiClient.PatchUsersID(ctx, params)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return userResponseToUser(response), nil
  122. }
  123. func (u *usersAPI) UpdateUserPassword(ctx context.Context, user *domain.User, password string) error {
  124. return u.UpdateUserPasswordWithID(ctx, *user.Id, password)
  125. }
  126. func (u *usersAPI) UpdateUserPasswordWithID(ctx context.Context, userID string, password string) error {
  127. params := &domain.PostUsersIDPasswordAllParams{
  128. UserID: userID,
  129. Body: domain.PostUsersIDPasswordJSONRequestBody(domain.PasswordResetBody{Password: password}),
  130. }
  131. return u.apiClient.PostUsersIDPassword(ctx, params)
  132. }
  133. func (u *usersAPI) DeleteUser(ctx context.Context, user *domain.User) error {
  134. return u.DeleteUserWithID(ctx, *user.Id)
  135. }
  136. func (u *usersAPI) DeleteUserWithID(ctx context.Context, userID string) error {
  137. params := &domain.DeleteUsersIDAllParams{
  138. UserID: userID,
  139. }
  140. return u.apiClient.DeleteUsersID(ctx, params)
  141. }
  142. func (u *usersAPI) Me(ctx context.Context) (*domain.User, error) {
  143. params := &domain.GetMeParams{}
  144. response, err := u.apiClient.GetMe(ctx, params)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return userResponseToUser(response), nil
  149. }
  150. func (u *usersAPI) MeUpdatePassword(ctx context.Context, oldPassword, newPassword string) error {
  151. u.lock.Lock()
  152. defer u.lock.Unlock()
  153. me, err := u.Me(ctx)
  154. if err != nil {
  155. return err
  156. }
  157. creds := base64.StdEncoding.EncodeToString([]byte(me.Name + ":" + oldPassword))
  158. auth := u.httpService.Authorization()
  159. defer u.httpService.SetAuthorization(auth)
  160. u.httpService.SetAuthorization("Basic " + creds)
  161. params := &domain.PutMePasswordAllParams{
  162. Body: domain.PutMePasswordJSONRequestBody(domain.PasswordResetBody{Password: newPassword}),
  163. }
  164. return u.apiClient.PutMePassword(ctx, params)
  165. }
  166. func (u *usersAPI) SignIn(ctx context.Context, username, password string) error {
  167. u.lock.Lock()
  168. defer u.lock.Unlock()
  169. if u.httpClient.Jar == nil {
  170. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  171. if err != nil {
  172. return err
  173. }
  174. u.httpClient.Jar = jar
  175. u.deleteCookieJar = true
  176. }
  177. creds := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  178. u.httpService.SetAuthorization("Basic " + creds)
  179. defer u.httpService.SetAuthorization("")
  180. return u.apiClient.PostSignin(ctx, &domain.PostSigninParams{})
  181. }
  182. func (u *usersAPI) SignOut(ctx context.Context) error {
  183. u.lock.Lock()
  184. defer u.lock.Unlock()
  185. err := u.apiClient.PostSignout(ctx, &domain.PostSignoutParams{})
  186. if u.deleteCookieJar {
  187. u.httpClient.Jar = nil
  188. }
  189. return err
  190. }
  191. func userResponseToUser(ur *domain.UserResponse) *domain.User {
  192. if ur == nil {
  193. return nil
  194. }
  195. user := &domain.User{
  196. Id: ur.Id,
  197. Name: ur.Name,
  198. Status: userResponseStatusToUserStatus(ur.Status),
  199. }
  200. return user
  201. }
  202. func userResponseStatusToUserStatus(urs *domain.UserResponseStatus) *domain.UserStatus {
  203. if urs == nil {
  204. return nil
  205. }
  206. us := domain.UserStatus(*urs)
  207. return &us
  208. }
  209. func userResponsesToUsers(urs *[]domain.UserResponse) *[]domain.User {
  210. if urs == nil {
  211. return nil
  212. }
  213. us := make([]domain.User, len(*urs))
  214. for i, ur := range *urs {
  215. us[i] = *userResponseToUser(&ur)
  216. }
  217. return &us
  218. }