Skip to content

Commit 58e19ae

Browse files
committed
Add sqlite3 support
1 parent 29fc988 commit 58e19ae

File tree

14 files changed

+410
-24
lines changed

14 files changed

+410
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ tmp.txt
3636
logs/
3737

3838
.env
39+
*.db

samples/oauth_sqlite3_app.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import os
4+
import sys
5+
6+
sys.path.insert(1, "../src")
7+
# ------------------------------------------------
8+
9+
import logging
10+
from slack_bolt import App
11+
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore
12+
from slack_sdk.oauth.state_store.sqlite3 import SQLite3OAuthStateStore
13+
14+
logging.basicConfig(level=logging.DEBUG)
15+
app = App(
16+
installation_store=SQLite3InstallationStore(
17+
database="./slackapp.db",
18+
client_id=os.environ["SLACK_CLIENT_ID"],
19+
),
20+
oauth_state_store=SQLite3OAuthStateStore(
21+
database="./slackapp.db",
22+
expiration_seconds=120,
23+
),
24+
)
25+
26+
27+
def log_request(logger, payload, next):
28+
logger.debug(payload)
29+
return next()
30+
31+
32+
app.use(log_request)
33+
34+
35+
@app.event("app_mention")
36+
def handle_app_mentions(payload, say, logger):
37+
logger.info(payload)
38+
say("What's up?")
39+
40+
41+
@app.event("message")
42+
def handle_messages():
43+
pass
44+
45+
46+
if __name__ == '__main__':
47+
app.start(3000)
48+
49+
# pip install slackclient
50+
# pip install -i https://test.pypi.org/simple/ slack_bolt
51+
# export SLACK_SIGNING_SECRET=***
52+
# export SLACK_BOT_TOKEN=xoxb-***
53+
# export SLACK_CLIENT_ID=111.111
54+
# export SLACK_CLIENT_SECRET=***
55+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
56+
# python oauth_app.py

src/slack_bolt/app/app.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from slack_bolt.request import BoltRequest
2929
from slack_bolt.response import BoltResponse
3030
from slack_sdk import WebClient
31+
from slack_sdk.oauth import OAuthStateUtils
3132
from slack_sdk.oauth.installation_store import InstallationStore, FileInstallationStore
3233
from slack_sdk.oauth.state_store import OAuthStateStore, FileOAuthStateStore
3334

