Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit eb5a0d1

Browse files
authored
soap: Fix auto-redirect to IdP when SOAP is enabled (#64184)
When exactly 1 auth provider is configured, Sourcegraph redirects users automatically to the IdP to speed up the sign-in process, so that users don't have to make an extra click to select the one and only sign-in provider. SOAP is a special case though because it is hidden by default, but enabled on all cloud instances. That caused this auto redirect to never fire for Cloud, since there are technically two auth providers. This PR fixes it by checking for the sourcegraph-operator query parameter which tells the UI to show the magic SOAP auth provider in the list. Closes SRC-500 Test plan: Tested on a cloud instance that indeed there is no auto redirect. Then tested locally with SOAP configured that auto redirects happen after this PR, and that there is no auto redirect when the ?sourcegraph-operator query parameter is set. ## Changelog When only a single auth provider is configured, users are again redirected correctly to the identity provider on Sourcegraph Cloud.
1 parent c1ff60f commit eb5a0d1

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

cmd/frontend/internal/auth/oauth/middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewMiddleware(db database.DB, serviceType, authPrefix string, isAPIHandler
6060
// instance, it's an app request, the sign-out cookie is not present, and access requests are disabled, redirect to sign-in immediately.
6161
//
6262
// For sign-out requests (sign-out cookie is present), the user will be redirected to the SG login page.
63-
pc := getExactlyOneOAuthProvider()
63+
pc := getExactlyOneOAuthProvider(!r.URL.Query().Has("sourcegraph-operator"))
6464
if pc != nil && !isAPIHandler && pc.AuthPrefix == authPrefix && !auth.HasSignOutCookie(r) && isHuman(r) && !conf.IsAccessRequestEnabled() {
6565
span.AddEvent("redirect to signin")
6666
v := make(url.Values)
@@ -210,8 +210,8 @@ func (l *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
210210
}
211211
}
212212

213-
func getExactlyOneOAuthProvider() *Provider {
214-
ps := providers.SignInProviders()
213+
func getExactlyOneOAuthProvider(skipSoap bool) *Provider {
214+
ps := providers.SignInProviders(skipSoap)
215215
if len(ps) != 1 {
216216
return nil
217217
}

cmd/frontend/internal/auth/openidconnect/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func handleOpenIDConnectAuth(logger log.Logger, db database.DB, w http.ResponseW
126126
// it's an app request, and the sign-out cookie is not present, redirect to sign-in immediately.
127127
//
128128
// For sign-out requests (sign-out cookie is present), the user is redirected to the Sourcegraph login page.
129-
ps := providers.SignInProviders()
129+
ps := providers.SignInProviders(!r.URL.Query().Has("sourcegraph-operator"))
130130
openIDConnectEnabled := len(ps) == 1 && ps[0].Config().Openidconnect != nil
131131
if openIDConnectEnabled && !auth.HasSignOutCookie(r) && !isAPIRequest {
132132
p, safeErrMsg, err := GetProviderAndRefresh(r.Context(), ps[0].ConfigID().ID, GetProvider)

cmd/frontend/internal/auth/providers/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
tags = [TAG_PLATFORM_SOURCE],
1212
visibility = ["//visibility:public"],
1313
deps = [
14+
"//internal/auth",
1415
"//internal/extsvc",
1516
"//schema",
1617
"@com_github_inconshreveable_log15//:log15",

cmd/frontend/internal/auth/providers/providers.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/inconshreveable/log15" //nolint:logging // TODO move all logging to sourcegraph/log
1111

12+
"github.com/sourcegraph/sourcegraph/internal/auth"
1213
"github.com/sourcegraph/sourcegraph/internal/extsvc"
1314
"github.com/sourcegraph/sourcegraph/schema"
1415
)
@@ -156,10 +157,13 @@ func Providers() []Provider {
156157

157158
// SignInProviders returns the list of currently registered authentication providers that aren't hidden.
158159
// The list is not sorted in any way.
159-
func SignInProviders() []Provider {
160+
func SignInProviders(skipSoap bool) []Provider {
160161
if MockProviders != nil {
161162
providers := make([]Provider, 0, len(MockProviders))
162163
for _, p := range MockProviders {
164+
if skipSoap && p.ConfigID().Type == auth.SourcegraphOperatorProviderType {
165+
continue
166+
}
163167
common := GetAuthProviderCommon(p)
164168
if !common.Hidden && !common.NoSignIn {
165169
providers = append(providers, p)
@@ -182,6 +186,9 @@ func SignInProviders() []Provider {
182186
providers := make([]Provider, 0, ct)
183187
for _, pkgProviders := range curProviders {
184188
for _, p := range pkgProviders {
189+
if skipSoap && p.ConfigID().Type == auth.SourcegraphOperatorProviderType {
190+
continue
191+
}
185192
common := GetAuthProviderCommon(p)
186193
if !common.Hidden && !common.NoSignIn {
187194
providers = append(providers, p)

cmd/frontend/internal/auth/saml/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func authHandler(db database.DB, w http.ResponseWriter, r *http.Request, next ht
6161
// app request, and the sign-out cookie is not present, redirect to the sso sign-in immediately.
6262
//
6363
// For sign-out requests (sign-out cookie is present), the user will be redirected to the Sourcegraph login page.
64-
ps := providers.SignInProviders()
64+
ps := providers.SignInProviders(!r.URL.Query().Has("sourcegraph-operator"))
6565
if len(ps) == 1 && ps[0].Config().Saml != nil && !auth.HasSignOutCookie(r) && !isAPIRequest {
6666
p, handled := handleGetProvider(r.Context(), w, ps[0].ConfigID().ID)
6767
if handled {

0 commit comments

Comments
 (0)