flash.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package login
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. )
  8. type FailCode int
  9. const (
  10. FailCodeSystemError FailCode = iota + 1
  11. FailCodeCompleteUserAuthFailed
  12. FailCodeUserNotFound
  13. FailCodeIncorrectAccountNameOrPassword
  14. FailCodeUserLocked
  15. FailCodeAccountIsRequired
  16. FailCodePasswordCannotBeEmpty
  17. FailCodePasswordNotMatch
  18. FailCodeIncorrectPassword
  19. FailCodeInvalidToken
  20. FailCodeTokenExpired
  21. FailCodeIncorrectTOTPCode
  22. FailCodeTOTPCodeHasBeenUsed
  23. FailCodeIncorrectRecaptchaToken
  24. )
  25. type WarnCode int
  26. const (
  27. WarnCodePasswordHasBeenChanged = iota + 1
  28. )
  29. type InfoCode int
  30. const (
  31. InfoCodePasswordSuccessfullyReset InfoCode = iota + 1
  32. InfoCodePasswordSuccessfullyChanged
  33. )
  34. const failCodeFlashCookieName = "qor5_fc_flash"
  35. const warnCodeFlashCookieName = "qor5_wc_flash"
  36. const infoCodeFlashCookieName = "qor5_ic_flash"
  37. func setFailCodeFlash(w http.ResponseWriter, c FailCode) {
  38. http.SetCookie(w, &http.Cookie{
  39. Name: failCodeFlashCookieName,
  40. Value: fmt.Sprint(c),
  41. Path: "/",
  42. HttpOnly: true,
  43. })
  44. }
  45. func setWarnCodeFlash(w http.ResponseWriter, c WarnCode) {
  46. http.SetCookie(w, &http.Cookie{
  47. Name: warnCodeFlashCookieName,
  48. Value: fmt.Sprint(c),
  49. Path: "/",
  50. HttpOnly: true,
  51. })
  52. }
  53. func setInfoCodeFlash(w http.ResponseWriter, c InfoCode) {
  54. http.SetCookie(w, &http.Cookie{
  55. Name: infoCodeFlashCookieName,
  56. Value: fmt.Sprint(c),
  57. Path: "/",
  58. HttpOnly: true,
  59. })
  60. }
  61. const customErrorMessageFlashCookieName = "qor5_cem_flash"
  62. func setCustomErrorMessageFlash(w http.ResponseWriter, f string) {
  63. http.SetCookie(w, &http.Cookie{
  64. Name: customErrorMessageFlashCookieName,
  65. Value: f,
  66. Path: "/",
  67. HttpOnly: true,
  68. })
  69. }
  70. const wrongLoginInputFlashCookieName = "qor5_wli_flash"
  71. type WrongLoginInputFlash struct {
  72. Account string
  73. Password string
  74. }
  75. func setWrongLoginInputFlash(w http.ResponseWriter, f WrongLoginInputFlash) {
  76. bf, _ := json.Marshal(&f)
  77. http.SetCookie(w, &http.Cookie{
  78. Name: wrongLoginInputFlashCookieName,
  79. Value: base64.StdEncoding.EncodeToString(bf),
  80. Path: "/",
  81. HttpOnly: true,
  82. Secure: true,
  83. })
  84. }
  85. const wrongForgetPasswordInputFlashCookieName = "qor5_wfpi_flash"
  86. type WrongForgetPasswordInputFlash struct {
  87. Account string
  88. TOTP string
  89. }
  90. func setWrongForgetPasswordInputFlash(w http.ResponseWriter, f WrongForgetPasswordInputFlash) {
  91. bf, _ := json.Marshal(&f)
  92. http.SetCookie(w, &http.Cookie{
  93. Name: wrongForgetPasswordInputFlashCookieName,
  94. Value: base64.StdEncoding.EncodeToString(bf),
  95. Path: "/",
  96. HttpOnly: true,
  97. Secure: true,
  98. })
  99. }
  100. const wrongResetPasswordInputFlashCookieName = "qor5_wrpi_flash"
  101. type WrongResetPasswordInputFlash struct {
  102. Password string
  103. ConfirmPassword string
  104. TOTP string
  105. }
  106. func setWrongResetPasswordInputFlash(w http.ResponseWriter, f WrongResetPasswordInputFlash) {
  107. bf, _ := json.Marshal(&f)
  108. http.SetCookie(w, &http.Cookie{
  109. Name: wrongResetPasswordInputFlashCookieName,
  110. Value: base64.StdEncoding.EncodeToString(bf),
  111. Path: "/",
  112. HttpOnly: true,
  113. Secure: true,
  114. })
  115. }
  116. const wrongChangePasswordInputFlashCookieName = "qor5_wcpi_flash"
  117. type WrongChangePasswordInputFlash struct {
  118. OldPassword string
  119. NewPassword string
  120. ConfirmPassword string
  121. TOTP string
  122. }
  123. func setWrongChangePasswordInputFlash(w http.ResponseWriter, f WrongChangePasswordInputFlash) {
  124. bf, _ := json.Marshal(&f)
  125. http.SetCookie(w, &http.Cookie{
  126. Name: wrongChangePasswordInputFlashCookieName,
  127. Value: base64.StdEncoding.EncodeToString(bf),
  128. Path: "/",
  129. HttpOnly: true,
  130. Secure: true,
  131. })
  132. }
  133. const secondsToRedoFlashCookieName = "qor5_stre_flash"
  134. func setSecondsToRedoFlash(w http.ResponseWriter, c int) {
  135. http.SetCookie(w, &http.Cookie{
  136. Name: secondsToRedoFlashCookieName,
  137. Value: fmt.Sprint(c),
  138. Path: "/",
  139. HttpOnly: true,
  140. })
  141. }