Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1059 by adding the token rotation feature support #1060

Merged
merged 5 commits into from Jul 15, 2021

Conversation

seratch
Copy link
Member

@seratch seratch commented Jul 12, 2021

Summary

This pull request fixes #1059 by adding token rotation feature support in this SDK.

TODOs:

  • Implement the token rotator in the OAuth module
  • Add example apps (sync/async) in integration tests
  • Update oauth_v2_access API method to have new arguments
  • Add oauth_v2_exchange API method support
  • Update database schema (SQLAlchemy, SQLite)
  • Add unit tests to verify if the verification logic works as expected
  • [ ] Update the OAuth module docs (in a different PR)

Category (place an x in each of the [ ])

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs-src (Documents, have you run ./docs.sh?)
  • /docs-src-v2 (Documents, have you run ./docs-v2.sh?)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements (place an x in each [ ])

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

@seratch seratch added enhancement M-T: A feature request for new functionality web-client Version: 3x oauth labels Jul 12, 2021
@seratch seratch added this to the 3.8.0 milestone Jul 12, 2021
@codecov
Copy link

codecov bot commented Jul 12, 2021

Codecov Report

Merging #1060 (2784350) into main (df9d71b) will decrease coverage by 0.19%.
The diff coverage is 69.91%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1060      +/-   ##
==========================================
- Coverage   84.33%   84.14%   -0.20%     
==========================================
  Files          95       99       +4     
  Lines        8938     9239     +301     
==========================================
+ Hits         7538     7774     +236     
- Misses       1400     1465      +65     
Impacted Files Coverage Δ
...sdk/oauth/installation_store/amazon_s3/__init__.py 0.00% <0.00%> (ø)
...lation_store/async_cacheable_installation_store.py 52.23% <35.29%> (-1.47%) ⬇️
...k_sdk/oauth/installation_store/sqlite3/__init__.py 83.33% <42.10%> (-5.06%) ⬇️
...uth/installation_store/async_installation_store.py 60.86% <50.00%> (-1.04%) ⬇️
...sdk/oauth/installation_store/installation_store.py 69.56% <50.00%> (-1.87%) ⬇️
slack_sdk/web/legacy_client.py 94.78% <55.55%> (-0.48%) ⬇️
...installation_store/cacheable_installation_store.py 79.41% <70.00%> (+25.70%) ⬆️
slack_sdk/oauth/token_rotation/async_rotator.py 74.54% <74.54%> (ø)
slack_sdk/oauth/token_rotation/rotator.py 74.54% <74.54%> (ø)
...dk/oauth/installation_store/models/installation.py 90.72% <75.67%> (-9.28%) ⬇️
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df9d71b...2784350. Read the comment docs.

@seratch seratch changed the title WIP: Fix #1059 by adding the token rotation feature support Fix #1059 by adding the token rotation feature support Jul 14, 2021
@seratch seratch marked this pull request as ready for review July 14, 2021 09:17
Copy link
Member Author

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments for reviews or future reference

@@ -116,7 +116,7 @@ async def oauth_callback(req: Request):
body=html,
)

error = req.args["error"] if "error" in req.args else ""
error = req.args.get("error") if "error" in req.args else ""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req.args["error"] returns an array in Sanic's latest version.

is_enterprise_install=is_enterprise_install,
)
if installation is not None:
updated_installation = token_rotator.perform_token_rotation(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bolt for Python internally uses this TokenRotator in authorize middleware. Bolt users do not need to directly use this.


raw_body = request.data.decode("utf-8")
body = parse_body(body=raw_body, content_type=extract_content_type(request.headers))
rotate_tokens(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run the rotation for all incoming requests for easy testing

Comment on lines +230 to +231
bot_refresh_token=oauth_response.get("refresh_token"),
bot_token_expires_in=oauth_response.get("expires_in"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newly added

Comment on lines +235 to +236
user_refresh_token=installer.get("refresh_token"),
user_token_expires_in=installer.get("expires_in"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newly added

self,
*,
installation: Installation,
minutes_before_expiration: int = 120, # 2 hours by default
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aligned with bolt-js

code: str,
# This field is required when processing the OAuth redirect URL requests
# while it's absent for token rotation
code: Optional[str] = None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now code is optional

# find bots
bot = store.find_bot(enterprise_id="E111", team_id="T111")
self.assertIsNotNone(bot)
self.assertEqual(bot.bot_refresh_token, "xoxe-1-refreshed")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test verifies if the find_bot call returns the refreshed token

bot_refresh_token="xoxe-1-initial",
bot_token_expires_in=43200,
)
refreshed = self.token_rotator.perform_token_rotation(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the perform_token_rotation method returns a new installation data, your app needs to call InstallationStore#save(installation) to save the new values.

@@ -70,6 +92,12 @@ def _handle(self):
if self.headers["authorization"] == "Basic MTExLjIyMjpzZWNyZXQ=":
self.wfile.write("""{"ok":true}""".encode("utf-8"))
return
elif (
self.headers["authorization"]
== "Basic MTExLjIyMjp0b2tlbl9yb3RhdGlvbl9zZWNyZXQ="
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means the values (client_id="111.222", client_secret="token_rotation_secret") that are used in the tests

@seratch seratch merged commit dcf1c7c into slackapi:main Jul 15, 2021
@seratch seratch deleted the issue-1059-token-rotation branch July 15, 2021 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement M-T: A feature request for new functionality oauth Version: 3x web-client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add token rotation feature support
1 participant