func U2FSign(ctx *context.Context) {
	signResp := web.GetForm(ctx).(*u2f.SignResponse)
	challSess := ctx.Session.Get("u2fChallenge")
	idSess := ctx.Session.Get("twofaUid")
	if challSess == nil || idSess == nil {
		ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
		return
	}
	challenge := challSess.(*u2f.Challenge)
	id := idSess.(int64)
	regs, err := models.GetU2FRegistrationsByUID(id)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}
	for _, reg := range regs {
		r, err := reg.Parse()
		if err != nil {
			log.Fatal("parsing u2f registration: %v", err)
			continue
		}
		newCounter, authErr := r.Authenticate(*signResp, *challenge, reg.Counter)
		if authErr == nil {
			reg.Counter = newCounter
			user, err := models.GetUserByID(id)
			if err != nil {
				ctx.ServerError("UserSignIn", err)
				return
			}
			remember := ctx.Session.Get("twofaRemember").(bool)
			if err := reg.UpdateCounter(); 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(user, gothUser.(goth.User))
				if err != nil {
					ctx.ServerError("UserSignIn", err)
					return
				}
			}
			redirect := handleSignInFull(ctx, user, remember, false)
			if redirect == "" {
				redirect = setting.AppSubURL + "/"
			}
			ctx.PlainText(200, []byte(redirect))
			return
		}
	}
	ctx.Error(http.StatusUnauthorized)
}
// U2FSign authenticates the user by signResp
U2FSign authenticates the user by signResp
// U2FSign authenticates the user by signResp
func U2FSign(ctx *context.Context) {
	signResp := web.GetForm(ctx).(*u2f.SignResponse)
	challSess := ctx.Session.Get("u2fChallenge")
	idSess := ctx.Session.Get("twofaUid")
	if challSess == nil || idSess == nil {
		ctx.ServerError("UserSignIn", errors.New("not in U2F session"))
		return
	}
	challenge := challSess.(*u2f.Challenge)
	id := idSess.(int64)
	regs, err := models.GetU2FRegistrationsByUID(id)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}
	for _, reg := range regs {
		r, err := reg.Parse()
		if err != nil {
			log.Fatal("parsing u2f registration: %v", err)
			continue
		}
		newCounter, authErr := r.Authenticate(*signResp, *challenge, reg.Counter)
		if authErr == nil {
			reg.Counter = newCounter
			user, err := models.GetUserByID(id)
			if err != nil {
				ctx.ServerError("UserSignIn", err)
				return
			}
			remember := ctx.Session.Get("twofaRemember").(bool)
			if err := reg.UpdateCounter(); 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(user, gothUser.(goth.User))
				if err != nil {
					ctx.ServerError("UserSignIn", err)
					return
				}
			}
			redirect := handleSignInFull(ctx, user, remember, false)
			if redirect == "" {
				redirect = setting.AppSubURL + "/"
			}
			ctx.PlainText(200, []byte(redirect))
			return
		}
	}
	ctx.Error(http.StatusUnauthorized)
}
func U2FSign ( ctx * context . Context ) { signResp := web . GetForm ( ctx ) . ( * u2f . SignResponse ) challSess := ctx . Session . Get ( " " ) idSess := ctx . Session . Get ( " " ) if challSess == nil || idSess == nil { ctx . ServerError ( " " , errors . New ( " " ) ) return } challenge := challSess . ( * u2f . Challenge ) id := idSess . ( int64 ) regs , err := models . GetU2FRegistrationsByUID ( id ) if err != nil { ctx . ServerError ( " " , err ) return } for _ , reg := range regs { r , err := reg . Parse ( ) if err != nil { log . Fatal ( " " , err ) continue } newCounter , authErr := r . Authenticate ( * signResp , * challenge , reg . Counter ) if authErr == nil { reg . Counter = newCounter user , err := models . GetUserByID ( id ) if err != nil { ctx . ServerError ( " " , err ) return } remember := ctx . Session . Get ( " " ) . ( bool ) if err := reg . UpdateCounter ( ) ; 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 ( user , gothUser . ( goth . User ) ) if err != nil { ctx . ServerError ( " " , err ) return } } redirect := handleSignInFull ( ctx , user , remember , false ) if redirect == " " { redirect = setting . AppSubURL + " " } ctx . PlainText ( 200 , [ ] byte ( redirect ) ) return } } ctx . Error ( http . StatusUnauthorized ) }
U2FSign authenticates the user by signResp
U2FSign authenticates the user by signResp