func TwoFactorPost(ctx *context.Context) {
	form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
	ctx.Data["Title"] = ctx.Tr("twofa")

	// Ensure user is in a 2FA session.
	idSess := ctx.Session.Get("twofaUid")
	if idSess == nil {
		ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
		return
	}

	id := idSess.(int64)
	twofa, err := models.GetTwoFactorByUID(id)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}

	// Validate the passcode with the stored TOTP secret.
	ok, err := twofa.ValidateTOTP(form.Passcode)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}

	if ok && twofa.LastUsedPasscode != form.Passcode {
		remember := ctx.Session.Get("twofaRemember").(bool)
		u, err := models.GetUserByID(id)
		if err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}

		if ctx.Session.Get("linkAccount") != nil {
			gothUser := ctx.Session.Get("linkAccountGothUser")
			if gothUser == nil {
				ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
				return
			}

			err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
			if err != nil {
				ctx.ServerError("UserSignIn", err)
				return
			}
		}

		twofa.LastUsedPasscode = form.Passcode
		if err = models.UpdateTwoFactor(twofa); err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}

		handleSignIn(ctx, u, remember)
		return
	}

	ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, forms.TwoFactorAuthForm{})
}
// TwoFactorPost validates a user's two-factor authentication token.
TwoFactorPost validates a user's two-factor authentication token.
// TwoFactorPost validates a user ' s two - factor authentication token .
func TwoFactorPost(ctx *context.Context) {
	form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
	ctx.Data["Title"] = ctx.Tr("twofa")
	idSess := ctx.Session.Get("twofaUid")
	if idSess == nil {
		ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
		return
	}
	id := idSess.(int64)
	twofa, err := models.GetTwoFactorByUID(id)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}
	ok, err := twofa.ValidateTOTP(form.Passcode)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}
	if ok && twofa.LastUsedPasscode != form.Passcode {
		remember := ctx.Session.Get("twofaRemember").(bool)
		u, err := models.GetUserByID(id)
		if err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		if ctx.Session.Get("linkAccount") != nil {
			gothUser := ctx.Session.Get("linkAccountGothUser")
			if gothUser == nil {
				ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
				return
			}
			err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
			if err != nil {
				ctx.ServerError("UserSignIn", err)
				return
			}
		}
		twofa.LastUsedPasscode = form.Passcode
		if err = models.UpdateTwoFactor(twofa); err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		handleSignIn(ctx, u, remember)
		return
	}
	ctx.RenderWithErr(ctx.Tr("auth.twofa_passcode_incorrect"), tplTwofa, forms.TwoFactorAuthForm{})
}
func TwoFactorPost ( ctx * context . Context ) { form := web . GetForm ( ctx ) . ( * forms . TwoFactorAuthForm ) ctx . Data [ " " ] = ctx . Tr ( " " ) idSess := ctx . Session . Get ( " " ) if idSess == nil { ctx . ServerError ( " " , errors . New ( " " ) ) return } id := idSess . ( int64 ) twofa , err := models . GetTwoFactorByUID ( id ) if err != nil { ctx . ServerError ( " " , err ) return } ok , err := twofa . ValidateTOTP ( form . Passcode ) if err != nil { ctx . ServerError ( " " , err ) return } if ok && twofa . LastUsedPasscode != form . Passcode { remember := ctx . Session . Get ( " " ) . ( bool ) u , err := models . GetUserByID ( id ) if err != nil { ctx . ServerError ( " " , err ) return } if ctx . Session . Get ( " " ) != nil { gothUser := ctx . Session . Get ( " " ) if gothUser == nil { ctx . ServerError ( " " , errors . New ( " " ) ) return } err = externalaccount . LinkAccountToUser ( u , gothUser . ( goth . User ) ) if err != nil { ctx . ServerError ( " " , err ) return } } twofa . LastUsedPasscode = form . Passcode if err = models . UpdateTwoFactor ( twofa ) ; err != nil { ctx . ServerError ( " " , err ) return } handleSignIn ( ctx , u , remember ) return } ctx . RenderWithErr ( ctx . Tr ( " " ) , tplTwofa , forms . TwoFactorAuthForm { } ) }
TwoFactorPost validates a user's two-factor authentication token.
TwoFactorPost validates a user ' s two - factor authentication token .
// Ensure user is in a 2FA session. // Validate the passcode with the stored TOTP secret.