view_helper.go 7.2 KB

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