Simplify ConnectorRegistry to pure store

Remove provider-default mutation from Register; defaults are now
applied via ApplyProviderDefaults before registration. Rename
receiver from cr to r. Fix error messages to follow the cannot
convention. Wrap providerProbeURLs in var () block.

Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
Aurélien Sibiril
2026-04-06 20:20:51 +02:00
parent f7085f98be
commit f3db9d186b

View File

@@ -36,33 +36,34 @@ func NewConnectorRegistry() *ConnectorRegistry {
} }
} }
func (cr *ConnectorRegistry) Register(provider string, connector Connector) error { func (r *ConnectorRegistry) Register(provider string, c Connector) error {
cr.Lock() r.Lock()
defer cr.Unlock() defer r.Unlock()
if _, ok := cr.connectors[provider]; ok { if _, ok := r.connectors[provider]; ok {
return fmt.Errorf("connector %q already registered", provider) return fmt.Errorf("cannot register connector %q: already registered", provider)
} }
cr.connectors[provider] = connector
r.connectors[provider] = c
return nil return nil
} }
func (cr *ConnectorRegistry) Get(provider string) (Connector, error) { func (r *ConnectorRegistry) Get(provider string) (Connector, error) {
cr.RLock() r.RLock()
defer cr.RUnlock() defer r.RUnlock()
connector, ok := cr.connectors[provider] c, ok := r.connectors[provider]
if !ok { if !ok {
return nil, fmt.Errorf("connector %q not found", provider) return nil, fmt.Errorf("cannot find connector %q", provider)
} }
return connector, nil return c, nil
} }
func (cr *ConnectorRegistry) Initiate(ctx context.Context, provider string, organizationID gid.GID, r *http.Request) (string, error) { func (r *ConnectorRegistry) Initiate(ctx context.Context, provider string, organizationID gid.GID, req *http.Request) (string, error) {
connector, err := cr.Get(provider) c, err := r.Get(provider)
if err != nil { if err != nil {
return "", fmt.Errorf("cannot initiate connector: %w", err) return "", fmt.Errorf("cannot initiate connector: %w", err)
} }
return connector.Initiate(ctx, provider, organizationID, r) return c.Initiate(ctx, provider, organizationID, req)
} }
// ExtractProviderFromState decodes the OAuth2 state token without // ExtractProviderFromState decodes the OAuth2 state token without
@@ -77,41 +78,42 @@ func ExtractProviderFromState(stateToken string) (string, error) {
} }
if payload.Data.Provider == "" { if payload.Data.Provider == "" {
return "", fmt.Errorf("state token has no provider") return "", fmt.Errorf("cannot extract provider from state token: missing provider field")
} }
return payload.Data.Provider, nil return payload.Data.Provider, nil
} }
func (cr *ConnectorRegistry) Complete(ctx context.Context, provider string, r *http.Request) (Connection, *gid.GID, string, error) { func (r *ConnectorRegistry) Complete(ctx context.Context, provider string, req *http.Request) (Connection, *gid.GID, string, error) {
connector, err := cr.Get(provider) c, err := r.Get(provider)
if err != nil { if err != nil {
return nil, nil, "", fmt.Errorf("cannot complete connector: %w", err) return nil, nil, "", fmt.Errorf("cannot complete connector: %w", err)
} }
return connector.Complete(ctx, r) return c.Complete(ctx, req)
} }
// CompleteWithState completes the OAuth2 flow and returns the full state // CompleteWithState completes the OAuth2 flow and returns the full state
// including any reconnection context (ConnectorID). // including any reconnection context (ConnectorID).
func (cr *ConnectorRegistry) CompleteWithState(ctx context.Context, provider string, r *http.Request) (Connection, *OAuth2State, error) { func (r *ConnectorRegistry) CompleteWithState(ctx context.Context, provider string, req *http.Request) (Connection, *OAuth2State, error) {
connector, err := cr.Get(provider) c, err := r.Get(provider)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("cannot complete connector: %w", err) return nil, nil, fmt.Errorf("cannot complete connector: %w", err)
} }
oauth2Connector, ok := connector.(*OAuth2Connector) oauth2Connector, ok := c.(*OAuth2Connector)
if !ok { if !ok {
return nil, nil, fmt.Errorf("connector %q is not an OAuth2 connector", provider) return nil, nil, fmt.Errorf("cannot complete connector %q: not an OAuth2 connector", provider)
} }
return oauth2Connector.CompleteWithState(ctx, r) return oauth2Connector.CompleteWithState(ctx, req)
} }
// providerProbeURLs maps provider names to lightweight API endpoints // providerProbeURLs maps provider names to lightweight API endpoints
// used to verify OAuth token validity. Each URL must accept a GET // used to verify OAuth token validity. Each URL must accept a GET
// request with a Bearer token and return 401/403 for invalid tokens. // request with a Bearer token and return 401/403 for invalid tokens.
var providerProbeURLs = map[string]string{ var (
providerProbeURLs = map[string]string{
"SLACK": "https://slack.com/api/users.list?limit=1", "SLACK": "https://slack.com/api/users.list?limit=1",
"GOOGLE_WORKSPACE": "https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=1", "GOOGLE_WORKSPACE": "https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=1",
"LINEAR": "https://api.linear.app/graphql", "LINEAR": "https://api.linear.app/graphql",
@@ -128,25 +130,26 @@ var providerProbeURLs = map[string]string{
"TALLY": "https://api.tally.so/me", "TALLY": "https://api.tally.so/me",
"RESEND": "https://api.resend.com/domains", "RESEND": "https://api.resend.com/domains",
"ONE_PASSWORD": "https://events.1password.com/api/v1/auditevents", "ONE_PASSWORD": "https://events.1password.com/api/v1/auditevents",
} }
)
// GetProbeURL returns the probe URL for a provider. // GetProbeURL returns the probe URL for a provider.
func (cr *ConnectorRegistry) GetProbeURL(provider string) string { func (r *ConnectorRegistry) GetProbeURL(provider string) string {
return providerProbeURLs[provider] return providerProbeURLs[provider]
} }
// GetOAuth2RefreshConfig returns the OAuth2 refresh configuration for a provider. // GetOAuth2RefreshConfig returns the OAuth2 refresh configuration for a provider.
// Returns nil if the provider is not found or is not an OAuth2 connector. // Returns nil if the provider is not found or is not an OAuth2 connector.
func (cr *ConnectorRegistry) GetOAuth2RefreshConfig(provider string) *OAuth2RefreshConfig { func (r *ConnectorRegistry) GetOAuth2RefreshConfig(provider string) *OAuth2RefreshConfig {
cr.RLock() r.RLock()
defer cr.RUnlock() defer r.RUnlock()
connector, ok := cr.connectors[provider] c, ok := r.connectors[provider]
if !ok { if !ok {
return nil return nil
} }
oauth2Connector, ok := connector.(*OAuth2Connector) oauth2Connector, ok := c.(*OAuth2Connector)
if !ok { if !ok {
return nil return nil
} }