Skip to content

Commit 9fe578d

Browse files
Make it work
1 parent 479c406 commit 9fe578d

4 files changed

Lines changed: 31 additions & 44 deletions

File tree

pydantic-ai/app_oauth.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import logging
22
import os
33

4-
from dotenv import load_dotenv
54
from slack_bolt import App
6-
from slack_bolt.oauth.oauth_settings import OAuthSettings
75
from slack_sdk import WebClient
86

97
from agent import get_model
108
from listeners import register_listeners
11-
from oauth import BOT_SCOPES, USER_SCOPES, installation_store, state_store
9+
from oauth import oauth_settings
1210

13-
load_dotenv(dotenv_path=".env", override=False)
1411
get_model() # Fail fast if no AI provider key is configured
1512

1613
logging.basicConfig(level=logging.DEBUG)
@@ -25,14 +22,7 @@
2522
# Allow bot-posted messages (e.g. issue modal submissions with metadata)
2623
# to reach the message handler instead of being silently dropped
2724
ignoring_self_events_enabled=False,
28-
oauth_settings=OAuthSettings(
29-
client_id=os.environ.get("SLACK_CLIENT_ID"),
30-
client_secret=os.environ.get("SLACK_CLIENT_SECRET"),
31-
scopes=BOT_SCOPES,
32-
user_scopes=USER_SCOPES,
33-
installation_store=installation_store,
34-
state_store=state_store,
35-
),
25+
oauth_settings=oauth_settings,
3626
)
3727

3828
register_listeners(app)

pydantic-ai/listeners/actions/account_connection.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ def handle_disconnect_account(
2121
"""Handle the Disconnect button click on App Home."""
2222
ack()
2323
try:
24-
from oauth import authorize_url_generator, installation_store, state_store
24+
from oauth import install_uri, oauth_settings
2525

2626
user_id = context.user_id
27-
installation_store.delete_installation(
28-
enterprise_id=context.enterprise_id or "",
29-
team_id=context.team_id or "",
30-
user_id=user_id,
31-
)
32-
state = state_store.issue()
33-
authorize_url = authorize_url_generator.generate(state)
34-
view = build_app_home_view(authorize_url=authorize_url)
27+
oauth_settings.installation_store.delete_installation(
28+
enterprise_id=context.enterprise_id,
29+
team_id=context.team_id,
30+
user_id=user_id,
31+
)
32+
oauth_settings.installation_store.delete_bot
33+
view = build_app_home_view(authorize_url=install_uri)
3534
client.views_publish(user_id=user_id, view=view)
3635
except Exception as e:
3736
logger.exception(f"Failed to handle disconnect: {e}")

pydantic-ai/listeners/events/app_home_opened.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ def handle_app_home_opened(client: WebClient, context: BoltContext, logger: Logg
1515
is_connected = False
1616

1717
if os.environ.get("SLACK_CLIENT_ID"):
18-
from oauth import authorize_url_generator, installation_store, state_store
18+
from oauth import install_uri
1919

20-
installation = installation_store.find_installation(
21-
enterprise_id=context.enterprise_id or "",
22-
team_id=context.team_id or "",
23-
user_id=user_id,
24-
)
25-
if installation and installation.user_token:
20+
if context.authorize_result.user_token:
2621
is_connected = True
2722
else:
28-
state = state_store.issue()
29-
authorize_url = authorize_url_generator.generate(state)
23+
authorize_url = install_uri
3024

3125
view = build_app_home_view(
3226
authorize_url=authorize_url, is_connected=is_connected

pydantic-ai/oauth.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import json
22
import os
33
from pathlib import Path
4+
from urllib.parse import urljoin
5+
from dotenv import load_dotenv
46

5-
from slack_sdk.oauth import AuthorizeUrlGenerator
7+
8+
from slack_bolt.oauth.oauth_settings import OAuthSettings
69
from slack_sdk.oauth.installation_store import FileInstallationStore
710
from slack_sdk.oauth.state_store import FileOAuthStateStore
811

12+
load_dotenv(dotenv_path=".env", override=False)
13+
914
_manifest = json.loads(Path("manifest.json").read_text())
1015
BOT_SCOPES = _manifest["oauth_config"]["scopes"]["bot"]
1116

@@ -20,19 +25,18 @@
2025
"users:read",
2126
]
2227

23-
installation_store = FileInstallationStore(
24-
base_dir="./data/installations",
25-
historical_data_enabled=False,
26-
)
27-
28-
state_store = FileOAuthStateStore(
29-
expiration_seconds=600,
30-
base_dir="./data/states",
31-
)
32-
33-
authorize_url_generator = AuthorizeUrlGenerator(
34-
client_id=os.environ.get("SLACK_CLIENT_ID", ""),
35-
redirect_uri=os.environ.get("SLACK_REDIRECT_URI", ""),
28+
oauth_settings = OAuthSettings(
29+
client_id=os.environ.get("SLACK_CLIENT_ID"),
30+
client_secret=os.environ.get("SLACK_CLIENT_SECRET"),
31+
redirect_uri=os.environ.get("SLACK_REDIRECT_URI"),
3632
scopes=BOT_SCOPES,
3733
user_scopes=USER_SCOPES,
34+
installation_store=FileInstallationStore(
35+
base_dir="./data/installations",
36+
),
37+
state_store=FileOAuthStateStore(
38+
expiration_seconds=600,
39+
base_dir="./data/states",
40+
),
3841
)
42+
install_uri = urljoin(oauth_settings.redirect_uri, oauth_settings.install_path)

0 commit comments

Comments
 (0)