7
7
"net/http"
8
8
"net/http/httputil"
9
9
"net/url"
10
+ "slices"
10
11
"sort"
11
12
"strings"
12
13
"time"
@@ -96,7 +97,10 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
96
97
}
97
98
98
99
// Determine which auth provider to use.
99
- var provider string
100
+ var (
101
+ provider string
102
+ fromCookie bool
103
+ )
100
104
if param := r .URL .Query ().Get (ObotAuthProviderQueryParam ); param != "" {
101
105
// If the provider is set in the query params, use that.
102
106
provider = param
@@ -110,6 +114,7 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110
114
111
115
var contents CookieContents
112
116
if err = json .Unmarshal ([]byte (cookieValue ), & contents ); err == nil {
117
+ fromCookie = true
113
118
provider = contents .AuthProvider
114
119
115
120
// 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) {
127
132
128
133
// If no provider is set, just use the alphabetically first provider.
129
134
if provider == "" {
130
- providers , err := pm .dispatcher .ListConfiguredAuthProviders (r .Context (), "default" )
135
+ configuredProviders , err := pm .dispatcher .ListConfiguredAuthProviders (r .Context (), "default" )
131
136
if err != nil {
132
137
http .Error (w , fmt .Sprintf ("failed to list configured auth providers: %v" , err ), http .StatusInternalServerError )
133
138
return
134
- }
135
- if len (providers ) == 0 {
139
+ } else if len (configuredProviders ) == 0 {
136
140
// There aren't any auth providers configured. Return an error, unless the user is signing out, in which case, just redirect.
137
141
if r .URL .Path == "/oauth2/sign_out" {
138
142
http .Redirect (w , r , rdParam , http .StatusFound )
@@ -142,10 +146,57 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
142
146
http .Error (w , "no auth providers configured" , http .StatusBadRequest )
143
147
return
144
148
}
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 ]
147
152
})
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
+ }
149
200
}
150
201
151
202
// If the legacy auth provider cookie exists, delete it.
0 commit comments