123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package login
- import (
- "encoding/base64"
- "encoding/json"
- "fmt"
- "net/http"
- )
- type NoticeLevel int
- const (
- NoticeLevel_Info NoticeLevel = iota
- NoticeLevel_Warn
- NoticeLevel_Error
- )
- type NoticeError struct {
- Level NoticeLevel
- Message string
- }
- func (e *NoticeError) Error() string {
- return e.Message
- }
- type FailCode int
- const (
- FailCodeSystemError FailCode = iota + 1
- FailCodeCompleteUserAuthFailed
- FailCodeUserNotFound
- FailCodeIncorrectAccountNameOrPassword
- FailCodeUserLocked
- FailCodeAccountIsRequired
- FailCodePasswordCannotBeEmpty
- FailCodePasswordNotMatch
- FailCodeIncorrectPassword
- FailCodeInvalidToken
- FailCodeTokenExpired
- FailCodeIncorrectTOTPCode
- FailCodeTOTPCodeHasBeenUsed
- FailCodeIncorrectRecaptchaToken
- )
- type WarnCode int
- const (
- WarnCodePasswordHasBeenChanged = iota + 1
- )
- type InfoCode int
- const (
- InfoCodePasswordSuccessfullyReset InfoCode = iota + 1
- InfoCodePasswordSuccessfullyChanged
- )
- const failCodeFlashCookieName = "qor5_fc_flash"
- const warnCodeFlashCookieName = "qor5_wc_flash"
- const infoCodeFlashCookieName = "qor5_ic_flash"
- func setFailCodeFlash(w http.ResponseWriter, c FailCode) {
- http.SetCookie(w, &http.Cookie{
- Name: failCodeFlashCookieName,
- Value: fmt.Sprint(c),
- Path: "/",
- HttpOnly: true,
- })
- }
- func setWarnCodeFlash(w http.ResponseWriter, c WarnCode) {
- http.SetCookie(w, &http.Cookie{
- Name: warnCodeFlashCookieName,
- Value: fmt.Sprint(c),
- Path: "/",
- HttpOnly: true,
- })
- }
- func setInfoCodeFlash(w http.ResponseWriter, c InfoCode) {
- http.SetCookie(w, &http.Cookie{
- Name: infoCodeFlashCookieName,
- Value: fmt.Sprint(c),
- Path: "/",
- HttpOnly: true,
- })
- }
- const noticeFlashCookieName = "qor5_notice_flash"
- func setNoticeFlash(w http.ResponseWriter, ne *NoticeError) {
- if ne == nil {
- return
- }
- http.SetCookie(w, &http.Cookie{
- Name: noticeFlashCookieName,
- Value: fmt.Sprintf("%d#%s", ne.Level, ne.Message),
- Path: "/",
- HttpOnly: true,
- })
- }
- func setNoticeOrFailCodeFlash(w http.ResponseWriter, err error, c FailCode) {
- if err == nil {
- return
- }
- ne, ok := err.(*NoticeError)
- if ok {
- setNoticeFlash(w, ne)
- return
- }
- setFailCodeFlash(w, c)
- }
- func setNoticeOrPanic(w http.ResponseWriter, err error) {
- if err == nil {
- return
- }
- ne, ok := err.(*NoticeError)
- if !ok {
- panic(err)
- }
- setNoticeFlash(w, ne)
- }
- const wrongLoginInputFlashCookieName = "qor5_wli_flash"
- type WrongLoginInputFlash struct {
- Account string
- Password string
- }
- func setWrongLoginInputFlash(w http.ResponseWriter, f WrongLoginInputFlash) {
- bf, _ := json.Marshal(&f)
- http.SetCookie(w, &http.Cookie{
- Name: wrongLoginInputFlashCookieName,
- Value: base64.StdEncoding.EncodeToString(bf),
- Path: "/",
- HttpOnly: true,
- Secure: true,
- })
- }
- const wrongForgetPasswordInputFlashCookieName = "qor5_wfpi_flash"
- type WrongForgetPasswordInputFlash struct {
- Account string
- TOTP string
- }
- func setWrongForgetPasswordInputFlash(w http.ResponseWriter, f WrongForgetPasswordInputFlash) {
- bf, _ := json.Marshal(&f)
- http.SetCookie(w, &http.Cookie{
- Name: wrongForgetPasswordInputFlashCookieName,
- Value: base64.StdEncoding.EncodeToString(bf),
- Path: "/",
- HttpOnly: true,
- Secure: true,
- })
- }
- const wrongResetPasswordInputFlashCookieName = "qor5_wrpi_flash"
- type WrongResetPasswordInputFlash struct {
- Password string
- ConfirmPassword string
- TOTP string
- }
- func setWrongResetPasswordInputFlash(w http.ResponseWriter, f WrongResetPasswordInputFlash) {
- bf, _ := json.Marshal(&f)
- http.SetCookie(w, &http.Cookie{
- Name: wrongResetPasswordInputFlashCookieName,
- Value: base64.StdEncoding.EncodeToString(bf),
- Path: "/",
- HttpOnly: true,
- Secure: true,
- })
- }
- const wrongChangePasswordInputFlashCookieName = "qor5_wcpi_flash"
- type WrongChangePasswordInputFlash struct {
- OldPassword string
- NewPassword string
- ConfirmPassword string
- TOTP string
- }
- func setWrongChangePasswordInputFlash(w http.ResponseWriter, f WrongChangePasswordInputFlash) {
- bf, _ := json.Marshal(&f)
- http.SetCookie(w, &http.Cookie{
- Name: wrongChangePasswordInputFlashCookieName,
- Value: base64.StdEncoding.EncodeToString(bf),
- Path: "/",
- HttpOnly: true,
- Secure: true,
- })
- }
- const secondsToRedoFlashCookieName = "qor5_stre_flash"
- func setSecondsToRedoFlash(w http.ResponseWriter, c int) {
- http.SetCookie(w, &http.Cookie{
- Name: secondsToRedoFlashCookieName,
- Value: fmt.Sprint(c),
- Path: "/",
- HttpOnly: true,
- })
- }
|