Skip to content

Commit 3ced960

Browse files
authored
chore: only redirect if login URL is overridden (#1876)
Signed-off-by: Jose I. Paris <jiparis@chainloop.dev>
1 parent e07cc0f commit 3ced960

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

app/controlplane/internal/service/auth.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ func NewAuthService(userUC *biz.UserUseCase, orgUC *biz.OrganizationUseCase, mUC
155155
}
156156

157157
type AuthURLs struct {
158-
Login, callback string
158+
Login, callback string
159+
loginIsOverridden bool
159160
}
160161

161162
// urlScheme is deprecated, now it will be inferred from the serverConfig externalURL
@@ -179,6 +180,8 @@ func getAuthURLs(serverConfig *conf.Server_HTTP, loginURLOverride string) (*Auth
179180
// Override the login URL if needed
180181
if loginURLOverride != "" {
181182
urls.Login = loginURLOverride
183+
// denote it's been overridden
184+
urls.loginIsOverridden = true
182185
}
183186

184187
return urls, nil
@@ -274,8 +277,14 @@ func (c *upstreamOIDCclaims) preferredEmail() string {
274277

275278
func callbackHandler(svc *AuthService, w http.ResponseWriter, r *http.Request) *oauthResp {
276279
ctx := context.Background()
277-
// if OIDC provider returns an error, redirect to the login page to and show it to the user
280+
// if OIDC provider returns an error, show the error to the user
278281
if desc := r.URL.Query().Get(oidcErrorParam); desc != "" {
282+
// Do not redirect if there is no dedicated login page
283+
if !svc.AuthURLs.loginIsOverridden {
284+
return newOauthResp(http.StatusUnauthorized, errors.New(desc), true)
285+
}
286+
287+
// redirect to the login page to and show it to the user
279288
redirectURL, err := url.Parse(svc.AuthURLs.Login)
280289
if err != nil {
281290
return newOauthResp(http.StatusInternalServerError, fmt.Errorf("failed to redirect to login: %w", err), false)
@@ -285,7 +294,7 @@ func callbackHandler(svc *AuthService, w http.ResponseWriter, r *http.Request) *
285294
q.Set(oidcErrorParam, desc)
286295
redirectURL.RawQuery = q.Encode()
287296

288-
return &oauthResp{http.StatusUnauthorized, errors.New(desc), true, redirectURL}
297+
return &oauthResp{http.StatusUnauthorized, nil, true, redirectURL}
289298
}
290299

291300
// Get information from google OIDC token

app/controlplane/internal/service/auth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ func TestGetAuthURLs(t *testing.T) {
6565
name: "external with override",
6666
config: &conf.Server_HTTP{Addr: "1.2.3.4", ExternalUrl: "https://foo.com"},
6767
loginURLOverride: "https://foo.override.com/auth/login",
68-
want: &AuthURLs{callback: "https://foo.com/auth/callback", Login: "https://foo.override.com/auth/login"},
68+
want: &AuthURLs{callback: "https://foo.com/auth/callback", Login: "https://foo.override.com/auth/login", loginIsOverridden: true},
6969
},
7070
{
7171
name: "internal with override",
7272
config: internalServer,
7373
loginURLOverride: "https://foo.override.com/auth/login",
74-
want: &AuthURLs{callback: "http://1.2.3.4/auth/callback", Login: "https://foo.override.com/auth/login"},
74+
want: &AuthURLs{callback: "http://1.2.3.4/auth/callback", Login: "https://foo.override.com/auth/login", loginIsOverridden: true},
7575
},
7676
}
7777

0 commit comments

Comments
 (0)