Skip to content

Commit 4baeb27

Browse files
refactor OIDC callback error handling (#485)
1 parent 93994bc commit 4baeb27

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

backend/auth/handler.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (ah *AuthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
7474
var authReq AuthorizeRequest
7575
if r.Method == http.MethodPost {
7676
if err := json.NewDecoder(r.Body).Decode(&authReq); err != nil {
77-
http.Error(w, "invalid JSON request", http.StatusBadRequest)
77+
ah.respondWithError(w, authReq.ClientType, "invalid JSON request", http.StatusBadRequest)
7878
return
7979
}
8080
} else {
@@ -135,19 +135,10 @@ func (ah *AuthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
135135

136136
func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
137137
logger := klog.FromContext(r.Context()).WithValues("method", r.Method, "url", r.URL.String())
138-
if errMsg := r.Form.Get("error"); errMsg != "" {
139-
logger.Error(errors.New(errMsg), "failed to authorize")
140-
http.Error(w, errMsg+": "+r.Form.Get("error_description"), http.StatusBadRequest)
141-
return
142-
}
143138

144-
code := r.Form.Get("code")
145-
if code == "" {
146-
code = r.URL.Query().Get("code")
147-
}
148-
if code == "" {
149-
logger.Error(errors.New("missing code"), "no code in request")
150-
http.Error(w, fmt.Sprintf("no code in request: %q", r.Form), http.StatusBadRequest)
139+
if err := r.ParseForm(); err != nil {
140+
logger.Error(err, "failed to parse form")
141+
ah.respondWithError(w, "", "failed to parse form", http.StatusBadRequest)
151142
return
152143
}
153144

@@ -164,22 +155,39 @@ func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
164155
decoded, err := base64.URLEncoding.DecodeString(state)
165156
if err != nil {
166157
logger.Error(err, "failed to decode state")
167-
http.Error(w, err.Error(), http.StatusBadRequest)
158+
ah.respondWithError(w, "", "invalid state parameter", http.StatusBadRequest)
168159
return
169160
}
170161

171162
authCode := &AuthorizeRequest{}
172163
if err := json.Unmarshal(decoded, authCode); err != nil {
173164
logger.Error(err, "failed to unmarshal authCode")
174-
http.Error(w, err.Error(), http.StatusBadRequest)
165+
ah.respondWithError(w, authCode.ClientType, "invalid state format", http.StatusBadRequest)
166+
return
167+
}
168+
169+
if errMsg := r.Form.Get("error"); errMsg != "" {
170+
desc := r.Form.Get("error_description")
171+
logger.Error(errors.New(errMsg), "OIDC provider returned error", "description", desc)
172+
ah.respondWithError(w, authCode.ClientType, fmt.Sprintf("%s: %s", errMsg, desc), http.StatusBadRequest)
173+
return
174+
}
175+
176+
code := r.Form.Get("code")
177+
if code == "" {
178+
code = r.URL.Query().Get("code")
179+
}
180+
if code == "" {
181+
logger.Error(errors.New("missing code"), "no code in request")
182+
ah.respondWithError(w, authCode.ClientType, "no authorization code in request", http.StatusBadRequest)
175183
return
176184
}
177185
logger.V(2).Info("HandleCallback state unmarshaled", "authCode", authCode)
178186

179187
provider, err := ah.oidc.GetOIDCProvider(r.Context())
180188
if err != nil {
181-
logger.Error(err, "failed to get OIDC provider")
182-
ah.respondWithError(w, authCode.ClientType, err.Error(), http.StatusInternalServerError)
189+
logger.Info("failed to get OIDC provider", "error", err)
190+
ah.respondWithError(w, authCode.ClientType, "failed to get OIDC provider", http.StatusInternalServerError)
183191
return
184192
}
185193

@@ -206,21 +214,21 @@ func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
206214
token, err := provider.OIDCProviderConfig(nil).Exchange(ctx, code, exchangeOpts...)
207215
if err != nil {
208216
logger.Error(err, "failed to exchange token")
209-
http.Error(w, "internal error", http.StatusInternalServerError)
217+
ah.respondWithError(w, authCode.ClientType, "failed to exchange authorization code for token", http.StatusInternalServerError)
210218
return
211219
}
212220

213221
sessionState, err := ah.createSessionState(authCode, token)
214222
if err != nil {
215223
logger.Error(err, "failed to create session sessionState")
216-
http.Error(w, "internal error", http.StatusInternalServerError)
224+
ah.respondWithError(w, authCode.ClientType, "failed to create user session", http.StatusInternalServerError)
217225
return
218226
}
219227
// Set session expiration and store in middleware
220228
err = ah.sessionStore.Save(sessionState)
221229
if err != nil {
222230
logger.Error(err, "failed to save session state")
223-
http.Error(w, "internal error", http.StatusInternalServerError)
231+
ah.respondWithError(w, authCode.ClientType, "failed to save session state", http.StatusInternalServerError)
224232
return
225233
}
226234

@@ -240,15 +248,15 @@ func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
240248
)
241249
if err != nil {
242250
logger.Error(err, "failed to generate JWT token")
243-
http.Error(w, "internal error", http.StatusInternalServerError)
251+
ah.respondWithError(w, authCode.ClientType, "failed to generate JWT token", http.StatusInternalServerError)
244252
return
245253
}
246254

247255
// Redirect to CLI redirect with JWT token
248256
parsedRedirectURL, err := url.Parse(authCode.RedirectURL)
249257
if err != nil {
250258
logger.Error(err, "failed to parse redirect URL")
251-
http.Error(w, "internal error", http.StatusInternalServerError)
259+
ah.respondWithError(w, authCode.ClientType, "failed to parse redirect URL", http.StatusInternalServerError)
252260
return
253261
}
254262

@@ -272,7 +280,7 @@ func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
272280
encoded, err := s.Encode(cookieName, sessionState)
273281
if err != nil {
274282
logger.Error(err, "failed to encode secure session cookie")
275-
http.Error(w, "internal error", http.StatusInternalServerError)
283+
ah.respondWithError(w, authCode.ClientType, "failed to encode session cookie", http.StatusInternalServerError)
276284
return
277285
}
278286

0 commit comments

Comments
 (0)