44 "net/http"
55 "regexp"
66 "strings"
7- "sync"
87
98 "github.com/elazarl/goproxy"
109
@@ -18,9 +17,8 @@ var simpleSuffixRe = regexp.MustCompile(`/\+?simple/?\z`)
1817
1918// PythonIndexHandler handles requests to Python indexes, adding auth.
2019type PythonIndexHandler struct {
21- credentials []pythonIndexCredentials
22- oidcCredentials map [string ]* oidc.OIDCCredential
23- mutex sync.RWMutex
20+ credentials []pythonIndexCredentials
21+ oidcRegistry * oidc.OIDCRegistry
2422}
2523
2624type pythonIndexCredentials struct {
@@ -34,8 +32,8 @@ type pythonIndexCredentials struct {
3432// NewPythonIndexHandler returns a new PythonIndexHandler.
3533func NewPythonIndexHandler (creds config.Credentials ) * PythonIndexHandler {
3634 handler := PythonIndexHandler {
37- credentials : []pythonIndexCredentials {},
38- oidcCredentials : make ( map [ string ] * oidc.OIDCCredential ),
35+ credentials : []pythonIndexCredentials {},
36+ oidcRegistry : oidc .NewOIDCRegistry ( ),
3937 }
4038
4139 for _ , cred := range creds {
@@ -47,16 +45,21 @@ func NewPythonIndexHandler(creds config.Credentials) *PythonIndexHandler {
4745
4846 oidcCredential , _ := oidc .CreateOIDCCredential (cred )
4947 if oidcCredential != nil {
50- host := cred .Host ()
51- if host == "" && indexURL != "" {
52- regURL , err := helpers .ParseURLLax (indexURL )
53- if err == nil {
54- host = regURL .Hostname ()
55- }
48+ // Normalize the registration URL by stripping the /simple or /+simple
49+ // suffix, matching how static credentials are matched at request time.
50+ // Without this, a config of /dependabot/+simple/ would not prefix-match
51+ // requests to /dependabot/pkg/a.
52+ regURL := indexURL
53+ if regURL == "" {
54+ regURL = cred .GetString ("url" )
5655 }
57- if host != "" {
58- handler .oidcCredentials [host ] = oidcCredential
59- logging .RequestLogf (nil , "registered %s OIDC credentials for python index: %s" , oidcCredential .Provider (), host )
56+ if regURL != "" {
57+ regURL = simpleSuffixRe .ReplaceAllString (regURL , "/" )
58+ } else {
59+ regURL = cred .Host ()
60+ }
61+ if regURL != "" {
62+ handler .oidcRegistry .RegisterURL (regURL , oidcCredential , "python index" )
6063 }
6164 continue
6265 }
@@ -85,7 +88,7 @@ func (h *PythonIndexHandler) HandleRequest(req *http.Request, ctx *goproxy.Proxy
8588 }
8689
8790 // Try OIDC credentials first
88- if oidc . TryAuthOIDCRequestWithPrefix ( & h . mutex , h . oidcCredentials , req , ctx ) {
91+ if h . oidcRegistry . TryAuth ( req , ctx ) {
8992 return req , nil
9093 }
9194
0 commit comments