Skip to content

Commit 542e36d

Browse files
authored
feat(workos-python): Add connection to get_authorization_url (#61)
1 parent dedbcf4 commit 542e36d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

tests/test_sso.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def setup_with_client_id(self, set_api_key_and_client_id):
1616
self.provider = ConnectionType.GoogleOAuth
1717
self.customer_domain = "workos.com"
1818
self.redirect_uri = "https://localhost/auth/callback"
19-
self.state = json.dumps({"things": "with_stuff",})
19+
self.state = json.dumps({"things": "with_stuff"})
20+
self.connection = "connection_123"
2021

2122
self.sso = SSO()
2223

@@ -25,7 +26,7 @@ def setup_with_project_id(self, set_api_key_and_project_id):
2526
self.provider = ConnectionType.GoogleOAuth
2627
self.customer_domain = "workos.com"
2728
self.redirect_uri = "https://localhost/auth/callback"
28-
self.state = json.dumps({"things": "with_stuff",})
29+
self.state = json.dumps({"things": "with_stuff"})
2930

3031
self.sso = SSO()
3132

@@ -99,7 +100,7 @@ def mock_connections(self):
99100
"listMetadata": {"before": None, "after": None},
100101
}
101102

102-
def test_authorization_url_throws_value_error_with_missing_domain_and_provider(
103+
def test_authorization_url_throws_value_error_with_missing_connection_domain_and_provider(
103104
self, setup_with_client_id
104105
):
105106
with pytest.raises(ValueError, match=r"Incomplete arguments.*"):
@@ -153,6 +154,25 @@ def test_authorization_url_has_expected_query_params_with_domain(
153154
"state": self.state,
154155
}
155156

157+
def test_authorization_url_has_expected_query_params_with_connection(
158+
self, setup_with_client_id
159+
):
160+
authorization_url = self.sso.get_authorization_url(
161+
connection=self.connection,
162+
redirect_uri=self.redirect_uri,
163+
state=self.state,
164+
)
165+
166+
parsed_url = urlparse(authorization_url)
167+
168+
assert dict(parse_qsl(parsed_url.query)) == {
169+
"connection": self.connection,
170+
"client_id": workos.client_id,
171+
"redirect_uri": self.redirect_uri,
172+
"response_type": RESPONSE_TYPE_CODE,
173+
"state": self.state,
174+
}
175+
156176
def test_authorization_url_has_expected_query_params_with_domain_and_provider(
157177
self, setup_with_client_id
158178
):

workos/sso.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def request_helper(self):
4040
return self._request_helper
4141

4242
def get_authorization_url(
43-
self, domain=None, redirect_uri=None, state=None, provider=None
43+
self, domain=None, redirect_uri=None, state=None, provider=None, connection=None
4444
):
4545
"""Generate an OAuth 2.0 authorization URL.
4646
@@ -53,6 +53,7 @@ def get_authorization_url(
5353
state (str) - An encoded string passed to WorkOS that'd be preserved through the authentication workflow, passed
5454
back as a query parameter
5555
provider (ConnectionType) - Authentication service provider descriptor
56+
connection (string) - Unique identifier for a WorkOS Connection
5657
5758
Returns:
5859
str: URL to redirect a User to to begin the OAuth workflow with WorkOS
@@ -63,16 +64,18 @@ def get_authorization_url(
6364
"response_type": RESPONSE_TYPE_CODE,
6465
}
6566

66-
if domain is None and provider is None:
67+
if domain is None and provider is None and connection is None:
6768
raise ValueError(
68-
"Incomplete arguments. Need to specify either a 'domain' or 'provider'"
69+
"Incomplete arguments. Need to specify either a 'connection', 'domain' or 'provider'"
6970
)
7071
if provider is not None:
7172
if not isinstance(provider, ConnectionType):
7273
raise ValueError("'provider' must be of type ConnectionType")
7374
params["provider"] = str(provider.value)
7475
if domain is not None:
7576
params["domain"] = domain
77+
if connection is not None:
78+
params["connection"] = connection
7679

7780
if state is not None:
7881
params["state"] = state

0 commit comments

Comments
 (0)