bolt-python icon indicating copy to clipboard operation
bolt-python copied to clipboard

Incorrect user ID being used when attempting to retrieve token from InstallationStore

Open vstratful opened this issue 3 years ago • 5 comments

Hey,

I'm attempting to create my own AsyncInstallationStore and running into issues, so as a debug measure, I'm trying to get things working first with the provided FileInstallationStore.

This seems to be working correctly, and I can complete the OAuth flow. I see the appropriate files created, and I'm able to respond to messages with say().

The problem begins if I try to use the AsyncApp.client method to call the Slack API.

I have a helper function:

async def get_users_email(user_id):
    response = await slack.client.users_info(user=user_id)
    return response["user"]["profile"]["email"]

I'm calling this helper function inside my message handler, like so:

@slack.message(re.compile(".*"))
async def incoming_message(message, say):
    # say() sends a message to the channel (which can be a DM) where the event was triggered
    await say(
        text=f"Hey there <@{message['user']}>, you said: *{message['text']}*, and your email address is {await get_users_email(message['user'])}!",
    )
    users_email = await get_users_email(message["user"])
    await insert_into_db(message["text"], users_email)

When I trigger this event without calling my helper function, everything works correctly and the token stored in /home/vstratful/.bolt-app-installation/5811363910007.5826251703859/none-T05PVAPSS07/installer-U05Q6UVF2S1-latest is retrieved and my response is sent.

When I trigger this event with my helper function, I'm seeing an error in the logs that authentication failed and I'm seeing the following log entry:

DEBUG:slack_sdk.oauth.installation_store.file:Installation data missing for enterprise: none, team: T05PVAPSS07: [Errno 2] No such file or directory: '/home/vstratful/.bolt-app-installation/5811363910007.5826251703859/none-T05PVAPSS07/installer-U05QP27SF89-latest'

Please note the ID in the path, U05QP27SF89. This is a different ID and this file does not exist. I'm unable to determine where this ID is coming from.

Please note, I do not experience this behaviour if I don't use FileInstallationStore and just hard code my SLACK_BOT_TOKEN instead. In that scenario, everything works as expected.

Expected behaviour: I should be able to call AsyncApp.client.users_info inside an event handler, and the AsyncApp.client should use the correct authentication details.

vstratful avatar Sep 07 '23 11:09 vstratful

It looks like the ID in question, U05QP27SF89, is the ID of my bot user. If I go to the profile of my bot, and click "Copy member ID", I'm seeing this value.

vstratful avatar Sep 07 '23 12:09 vstratful

Hi @vstratful, thank you for writing in.

When you load the SLACK_BOT_TOKEN environment variable, the singleton instance of WebClient (in your case, AsyncWebClient), which is associated with App/AsyncApp, operates using the attached token. However, when you enable an OAuth flow for app installation, AsyncApp#client isn't persistently linked with a particular workspace. Therefore, it ceases to function. Instead, your app must resolve which token to use for the workspace for an incoming request.

Regarding the user ID topic, if your InstallationStore doesn't support any user-level scopes (meaning a xoxp- prefix token for each end-user, not just a bot user for a workspace), you don't need to worry about it. The FileInstallationStore prints debug logs when attempting to locate a per-user installation, as it also supports this use case. This doesn't mean your custom code must do the same. I highly suggest that you read the code of the built-in InstallationStores to learn how to build your own. I've written most of them. If you have more specific questions, I am happy to provide answers. Hope you'll figure what to do out soon!

seratch avatar Sep 07 '23 12:09 seratch

Thank you very much @seratch, that helps point me in the right direction.

Is there a supported way to retrieve the token from the current context inside an event and pass that to the WebClient? I don't see any way to pass the token to WebClient.users_info

vstratful avatar Sep 07 '23 12:09 vstratful

The client argument for a listener embodies context.token, which can be primarily the bot token. If you have a different token such as a user token, you can always pass it to WebClient. By doing so, you can overwrite the token to use for a specific API call:

@app.event("message")
def handle_events(client: WebClient, body: dict):
    client.auth_test()  # with the bot token
    client.auth_test(token=resolve_user_token_in_some_way(body)). # with a user token

So, if an incoming request is associated with User A plus the user A already installed your app with their user scopes, context.user_token can exist (as long as your InstallationStore is properly implemented) in addition to context.token / context.bot_token. In this case, you can pass context.user_token to any API calls as necessary. I hope this clarifies.

seratch avatar Sep 07 '23 12:09 seratch

Thanks again @seratch, your responsiveness is amazing and truly helpful.

If I grab the client argument and pass that through to my helper function, then use that for the API call instead of using AsyncApp.client, everything works as expected.

Closing issue.

vstratful avatar Sep 07 '23 12:09 vstratful