system.resetpw.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "path/filepath"
  7. auth "imuslab.com/arozos/mod/auth"
  8. fs "imuslab.com/arozos/mod/filesystem"
  9. "imuslab.com/arozos/mod/utils"
  10. )
  11. /*
  12. Password Reset Module
  13. This module exists to serve the password restart page with security check
  14. */
  15. func system_resetpw_init() {
  16. http.HandleFunc("/system/reset/validateResetKey", system_resetpw_validateResetKeyHandler)
  17. http.HandleFunc("/system/reset/confirmPasswordReset", system_resetpw_confirmReset)
  18. }
  19. // Validate if the ysername and rkey is valid
  20. func system_resetpw_validateResetKeyHandler(w http.ResponseWriter, r *http.Request) {
  21. username, err := utils.PostPara(r, "username")
  22. if err != nil {
  23. utils.SendErrorResponse(w, "Invalid username or key")
  24. return
  25. }
  26. rkey, err := utils.PostPara(r, "rkey")
  27. if err != nil {
  28. utils.SendErrorResponse(w, "Invalid username or key")
  29. return
  30. }
  31. if username == "" || rkey == "" {
  32. utils.SendErrorResponse(w, "Invalid username or rkey")
  33. return
  34. }
  35. //Check if the pair is valid
  36. err = system_resetpw_validateResetKey(username, rkey)
  37. if err != nil {
  38. utils.SendErrorResponse(w, err.Error())
  39. return
  40. }
  41. utils.SendOK(w)
  42. }
  43. func system_resetpw_confirmReset(w http.ResponseWriter, r *http.Request) {
  44. username, _ := utils.PostPara(r, "username")
  45. rkey, _ := utils.PostPara(r, "rkey")
  46. newpw, _ := utils.PostPara(r, "pw")
  47. if username == "" || rkey == "" || newpw == "" {
  48. utils.SendErrorResponse(w, "Internal Server Error")
  49. return
  50. }
  51. //Check user exists
  52. if !authAgent.UserExists(username) {
  53. utils.SendErrorResponse(w, "Username not exists")
  54. return
  55. }
  56. //Validate rkey
  57. err := system_resetpw_validateResetKey(username, rkey)
  58. if err != nil {
  59. utils.SendErrorResponse(w, err.Error())
  60. return
  61. }
  62. //OK to procced
  63. newHashedPassword := auth.Hash(newpw)
  64. err = sysdb.Write("auth", "passhash/"+username, newHashedPassword)
  65. if err != nil {
  66. utils.SendErrorResponse(w, err.Error())
  67. return
  68. }
  69. utils.SendOK(w)
  70. }
  71. func system_resetpw_validateResetKey(username string, key string) error {
  72. //Get current password from db
  73. passwordInDB := ""
  74. err := sysdb.Read("auth", "passhash/"+username, &passwordInDB)
  75. if err != nil {
  76. return err
  77. }
  78. //Get hashed user key
  79. hashedKey := auth.Hash(key)
  80. if passwordInDB != hashedKey {
  81. return errors.New("Invalid Password Reset Key")
  82. }
  83. return nil
  84. }
  85. func system_resetpw_handlePasswordReset(w http.ResponseWriter, r *http.Request) {
  86. //Check if the user click on this link with reset password key string. If not, ask the user to input one
  87. acc, err := utils.GetPara(r, "acc")
  88. if err != nil || acc == "" {
  89. system_resetpw_serveIdEnterInterface(w, r)
  90. return
  91. }
  92. resetkey, err := utils.GetPara(r, "rkey")
  93. if err != nil || resetkey == "" {
  94. system_resetpw_serveIdEnterInterface(w, r)
  95. return
  96. }
  97. //Check if the code is valid
  98. err = system_resetpw_validateResetKey(acc, resetkey)
  99. if err != nil {
  100. utils.SendErrorResponse(w, "Invalid username or resetKey")
  101. return
  102. }
  103. //OK. Create the New Password Entering UI
  104. vendorIconSrc := filepath.Join(vendorResRoot, "vendor_icon.png")
  105. if !fs.FileExists(vendorIconSrc) {
  106. vendorIconSrc = "./web/img/public/vendor_icon.png"
  107. }
  108. imageBase64, _ := utils.LoadImageAsBase64(vendorIconSrc)
  109. template, err := utils.Templateload("system/reset/resetPasswordTemplate.html", map[string]interface{}{
  110. "vendor_logo": imageBase64,
  111. "host_name": *host_name,
  112. "username": acc,
  113. "rkey": resetkey,
  114. })
  115. if err != nil {
  116. log.Fatal(err)
  117. }
  118. w.Header().Set("Content-Type", "text/html; charset=UTF-8")
  119. w.Write([]byte(template))
  120. }
  121. func system_resetpw_serveIdEnterInterface(w http.ResponseWriter, r *http.Request) {
  122. //Reset Key or Username not found, Serve entering interface
  123. imgsrc := filepath.Join(vendorResRoot, "vendor_icon.png")
  124. if !fs.FileExists(imgsrc) {
  125. imgsrc = "./web/img/public/vendor_icon.png"
  126. }
  127. imageBase64, _ := utils.LoadImageAsBase64(imgsrc)
  128. template, err := utils.Templateload("system/reset/resetCodeTemplate.html", map[string]interface{}{
  129. "vendor_logo": imageBase64,
  130. "host_name": *host_name,
  131. })
  132. if err != nil {
  133. log.Fatal(err)
  134. }
  135. w.Header().Set("Content-Type", "text/html; charset=UTF-8")
  136. w.Write([]byte(template))
  137. }