123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package login
- import (
- "encoding/base64"
- "encoding/json"
- "net/http"
- "strconv"
- "strings"
- "github.com/qor5/x/i18n"
- )
- type ViewHelper struct {
- b *Builder
- }
- func (vh *ViewHelper) I18n() *i18n.Builder {
- return vh.b.i18nBuilder
- }
- func (vh *ViewHelper) OAuthEnabled() bool {
- return vh.b.oauthEnabled
- }
- func (vh *ViewHelper) RecaptchaEnabled() bool {
- return vh.b.recaptchaEnabled
- }
- func (vh *ViewHelper) UserPassEnabled() bool {
- return vh.b.userPassEnabled
- }
- func (vh *ViewHelper) TOTPEnabled() bool {
- return vh.b.totpEnabled
- }
- func (vh *ViewHelper) NoForgetPasswordLink() bool {
- return vh.b.noForgetPasswordLink
- }
- func (vh *ViewHelper) OAuthProviders() []*Provider {
- return vh.b.providers
- }
- func (vh *ViewHelper) OAuthBeginURL() string {
- return vh.b.oauthBeginURL
- }
- func (vh *ViewHelper) PasswordLoginURL() string {
- return vh.b.passwordLoginURL
- }
- func (vh *ViewHelper) ForgetPasswordPageURL() string {
- return vh.b.forgetPasswordPageURL
- }
- func (vh *ViewHelper) SendResetPasswordLinkURL() string {
- return vh.b.sendResetPasswordLinkURL
- }
- func (vh *ViewHelper) ResetPasswordURL() string {
- return vh.b.resetPasswordURL
- }
- func (vh *ViewHelper) ChangePasswordURL() string {
- return vh.b.changePasswordURL
- }
- func (vh *ViewHelper) ValidateTOTPURL() string {
- return vh.b.validateTOTPURL
- }
- func (vh *ViewHelper) RecaptchaSiteKey() string {
- return vh.b.recaptchaConfig.SiteKey
- }
- func (vh *ViewHelper) TOTPIssuer() string {
- return vh.b.totpConfig.Issuer
- }
- func (vh *ViewHelper) FindUserByID(id string) (user interface{}, err error) {
- return vh.b.findUserByID(id)
- }
- func (vh *ViewHelper) GetFailCodeFlash(w http.ResponseWriter, r *http.Request) FailCode {
- c, err := r.Cookie(failCodeFlashCookieName)
- if err != nil {
- return 0
- }
- http.SetCookie(w, &http.Cookie{
- Name: failCodeFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- })
- v, _ := strconv.Atoi(c.Value)
- return FailCode(v)
- }
- func (vh *ViewHelper) GetWarnCodeFlash(w http.ResponseWriter, r *http.Request) WarnCode {
- c, err := r.Cookie(warnCodeFlashCookieName)
- if err != nil {
- return 0
- }
- http.SetCookie(w, &http.Cookie{
- Name: warnCodeFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- })
- v, _ := strconv.Atoi(c.Value)
- return WarnCode(v)
- }
- func (vh *ViewHelper) GetInfoCodeFlash(w http.ResponseWriter, r *http.Request) InfoCode {
- c, err := r.Cookie(infoCodeFlashCookieName)
- if err != nil {
- return 0
- }
- http.SetCookie(w, &http.Cookie{
- Name: infoCodeFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- })
- v, _ := strconv.Atoi(c.Value)
- return InfoCode(v)
- }
- func (vh *ViewHelper) GetNoticeFlash(w http.ResponseWriter, r *http.Request) *NoticeError {
- c, err := r.Cookie(noticeFlashCookieName)
- if err != nil {
- return nil
- }
- var level NoticeLevel
- var message string
- {
- vs := strings.SplitN(c.Value, "#", 2)
- if len(vs) != 2 {
- return nil
- }
- n, err := strconv.Atoi(vs[0])
- if err != nil {
- return nil
- }
- level = NoticeLevel(n)
- message = vs[1]
- }
- http.SetCookie(w, &http.Cookie{
- Name: noticeFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- })
- return &NoticeError{
- Level: level,
- Message: message,
- }
- }
- func (vh *ViewHelper) GetWrongLoginInputFlash(w http.ResponseWriter, r *http.Request) WrongLoginInputFlash {
- c, err := r.Cookie(wrongLoginInputFlashCookieName)
- if err != nil {
- return WrongLoginInputFlash{}
- }
- http.SetCookie(w, &http.Cookie{
- Name: wrongLoginInputFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- Secure: true,
- })
- v, _ := base64.StdEncoding.DecodeString(c.Value)
- wi := WrongLoginInputFlash{}
- json.Unmarshal([]byte(v), &wi)
- return wi
- }
- func (vh *ViewHelper) GetWrongForgetPasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongForgetPasswordInputFlash {
- c, err := r.Cookie(wrongForgetPasswordInputFlashCookieName)
- if err != nil {
- return WrongForgetPasswordInputFlash{}
- }
- http.SetCookie(w, &http.Cookie{
- Name: wrongForgetPasswordInputFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- Secure: true,
- })
- v, _ := base64.StdEncoding.DecodeString(c.Value)
- f := WrongForgetPasswordInputFlash{}
- json.Unmarshal([]byte(v), &f)
- return f
- }
- func (vh *ViewHelper) GetWrongResetPasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongResetPasswordInputFlash {
- c, err := r.Cookie(wrongResetPasswordInputFlashCookieName)
- if err != nil {
- return WrongResetPasswordInputFlash{}
- }
- http.SetCookie(w, &http.Cookie{
- Name: wrongResetPasswordInputFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- Secure: true,
- })
- v, _ := base64.StdEncoding.DecodeString(c.Value)
- f := WrongResetPasswordInputFlash{}
- json.Unmarshal([]byte(v), &f)
- return f
- }
- func (vh *ViewHelper) GetWrongChangePasswordInputFlash(w http.ResponseWriter, r *http.Request) WrongChangePasswordInputFlash {
- c, err := r.Cookie(wrongChangePasswordInputFlashCookieName)
- if err != nil {
- return WrongChangePasswordInputFlash{}
- }
- http.SetCookie(w, &http.Cookie{
- Name: wrongChangePasswordInputFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- Secure: true,
- })
- v, _ := base64.StdEncoding.DecodeString(c.Value)
- f := WrongChangePasswordInputFlash{}
- json.Unmarshal([]byte(v), &f)
- return f
- }
- func (vh *ViewHelper) GetSecondsToRedoFlash(w http.ResponseWriter, r *http.Request) int {
- c, err := r.Cookie(secondsToRedoFlashCookieName)
- if err != nil {
- return 0
- }
- http.SetCookie(w, &http.Cookie{
- Name: secondsToRedoFlashCookieName,
- Path: "/",
- MaxAge: -1,
- HttpOnly: true,
- })
- v, _ := strconv.Atoi(c.Value)
- return v
- }
- func (vh *ViewHelper) GetFailFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
- code := vh.GetFailCodeFlash(w, r)
- switch code {
- case FailCodeSystemError:
- return msgr.ErrorSystemError
- case FailCodeCompleteUserAuthFailed:
- return msgr.ErrorCompleteUserAuthFailed
- case FailCodeUserNotFound:
- return msgr.ErrorUserNotFound
- case FailCodeIncorrectAccountNameOrPassword:
- return msgr.ErrorIncorrectAccountNameOrPassword
- case FailCodeUserLocked:
- return msgr.ErrorUserLocked
- case FailCodeAccountIsRequired:
- return msgr.ErrorAccountIsRequired
- case FailCodePasswordCannotBeEmpty:
- return msgr.ErrorPasswordCannotBeEmpty
- case FailCodePasswordNotMatch:
- return msgr.ErrorPasswordNotMatch
- case FailCodeIncorrectPassword:
- return msgr.ErrorIncorrectPassword
- case FailCodeInvalidToken:
- return msgr.ErrorInvalidToken
- case FailCodeTokenExpired:
- return msgr.ErrorTokenExpired
- case FailCodeIncorrectTOTPCode:
- return msgr.ErrorIncorrectTOTPCode
- case FailCodeTOTPCodeHasBeenUsed:
- return msgr.ErrorTOTPCodeReused
- case FailCodeIncorrectRecaptchaToken:
- return msgr.ErrorIncorrectRecaptchaToken
- }
- return ""
- }
- func (vh *ViewHelper) GetWarnFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
- code := vh.GetWarnCodeFlash(w, r)
- switch code {
- case WarnCodePasswordHasBeenChanged:
- return msgr.WarnPasswordHasBeenChanged
- }
- return ""
- }
- func (vh *ViewHelper) GetInfoFlashMessage(msgr *Messages, w http.ResponseWriter, r *http.Request) string {
- code := vh.GetInfoCodeFlash(w, r)
- switch code {
- case InfoCodePasswordSuccessfullyReset:
- return msgr.InfoPasswordSuccessfullyReset
- case InfoCodePasswordSuccessfullyChanged:
- return msgr.InfoPasswordSuccessfullyChanged
- }
- return ""
- }
|