zTinyPasswd.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package zTinyPasswd
  2. import (
  3. "os"
  4. "git.swzry.com/zry/pathutils"
  5. "encoding/json"
  6. "io/ioutil"
  7. "sync"
  8. "golang.org/x/crypto/bcrypt"
  9. "errors"
  10. )
  11. type UserData struct {
  12. username string `json:"-"`
  13. Password string `json:"password"`
  14. RoleName string `json:"role"`
  15. External string `json:"extdata"`
  16. }
  17. type UserDatabase struct {
  18. UserMap map[string]UserData `json:"user_map"`
  19. External map[string]string `json:"external_data"`
  20. }
  21. type PasswdManager struct{
  22. fromFileName bool
  23. storage_file *os.File
  24. storage UserDatabase
  25. stlock sync.RWMutex
  26. }
  27. func NewPasswdManagerFromFileName(filename string) (*PasswdManager, error) {
  28. ex,_ := pathutils.PathExists(filename)
  29. var file *os.File
  30. var err error
  31. if !ex {
  32. file,err = os.Create(filename)
  33. if err != nil {
  34. return nil, err
  35. }
  36. }else {
  37. file,err = os.OpenFile(filename, os.O_RDWR, 0666)
  38. if err != nil{
  39. return nil, err
  40. }
  41. }
  42. p, err := NewPasswdManagerFromFileObject(file)
  43. if err == nil {
  44. p.fromFileName = true
  45. }
  46. return p, err
  47. }
  48. func NewPasswdManagerFromFileObject(file *os.File) (*PasswdManager, error) {
  49. p := &PasswdManager{
  50. storage_file: file,
  51. fromFileName: false,
  52. }
  53. jdata,err := ioutil.ReadAll(p.storage_file)
  54. if err != nil{
  55. return nil, err
  56. }
  57. if len(jdata) == 0{
  58. p.storage = UserDatabase{
  59. UserMap: make(map[string]UserData),
  60. External: make(map[string]string),
  61. }
  62. p.sync()
  63. }else {
  64. err = json.Unmarshal(jdata, &p.storage)
  65. if err != nil {
  66. return nil,err
  67. }
  68. }
  69. for k,v := range p.storage.UserMap {
  70. v.username = k
  71. }
  72. return p, nil
  73. }
  74. func (this *PasswdManager) Close() {
  75. if this.fromFileName {
  76. this.storage_file.Close()
  77. }
  78. }
  79. func (this *PasswdManager) Add(username string, passwd []byte, role string) error {
  80. brpasswd,err := bcrypt.GenerateFromPassword(passwd, 10)
  81. if err != nil{
  82. return err
  83. }
  84. passwdinstr := string(brpasswd)
  85. this.stlock.Lock()
  86. this.storage.UserMap[username] = UserData{
  87. username: username,
  88. Password: passwdinstr,
  89. RoleName: role,
  90. External: "",
  91. }
  92. this.stlock.Unlock()
  93. this.sync()
  94. return nil
  95. }
  96. func (this *PasswdManager) sync() error {
  97. this.stlock.RLock()
  98. defer this.stlock.RUnlock()
  99. jdata,err := json.MarshalIndent(this.storage, "", "\t")
  100. if err != nil {
  101. return err
  102. }
  103. this.storage_file.Truncate(0)
  104. this.storage_file.Seek(0,0)
  105. this.storage_file.Write(jdata)
  106. this.storage_file.Sync()
  107. return nil
  108. }
  109. func (this *PasswdManager) GetUserInfo(username string) (role string, external string){
  110. this.stlock.RLock()
  111. defer this.stlock.RUnlock()
  112. v, ok := this.storage.UserMap[username]
  113. if ok {
  114. rs := v.RoleName
  115. es := v.External
  116. return rs,es
  117. }else {
  118. return "",""
  119. }
  120. }
  121. func (this *PasswdManager) _getUser(username string) *UserData {
  122. this.stlock.RLock()
  123. defer this.stlock.RUnlock()
  124. v, ok := this.storage.UserMap[username]
  125. if ok {
  126. return &v
  127. }else {
  128. return nil
  129. }
  130. }
  131. func (this *PasswdManager) ListUser() []string {
  132. this.stlock.RLock()
  133. defer this.stlock.RUnlock()
  134. ud := make([]string, len(this.storage.UserMap))
  135. i := 0
  136. for k := range this.storage.UserMap {
  137. ud[i] = k
  138. i++
  139. }
  140. return ud
  141. }
  142. func (this *PasswdManager) Auth(username string, passwd []byte) bool {
  143. u := this._getUser(username)
  144. if u == nil {
  145. return false
  146. }
  147. this.stlock.RLock()
  148. defer this.stlock.RUnlock()
  149. err := bcrypt.CompareHashAndPassword([]byte(u.Password), passwd)
  150. if err != nil{
  151. return false
  152. }
  153. return true
  154. }
  155. func (this *PasswdManager) ChangePassword(username string, newpass []byte) error {
  156. brpasswd,err := bcrypt.GenerateFromPassword(newpass, 10)
  157. if err != nil{
  158. return err
  159. }
  160. passwdinstr := string(brpasswd)
  161. user := this._getUser(username)
  162. if user == nil{
  163. return errors.New("NoSuchUser")
  164. }
  165. this.stlock.Lock()
  166. user.Password = passwdinstr
  167. this.stlock.Unlock()
  168. this.sync()
  169. return nil
  170. }
  171. func (this *PasswdManager) ChangeExternal(username string, new_external string) error {
  172. user := this._getUser(username)
  173. if user == nil{
  174. return errors.New("NoSuchUser")
  175. }
  176. this.stlock.Lock()
  177. user.External = new_external
  178. this.stlock.Unlock()
  179. this.sync()
  180. return nil
  181. }
  182. func (this *PasswdManager) ChangeRole(username string, new_role string) error {
  183. user := this._getUser(username)
  184. if user == nil{
  185. return errors.New("NoSuchUser")
  186. }
  187. this.stlock.Lock()
  188. user.RoleName = new_role
  189. this.stlock.Unlock()
  190. this.sync()
  191. return nil
  192. }
  193. func (this *PasswdManager) DeleteUser(username string) error {
  194. this.stlock.Lock()
  195. defer this.stlock.Unlock()
  196. _, ok := this.storage.UserMap[username]
  197. if ok{
  198. delete(this.storage.UserMap, username)
  199. return nil
  200. }else {
  201. return errors.New("NoSuchUser")
  202. }
  203. }
  204. func (this *PasswdManager) RenameUser(oldname string, new_name string) error {
  205. user := this._getUser(oldname)
  206. if user == nil{
  207. return errors.New("NoSuchUser")
  208. }
  209. oldn := user.username
  210. upwd := user.Password
  211. role := user.RoleName
  212. uext := user.External
  213. this.DeleteUser(oldn)
  214. this.Add(new_name, []byte(""), role)
  215. this.stlock.Lock()
  216. nu := this.storage.UserMap[new_name]
  217. nu.Password = upwd
  218. nu.External = uext
  219. this.stlock.Unlock()
  220. return nil
  221. }
  222. func (this *PasswdManager) UserExists(username string) bool {
  223. u := this._getUser(username)
  224. if u == nil{
  225. return false
  226. }else {
  227. return true
  228. }
  229. }