flash.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. func setNoticeOrPanic(w http.ResponseWriter, err error) {
  98. if err == nil {
  99. return
  100. }
  101. ne, ok := err.(*NoticeError)
  102. if !ok {
  103. panic(err)
  104. }
  105. setNoticeFlash(w, ne)
  106. }
  107. const wrongLoginInputFlashCookieName = "qor5_wli_flash"
  108. type WrongLoginInputFlash struct {
  109. Account string
  110. Password string
  111. }
  112. func setWrongLoginInputFlash(w http.ResponseWriter, f WrongLoginInputFlash) {
  113. bf, _ := json.Marshal(&f)
  114. http.SetCookie(w, &http.Cookie{
  115. Name: wrongLoginInputFlashCookieName,
  116. Value: base64.StdEncoding.EncodeToString(bf),
  117. Path: "/",
  118. HttpOnly: true,
  119. Secure: true,
  120. })
  121. }
  122. const wrongForgetPasswordInputFlashCookieName = "qor5_wfpi_flash"
  123. type WrongForgetPasswordInputFlash struct {
  124. Account string
  125. TOTP string
  126. }
  127. func setWrongForgetPasswordInputFlash(w http.ResponseWriter, f WrongForgetPasswordInputFlash) {
  128. bf, _ := json.Marshal(&f)
  129. http.SetCookie(w, &http.Cookie{
  130. Name: wrongForgetPasswordInputFlashCookieName,
  131. Value: base64.StdEncoding.EncodeToString(bf),
  132. Path: "/",
  133. HttpOnly: true,
  134. Secure: true,
  135. })
  136. }
  137. const wrongResetPasswordInputFlashCookieName = "qor5_wrpi_flash"
  138. type WrongResetPasswordInputFlash struct {
  139. Password string
  140. ConfirmPassword string
  141. TOTP string
  142. }
  143. func setWrongResetPasswordInputFlash(w http.ResponseWriter, f WrongResetPasswordInputFlash) {
  144. bf, _ := json.Marshal(&f)
  145. http.SetCookie(w, &http.Cookie{
  146. Name: wrongResetPasswordInputFlashCookieName,
  147. Value: base64.StdEncoding.EncodeToString(bf),
  148. Path: "/",
  149. HttpOnly: true,
  150. Secure: true,
  151. })
  152. }
  153. const wrongChangePasswordInputFlashCookieName = "qor5_wcpi_flash"
  154. type WrongChangePasswordInputFlash struct {
  155. OldPassword string
  156. NewPassword string
  157. ConfirmPassword string
  158. TOTP string
  159. }
  160. func setWrongChangePasswordInputFlash(w http.ResponseWriter, f WrongChangePasswordInputFlash) {
  161. bf, _ := json.Marshal(&f)
  162. http.SetCookie(w, &http.Cookie{
  163. Name: wrongChangePasswordInputFlashCookieName,
  164. Value: base64.StdEncoding.EncodeToString(bf),
  165. Path: "/",
  166. HttpOnly: true,
  167. Secure: true,
  168. })
  169. }
  170. const secondsToRedoFlashCookieName = "qor5_stre_flash"
  171. func setSecondsToRedoFlash(w http.ResponseWriter, c int) {
  172. http.SetCookie(w, &http.Cookie{
  173. Name: secondsToRedoFlashCookieName,
  174. Value: fmt.Sprint(c),
  175. Path: "/",
  176. HttpOnly: true,
  177. })
  178. }