Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.0 KB
Raw Blame
package composio

import (
	"context"
	"errors"
	"net/http"
	"net/url"
	"strconv"
	"strings"
)

// AuthConfig mirrors a subset of a Composio auth config — the project-level
// record that defines HOW users authenticate with a toolkit (the OAuth client,
// API-key scheme, etc.). The connect-link flow needs its opaque `id` (ac_…);
// the other fields drive selection when a toolkit has more than one.
//
// Spec: https://docs.composio.dev/reference/v3/api-reference/auth-configs/getAuthConfigs
type AuthConfig struct {
	ID   string `json:"id"`
	Name string `json:"name,omitempty"`
	// Toolkit carries at least the slug (and a logo) the config belongs to.
	Toolkit    Toolkit `json:"toolkit"`
	AuthScheme string  `json:"auth_scheme,omitempty"`
	// IsComposioManaged is true for Composio's managed OAuth app and false for a
	// custom (bring-your-own client_id/secret) config — the white-label case.
	IsComposioManaged bool `json:"is_composio_managed"`
	// Status is "ENABLED" or "DISABLED". The list endpoint hides disabled
	// configs by default (show_disabled=false).
	Status        string `json:"status,omitempty"`
	CreatedAt     string `json:"created_at,omitempty"`
	LastUpdatedAt string `json:"last_updated_at,omitempty"`
}

// ListAuthConfigsRequest collects the optional filters of GET /auth_configs.
// Zero values are omitted from the query string.
type ListAuthConfigsRequest struct {
	// ToolkitSlugs filters to specific toolkits; sent as a single
	// comma-separated `toolkit_slug` query param per the v3 spec.
	ToolkitSlugs []string
	// IsComposioManaged, when non-nil, filters by managed vs custom configs.
	IsComposioManaged *bool
	// ShowDisabled includes disabled configs (default false = enabled only).
	ShowDisabled bool
	// Search matches auth configs by name or id.
	Search string
	// Limit is the page size (max 1000 upstream). 0 = upstream default.
	Limit int
	// Cursor pages through results.
	Cursor string
}

// ListAuthConfigsResponse is the typed paginated response.
type ListAuthConfigsResponse struct {
	Items      []AuthConfig `json:"items"`
	NextCursor string       `json:"next_cursor,omitempty"`
	TotalItems int          `json:"total_items,omitempty"`
}

type createAuthConfigRequest struct {
	Toolkit    createAuthConfigToolkit `json:"toolkit"`
	AuthConfig createAuthConfigBody    `json:"auth_config"`
}

type createAuthConfigToolkit struct {
	Slug string `json:"slug"`
}

type createAuthConfigBody struct {
	Type string `json:"type"`
}

type createAuthConfigResponse struct {
	AuthConfig AuthConfig `json:"auth_config"`
}

// CreateManagedAuthConfig registers an auth config for a toolkit that uses
// Composio's managed OAuth app, making the toolkit connectable in the project.
// It returns the new auth_config_id.
//
// Spec: https://docs.composio.dev/reference/v3/api-reference/auth-configs/postAuthConfigs
func (c *Client) CreateManagedAuthConfig(ctx context.Context, toolkitSlug string) (string, error) {
	if toolkitSlug == "" {
		return "", errors.New("composio: CreateManagedAuthConfig: toolkitSlug is required")
	}
	var out createAuthConfigResponse
	body := createAuthConfigRequest{
		Toolkit:    createAuthConfigToolkit{Slug: toolkitSlug},
		AuthConfig: createAuthConfigBody{Type: "use_composio_managed_auth"},
	}
	if err := c.do(c.newRequest(ctx).SetBody(body), http.MethodPost, "/auth_configs", &out); err != nil {
		return "", err
	}
	if out.AuthConfig.ID == "" {
		return "", errors.New("composio: CreateManagedAuthConfig: response has no auth config id")
	}
	return out.AuthConfig.ID, nil
}

type createCustomAuthConfigRequest struct {
	Toolkit    createAuthConfigToolkit `json:"toolkit"`
	AuthConfig customAuthConfigBody    `json:"auth_config"`
}

type customAuthConfigBody struct {
	Type        string         `json:"type"`
	AuthScheme  string         `json:"authScheme"`
	Name        string         `json:"name,omitempty"`
	Credentials map[string]any `json:"credentials"`
}

// CreateCustomAuthConfig registers an auth config with the given scheme
// (API_KEY, BEARER_TOKEN, BASIC) and no project credentials, so each user
// enters their own on the hosted Connect Link page. It returns the new
// auth_config_id.
//
// Spec: https://docs.composio.dev/reference/v3/api-reference/auth-configs/postAuthConfigs
func (c *Client) CreateCustomAuthConfig(ctx context.Context, toolkitSlug, authScheme, name string) (string, error) {
	if toolkitSlug == "" || authScheme == "" {
		return "", errors.New("composio: CreateCustomAuthConfig: toolkitSlug and authScheme are required")
	}
	var out createAuthConfigResponse
	body := createCustomAuthConfigRequest{
		Toolkit: createAuthConfigToolkit{Slug: toolkitSlug},
		AuthConfig: customAuthConfigBody{
			Type:        "use_custom_auth",
			AuthScheme:  strings.ToUpper(authScheme),
			Name:        name,
			Credentials: map[string]any{},
		},
	}
	if err := c.do(c.newRequest(ctx).SetBody(body), http.MethodPost, "/auth_configs", &out); err != nil {
		return "", err
	}
	if out.AuthConfig.ID == "" {
		return "", errors.New("composio: CreateCustomAuthConfig: response has no auth config id")
	}
	return out.AuthConfig.ID, nil
}

// ListAuthConfigs returns the auth configs registered in the project, with
// optional filters. The project is resolved from the x-api-key (a project API
// key authenticates to exactly one project), so no project id is passed.
func (c *Client) ListAuthConfigs(ctx context.Context, req ListAuthConfigsRequest) (*ListAuthConfigsResponse, error) {
	q := url.Values{}
	if len(req.ToolkitSlugs) > 0 {
		q.Set("toolkit_slug", strings.Join(req.ToolkitSlugs, ","))
	}
	if req.IsComposioManaged != nil {
		q.Set("is_composio_managed", strconv.FormatBool(*req.IsComposioManaged))
	}
	if req.ShowDisabled {
		q.Set("show_disabled", "true")
	}
	if req.Search != "" {
		q.Set("search", req.Search)
	}
	if req.Limit > 0 {
		q.Set("limit", strconv.Itoa(req.Limit))
	}
	if req.Cursor != "" {
		q.Set("cursor", req.Cursor)
	}

	path := "/auth_configs"
	if encoded := q.Encode(); encoded != "" {
		path += "?" + encoded
	}

	var out ListAuthConfigsResponse
	if err := c.do(c.newRequest(ctx), http.MethodGet, path, &out); err != nil {
		return nil, err
	}
	return &out, nil
}