def test_instance(self): store = SQLite3InstallationStore(database="logs/test.db", client_id="111.222") self.assertIsNotNone(store) conn = store.connect() self.assertIsNotNone(conn) conn.close()
async def test_save_and_find(self): sqlite3_store = SQLite3InstallationStore( database="logs/cacheable.db", client_id="111.222" ) sqlite3_store.init() store = AsyncCacheableInstallationStore(sqlite3_store) installation = Installation( app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxb-111", bot_scopes=["chat:write"], bot_user_id="U222", ) await store.async_save(installation) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) os.remove("logs/cacheable.db") bot = await sqlite3_store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNone(bot) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot)
def sqlite3( cls, database: str, # OAuth flow parameters/credentials client_id: Optional[str] = None, # required client_secret: Optional[str] = None, # required scopes: Optional[Sequence[str]] = None, user_scopes: Optional[Sequence[str]] = None, redirect_uri: Optional[str] = None, # Handler configuration install_path: Optional[str] = None, redirect_uri_path: Optional[str] = None, callback_options: Optional[CallbackOptions] = None, success_url: Optional[str] = None, failure_url: Optional[str] = None, authorization_url: Optional[str] = None, # Installation Management # state parameter related configurations state_cookie_name: str = OAuthStateUtils.default_cookie_name, state_expiration_seconds: int = OAuthStateUtils.default_expiration_seconds, client: Optional[WebClient] = None, logger: Optional[Logger] = None, ) -> "OAuthFlow": client_id = client_id or os.environ["SLACK_CLIENT_ID"] # required client_secret = client_secret or os.environ["SLACK_CLIENT_SECRET"] # required scopes = scopes or os.environ.get("SLACK_SCOPES", "").split(",") user_scopes = user_scopes or os.environ.get("SLACK_USER_SCOPES", "").split(",") redirect_uri = redirect_uri or os.environ.get("SLACK_REDIRECT_URI") return OAuthFlow( client=client or WebClient(), logger=logger, settings=OAuthSettings( # OAuth flow parameters/credentials client_id=client_id, client_secret=client_secret, scopes=scopes, user_scopes=user_scopes, redirect_uri=redirect_uri, # Handler configuration install_path=install_path, redirect_uri_path=redirect_uri_path, callback_options=callback_options, success_url=success_url, failure_url=failure_url, authorization_url=authorization_url, # Installation Management installation_store=SQLite3InstallationStore( database=database, client_id=client_id, logger=logger, ), # state parameter related configurations state_store=SQLite3OAuthStateStore( database=database, expiration_seconds=state_expiration_seconds, logger=logger, ), state_cookie_name=state_cookie_name, state_expiration_seconds=state_expiration_seconds, ), )
def test_org_installation(self): store = SQLite3InstallationStore(database="logs/test.db", client_id="111.222") installation = Installation( app_id="AO111", enterprise_id="EO111", user_id="UO111", bot_id="BO111", bot_token="xoxb-O111", bot_scopes=["chat:write"], bot_user_id="UO222", is_enterprise_install=True, ) store.save(installation) bot = store.find_bot(enterprise_id="EO111", team_id=None) self.assertIsNotNone(bot) bot = store.find_bot(enterprise_id="EO111", team_id="TO222", is_enterprise_install=True) self.assertIsNotNone(bot) bot = store.find_bot(enterprise_id="EO111", team_id="TO222") self.assertIsNone(bot) bot = store.find_bot(enterprise_id=None, team_id="TO111") self.assertIsNone(bot) i = store.find_installation(enterprise_id="EO111", team_id=None) self.assertIsNotNone(i) i = store.find_installation(enterprise_id="EO111", team_id="T111", is_enterprise_install=True) self.assertIsNotNone(i) i = store.find_installation(enterprise_id="EO111", team_id="T222") self.assertIsNone(i) i = store.find_installation(enterprise_id=None, team_id="T111") self.assertIsNone(i) i = store.find_installation(enterprise_id="EO111", team_id=None, user_id="UO111") self.assertIsNotNone(i) i = store.find_installation( enterprise_id="E111", team_id="T111", is_enterprise_install=True, user_id="U222", ) self.assertIsNone(i) i = store.find_installation(enterprise_id=None, team_id="T222", user_id="U111") self.assertIsNone(i)
async def test_save_and_find_token_rotation(self): sqlite3_store = SQLite3InstallationStore( database="logs/cacheable.db", client_id="111.222" ) sqlite3_store.init() store = AsyncCacheableInstallationStore(sqlite3_store) installation = Installation( app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxb-initial", bot_scopes=["chat:write"], bot_user_id="U222", bot_refresh_token="xoxe-1-initial", bot_token_expires_in=43200, ) await store.async_save(installation) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) self.assertEqual(bot.bot_refresh_token, "xoxe-1-initial") installation = Installation( app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxb-refreshed", bot_scopes=["chat:write"], bot_user_id="U222", bot_refresh_token="xoxe-1-refreshed", bot_token_expires_in=43200, ) await store.async_save(installation) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) self.assertEqual(bot.bot_refresh_token, "xoxe-1-refreshed") os.remove("logs/cacheable.db") bot = await sqlite3_store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNone(bot) bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot)
def test_save_and_find(self): store = SQLite3InstallationStore(database="logs/test.db", client_id="111.222") installation = Installation(app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxb-111", bot_scopes=["chat:write"], bot_user_id="U222") store.save(installation) bot = store.find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) bot = store.find_bot(enterprise_id="E111", team_id="T222") self.assertIsNone(bot) bot = store.find_bot(enterprise_id=None, team_id="T111") self.assertIsNone(bot)
def sqlite3( cls, database: str, client_id: Optional[str] = os.environ.get("SLACK_CLIENT_ID", None), client_secret: Optional[str] = os.environ.get("SLACK_CLIENT_SECRET", None), scopes: List[str] = os.environ.get("SLACK_SCOPES", "").split(","), user_scopes: List[str] = os.environ.get("SLACK_USER_SCOPES", "").split(","), redirect_uri: Optional[str] = os.environ.get("SLACK_REDIRECT_URI", None), oauth_state_cookie_name: str = OAuthStateUtils.default_cookie_name, oauth_state_expiration_seconds: int = OAuthStateUtils. default_expiration_seconds, logger: Optional[Logger] = None, ) -> "AsyncOAuthFlow": return AsyncOAuthFlow( client=create_async_web_client(), logger=logger, installation_store=SQLite3InstallationStore( database=database, client_id=client_id, logger=logger, ), oauth_state_store=SQLite3OAuthStateStore( database=database, expiration_seconds=oauth_state_expiration_seconds, logger=logger, ), oauth_state_cookie_name=oauth_state_cookie_name, oauth_state_expiration_seconds=oauth_state_expiration_seconds, client_id=client_id, client_secret=client_secret, scopes=scopes, user_scopes=user_scopes, redirect_uri=redirect_uri, )
def test_init(self): store = SQLite3InstallationStore(database="logs/test.db", client_id="111.222") store.init()
def test_save_and_find_token_rotation(self): sqlite3_store = SQLite3InstallationStore( database="logs/cacheable.db", client_id="111.222" ) sqlite3_store.init() store = CacheableInstallationStore(sqlite3_store) installation = Installation( app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxe.xoxp-1-initial", bot_scopes=["chat:write"], bot_user_id="U222", bot_refresh_token="xoxe-1-initial", bot_token_expires_in=43200, ) store.save(installation) bot = store.find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) self.assertEqual(bot.bot_refresh_token, "xoxe-1-initial") # Update the existing data refreshed_installation = Installation( app_id="A111", enterprise_id="E111", team_id="T111", user_id="U111", bot_id="B111", bot_token="xoxe.xoxp-1-refreshed", bot_scopes=["chat:write"], bot_user_id="U222", bot_refresh_token="xoxe-1-refreshed", bot_token_expires_in=43200, ) store.save(refreshed_installation) # find bots bot = store.find_bot(enterprise_id="E111", team_id="T111") self.assertIsNotNone(bot) self.assertEqual(bot.bot_refresh_token, "xoxe-1-refreshed") bot = store.find_bot(enterprise_id="E111", team_id="T222") self.assertIsNone(bot) bot = store.find_bot(enterprise_id=None, team_id="T111") self.assertIsNone(bot) # delete bots store.delete_bot(enterprise_id="E111", team_id="T222") bot = store.find_bot(enterprise_id="E111", team_id="T222") self.assertIsNone(bot) # find installations i = store.find_installation(enterprise_id="E111", team_id="T111") self.assertIsNotNone(i) i = store.find_installation(enterprise_id="E111", team_id="T222") self.assertIsNone(i) i = store.find_installation(enterprise_id=None, team_id="T111") self.assertIsNone(i) i = store.find_installation( enterprise_id="E111", team_id="T111", user_id="U111" ) self.assertIsNotNone(i) i = store.find_installation( enterprise_id="E111", team_id="T111", user_id="U222" ) self.assertIsNone(i) i = store.find_installation( enterprise_id="E111", team_id="T222", user_id="U111" ) self.assertIsNone(i) # delete installations store.delete_installation(enterprise_id="E111", team_id="T111", user_id="U111") i = store.find_installation( enterprise_id="E111", team_id="T111", user_id="U111" ) self.assertIsNone(i) i = store.find_installation(enterprise_id="E111", team_id="T111") self.assertIsNone(i) # delete all store.save(installation) store.delete_all(enterprise_id="E111", team_id="T111") i = store.find_installation(enterprise_id="E111", team_id="T111") self.assertIsNone(i) i = store.find_installation( enterprise_id="E111", team_id="T111", user_id="U111" ) self.assertIsNone(i) bot = store.find_bot(enterprise_id="E111", team_id="T222") self.assertIsNone(bot)
user_scopes = ["search:read"] logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO) import sqlalchemy from sqlalchemy.engine import Engine database_url = "sqlite:///slackapp.db" # database_url = "postgresql://localhost/slackapp" # pip install psycopg2 engine: Engine = sqlalchemy.create_engine(database_url) installation_store = SQLite3InstallationStore( database="test.db", client_id=client_id, logger=logger, ) installation_store.init() token_rotator = TokenRotator( client_id=client_id, client_secret=client_secret, ) state_store = SQLite3OAuthStateStore( database="test.db", logger=logger, expiration_seconds=300, ) state_store.init()