Standardized app authentication
Standardized app authentication gives every app and third-party agent a consistent way to connect accounts, check and display connection status, and manage access, while allowing each app or agent to retain its own authentication experience.
Enabling standardized authentication for your app
To get started, your app will need:
- A bot user.
- Event Subscriptions (or Socket Mode), subscribed to the
user_connectionanduser_connection_status_requestedevents. Slack sendsuser_connectionwhen a user clicks Connect, anduser_connection_status_requestedwhen it needs to know whether a user is connected. - Interactivity (or Socket Mode). The buttons inside Slack's connect modal reach your app as
block_actionspayloads.
Then, set features.user_connections.is_enabled to true in your app manifest, and list the hosts your Connect and Disconnect URLs point to in features.user_connections.auth_hosts. Slack won't save the manifest unless your app subscribes to both events. Refer to the app manifest reference for details.
Here are the parts of a manifest this feature needs:
{
"display_information": {
"name": "Borant"
},
"features": {
"bot_user": {
"display_name": "Borant"
},
"user_connections": {
"is_enabled": true,
"auth_hosts": ["accounts.example.com"]
}
},
"oauth_config": {
"scopes": {
"bot": ["chat:write"]
}
},
"settings": {
"event_subscriptions": {
"request_url": "https://example.com/slack/events",
"bot_events": ["user_connection", "user_connection_status_requested"]
},
"interactivity": {
"is_enabled": true,
"request_url": "https://example.com/slack/interactions"
}
}
}
If your app uses Socket Mode, set settings.socket_mode_enabled to true instead of adding the two Request URLs.
SDK support
The Slack SDKs support the apps.user.connection.* API methods, the user_connection and user_connection_status_requested events, and the auth provider block starting with these releases. Any later release also works.
| Language | Package | Minimum version |
|---|---|---|
| JavaScript | @slack/web-api | 8.3.0 |
| JavaScript | @slack/types | 3.3.0 |
| Python | slack_sdk | 3.46.0 |
| Java | Java Slack SDK | 1.53.0 |
Testing against a local server
While you build, your Connect and Disconnect URLs can point at a server on your own machine: http://localhost, https://localhost, http://127.0.0.1, or https://127.0.0.1, with an optional port, such as http://localhost:3000/oauth/start. You don't add localhost to auth_hosts for this.
Localhost URLs have two limits:
- Only your app's collaborators get them: Slack sends a localhost URL only to people listed as collaborators on the app. For anyone else in the workspace, the same call fails with
invalid_auth_urlorinvalid_disconnect_url, so add teammates who test with you as collaborators. On Enterprise Grid, test from the workspace that owns the app. A collaborator who clicks Connect in another workspace of the org gets the same error. - Only while your app isn't distributed or listed in the Slack Marketplace: once it is, localhost URLs fail the same way, so switch to your real
httpshost before you distribute your app.
Localhost works in the connect modal that the apps.user.connection.requestAuth API method opens, and in an auth provider block in a modal you open or a Home tab you publish for a collaborator. The views.update and views.push API methods don't say who will see the view, so they reject a localhost URL.
Localhost means the device the user clicks Connect on, so test from the computer running your server. Other local addresses, like 0.0.0.0, [::1], or a name ending in .localhost, aren't accepted.
Standardized app authentication flow
Here's what happens when a user connects their account to your app:
- The user clicks Connect in your app in Slack. Slack sends your app the
user_connectionevent with asubtypeofconnect, the user's ID inuser, and atrigger_id. - Your app calls the
apps.user.connection.requestAuthAPI method with thattrigger_idand aprimary_providerdescribing the account to connect. Slack opens its connect modal, built from an auth provider block. - The user clicks Connect in the modal. Slack opens the provider's
auth_urlin a new tab and sends your app ablock_actionspayload. - The user signs in to your service. Your app finishes sign-in: it calls the
apps.user.connection.requestAuthAPI method again with the user'suser_id(and notrigger_id) andis_connected: true. That records the user as connected and updates the open modal.
When a connected user clicks Manage connection, Slack sends the same connect event, so always pass your current is_connected value and account details.
Enterprise Search apps receive the same user_connection event for connection reporting, which works a little differently. Refer to connection reporting using Enterprise Search for that flow.
Handling the user_connection event
Slack sends this event once and doesn't retry it, since its trigger_id can only be used once. Call the apps.user.connection.requestAuth API method right away: if the modal doesn't open within about 3 seconds, the user sees an error, and Slack may refuse a later call with an expired_trigger_id error. The trigger_id itself expires after 30 seconds.
- Bolt JS
- Bolt Python
app.event('user_connection', async ({ event, context, client, logger }) => {
// Standardized app authentication only sends the `connect` subtype
if (event.subtype !== 'connect') return;
try {
await client.apps.user.connection.requestAuth({
trigger_id: event.trigger_id,
primary_provider: await buildProvider(event.user, context),
});
} catch (error) {
logger.error(error);
}
});
@app.event("user_connection")
def handle_user_connection(event, context, client, logger):
# Standardized app authentication only sends the `connect` subtype
if event["subtype"] != "connect":
return
try:
client.apps_user_connection_requestAuth(
trigger_id=event["trigger_id"],
primary_provider=build_provider(event["user"], context),
)
except Exception as e:
logger.error(f"Error opening the connect modal: {e}")
buildProvider stands in for your own code. It returns the provider for this user, with value, name, and the user's current is_connected value:
- When the user is connected, include
connected_identity(andconnected_context, if you show one) so the modal shows their account. Slack keeps these for up to an hour and never saves them with a view, so send them every time. Manage connection opens this same modal. - When they aren't connected, include an
auth_urlwith a signed, single-usestatevalue. Put the user's ID and theteamIdorenterpriseIdfromcontext(team_idorenterprise_idin Python) in it, so your OAuth callback knows whose status to report and which install's token to use. The user may not be signed in to your service in the browser that opensauth_url, so start sign-in from thestatevalue rather than relying on an existing web session.
Here's a provider for a user who isn't connected yet:
{
"value": "borant",
"name": {
"type": "plain_text",
"text": "Borant"
},
"description": {
"type": "plain_text",
"text": "Get updates from Borant in Slack."
},
"is_connected": false,
"auth_url": "https://accounts.example.com/oauth/start?state=SIGNED_STATE"
}
The primary_provider field isn't a block, so leave out type, block_id, and fallback. You only build a full auth provider block for a modal or Home tab you publish yourself.
For an org-wide install, the event's top-level team_id is the org's ID, and the payload doesn't say which workspace the user clicked Connect from. Use authorizations to find your install. For a workspace install, don't rely on team_id being the workspace the user is looking at: from a DM, it can be another workspace the user belongs to. On Enterprise Grid, a user's connection status applies across the org.
Handling clicks in the connect modal
The buttons in the connect modal reach your app as block_actions payloads with fixed action_ids. The provider's value is in actions[0].value.
- Connect has the
action_idauth_provider_connect. If you set anauth_url, Slack has already opened it, so you only need to acknowledge the request. Slack may also skip the modal and open yourauth_urlfor the user straight away, in which case no Connect action reaches your app, so don't rely on it to know the user started connecting. If anauth_urldoesn't fit your sign-in flow, leave it out and handle the click like any other Block Kit action. Use the payload'strigger_idto push your own view on top of the connect modal withviews.push, for example a form for an API key. Block action triggers expire after 3 seconds, so push the view right away. - Disconnect has the
action_idauth_provider_disconnect. Slack always includes it for a connected provider, either as a button or as the first option in your overflow menu. If you set adisconnect_url, Slack opens it too, so you can revoke access there or in this handler. Either way, report the new status.
- Bolt JS
- Bolt Python
app.action('auth_provider_connect', async ({ ack }) => {
// Slack already opened auth_url in a new tab
await ack();
});
app.action('auth_provider_disconnect', async ({ ack, body, context, client, logger }) => {
await ack();
const userId = body.user.id;
const provider = body.actions[0].value;
try {
await revokeAccess(userId, provider);
// Flip the open modal back to Connect
await client.apps.user.connection.requestAuth({
user_id: userId,
primary_provider: await buildProvider(userId, context),
});
} catch (error) {
logger.error(error);
}
});
@app.action("auth_provider_connect")
def handle_connect_click(ack):
# Slack already opened auth_url in a new tab
ack()
@app.action("auth_provider_disconnect")
def handle_disconnect_click(ack, body, context, client, logger):
ack()
user_id = body["user"]["id"]
provider = body["actions"][0]["value"]
try:
revoke_access(user_id, provider)
# Flip the open modal back to Connect
client.apps_user_connection_requestAuth(
user_id=user_id,
primary_provider=build_provider(user_id, context),
)
except Exception as e:
logger.error(f"Error disconnecting: {e}")
If you add your own overflow menu with the accessory field, Slack puts Disconnect first in that menu, and picking it arrives on your overflow's action_id instead. Refer to handling clicks on the auth provider block page for details.
Finishing sign-in
When your OAuth callback has verified the state value and stored the user's token, call the apps.user.connection.requestAuth API method with the user's user_id and is_connected: true. It records the user as connected and updates the modal they still have open. The callback runs outside Bolt, so create a client with the bot token for the install you saved in state.
- Bolt JS
- Bolt Python
const { WebClient } = require('@slack/web-api');
async function finishSignIn({ userId, teamId, enterpriseId, accountName }) {
const client = new WebClient(await botTokenFor({ teamId, enterpriseId }));
await client.apps.user.connection.requestAuth({
user_id: userId,
primary_provider: {
value: 'borant',
name: { type: 'plain_text', text: 'Borant' },
is_connected: true,
connected_identity: { type: 'plain_text', text: accountName },
},
});
}
from slack_sdk import WebClient
def finish_sign_in(user_id, team_id, enterprise_id, account_name):
client = WebClient(token=bot_token_for(team_id, enterprise_id))
client.apps_user_connection_requestAuth(
user_id=user_id,
primary_provider={
"value": "borant",
"name": {"type": "plain_text", "text": "Borant"},
"is_connected": True,
"connected_identity": {"type": "plain_text", "text": account_name},
},
)
If no modal is open, because the user closed it or connected without one, the call to the apps.user.connection.requestAuth API method still records the status and succeeds with an auth_modal_not_found warning.
While the user signs in, the modal shows that it's connecting. Slack doesn't time out while the user is away, so sign-in can take as long as it needs, for example to complete multi-factor authentication. If the user comes back to Slack before your callback reports in, the modal shows "Still signing in? Finish in the other tab, or try again." and offers Connect again. That opens the same auth_url, so keep the state value valid until a callback uses it. When sign-in does finish, make the same call: the modal switches to Connected if it's still open, and the status is recorded either way.
Reporting connection status
Call the apps.user.connection.update API method whenever a user's connection changes, including changes that happen outside Slack (for example, the user revokes access in your service). If you don't, Slack keeps showing the old status.
When the user connects or disconnects in the connect modal, the apps.user.connection.requestAuth API method records the is_connected value you pass, so you don't need to call update as well. Use update for changes with no modal to refresh, like the ones above.
Answering status requests
When Slack needs a user's status and doesn't have a record of it (for example, when the user views your app in Slack), it sends your app the user_connection_status_requested event. Bolt acknowledges events for you, but acknowledging isn't enough: until you call the apps.user.connection.update API method with the user's current status, Slack shows no Connect button for that user. Slack asks at most once an hour for each user, so a missed answer can leave the user without a button for up to an hour.
- Bolt JS
- Bolt Python
app.event('user_connection_status_requested', async ({ event, client }) => {
await client.apps.user.connection.update({
user_id: event.user,
status: (await isConnected(event.user)) ? 'connected' : 'disconnected',
});
});
@app.event("user_connection_status_requested")
def handle_status_requested(event, client):
client.apps_user_connection_update(
user_id=event["user"],
status="connected" if is_connected(event["user"]) else "disconnected",
)
To report many users at once: for example, to backfill users who connected before you turned on standardized authentication, call the apps.user.connection.bulkUpdate API method with up to 1,000 user IDs per call.
When you can, report a user's status before Slack asks, so they see Connect the first time they look. For example, call the apps.user.connection.update API method for the installing user when your app is installed, report users you already know with the apps.user.connection.bulkUpdate API method, or call update when you receive the app_home_opened event from a user you haven't reported yet.
Common patterns
Asking users to connect in your Home tab
To show your Home tab's content only to connected users, publish a view with an auth provider block for users who aren't connected, and your usual content once they are. Republish the Home tab after sign-in finishes and after a disconnect.
Several accounts or orgs
Slack keeps one connection status per user for your app, and its connect modal shows one provider. If a user can connect several accounts or orgs in your service, put one auth provider block per account in your Home tab or in a modal you open, each with its own value, and report the user as connected while any of them is. Status per account, org, or workspace isn't supported.
Enterprise-managed authorization
If your manifest sets oauth_config.is_enterprise_managed_auth_supported to true, the org has enterprise-managed authorization turned on, and your app is installed org-wide, the user's status is ema_configured. Users see Connection managed by your admin instead of a Connect button. Combining enterprise-managed authorization with per-user connections isn't supported.
Slack Marketplace review
If your app is listed in the Slack Marketplace, your auth_url must take the user to a sign-in or connection flow for your service, not to a marketing or home page.