flash.go 4.3 KB

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