-
Notifications
You must be signed in to change notification settings - Fork 248
/
async_oauth_app.py
240 lines (202 loc) · 7.57 KB
/
async_oauth_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import logging
import os
import time
from datetime import datetime
from logging import Logger
from typing import Optional
from uuid import uuid4
import sqlalchemy
from databases import Database
from slack_sdk.oauth.installation_store import Bot, Installation
from slack_sdk.oauth.installation_store.async_installation_store import (
AsyncInstallationStore,
)
from slack_sdk.oauth.installation_store.sqlalchemy import SQLAlchemyInstallationStore
from slack_sdk.oauth.state_store.async_state_store import AsyncOAuthStateStore
from slack_sdk.oauth.state_store.sqlalchemy import SQLAlchemyOAuthStateStore
from sqlalchemy import and_, desc, Table, MetaData
from slack_bolt.adapter.sanic import AsyncSlackRequestHandler
from slack_bolt.async_app import AsyncApp
from slack_bolt.context.say.async_say import AsyncSay
from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings
class AsyncSQLAlchemyInstallationStore(AsyncInstallationStore):
database_url: str
client_id: str
metadata: MetaData
installations: Table
bots: Table
def __init__(
self,
client_id: str,
database_url: str,
logger: Logger = logging.getLogger(__name__),
):
self.client_id = client_id
self.database_url = database_url
self._logger = logger
self.metadata = MetaData()
self.installations = SQLAlchemyInstallationStore.build_installations_table(
metadata=self.metadata,
table_name=SQLAlchemyInstallationStore.default_installations_table_name,
)
self.bots = SQLAlchemyInstallationStore.build_bots_table(
metadata=self.metadata,
table_name=SQLAlchemyInstallationStore.default_bots_table_name,
)
@property
def logger(self) -> Logger:
return self._logger
async def async_save(self, installation: Installation):
async with Database(self.database_url) as database:
async with database.transaction():
i = installation.to_dict()
i["client_id"] = self.client_id
await database.execute(self.installations.insert(), i)
b = installation.to_bot().to_dict()
b["client_id"] = self.client_id
await database.execute(self.bots.insert(), b)
async def async_find_bot(
self,
*,
enterprise_id: Optional[str],
team_id: Optional[str],
is_enterprise_install: Optional[bool] = False,
) -> Optional[Bot]:
c = self.bots.c
query = (
self.bots.select()
.where(
and_(
c.enterprise_id == enterprise_id,
c.team_id == team_id,
c.is_enterprise_install == is_enterprise_install,
)
)
.order_by(desc(c.installed_at))
.limit(1)
)
async with Database(self.database_url) as database:
result = await database.fetch_one(query)
if result:
return Bot(
app_id=result["app_id"],
enterprise_id=result["enterprise_id"],
team_id=result["team_id"],
bot_token=result["bot_token"],
bot_id=result["bot_id"],
bot_user_id=result["bot_user_id"],
bot_scopes=result["bot_scopes"],
installed_at=result["installed_at"],
)
else:
return None
class AsyncSQLAlchemyOAuthStateStore(AsyncOAuthStateStore):
database_url: str
expiration_seconds: int
metadata: MetaData
oauth_states: Table
def __init__(
self,
*,
expiration_seconds: int,
database_url: str,
logger: Logger = logging.getLogger(__name__),
):
self.expiration_seconds = expiration_seconds
self.database_url = database_url
self._logger = logger
self.metadata = MetaData()
self.oauth_states = SQLAlchemyOAuthStateStore.build_oauth_states_table(
metadata=self.metadata,
table_name=SQLAlchemyOAuthStateStore.default_table_name,
)
@property
def logger(self) -> Logger:
return self._logger
async def async_issue(self) -> str:
state: str = str(uuid4())
now = datetime.utcfromtimestamp(time.time() + self.expiration_seconds)
async with Database(self.database_url) as database:
await database.execute(self.oauth_states.insert(), {"state": state, "expire_at": now})
return state
async def async_consume(self, state: str) -> bool:
try:
async with Database(self.database_url) as database:
async with database.transaction():
c = self.oauth_states.c
query = self.oauth_states.select().where(and_(c.state == state, c.expire_at > datetime.utcnow()))
row = await database.fetch_one(query)
self.logger.debug(f"consume's query result: {row}")
await database.execute(self.oauth_states.delete().where(c.id == row["id"]))
return True
return False
except Exception as e:
message = f"Failed to find any persistent data for state: {state} - {e}"
self.logger.warning(message)
return False
database_url = "sqlite:///slackapp.db"
# database_url = "postgresql://localhost/slackapp" # pip install psycopg2 databases[postgresql]
logger = logging.getLogger(__name__)
client_id, client_secret, signing_secret = (
os.environ["SLACK_CLIENT_ID"],
os.environ["SLACK_CLIENT_SECRET"],
os.environ["SLACK_SIGNING_SECRET"],
)
installation_store = AsyncSQLAlchemyInstallationStore(
client_id=client_id,
database_url=database_url,
logger=logger,
)
oauth_state_store = AsyncSQLAlchemyOAuthStateStore(
expiration_seconds=120,
database_url=database_url,
logger=logger,
)
app = AsyncApp(
logger=logger,
signing_secret=signing_secret,
installation_store=installation_store,
oauth_settings=AsyncOAuthSettings(
client_id=client_id,
client_secret=client_secret,
state_store=oauth_state_store,
),
)
app_handler = AsyncSlackRequestHandler(app)
@app.event("app_mention")
async def handle_command(say: AsyncSay):
await say("Hi!")
from sanic import Sanic
from sanic.request import Request
api = Sanic(name="awesome-slack-app")
@api.post("/slack/events")
async def endpoint(req: Request):
return await app_handler.handle(req)
@api.get("/slack/install")
async def install(req: Request):
return await app_handler.handle(req)
@api.get("/slack/oauth_redirect")
async def oauth_redirect(req: Request):
return await app_handler.handle(req)
async def init():
try:
async with Database(database_url) as database:
await database.fetch_one("select count(*) from slack_bots")
except Exception:
engine = sqlalchemy.create_engine(database_url)
installation_store.metadata.create_all(engine)
oauth_state_store.metadata.create_all(engine)
if __name__ == "__main__":
import asyncio
logging.basicConfig(level=logging.DEBUG)
asyncio.run(init())
api.run(host="0.0.0.0", port=int(os.environ.get("PORT", 3000)))
# pip install -r requirements_async.txt
# # -- OAuth flow -- #
# export SLACK_SIGNING_SECRET=***
# export SLACK_CLIENT_ID=111.111
# export SLACK_CLIENT_SECRET=***
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
# python async_oauth_app.py
# or
# uvicorn async_oauth_app:api --reload --port 3000 --log-level warning