Skip to content

Commit 6a3a607

Browse files
committed
Fix #690 by adding basic auth handling in the new sync client
1 parent 03cc4d8 commit 6a3a607

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ------------------
2+
# Only for running this script here
3+
import logging
4+
import sys
5+
from os.path import dirname
6+
7+
sys.path.insert(1, f"{dirname(__file__)}/../../..")
8+
logging.basicConfig(level=logging.DEBUG)
9+
# ------------------
10+
11+
# ---------------------
12+
# Flask App
13+
# ---------------------
14+
15+
import os
16+
17+
# pip install flask
18+
from flask import Flask, make_response, request
19+
20+
app = Flask(__name__)
21+
logger = logging.getLogger(__name__)
22+
23+
24+
@app.route("/slack/oauth/callback", methods=["GET"])
25+
def endpoint():
26+
code = request.args.get("code")
27+
try:
28+
from slack import WebClient
29+
from slack.errors import SlackApiError
30+
client = WebClient(token="")
31+
client_id = os.environ["SLACK_CLIENT_ID"]
32+
client_secret = os.environ["SLACK_CLIENT_SECRET"]
33+
response = client.oauth_v2_access(client_id=client_id, client_secret=client_secret, code=code)
34+
result = response.get("error", "success!")
35+
return str(result)
36+
except SlackApiError as e:
37+
return make_response(str(e), 400)
38+
39+
40+
if __name__ == "__main__":
41+
# export SLACK_CLIENT_ID=111.222
42+
# export SLACK_CLIENT_SECRET=
43+
# FLASK_ENV=development python integration_tests/samples/issues/issue_690.py
44+
app.run(debug=True, host="localhost", port=3000)

slack/web/base_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ def _sync_send(self, api_url, req_args):
304304
_json = req_args["json"] if "json" in req_args else None
305305
headers = req_args["headers"] if "headers" in req_args else None
306306
token = params.get("token") if params and "token" in params else None
307+
auth = (
308+
req_args["auth"] if "auth" in req_args else None
309+
) # Basic Auth for oauth.v2.access / oauth.access
310+
if auth is not None:
311+
if isinstance(auth, BasicAuth):
312+
headers["Authorization"] = auth.encode()
313+
elif isinstance(auth, str):
314+
headers["Authorization"] = auth
315+
else:
316+
self._logger.warning(
317+
f"As the auth: {auth}: {type(auth)} is unsupported, skipped"
318+
)
319+
307320
body_params = {}
308321
if params:
309322
body_params.update(params)

tests/web/mock_web_api_server.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ def set_common_headers(self):
4545

4646
def _handle(self):
4747
try:
48+
if self.path in {"/oauth.access", "/oauth.v2.access"}:
49+
self.send_response(200)
50+
self.set_common_headers()
51+
if self.headers["authorization"] == "Basic MTExLjIyMjpzZWNyZXQ=":
52+
self.wfile.write("""{"ok":true}""".encode("utf-8"))
53+
return
54+
else:
55+
self.wfile.write("""{"ok":false, "error":"invalid"}""".encode("utf-8"))
56+
return
57+
4858
if self.is_valid_token() and self.is_valid_user_agent():
4959
parsed_path = urlparse(self.path)
5060

tests/web/test_web_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,33 @@ async def test_issue_560_bool_in_params_async(self):
147147
await self.async_client.conversations_list(exclude_archived=1) # ok
148148
await self.async_client.conversations_list(exclude_archived="true") # ok
149149
await self.async_client.conversations_list(exclude_archived=True) # TypeError
150+
151+
def test_issue_690_oauth_v2_access(self):
152+
self.client.token = ""
153+
resp = self.client.oauth_v2_access(client_id="111.222", client_secret="secret", code="codeeeeeeeeee")
154+
self.assertIsNone(resp["error"])
155+
with self.assertRaises(err.SlackApiError):
156+
self.client.oauth_v2_access(client_id="999.999", client_secret="secret", code="codeeeeeeeeee")
157+
158+
@async_test
159+
async def test_issue_690_oauth_v2_access_async(self):
160+
self.async_client.token = ""
161+
resp = await self.async_client.oauth_v2_access(client_id="111.222", client_secret="secret", code="codeeeeeeeeee")
162+
self.assertIsNone(resp["error"])
163+
with self.assertRaises(err.SlackApiError):
164+
await self.async_client.oauth_v2_access(client_id="999.999", client_secret="secret", code="codeeeeeeeeee")
165+
166+
def test_issue_690_oauth_access(self):
167+
self.client.token = ""
168+
resp = self.client.oauth_access(client_id="111.222", client_secret="secret", code="codeeeeeeeeee")
169+
self.assertIsNone(resp["error"])
170+
with self.assertRaises(err.SlackApiError):
171+
self.client.oauth_access(client_id="999.999", client_secret="secret", code="codeeeeeeeeee")
172+
173+
@async_test
174+
async def test_issue_690_oauth_access_async(self):
175+
self.async_client.token = ""
176+
resp = await self.async_client.oauth_access(client_id="111.222", client_secret="secret", code="codeeeeeeeeee")
177+
self.assertIsNone(resp["error"])
178+
with self.assertRaises(err.SlackApiError):
179+
await self.async_client.oauth_access(client_id="999.999", client_secret="secret", code="codeeeeeeeeee")

0 commit comments

Comments
 (0)