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:
@@ -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,76 +78,78 @@ 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 (
|
||||||
"SLACK": "https://slack.com/api/users.list?limit=1",
|
providerProbeURLs = map[string]string{
|
||||||
"GOOGLE_WORKSPACE": "https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=1",
|
"SLACK": "https://slack.com/api/users.list?limit=1",
|
||||||
"LINEAR": "https://api.linear.app/graphql",
|
"GOOGLE_WORKSPACE": "https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=1",
|
||||||
"BREX": "https://platform.brexapis.com/v2/users/me",
|
"LINEAR": "https://api.linear.app/graphql",
|
||||||
"HUBSPOT": "https://api.hubapi.com/account-info/v3/details",
|
"BREX": "https://platform.brexapis.com/v2/users/me",
|
||||||
"DOCUSIGN": "https://account-d.docusign.com/oauth/userinfo",
|
"HUBSPOT": "https://api.hubapi.com/account-info/v3/details",
|
||||||
"NOTION": "https://api.notion.com/v1/users/me",
|
"DOCUSIGN": "https://account-d.docusign.com/oauth/userinfo",
|
||||||
"GITHUB": "https://api.github.com/user",
|
"NOTION": "https://api.notion.com/v1/users/me",
|
||||||
"SENTRY": "https://sentry.io/api/0/organizations/",
|
"GITHUB": "https://api.github.com/user",
|
||||||
"INTERCOM": "https://api.intercom.io/me",
|
"SENTRY": "https://sentry.io/api/0/organizations/",
|
||||||
"CLOUDFLARE": "https://api.cloudflare.com/client/v4/user/tokens/verify",
|
"INTERCOM": "https://api.intercom.io/me",
|
||||||
"OPENAI": "https://api.openai.com/v1/models",
|
"CLOUDFLARE": "https://api.cloudflare.com/client/v4/user/tokens/verify",
|
||||||
"SUPABASE": "https://api.supabase.com/v1/organizations",
|
"OPENAI": "https://api.openai.com/v1/models",
|
||||||
"TALLY": "https://api.tally.so/me",
|
"SUPABASE": "https://api.supabase.com/v1/organizations",
|
||||||
"RESEND": "https://api.resend.com/domains",
|
"TALLY": "https://api.tally.so/me",
|
||||||
"ONE_PASSWORD": "https://events.1password.com/api/v1/auditevents",
|
"RESEND": "https://api.resend.com/domains",
|
||||||
}
|
"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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user