|
1 | 1 | import json |
| 2 | +import logging |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
4 | 5 |
|
| 6 | +from slack_bolt.authorization.authorize_result import AuthorizeResult |
| 7 | +from slack_bolt.oauth.oauth_settings import OAuthSettings |
| 8 | +from slack_sdk import WebClient |
5 | 9 | from slack_sdk.oauth import AuthorizeUrlGenerator |
6 | 10 | from slack_sdk.oauth.installation_store import FileInstallationStore |
7 | 11 | from slack_sdk.oauth.state_store import FileOAuthStateStore |
8 | 12 |
|
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
9 | 15 | _manifest = json.loads(Path("manifest.json").read_text()) |
10 | 16 | BOT_SCOPES = _manifest["oauth_config"]["scopes"]["bot"] |
11 | 17 |
|
|
36 | 42 | scopes=BOT_SCOPES, |
37 | 43 | user_scopes=USER_SCOPES, |
38 | 44 | ) |
| 45 | + |
| 46 | +oauth_settings = OAuthSettings( |
| 47 | + client_id=os.environ.get("SLACK_CLIENT_ID"), |
| 48 | + client_secret=os.environ.get("SLACK_CLIENT_SECRET"), |
| 49 | + scopes=BOT_SCOPES, |
| 50 | + user_scopes=USER_SCOPES, |
| 51 | + installation_store=installation_store, |
| 52 | + state_store=state_store, |
| 53 | +) |
| 54 | + |
| 55 | +# --------------------------------------------------------------------------- |
| 56 | +# Bot-token fallback for pre-OAuth first-run experience |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | +# When installed via Slack CLI, SLACK_BOT_TOKEN is available but Bolt clears |
| 59 | +# it when oauth_settings is present. This wrapper lets the bot token serve as |
| 60 | +# a fallback so App Home (with the OAuth install URL) and basic bot operations |
| 61 | +# work before anyone has completed the OAuth flow. |
| 62 | + |
| 63 | +_fallback_bot_token = os.environ.get("SLACK_BOT_TOKEN") |
| 64 | + |
| 65 | +if _fallback_bot_token: |
| 66 | + _original_authorize = oauth_settings.authorize |
| 67 | + |
| 68 | + def _authorize_with_fallback_bot_token( |
| 69 | + *, |
| 70 | + context, |
| 71 | + enterprise_id, |
| 72 | + team_id, |
| 73 | + user_id, |
| 74 | + actor_enterprise_id=None, |
| 75 | + actor_team_id=None, |
| 76 | + actor_user_id=None, |
| 77 | + ): |
| 78 | + result = _original_authorize( |
| 79 | + context=context, |
| 80 | + enterprise_id=enterprise_id, |
| 81 | + team_id=team_id, |
| 82 | + user_id=user_id, |
| 83 | + actor_enterprise_id=actor_enterprise_id, |
| 84 | + actor_team_id=actor_team_id, |
| 85 | + actor_user_id=actor_user_id, |
| 86 | + ) |
| 87 | + if result is not None: |
| 88 | + return result |
| 89 | + |
| 90 | + logger.info( |
| 91 | + "No OAuth installation found (team=%s); falling back to SLACK_BOT_TOKEN", |
| 92 | + team_id, |
| 93 | + ) |
| 94 | + try: |
| 95 | + fallback_client = WebClient( |
| 96 | + base_url=os.environ.get("SLACK_API_URL", "https://slack.com/api"), |
| 97 | + token=_fallback_bot_token, |
| 98 | + ) |
| 99 | + auth_test = fallback_client.auth_test() |
| 100 | + return AuthorizeResult.from_auth_test_response( |
| 101 | + auth_test_response=auth_test, |
| 102 | + bot_token=_fallback_bot_token, |
| 103 | + ) |
| 104 | + except Exception: |
| 105 | + logger.exception("Fallback bot token auth.test failed") |
| 106 | + return None |
| 107 | + |
| 108 | + oauth_settings.authorize = _authorize_with_fallback_bot_token |
0 commit comments