@@ -49,6 +50,8 @@ def __init__(
4950
# for multi-workspace apps
5051
installation_store: Optional[InstallationStore] = None,
5152
oauth_state_store: Optional[OAuthStateStore] = None,
53+
oauth_state_cookie_name: str = OAuthStateUtils.default_cookie_name,
54+
oauth_state_expiration_seconds: int = OAuthStateUtils.default_expiration_seconds,
5255

5356
# for the OAuth flow
5457
oauth_flow: Optional[OAuthFlow] = None,
@@ -83,9 +86,15 @@ def __init__(
8386

8487
self._installation_store: Optional[InstallationStore] = installation_store
8588
self._oauth_state_store: Optional[OAuthStateStore] = oauth_state_store
89+
if self._installation_store.logger is None:
90+
self._installation_store.logger = self._framework_logger
91+
if self._oauth_state_store.logger is None:
92+
self._oauth_state_store.logger = self._framework_logger
8693

87-
self.oauth_flow: Optional[OAuthFlow] = None
94+
self._oauth_state_cookie_name = oauth_state_cookie_name
95+
self._oauth_state_expiration_seconds = oauth_state_expiration_seconds
8896

97+
self.oauth_flow: Optional[OAuthFlow] = None
8998
if oauth_flow:
9099
self.oauth_flow = oauth_flow
91100
self._sync_client_logger_with_oauth_flow()
@@ -102,6 +111,7 @@ def __init__(
102111
)
103112
self._oauth_state_store = FileOAuthStateStore(
104113
logger=self._framework_logger,
114+
expiration_seconds=self._oauth_state_expiration_seconds,
105115
client_id=client_id,
106116
)
107117

@@ -114,6 +124,8 @@ def __init__(
114124
# required storage implementations
115125
installation_store=self._installation_store,
116126
oauth_state_store=self._oauth_state_store,
127+
oauth_state_cookie_name=self._oauth_state_cookie_name,
128+
oauth_state_expiration_seconds=self._oauth_state_expiration_seconds,
117129
# used for oauth.v2.access calls
118130
client_id=client_id,
119131
client_secret=client_secret,

src/slack_bolt/oauth/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from slack_bolt.request import BoltRequest
66
from slack_bolt.response import BoltResponse
77
from slack_sdk.errors import SlackApiError
8-
from slack_sdk.oauth import AuthorizeUrlGenerator, OAuthStateCookieUtils, RedirectUriPageRenderer
8+
from slack_sdk.oauth import AuthorizeUrlGenerator, OAuthStateUtils, RedirectUriPageRenderer
99
from slack_sdk.oauth.installation_store import InstallationStore, Installation
1010
from slack_sdk.oauth.state_store import OAuthStateStore
1111
from slack_sdk.web import WebClient, SlackResponse
@@ -21,8 +21,8 @@ def __init__(
2121

2222
installation_store: InstallationStore,
2323
oauth_state_store: OAuthStateStore,
24-
oauth_state_cookie_name: str = OAuthStateCookieUtils.default_cookie_name,
25-
oauth_state_expiration_seconds: int = OAuthStateCookieUtils.default_expiration_seconds,
24+
oauth_state_cookie_name: str = OAuthStateUtils.default_cookie_name,
25+
oauth_state_expiration_seconds: int = OAuthStateUtils.default_expiration_seconds,
2626

2727
client_id: str,
2828
client_secret: str,
@@ -42,7 +42,7 @@ def __init__(
4242
self.installation_store = installation_store
4343
self.oauth_state_store = oauth_state_store
4444
self.oauth_state_cookie_name = oauth_state_cookie_name
45-
self.oauth_state_cookie_utils = OAuthStateCookieUtils(
45+
self.oauth_state_cookie_utils = OAuthStateUtils(
4646
cookie_name=oauth_state_cookie_name,
4747
expiration_seconds=oauth_state_expiration_seconds,
4848
)
@@ -88,7 +88,7 @@ def build_authorize_url_redirection(self, request: BoltRequest, state: str) -> B
8888
status=302,
8989
headers={
9090
"Location": [self.authorize_url_generator.generate(state)],
91-
"Set-Cookie": [self.oauth_state_cookie_utils.build_creation_header(state)]
91+
"Set-Cookie": [self.oauth_state_cookie_utils.build_set_cookie_for_new_state(state)]
9292
},
9393
)
9494

@@ -195,7 +195,7 @@ def build_callback_failure_response(
195195
headers={
196196
"Content-Type": "text/html; charset=utf-8",
197197
"Content-Length": len(html),
198-
"Set-Cookie": self.oauth_state_cookie_utils.build_deletion_header(),
198+
"Set-Cookie": self.oauth_state_cookie_utils.build_set_cookie_for_deletion(),
199199
},
200200
body=html,
201201
)
@@ -217,7 +217,7 @@ def build_callback_success_response(
217217
headers={
218218
"Content-Type": "text/html; charset=utf-8",
219219
"Content-Length": len(html),
220-
"Set-Cookie": self.oauth_state_cookie_utils.build_deletion_header(),
220+
"Set-Cookie": self.oauth_state_cookie_utils.build_set_cookie_for_deletion(),
221221
},
222222
body=html,
223223
)

src/slack_sdk/oauth/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .authorize_url_generator import AuthorizeUrlGenerator
22
from .installation_store import InstallationStore
33
from .redirect_uri_page_renderer import RedirectUriPageRenderer
4-
from .state_cookie_utils import OAuthStateCookieUtils
4+
from .state_utils import OAuthStateUtils
55
from .state_store import OAuthStateStore

src/slack_sdk/oauth/installation_store/amazon_s3/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def find_bot(
8686
*,
8787
enterprise_id: Optional[str],
8888
team_id: Optional[str],
89-
) -> Optional[Installation]:
89+
) -> Optional[Bot]:
9090
# TODO: org-apps support
9191
none = "none"
9292
e_id = enterprise_id or none

src/slack_sdk/oauth/installation_store/file/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def find_bot(
6464
*,
6565
enterprise_id: Optional[str],
6666
team_id: Optional[str],
67-
) -> Optional[Installation]:
67+
) -> Optional[Bot]:
6868
# TODO: org-apps support
6969
none = "none"
7070
e_id = enterprise_id or none

src/slack_sdk/oauth/installation_store/installation_store.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from abc import abstractmethod
2+
from logging import Logger
23
from typing import Optional
34

5+
from .models.bot import Bot
46
from .models.installation import Installation
57

68

79
class InstallationStore():
10+
def __init__(self, logger: Optional[Logger] = None):
11+
self.logger: Optional[Logger] = logger
812

913
@abstractmethod
1014
def save(self, installation: Installation):
@@ -16,5 +20,5 @@ def find_bot(
1620
*,
1721
enterprise_id: Optional[str],
1822
team_id: Optional[str],
19-
) -> Optional[Installation]:
23+
) -> Optional[Bot]:
2024
raise NotImplementedError

0 commit comments

Comments
 (0)