Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions internal/handlers/rubygems_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"net/http"
"strings"
"sync"

"github.com/elazarl/goproxy"

Expand All @@ -15,9 +14,8 @@ import (

// RubyGemsServerHandler handles requests to rubygems servers, adding auth.
type RubyGemsServerHandler struct {
credentials []rubyGemsServerCredentials
oidcCredentials map[string]*oidc.OIDCCredential
mutex sync.RWMutex
credentials []rubyGemsServerCredentials
oidcRegistry *oidc.OIDCRegistry
}

type rubyGemsServerCredentials struct {
Expand All @@ -29,8 +27,8 @@ type rubyGemsServerCredentials struct {
// NewRubyGemsServerHandler returns a new RubyGemsServerHandler.
func NewRubyGemsServerHandler(creds config.Credentials) *RubyGemsServerHandler {
handler := RubyGemsServerHandler{
credentials: []rubyGemsServerCredentials{},
oidcCredentials: make(map[string]*oidc.OIDCCredential),
credentials: []rubyGemsServerCredentials{},
oidcRegistry: oidc.NewOIDCRegistry(),
}

for _, cred := range creds {
Expand All @@ -41,16 +39,8 @@ func NewRubyGemsServerHandler(creds config.Credentials) *RubyGemsServerHandler {
host := cred.Host()
url := cred.GetString("url")

oidcCredential, _ := oidc.CreateOIDCCredential(cred)
if oidcCredential != nil {
hostURL := url
if hostURL == "" {
hostURL = host
}
if hostURL != "" {
handler.oidcCredentials[hostURL] = oidcCredential
logging.RequestLogf(nil, "registered %s OIDC credentials for rubygems server: %s", oidcCredential.Provider(), hostURL)
}
// OIDC credentials are not used as static credentials.
if oidcCred, _, _ := handler.oidcRegistry.Register(cred, []string{"url"}, "rubygems server"); oidcCred != nil {
continue
}

Expand All @@ -72,7 +62,7 @@ func (h *RubyGemsServerHandler) HandleRequest(req *http.Request, ctx *goproxy.Pr
}

// Try OIDC credentials first
if oidc.TryAuthOIDCRequestWithPrefix(&h.mutex, h.oidcCredentials, req, ctx) {
if h.oidcRegistry.TryAuth(req, ctx) {
return req, nil
}

Expand Down
Loading