connect
manages the NATS connections used by Overmind infrastructure to the NATS network. This includes:
- Connecting
- Reconnects
- Error handing
- Auth
In order to connect to NATS, a user needs an Nkey and a JWT. This JWT is issued by the Overmind API based on the permissions that the user calling the API has in the OAuth token they supply when calling it. This OAuth token comes from one of the following flows:
If the thing that is connecting already has the required permissions, then you can use the client credentials flow to get a token. If the application whose Client ID you supply is linked to a specific Organization then you can let the API connect you automatically:
flowConfig := ClientCredentialsConfig{
ClientID: "SOMETHING",
ClientSecret: "SECRET",
}
client := NewOAuthTokenClient(
fmt.Sprintf("https://%v/oauth/token", domain),
exchangeURL,
flowConfig,
)
o := NATSOptions{
Servers: []string{"nats://something"},
TokenClient: client,
NumRetries: 3,
RetryDelay: 100 * time.Millisecond,
}
conn, err := o.Connect()
If however you need to connect to a specific Org, then you'll need an application with admin:write
permissions, and to supply the org you want to connect to:
flowConfig := ClientCredentialsConfig{
ClientID: "SOMETHING",
ClientSecret: "SECRET",
Org: "org_somethingHere",
}
client := NewOAuthTokenClient(
fmt.Sprintf("https://%v/oauth/token", domain),
exchangeURL,
flowConfig,
)
o := NATSOptions{
Servers: []string{"nats://something"},
TokenClient: client,
NumRetries: 3,
RetryDelay: 100 * time.Millisecond,
}
conn, err := o.Connect()