diff --git a/tests/test_sso.py b/tests/test_sso.py index 47537739..7fd62921 100644 --- a/tests/test_sso.py +++ b/tests/test_sso.py @@ -1,4 +1,5 @@ import json +from requests import Response from six.moves.urllib.parse import parse_qsl, urlparse import pytest @@ -60,6 +61,35 @@ def mock_connection(self): ], } + @pytest.fixture + def mock_connections(self): + return { + "data": [ + { + "object": "connection", + "id": "conn_id", + "status": "linked", + "name": "Google OAuth 2.0", + "connection_type": "GoogleOAuth", + "oauth_uid": "oauth-uid.apps.googleusercontent.com", + "oauth_secret": "oauth-secret", + "oauth_redirect_uri": "https://auth.workos.com/sso/oauth/google/chicken/callback", + "saml_entity_id": None, + "saml_idp_url": None, + "saml_relying_party_trust_cert": None, + "saml_x509_certs": None, + "domains": [ + { + "object": "connection_domain", + "id": "domain_id", + "domain": "terrace-house.com", + }, + ], + } + ], + "listMetadata": {"before": None, "after": None}, + } + def test_authorization_url_throws_value_error_with_missing_domain_and_provider( self, ): @@ -177,3 +207,12 @@ def test_create_connection(self, mock_request_method, mock_connection): connection = self.sso.create_connection("draft_conn_id") assert connection == response_dict + + def test_list_connections(self, mock_connections, mock_request_method): + mock_response = Response() + mock_response.status_code = 200 + mock_response.response_dict = mock_connections + mock_request_method("get", mock_response, 200) + response = self.sso.list_connections() + assert response.status_code == 200 + assert response.response_dict == mock_connections diff --git a/workos/sso.py b/workos/sso.py index a6cca1e4..b97dd2cc 100644 --- a/workos/sso.py +++ b/workos/sso.py @@ -7,7 +7,12 @@ from workos.exceptions import ConfigurationException from workos.resources.sso import WorkOSProfile from workos.utils.connection_types import ConnectionType -from workos.utils.request import RequestHelper, RESPONSE_TYPE_CODE, REQUEST_METHOD_POST +from workos.utils.request import ( + RequestHelper, + RESPONSE_TYPE_CODE, + REQUEST_METHOD_GET, + REQUEST_METHOD_POST, +) from workos.utils.validation import SSO_MODULE, validate_settings AUTHORIZATION_PATH = "sso/authorize" @@ -17,6 +22,8 @@ OAUTH_GRANT_TYPE = "authorization_code" +RESPONSE_LIMIT = 10 + class SSO(object): """Offers methods to assist in authenticating through the WorkOS SSO service.""" @@ -157,3 +164,37 @@ def create_connection(self, source): params=params, token=workos.api_key, ) + + def list_connections( + self, + connection_type=None, + domain=None, + limit=RESPONSE_LIMIT, + before=None, + after=None, + ): + """Gets details for existing Connections. + + Args: + connection_type (ConnectionType): Authentication service provider descriptor. (Optional) + domain (str): Domain of a Connection. (Optional) + limit (int): Maximum number of records to return. (Optional) + before (str): Pagination cursor to receive records before a provided Connection ID. (Optional) + after (str): Pagination cursor to receive records after a provided Connection ID. (Optional) + + Returns: + dict: Connections response from WorkOS. + """ + params = { + "connetion_type": connection_type, + "domain": domain, + "limit": limit, + "before": before, + "after": after, + } + return self.request_helper.request( + "connections", + method=REQUEST_METHOD_GET, + params=params, + token=workos.api_key, + )