auth.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/go-playground/validator/v10"
  6. "github.com/gravitl/netmaker/database"
  7. "github.com/gravitl/netmaker/functions"
  8. "github.com/gravitl/netmaker/models"
  9. "golang.org/x/crypto/bcrypt"
  10. )
  11. // HasAdmin - checks if server has an admin
  12. func HasAdmin() (bool, error) {
  13. collection, err := database.FetchRecords(database.USERS_TABLE_NAME)
  14. if err != nil {
  15. if database.IsEmptyRecord(err) {
  16. return false, nil
  17. } else {
  18. return true, err
  19. }
  20. }
  21. for _, value := range collection { // filter for isadmin true
  22. var user models.User
  23. err = json.Unmarshal([]byte(value), &user)
  24. if err != nil {
  25. continue
  26. }
  27. if user.IsAdmin {
  28. return true, nil
  29. }
  30. }
  31. return false, err
  32. }
  33. // GetUser - gets a user
  34. func GetUser(username string) (models.ReturnUser, error) {
  35. var user models.ReturnUser
  36. record, err := database.FetchRecord(database.USERS_TABLE_NAME, username)
  37. if err != nil {
  38. return user, err
  39. }
  40. if err = json.Unmarshal([]byte(record), &user); err != nil {
  41. return models.ReturnUser{}, err
  42. }
  43. return user, err
  44. }
  45. // GetUsers - gets users
  46. func GetUsers() ([]models.ReturnUser, error) {
  47. var users []models.ReturnUser
  48. collection, err := database.FetchRecords(database.USERS_TABLE_NAME)
  49. if err != nil {
  50. return users, err
  51. }
  52. for _, value := range collection {
  53. var user models.ReturnUser
  54. err = json.Unmarshal([]byte(value), &user)
  55. if err != nil {
  56. continue // get users
  57. }
  58. users = append(users, user)
  59. }
  60. return users, err
  61. }
  62. // CreateUser - creates a user
  63. func CreateUser(user models.User) (models.User, error) {
  64. // check if user exists
  65. if _, err := GetUser(user.UserName); err == nil {
  66. return models.User{}, errors.New("user exists")
  67. }
  68. var err = ValidateUser(user)
  69. if err != nil {
  70. return models.User{}, err
  71. }
  72. // encrypt that password so we never see it again
  73. hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 5)
  74. if err != nil {
  75. return user, err
  76. }
  77. // set password to encrypted password
  78. user.Password = string(hash)
  79. tokenString, _ := functions.CreateUserJWT(user.UserName, user.Networks, user.IsAdmin)
  80. if tokenString == "" {
  81. // returnErrorResponse(w, r, errorResponse)
  82. return user, err
  83. }
  84. // connect db
  85. data, err := json.Marshal(&user)
  86. if err != nil {
  87. return user, err
  88. }
  89. err = database.Insert(user.UserName, string(data), database.USERS_TABLE_NAME)
  90. return user, err
  91. }
  92. // CreateAdmin - creates an admin user
  93. func CreateAdmin(admin models.User) (models.User, error) {
  94. hasadmin, err := HasAdmin()
  95. if err != nil {
  96. return models.User{}, err
  97. }
  98. if hasadmin {
  99. return models.User{}, errors.New("admin user already exists")
  100. }
  101. admin.IsAdmin = true
  102. return CreateUser(admin)
  103. }
  104. // UpdateUser - updates a given user
  105. func UpdateUser(userchange models.User, user models.User) (models.User, error) {
  106. //check if user exists
  107. if _, err := GetUser(user.UserName); err != nil {
  108. return models.User{}, err
  109. }
  110. err := ValidateUser(userchange)
  111. if err != nil {
  112. return models.User{}, err
  113. }
  114. queryUser := user.UserName
  115. if userchange.UserName != "" {
  116. user.UserName = userchange.UserName
  117. }
  118. if len(userchange.Networks) > 0 {
  119. user.Networks = userchange.Networks
  120. }
  121. if userchange.Password != "" {
  122. // encrypt that password so we never see it again
  123. hash, err := bcrypt.GenerateFromPassword([]byte(userchange.Password), 5)
  124. if err != nil {
  125. return userchange, err
  126. }
  127. // set password to encrypted password
  128. userchange.Password = string(hash)
  129. user.Password = userchange.Password
  130. }
  131. if err = database.DeleteRecord(database.USERS_TABLE_NAME, queryUser); err != nil {
  132. return models.User{}, err
  133. }
  134. data, err := json.Marshal(&user)
  135. if err != nil {
  136. return models.User{}, err
  137. }
  138. if err = database.Insert(user.UserName, string(data), database.USERS_TABLE_NAME); err != nil {
  139. return models.User{}, err
  140. }
  141. functions.PrintUserLog(models.NODE_SERVER_NAME, "updated user "+queryUser, 1)
  142. return user, nil
  143. }
  144. // ValidateUser - validates a user model
  145. func ValidateUser(user models.User) error {
  146. v := validator.New()
  147. err := v.Struct(user)
  148. if err != nil {
  149. for _, e := range err.(validator.ValidationErrors) {
  150. Log(e.Error(), 2)
  151. }
  152. }
  153. return err
  154. }
  155. // DeleteUser - deletes a given user
  156. func DeleteUser(user string) (bool, error) {
  157. if userRecord, err := database.FetchRecord(database.USERS_TABLE_NAME, user); err != nil || len(userRecord) == 0 {
  158. return false, errors.New("user does not exist")
  159. }
  160. err := database.DeleteRecord(database.USERS_TABLE_NAME, user)
  161. if err != nil {
  162. return false, err
  163. }
  164. return true, nil
  165. }