|
| 1 | +# from slack.web import WebClient |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import TypeVar |
| 4 | + |
| 5 | +from fastapi import Depends, Path |
| 6 | +from slack_sdk import WebClient |
| 7 | +from slack_sdk.oauth import AuthorizeUrlGenerator |
| 8 | + |
| 9 | +from reworkd_platform.db.crud.oauth import OAuthCrud |
| 10 | +from reworkd_platform.db.models.auth import OauthCredentials |
| 11 | +from reworkd_platform.schemas import UserBase |
| 12 | +from reworkd_platform.settings import Settings, settings as platform_settings |
| 13 | +from reworkd_platform.web.api.http_responses import forbidden |
| 14 | + |
| 15 | +T = TypeVar("T", bound="OAuthInstaller") |
| 16 | + |
| 17 | + |
| 18 | +class OAuthInstaller(ABC): |
| 19 | + def __init__(self, crud: OAuthCrud, settings: Settings): |
| 20 | + self.crud = crud |
| 21 | + self.settings = settings |
| 22 | + |
| 23 | + @abstractmethod |
| 24 | + async def install(self, user: UserBase) -> str: |
| 25 | + raise NotImplementedError() |
| 26 | + |
| 27 | + @abstractmethod |
| 28 | + async def install_callback(self, code: str, state: str) -> None: |
| 29 | + raise NotImplementedError() |
| 30 | + |
| 31 | + |
| 32 | +class SlackInstaller(OAuthInstaller): |
| 33 | + PROVIDER = "slack" |
| 34 | + |
| 35 | + async def install(self, user: UserBase) -> str: |
| 36 | + installation = await self.crud.create_installation(user, self.PROVIDER) |
| 37 | + |
| 38 | + return AuthorizeUrlGenerator( |
| 39 | + client_id=self.settings.slack_client_id, |
| 40 | + redirect_uri=self.settings.slack_redirect_uri, |
| 41 | + scopes=["chat:write"], |
| 42 | + ).generate( |
| 43 | + state=installation.state, |
| 44 | + ) |
| 45 | + |
| 46 | + async def install_callback(self, code: str, state: str) -> None: |
| 47 | + installation = await self.crud.get_installation_by_state(state) |
| 48 | + if not installation: |
| 49 | + raise forbidden() |
| 50 | + |
| 51 | + oauth_response = WebClient().oauth_v2_access( |
| 52 | + client_id=self.settings.slack_client_id, |
| 53 | + client_secret=self.settings.slack_client_secret, |
| 54 | + code=code, |
| 55 | + state=state, |
| 56 | + ) |
| 57 | + |
| 58 | + # We should handle token rotation / refresh tokens eventually |
| 59 | + # TODO: encode token |
| 60 | + await OauthCredentials( |
| 61 | + installation_id=installation.id, |
| 62 | + provider="slack", |
| 63 | + token_type=oauth_response["token_type"], |
| 64 | + access_token=oauth_response["access_token"], |
| 65 | + scope=oauth_response["scope"], |
| 66 | + data=oauth_response.data, |
| 67 | + ).save(self.crud.session) |
| 68 | + |
| 69 | + |
| 70 | +integrations = { |
| 71 | + SlackInstaller.PROVIDER: SlackInstaller, |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +def installer_factory( |
| 76 | + provider: str = Path(description="OAuth Provider"), |
| 77 | + crud: OAuthCrud = Depends(OAuthCrud.inject), |
| 78 | +) -> OAuthInstaller: |
| 79 | + if provider in integrations: |
| 80 | + return integrations[provider](crud, platform_settings) |
| 81 | + raise NotImplementedError() |
0 commit comments