Skip to content

Commit eec13cc

Browse files
committed
allow ACCOUNT_LINKING=auto to work without ENABLE_AUTO_REGISTRATION
1 parent 0fb3be7 commit eec13cc

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

routers/web/auth/oauth.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,47 @@ func SignInOAuthCallback(ctx *context.Context) {
206206
ctx.ServerError("SyncGroupsToTeams", err)
207207
return
208208
}
209+
} else if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto {
210+
// allow ACCOUNT_LINKING=auto to work without ENABLE_AUTO_REGISTRATION.
211+
user := &user_model.User{Email: gothUser.Email}
212+
hasUser, err := user_model.GetUser(ctx, user)
213+
if err != nil {
214+
ctx.ServerError("UserLinkAccount", err)
215+
return
216+
}
217+
218+
if hasUser {
219+
if user.ProhibitLogin || !user.IsActive {
220+
log.Info("Failed authentication attempt for %s from %s: user has disabled sign-in", user.Name, ctx.RemoteAddr())
221+
ctx.Flash.Error(ctx.Tr("auth.prohibit_login"))
222+
ctx.Redirect(setting.AppSubURL + "/user/login")
223+
return
224+
}
225+
if err := externalaccount.LinkAccountToUser(ctx, authSource.ID, user, gothUser); err != nil {
226+
ctx.ServerError("LinkAccountToUser", err)
227+
return
228+
}
229+
230+
userHasTwoFactorAuth, err := auth.HasTwoFactorOrWebAuthn(ctx, user.ID)
231+
if err != nil {
232+
ctx.ServerError("HasTwoFactorOrWebAuthn", err)
233+
return
234+
}
235+
if err := updateSession(ctx, nil, map[string]any{
236+
session.KeyUID: user.ID,
237+
session.KeyUname: user.Name,
238+
session.KeyUserHasTwoFactorAuth: userHasTwoFactorAuth,
239+
}); err != nil {
240+
ctx.ServerError("updateSession", err)
241+
return
242+
}
243+
ctx.Csrf.PrepareForSessionUser(ctx)
244+
ctx.Redirect(setting.AppSubURL + "/")
245+
return
246+
}
247+
248+
showLinkingLogin(ctx, authSource.ID, gothUser)
249+
return
209250
} else {
210251
// no existing user is found, request attach or new account
211252
showLinkingLogin(ctx, authSource.ID, gothUser)

0 commit comments

Comments
 (0)