idm.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package dao
  2. /*
  3. This is the identity management system for the AutoRest service. Here you will find all methods regarding the identity of a user,
  4. authentication and authorisation.
  5. */
  6. import (
  7. "crypto/sha1"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "time"
  12. "golang.org/x/crypto/pbkdf2"
  13. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/internal/slicesutils"
  14. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/model"
  15. )
  16. //IDM the idm cache struct with users and salts
  17. type IDM struct {
  18. users map[string]string
  19. salts map[string][]byte
  20. }
  21. const hashPrefix = "hash:"
  22. // time to reload all users
  23. const userReloadPeriod = 1 * time.Hour
  24. var idm IDM
  25. //NewIDM creating a new idm object
  26. func NewIDM() IDM {
  27. return IDM{
  28. users: make(map[string]string),
  29. salts: make(map[string][]byte),
  30. }
  31. }
  32. //GetIDM getting the actual idm object
  33. func GetIDM() IDM {
  34. return idm
  35. }
  36. //SetIDM setting the actual idm object
  37. func SetIDM(newIdm IDM) {
  38. idm = newIdm
  39. }
  40. //BuildPasswordHash building a hash value of the password
  41. func BuildPasswordHash(password string, salt []byte) string {
  42. if !strings.HasPrefix(password, hashPrefix) {
  43. hash := pbkdf2.Key([]byte(password), salt, 4096, 32, sha1.New)
  44. // hash := md5.Sum([]byte(password))
  45. password = fmt.Sprintf("%s:%x", hashPrefix, hash)
  46. }
  47. return password
  48. }
  49. //InitIDM initialise the local idm system
  50. func (i *IDM) InitIDM() {
  51. i.initUsers()
  52. }
  53. func (i *IDM) initUsers() {
  54. i.reloadUsers()
  55. go func() {
  56. background := time.NewTicker(userReloadPeriod)
  57. for range background.C {
  58. i.reloadUsers()
  59. }
  60. }()
  61. }
  62. //GetSalt getting the salt for a user.
  63. func (i *IDM) GetSalt(username string) ([]byte, bool) {
  64. username = strings.ToLower(username)
  65. salt, ok := i.salts[username]
  66. if ok {
  67. return salt, true
  68. }
  69. return []byte{}, false
  70. }
  71. func (i *IDM) reloadUsers() {
  72. users, err := GetStorage().GetUsers()
  73. if err != nil {
  74. log.Alertf("error loading users: %v", err)
  75. return
  76. }
  77. localUsers := make(map[string]string)
  78. localSalts := make(map[string][]byte)
  79. for _, user := range users {
  80. username := user.Name
  81. password := user.Password
  82. salt := user.Salt
  83. localSalts[username] = salt
  84. localUsers[username] = BuildPasswordHash(password, salt)
  85. }
  86. i.users = localUsers
  87. i.salts = localSalts
  88. if len(i.users) == 0 {
  89. admin := model.User{
  90. Name: "admin",
  91. Firstname: "",
  92. Lastname: "Admin",
  93. Password: "admin",
  94. Admin: true,
  95. Roles: []string{"admin"},
  96. }
  97. user, err := GetStorage().AddUser(admin)
  98. if err != nil {
  99. log.Alertf("error adding user: %v", err)
  100. return
  101. }
  102. i.addUserToMap(user)
  103. editor := model.User{
  104. Name: "editor",
  105. Firstname: "",
  106. Lastname: "Editor",
  107. Password: "editor",
  108. Admin: false,
  109. Guest: false,
  110. Roles: []string{"edit"},
  111. }
  112. user, err = GetStorage().AddUser(editor)
  113. if err != nil {
  114. log.Alertf("error adding user: %v", err)
  115. return
  116. }
  117. i.addUserToMap(user)
  118. guest := model.User{
  119. Name: "guest",
  120. Firstname: "",
  121. Lastname: "Guest",
  122. Password: "guest",
  123. Admin: false,
  124. Guest: true,
  125. Roles: []string{"read"},
  126. }
  127. user, err = GetStorage().AddUser(guest)
  128. if err != nil {
  129. log.Alertf("error adding user: %v", err)
  130. return
  131. }
  132. i.addUserToMap(user)
  133. }
  134. }
  135. //CheckUser checking username and password... returns true if the user is active and the password for this user is correct
  136. func (i *IDM) CheckUser(username string, password string) bool {
  137. username = strings.ToLower(username)
  138. pwd, ok := i.users[username]
  139. if ok {
  140. if pwd == password {
  141. return true
  142. }
  143. user, ok := GetStorage().GetUser(username)
  144. if ok {
  145. if user.Password == password {
  146. //change password on another node
  147. i.addUserToMap(user)
  148. return true
  149. }
  150. }
  151. }
  152. if !ok {
  153. user, ok := GetStorage().GetUser(username)
  154. if ok {
  155. if user.Password == password {
  156. //change password on another node
  157. i.addUserToMap(user)
  158. return true
  159. }
  160. }
  161. }
  162. return false
  163. }
  164. //UserInRoles is a user in the given role
  165. func (i *IDM) UserInRoles(username string, roles []string) bool {
  166. user, ok := GetStorage().GetUser(username)
  167. if !ok {
  168. return false
  169. }
  170. for _, role := range roles {
  171. if slicesutils.Contains(user.Roles, role) {
  172. return true
  173. }
  174. }
  175. return false
  176. }
  177. // AddUser adding a new user to the system
  178. func (i *IDM) AddUser(user model.User) (model.User, error) {
  179. if user.Name == "" {
  180. return model.User{}, errors.New("username should not be empty")
  181. }
  182. user.Name = strings.ToLower(user.Name)
  183. _, ok := GetStorage().GetUser(user.Name)
  184. if ok {
  185. return model.User{}, errors.New("username already exists")
  186. }
  187. user, err := GetStorage().AddUser(user)
  188. if err != nil {
  189. return model.User{}, err
  190. }
  191. return user, nil
  192. }
  193. //DeleteUser deletes a user from the idm
  194. func (i *IDM) DeleteUser(username string) error {
  195. err := GetStorage().DeleteUser(username)
  196. if err != nil {
  197. return err
  198. }
  199. delete(i.users, username)
  200. delete(i.salts, username)
  201. return nil
  202. }
  203. // ChangePWD changes the apssword of a single user
  204. func (i *IDM) ChangePWD(username string, newpassword string, oldpassword string) error {
  205. if username == "" {
  206. return errors.New("username should not be empty")
  207. }
  208. username = strings.ToLower(username)
  209. pwd, ok := i.users[username]
  210. if !ok {
  211. return errors.New("username not registered")
  212. }
  213. usermodel, ok := GetStorage().GetUser(username)
  214. if !ok {
  215. return errors.New("username not registered")
  216. }
  217. oldpassword = BuildPasswordHash(oldpassword, usermodel.Salt)
  218. if pwd != oldpassword {
  219. return errors.New("actual password incorrect")
  220. }
  221. user, err := GetStorage().ChangePWD(username, newpassword)
  222. if err != nil {
  223. return err
  224. }
  225. i.addUserToMap(user)
  226. return nil
  227. }
  228. func (i *IDM) addUserToMap(user model.User) {
  229. i.users[user.Name] = user.Password
  230. i.salts[user.Name] = user.Salt
  231. }