Skip to content

Commit 8947b71

Browse files
willemvdlunny
authored andcommitted
Link OAuth2 account to 2FA enabled account (fix #1050) (#1052)
* fixes #1050 where linking an account to a 2fa enabled account failed because we forgot to really link the account when 2fa is completed * handle errors
1 parent c0f99e8 commit 8947b71

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

routers/user/auth.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
221221
return
222222
}
223223

224+
if ctx.Session.Get("linkAccount") != nil {
225+
gothUser := ctx.Session.Get("linkAccountGothUser")
226+
if gothUser == nil {
227+
ctx.Handle(500, "UserSignIn", errors.New("not in LinkAccount session"))
228+
return
229+
}
230+
231+
err = models.LinkAccountToUser(u, gothUser.(goth.User))
232+
if err != nil {
233+
ctx.Handle(500, "UserSignIn", err)
234+
return
235+
}
236+
}
237+
224238
handleSignIn(ctx, u, remember)
225239
return
226240
}
@@ -532,8 +546,12 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
532546
_, err = models.GetTwoFactorByUID(u.ID)
533547
if err != nil {
534548
if models.IsErrTwoFactorNotEnrolled(err) {
535-
models.LinkAccountToUser(u, gothUser.(goth.User))
536-
handleSignIn(ctx, u, signInForm.Remember)
549+
err = models.LinkAccountToUser(u, gothUser.(goth.User))
550+
if err != nil {
551+
ctx.Handle(500, "UserLinkAccount", err)
552+
} else {
553+
handleSignIn(ctx, u, signInForm.Remember)
554+
}
537555
} else {
538556
ctx.Handle(500, "UserLinkAccount", err)
539557
}

0 commit comments

Comments
 (0)