Skip to content

Commit 5c37345

Browse files
authored
fix: return error when trying to use an unconfigured auth provider (#1445)
Signed-off-by: Grant Linville <[email protected]>
1 parent 5fe7c57 commit 5c37345

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

pkg/proxy/proxy.go

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/http/httputil"
99
"net/url"
10+
"slices"
1011
"sort"
1112
"strings"
1213
"time"
@@ -96,7 +97,10 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9697
}
9798

9899
// Determine which auth provider to use.
99-
var provider string
100+
var (
101+
provider string
102+
fromCookie bool
103+
)
100104
if param := r.URL.Query().Get(ObotAuthProviderQueryParam); param != "" {
101105
// If the provider is set in the query params, use that.
102106
provider = param
@@ -110,6 +114,7 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110114

111115
var contents CookieContents
112116
if err = json.Unmarshal([]byte(cookieValue), &contents); err == nil {
117+
fromCookie = true
113118
provider = contents.AuthProvider
114119

115120
// Update the cookie to just be the token, which is what the auth provider expects.
@@ -127,12 +132,11 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
127132

128133
// If no provider is set, just use the alphabetically first provider.
129134
if provider == "" {
130-
providers, err := pm.dispatcher.ListConfiguredAuthProviders(r.Context(), "default")
135+
configuredProviders, err := pm.dispatcher.ListConfiguredAuthProviders(r.Context(), "default")
131136
if err != nil {
132137
http.Error(w, fmt.Sprintf("failed to list configured auth providers: %v", err), http.StatusInternalServerError)
133138
return
134-
}
135-
if len(providers) == 0 {
139+
} else if len(configuredProviders) == 0 {
136140
// There aren't any auth providers configured. Return an error, unless the user is signing out, in which case, just redirect.
137141
if r.URL.Path == "/oauth2/sign_out" {
138142
http.Redirect(w, r, rdParam, http.StatusFound)
@@ -142,10 +146,57 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
142146
http.Error(w, "no auth providers configured", http.StatusBadRequest)
143147
return
144148
}
145-
sort.Slice(providers, func(i, j int) bool {
146-
return providers[i] < providers[j]
149+
150+
sort.Slice(configuredProviders, func(i, j int) bool {
151+
return configuredProviders[i] < configuredProviders[j]
147152
})
148-
provider = "default/" + providers[0]
153+
provider = "default/" + configuredProviders[0]
154+
} else {
155+
namespace, name, _ := strings.Cut(provider, "/")
156+
if namespace == "" {
157+
http.Error(w, "invalid auth provider:"+provider, http.StatusBadRequest)
158+
return
159+
}
160+
161+
// Check if the provider is configured.
162+
configuredProviders, err := pm.dispatcher.ListConfiguredAuthProviders(r.Context(), namespace)
163+
if err != nil {
164+
http.Error(w, fmt.Sprintf("failed to list configured auth providers: %v", err), http.StatusInternalServerError)
165+
return
166+
}
167+
168+
if !slices.Contains(configuredProviders, name) {
169+
// The requested auth provider isn't configured. Return an error, unless the user is signing out, in which case, just redirect.
170+
if r.URL.Path == "/oauth2/sign_out" {
171+
// Clear the cookie if it's there too.
172+
http.SetCookie(w, &http.Cookie{
173+
Name: ObotAccessTokenCookie,
174+
Value: "",
175+
Path: "/",
176+
MaxAge: -1,
177+
})
178+
179+
http.Redirect(w, r, rdParam, http.StatusFound)
180+
return
181+
}
182+
183+
if fromCookie {
184+
// Delete the cookie since it is bad.
185+
http.SetCookie(w, &http.Cookie{
186+
Name: ObotAccessTokenCookie,
187+
Value: "",
188+
Path: "/",
189+
MaxAge: -1,
190+
})
191+
192+
// Just refresh the page and try again.
193+
http.Redirect(w, r, r.URL.String(), http.StatusFound)
194+
return
195+
}
196+
197+
http.Error(w, "auth provider not configured: "+provider, http.StatusBadRequest)
198+
return
199+
}
149200
}
150201

151202
// If the legacy auth provider cookie exists, delete it.

0 commit comments

Comments
 (0)