Skip to content

Commit 20219dd

Browse files
committed
Reverse token path logic and add redirect path for existing tokens
1 parent a66e6e7 commit 20219dd

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

oauth.go

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,43 @@ func oauthRequestHandler(w http.ResponseWriter, r *http.Request) {
423423
accept = v[0]
424424
}
425425
}
426-
if hasToken {
426+
if hasToken && !strings.Contains(r.URL.Path, "token") {
427+
// case of existing token CERN SSO or IAM and we not asked for token path
428+
token := getToken(r)
429+
if token == "" {
430+
http.Error(w, "unable to get user token", http.StatusUnauthorized)
431+
return
432+
}
433+
attrs, err := inspectTokenProviders(token)
434+
if err != nil {
435+
log.Println("fail to inspect user token", err)
436+
http.Error(w, "unable to get user token", http.StatusInternalServerError)
437+
return
438+
}
439+
// in case of IAM token we'll get token attributes as user info
440+
userData["email"] = attrs.Email
441+
userData["id"] = attrs.ClientID
442+
userData["name"] = attrs.UserName
443+
userData["exp"] = attrs.Expiration
444+
// set CMS headers
445+
if Config.CMSHeaders {
446+
if Config.Verbose > 2 {
447+
if err := printJSON(userData, "user data"); err != nil {
448+
log.Println("unable to print user data")
449+
}
450+
}
451+
if Config.Verbose > 3 {
452+
CMSAuth.SetCMSHeadersByKey(r, userData, CricRecords, "id", "oauth", true)
453+
} else {
454+
CMSAuth.SetCMSHeadersByKey(r, userData, CricRecords, "id", "oauth", false)
455+
}
456+
if Config.Verbose > 0 {
457+
printHTTPRequest(r, "cms headers")
458+
}
459+
}
460+
redirect(w, r)
461+
return
462+
} else if userInfo != nil || hasToken {
427463
// renew existing token
428464
if r.URL.Path == fmt.Sprintf("%s/token/renew", Config.Base) {
429465
var token string
@@ -460,35 +496,15 @@ func oauthRequestHandler(w http.ResponseWriter, r *http.Request) {
460496
return
461497
}
462498
// decode userInfo
463-
if userInfo != nil {
464-
switch t := userInfo.(type) {
465-
case *json.RawMessage:
466-
err := json.Unmarshal(*t, &userData)
467-
if err != nil {
468-
msg := fmt.Sprintf("unable to decode user data, %v", err)
469-
status = http.StatusInternalServerError
470-
http.Error(w, msg, status)
471-
return
472-
}
473-
}
474-
} else {
475-
// in case of IAM token we'll get token attributes as user info
476-
// extract token from a request
477-
token := getToken(r)
478-
if token == "" {
479-
http.Error(w, "unable to get user token", http.StatusUnauthorized)
480-
return
481-
}
482-
attrs, err := inspectTokenProviders(token)
499+
switch t := userInfo.(type) {
500+
case *json.RawMessage:
501+
err := json.Unmarshal(*t, &userData)
483502
if err != nil {
484-
log.Println("fail to inspect user token", err)
485-
http.Error(w, "unable to get user token", http.StatusInternalServerError)
503+
msg := fmt.Sprintf("unable to decode user data, %v", err)
504+
status = http.StatusInternalServerError
505+
http.Error(w, msg, status)
486506
return
487507
}
488-
userData["email"] = attrs.Email
489-
userData["id"] = attrs.ClientID
490-
userData["name"] = attrs.UserName
491-
userData["exp"] = attrs.Expiration
492508
}
493509
// set CMS headers
494510
if Config.CMSHeaders {

0 commit comments

Comments
 (0)