view_helper.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package login
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "strconv"
  7. "github.com/qor5/x/i18n"
  8. )
  9. type ViewHelper struct {
  10. b *Builder
  11. }
  12. func (vh *ViewHelper) I18n() *i18n.Builder {
  13. return vh.b.i18nBuilder
  14. }
  15. func (vh *ViewHelper) OAuthEnabled() bool {
  16. return vh.b.oauthEnabled
  17. }
  18. func (vh *ViewHelper) RecaptchaEnabled() bool {
  19. return vh.b.recaptchaEnabled
  20. }
  21. func (vh *ViewHelper) UserPassEnabled() bool {
  22. return vh.b.userPassEnabled
  23. }
  24. func (vh *ViewHelper) TOTPEnabled() bool {
  25. return vh.b.totpEnabled
  26. }
  27. func (vh *ViewHelper) NoForgetPasswordLink() bool {
  28. return vh.b.noForgetPasswordLink
  29. }
  30. func (vh *ViewHelper) OAuthProviders() []*Provider {
  31. return vh.b.providers
  32. }
  33. func (vh *ViewHelper) OAuthBeginURL() string {
  34. return vh.b.oauthBeginURL
  35. }
  36. func (vh *ViewHelper) PasswordLoginURL() string {
  37. return vh.b.passwordLoginURL
  38. }
  39. func (vh *ViewHelper) ForgetPasswordPageURL() string {
  40. return vh.b.forgetPasswordPageURL
  41. }
  42. func (vh *ViewHelper) SendResetPasswordLinkURL() string {
  43. return vh.b.sendResetPasswordLinkURL
  44. }
  45. func (vh *ViewHelper) ResetPasswordURL() string {
  46. return vh.b.resetPasswordURL
  47. }
  48. func (vh *ViewHelper) ChangePasswordURL() string {
  49. return vh.b.changePasswordURL
  50. }
  51. func (vh *ViewHelper) ValidateTOTPURL() string {
  52. return vh.b.validateTOTPURL
  53. }
  54. func (vh *ViewHelper) RecaptchaSiteKey() string {
  55. return vh.b.recaptchaConfig.SiteKey
  56. }
  57. func (vh *ViewHelper) TOTPIssuer() string {
  58. return vh.b.totpIssuer
  59. }
  60. func (vh *ViewHelper) FindUserByID(id string) (user interface{}, err error) {
  61. return vh.b.findUserByID(id)
  62. }
  63. func (vh *ViewHelper) GetFailCodeFlash(w http.ResponseWriter, r *http.Request) FailCode {
  64. c, err := r.Cookie(failCodeFlashCookieName)
  65. if err != nil {
  66. return 0
  67. }
  68. http.SetCookie(w, &http.Cookie{
  69. Name: failCodeFlashCookieName,
  70. Path: "/",
  71. MaxAge: -1,
  72. HttpOnly: true,
  73. })
  74. v, _ := strconv.Atoi(c.Value)
  75. return FailCode(v)
  76. }
  77. func (vh *ViewHelper) GetWarnCodeFlash(w http.ResponseWriter, r *http.Request) WarnCode {
  78. c, err := r.Cookie(warnCodeFlashCookieName)
  79. if err != nil {
  80. return 0
  81. }
  82. http.SetCookie(w, &http.Cookie{
  83. Name: warnCodeFlashCookieName,
  84. Path: "/",
  85. MaxAge: -1,
  86. HttpOnly: true,
  87. })
  88. v, _ := strconv.Atoi(c.Value)
  89. return WarnCode(v)
  90. }
  91. func (vh *ViewHelper) GetInfoCodeFlash(w http.ResponseWriter, r *http.Request) InfoCode {
  92. c, err := r.Cookie(infoCodeFlashCookieName)
  93. if err != nil {
  94. return 0
  95. }
  96. http.SetCookie(w, &http.Cookie{
  97. Name: infoCodeFlashCookieName,
  98. Path: "/",
  99. MaxAge: -1,
  100. HttpOnly: true,
  101. })
  102. v, _ := strconv.Atoi(c.Value)
  103. return InfoCode(v)
  104. }
  105. func (vh *ViewHelper) GetCustomErrorMessageFlash(w http.ResponseWriter, r *http.Request) string {
  106. c, err := r.Cookie(customErrorMessageFlashCookieName)
  107. if err != nil {
  108. return ""
  109. }
  110. http.SetCookie(w, &http.Cookie{
  111. Name: customErrorMessageFlashCookieName,
  112. Path: "/",
  113. MaxAge: -1,
  114. HttpOnly: true,
  115. })
  116. return c.Value
  117. }
  118. func (vh *ViewHelper) GetWrongLoginInputFlash(w http.ResponseWriter, r *http.Request) WrongLoginInputFlash {
  119. c, err := r.Cookie(wrongLoginInputFlashCookieName)
  120. if err != nil {
  121. return WrongLoginInputFlash{}
  122. }
  123. http.SetCookie(w, &http.Cookie{
  124. Name: wrongLoginInputFlashCookieName,
  125. Path: "/",
  126. MaxAge: -1,
  127. HttpOnly: true,
  128. Secure: true,
  129. })
  130. v, _ := base64.StdEncoding.DecodeString(c.Value)
  131. wi := WrongLoginInputFlash{}
  132. json.Unmarshal([]byte(v), &wi)
  133. return wi
  134. }
  135. func (vh *ViewHelper) GetWrongForgetPasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongForgetPasswordInputFlash {
  136. c, err := r.Cookie(wrongForgetPasswordInputFlashCookieName)
  137. if err != nil {
  138. return WrongForgetPasswordInputFlash{}
  139. }
  140. http.SetCookie(w, &http.Cookie{
  141. Name: wrongForgetPasswordInputFlashCookieName,
  142. Path: "/",
  143. MaxAge: -1,
  144. HttpOnly: true,
  145. Secure: true,
  146. })
  147. v, _ := base64.StdEncoding.DecodeString(c.Value)
  148. f := WrongForgetPasswordInputFlash{}
  149. json.Unmarshal([]byte(v), &f)
  150. return f
  151. }
  152. func (vh *ViewHelper) GetWrongResetPasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongResetPasswordInputFlash {
  153. c, err := r.Cookie(wrongResetPasswordInputFlashCookieName)
  154. if err != nil {
  155. return WrongResetPasswordInputFlash{}
  156. }
  157. http.SetCookie(w, &http.Cookie{
  158. Name: wrongResetPasswordInputFlashCookieName,
  159. Path: "/",
  160. MaxAge: -1,
  161. HttpOnly: true,
  162. Secure: true,
  163. })
  164. v, _ := base64.StdEncoding.DecodeString(c.Value)
  165. f := WrongResetPasswordInputFlash{}
  166. json.Unmarshal([]byte(v), &f)
  167. return f
  168. }
  169. func (vh *ViewHelper) GetWrongChangePasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongChangePasswordInputFlash {
  170. c, err := r.Cookie(wrongChangePasswordInputFlashCookieName)
  171. if err != nil {
  172. return WrongChangePasswordInputFlash{}
  173. }
  174. http.SetCookie(w, &http.Cookie{
  175. Name: wrongChangePasswordInputFlashCookieName,
  176. Path: "/",
  177. MaxAge: -1,
  178. HttpOnly: true,
  179. Secure: true,
  180. })
  181. v, _ := base64.StdEncoding.DecodeString(c.Value)
  182. f := WrongChangePasswordInputFlash{}
  183. json.Unmarshal([]byte(v), &f)
  184. return f
  185. }
  186. func (vh *ViewHelper) GetSecondsToRedoFlash(w http.ResponseWriter, r *http.Request) int {
  187. c, err := r.Cookie(secondsToRedoFlashCookieName)
  188. if err != nil {
  189. return 0
  190. }
  191. http.SetCookie(w, &http.Cookie{
  192. Name: secondsToRedoFlashCookieName,
  193. Path: "/",
  194. MaxAge: -1,
  195. HttpOnly: true,
  196. })
  197. v, _ := strconv.Atoi(c.Value)
  198. return v
  199. }
  200. func (vh *ViewHelper) GetFailFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
  201. code := vh.GetFailCodeFlash(w, r)
  202. switch code {
  203. case FailCodeSystemError:
  204. return msgr.ErrorSystemError
  205. case FailCodeCompleteUserAuthFailed:
  206. return msgr.ErrorCompleteUserAuthFailed
  207. case FailCodeUserNotFound:
  208. return msgr.ErrorUserNotFound
  209. case FailCodeIncorrectAccountNameOrPassword:
  210. return msgr.ErrorIncorrectAccountNameOrPassword
  211. case FailCodeUserLocked:
  212. return msgr.ErrorUserLocked
  213. case FailCodeAccountIsRequired:
  214. return msgr.ErrorAccountIsRequired
  215. case FailCodePasswordCannotBeEmpty:
  216. return msgr.ErrorPasswordCannotBeEmpty
  217. case FailCodePasswordNotMatch:
  218. return msgr.ErrorPasswordNotMatch
  219. case FailCodeIncorrectPassword:
  220. return msgr.ErrorIncorrectPassword
  221. case FailCodeInvalidToken:
  222. return msgr.ErrorInvalidToken
  223. case FailCodeTokenExpired:
  224. return msgr.ErrorTokenExpired
  225. case FailCodeIncorrectTOTPCode:
  226. return msgr.ErrorIncorrectTOTPCode
  227. case FailCodeTOTPCodeHasBeenUsed:
  228. return msgr.ErrorTOTPCodeReused
  229. case FailCodeIncorrectRecaptchaToken:
  230. return msgr.ErrorIncorrectRecaptchaToken
  231. }
  232. return ""
  233. }
  234. func (vh *ViewHelper) GetWarnFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
  235. code := vh.GetWarnCodeFlash(w, r)
  236. switch code {
  237. case WarnCodePasswordHasBeenChanged:
  238. return msgr.WarnPasswordHasBeenChanged
  239. }
  240. return ""
  241. }
  242. func (vh *ViewHelper) GetInfoFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
  243. code := vh.GetInfoCodeFlash(w, r)
  244. switch code {
  245. case InfoCodePasswordSuccessfullyReset:
  246. return msgr.InfoPasswordSuccessfullyReset
  247. case InfoCodePasswordSuccessfullyChanged:
  248. return msgr.InfoPasswordSuccessfullyChanged
  249. }
  250. return ""
  251. }