Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit fa0083f

Browse files
mattjackson220ocket8888
authored andcommitted
updated Oauth (#6508)
(cherry picked from commit b558554)
1 parent 238f1a3 commit fa0083f

File tree

1 file changed

+16
-9
lines changed
  • traffic_ops/traffic_ops_golang/login

1 file changed

+16
-9
lines changed

traffic_ops/traffic_ops_golang/login/login.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func TokenLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
217217
// OauthLoginHandler accepts a JSON web token previously obtained from an OAuth provider, decodes it, validates it, authorizes the user against the database, and returns the login result as either an error or success message
218218
func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
219219
return func(w http.ResponseWriter, r *http.Request) {
220-
handleErrs := tc.GetHandleErrorsFunc(w, r)
221220
defer r.Body.Close()
222221
authenticated := false
223222
resp := struct {
@@ -233,7 +232,17 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
233232
}{}
234233

235234
if err := json.NewDecoder(r.Body).Decode(&parameters); err != nil {
236-
handleErrs(http.StatusBadRequest, err)
235+
api.HandleErr(w, r, nil, http.StatusBadRequest, err, nil)
236+
return
237+
}
238+
239+
matched, err := VerifyUrlOnWhiteList(parameters.AuthCodeTokenUrl, cfg.ConfigTrafficOpsGolang.WhitelistedOAuthUrls)
240+
if err != nil {
241+
api.HandleErr(w, r, nil, http.StatusInternalServerError, nil, err)
242+
return
243+
}
244+
if !matched {
245+
api.HandleErr(w, r, nil, http.StatusForbidden, nil, errors.New("Key URL from token is not included in the whitelisted urls. Received: "+parameters.AuthCodeTokenUrl))
237246
return
238247
}
239248

@@ -249,7 +258,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
249258
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(parameters.ClientId+":"+cfg.OAuthClientSecret))) // per RFC6749 section 2.3.1
250259
}
251260
if err != nil {
252-
log.Errorf("obtaining token using code from oauth provider: %s", err.Error())
261+
api.HandleErr(w, r, nil, http.StatusInternalServerError, nil, fmt.Errorf("obtaining token using code from oauth provider: %w", err))
253262
return
254263
}
255264

@@ -258,7 +267,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
258267
}
259268
response, err := client.Do(req)
260269
if err != nil {
261-
log.Errorf("getting an http client: %s", err.Error())
270+
api.HandleErr(w, r, nil, http.StatusInternalServerError, nil, fmt.Errorf("getting an http client: %w", err))
262271
return
263272
}
264273
defer response.Body.Close()
@@ -289,8 +298,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
289298
}
290299

291300
if encodedToken == "" {
292-
log.Errorf("Token not found in request but is required")
293-
handleErrs(http.StatusBadRequest, errors.New("Token not found in request but is required"))
301+
api.HandleErr(w, r, nil, http.StatusBadRequest, errors.New("Token not found in request but is required"), nil)
294302
return
295303
}
296304

@@ -324,8 +332,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
324332
return selectedKey, nil
325333
})
326334
if err != nil {
327-
handleErrs(http.StatusInternalServerError, errors.New("Error decoding token with message: "+err.Error()))
328-
log.Errorf("Error decoding token: %s\n", err.Error())
335+
api.HandleErr(w, r, nil, http.StatusInternalServerError, nil, errors.New("Error decoding token with message: "+err.Error()))
329336
return
330337
}
331338

@@ -357,7 +364,7 @@ func OauthLoginHandler(db *sqlx.DB, cfg config.Config) http.HandlerFunc {
357364

358365
respBts, err := json.Marshal(resp)
359366
if err != nil {
360-
handleErrs(http.StatusInternalServerError, err)
367+
api.HandleErr(w, r, nil, http.StatusInternalServerError, nil, err)
361368
return
362369
}
363370
w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)

0 commit comments

Comments
 (0)