func TwoFactorScratchPost(ctx *context.Context) {
	form := web.GetForm(ctx).(*forms.TwoFactorScratchAuthForm)
	ctx.Data["Title"] = ctx.Tr("twofa_scratch")

	// 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.
	if twofa.VerifyScratchToken(form.Token) {
		// Invalidate the scratch token.
		_, err = twofa.GenerateScratchToken()
		if err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		if err = models.UpdateTwoFactor(twofa); err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}

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

		handleSignInFull(ctx, u, remember, false)
		ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
		ctx.Redirect(setting.AppSubURL + "/user/settings/security")
		return
	}

	ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, forms.TwoFactorScratchAuthForm{})
}
// TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
// TwoFactorScratchPost validates and invalidates a user ' s two - factor scratch token .
func TwoFactorScratchPost(ctx *context.Context) {
	form := web.GetForm(ctx).(*forms.TwoFactorScratchAuthForm)
	ctx.Data["Title"] = ctx.Tr("twofa_scratch")
	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
	}
	if twofa.VerifyScratchToken(form.Token) {
		_, err = twofa.GenerateScratchToken()
		if err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		if err = models.UpdateTwoFactor(twofa); err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		remember := ctx.Session.Get("twofaRemember").(bool)
		u, err := models.GetUserByID(id)
		if err != nil {
			ctx.ServerError("UserSignIn", err)
			return
		}
		handleSignInFull(ctx, u, remember, false)
		ctx.Flash.Info(ctx.Tr("auth.twofa_scratch_used"))
		ctx.Redirect(setting.AppSubURL + "/user/settings/security")
		return
	}
	ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplTwofaScratch, forms.TwoFactorScratchAuthForm{})
}
func TwoFactorScratchPost ( ctx * context . Context ) { form := web . GetForm ( ctx ) . ( * forms . TwoFactorScratchAuthForm ) 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 } if twofa . VerifyScratchToken ( form . Token ) { _ , err = twofa . GenerateScratchToken ( ) if err != nil { ctx . ServerError ( " " , err ) return } if err = models . UpdateTwoFactor ( twofa ) ; err != nil { ctx . ServerError ( " " , err ) return } remember := ctx . Session . Get ( " " ) . ( bool ) u , err := models . GetUserByID ( id ) if err != nil { ctx . ServerError ( " " , err ) return } handleSignInFull ( ctx , u , remember , false ) ctx . Flash . Info ( ctx . Tr ( " " ) ) ctx . Redirect ( setting . AppSubURL + " " ) return } ctx . RenderWithErr ( ctx . Tr ( " " ) , tplTwofaScratch , forms . TwoFactorScratchAuthForm { } ) }
TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
TwoFactorScratchPost validates and invalidates a user ' s two - factor scratch token .
// Ensure user is in a 2FA session. // Validate the passcode with the stored TOTP secret. // Invalidate the scratch token.