Documentation
¶
Overview ¶
Package nango is a credentials.Broker backed by Nango (https://nango.dev).
Nango runs the OAuth flow (Connect UI), stores and refreshes the tokens, and proxies API calls, so the agent only ever handles a connection ID. Only Nango's HTTP API is used, which works both against Nango Cloud and a self-hosted Nango (whose free tier includes Auth, Proxy and Connect UI). Docs:
- connect sessions: https://nango.dev/docs/guides/auth/share-connect-link
- list connections: https://nango.dev/docs/reference/backend/http-api/connections/list
- proxy: https://nango.dev/docs/reference/backend/http-api/proxy/post
- auth webhooks: https://nango.dev/docs/guides/platform/webhooks-from-nango
Index ¶
- Constants
- type Broker
- func (b *Broker) Await(ctx context.Context, c *credentials.Challenge) (credentials.Grant, error)
- func (b *Broker) Begin(ctx context.Context, user, integration string) (*credentials.Challenge, error)
- func (b *Broker) Lookup(ctx context.Context, user, integration string) (credentials.Grant, error)
- func (b *Broker) WebhookHandler() http.Handler
- type BrokerConfig
- type Client
- func (c *Client) CreateConnectSession(ctx context.Context, tags map[string]string, allowedIntegrations []string) (*ConnectSession, error)
- func (c *Client) ListConnections(ctx context.Context, tags map[string]string) ([]Connection, error)
- func (c *Client) Proxy(ctx context.Context, method, integration, connectionID, path string, ...) (*http.Response, error)
- type ConnectSession
- type Connection
Constants ¶
const DefaultBaseURL = "https://api.nango.dev"
DefaultBaseURL is Nango Cloud's API.
const EndUserTag = "end_user_id"
EndUserTag is the connection tag that ties a Nango connection to the agent's user.
const SignatureHeader = "X-Nango-Hmac-Sha256"
SignatureHeader carries the HMAC-SHA256 of a webhook body, keyed with the environment's webhook signing key (which is not the API secret key).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker implements credentials.Broker with Nango Connect and the Nango proxy. Connections are tagged with EndUserTag = the agent's user.
func NewBroker ¶
func NewBroker(client *Client, cfg BrokerConfig) *Broker
NewBroker creates a broker on top of client.
func (*Broker) Await ¶
func (b *Broker) Await(ctx context.Context, c *credentials.Challenge) (credentials.Grant, error)
Await implements credentials.Broker. It polls Nango for the user's connection, and checks at once when WebhookHandler reports one. Either way the grant comes from Lookup, never from the webhook body: a webhook only says when to look.
func (*Broker) Begin ¶
func (b *Broker) Begin(ctx context.Context, user, integration string) (*credentials.Challenge, error)
Begin implements credentials.Broker by creating a Nango connect session.
func (*Broker) Lookup ¶
Lookup implements credentials.Broker. It returns the newest healthy connection the user has for the integration.
func (*Broker) WebhookHandler ¶
WebhookHandler receives Nango's webhooks. A signed auth/creation webhook makes the matching Await check for the connection at once instead of at the next poll. Point the environment's webhook URL at it when Nango can reach this server. Without a signing key it answers 403 to everything.
type BrokerConfig ¶
type BrokerConfig struct {
// PollInterval is how often Await checks for the new connection. Default 2s.
// Polling is what makes the flow work locally, where Nango can't reach a webhook.
PollInterval time.Duration
// WebhookSigningKey verifies webhooks (Nango environment settings, not the API key).
// Without it WebhookHandler refuses every webhook and Await relies on polling.
WebhookSigningKey string
Logger *slog.Logger
}
BrokerConfig configures a Broker.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls Nango's HTTP API with a secret key.
func NewClient ¶
NewClient creates a client. baseURL defaults to DefaultBaseURL and httpClient to a client with a 30 second timeout.
func (*Client) CreateConnectSession ¶
func (c *Client) CreateConnectSession(ctx context.Context, tags map[string]string, allowedIntegrations []string) (*ConnectSession, error)
CreateConnectSession creates a session that lets a user connect one of the allowed integrations. The tags are copied onto the connection the user creates.
func (*Client) ListConnections ¶
ListConnections lists the connections that carry all the given tags.
func (*Client) Proxy ¶
func (c *Client) Proxy(ctx context.Context, method, integration, connectionID, path string, body io.Reader) (*http.Response, error)
Proxy sends a request to the provider's API through Nango, which adds the connection's credentials. path is the provider API path, for example "/calendar/v3/calendars/primary/events".
type ConnectSession ¶
type ConnectSession struct {
Token string `json:"token"`
ConnectLink string `json:"connect_link"`
ExpiresAt time.Time `json:"expires_at"`
}
ConnectSession is a short-lived session for Nango Connect (30 minutes).
type Connection ¶
type Connection struct {
ID int `json:"id"`
ConnectionID string `json:"connection_id"`
Provider string `json:"provider"`
ProviderConfigKey string `json:"provider_config_key"`
Created time.Time `json:"created"`
Tags map[string]string `json:"tags"`
Errors []struct {
Type string `json:"type"`
LogID string `json:"log_id"`
} `json:"errors"`
}
Connection is a connection as listed by Nango (without credentials).