Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tests/test_sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_authorization_url_has_expected_query_params_with_domain_and_provider(
"state": self.state,
}

def test_get_profile_returns_expected_workosprofile_object(
def test_get_profile_and_token_returns_expected_workosprofile_object(
self, setup_with_client_id, mock_profile, mock_request_method
):
response_dict = {
Expand All @@ -209,9 +209,10 @@ def test_get_profile_returns_expected_workosprofile_object(

mock_request_method("post", response_dict, 200)

profile = self.sso.get_profile(123)
profile_and_token = self.sso.get_profile_and_token(123)

assert profile.to_dict() == mock_profile
assert profile_and_token.access_token == "01DY34ACQTM3B1CSX1YSZ8Z00D"
assert profile_and_token.profile.to_dict() == mock_profile

def test_create_connection(
self, setup_with_client_id, mock_request_method, mock_connection
Expand Down
32 changes: 32 additions & 0 deletions workos/resources/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,35 @@ class WorkOSProfile(WorkOSBaseResource):
"idp_id",
"raw_attributes",
]


class WorkOSProfileAndToken(WorkOSBaseResource):
"""Representation of a User Profile and Access Token as returned by WorkOS through the SSO feature.

Attributes:
OBJECT_FIELDS (list): List of fields a WorkOSProfileAndToken is comprised of.
"""

OBJECT_FIELDS = [
"access_token",
]

@classmethod
def construct_from_response(cls, response):
profile_and_token = super(WorkOSProfileAndToken, cls).construct_from_response(
response
)

profile_and_token.profile = WorkOSProfile.construct_from_response(
response["profile"]
)

return profile_and_token

def to_dict(self):
profile_and_token_dict = super(WorkOSProfileAndToken, self).to_dict()

profile_dict = self.profile.to_dict()
profile_and_token_dict["profile"] = profile_dict

return profile_and_token_dict
8 changes: 4 additions & 4 deletions workos/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import workos
from workos.exceptions import ConfigurationException
from workos.resources.sso import WorkOSProfile
from workos.resources.sso import WorkOSProfileAndToken
from workos.utils.connection_types import ConnectionType
from workos.utils.request import (
RequestHelper,
Expand Down Expand Up @@ -88,7 +88,7 @@ def get_authorization_url(

return prepared_request.url

def get_profile(self, code):
def get_profile_and_token(self, code):
"""Get the profile of an authenticated User

Once authenticated, using the code returned having followed the authorization URL,
Expand All @@ -98,7 +98,7 @@ def get_profile(self, code):
code (str): Code returned by WorkOS on completion of OAuth 2.0 workflow

Returns:
WorkOSProfile: WorkOSProfile object representing the User
WorkOSProfileAndToken: WorkOSProfileAndToken object representing the User
"""
params = {
"client_id": workos.client_id,
Expand All @@ -111,7 +111,7 @@ def get_profile(self, code):
TOKEN_PATH, method=REQUEST_METHOD_POST, params=params
)

return WorkOSProfile.construct_from_response(response["profile"])
return WorkOSProfileAndToken.construct_from_response(response)

def promote_draft_connection(self, token):
"""Promote a Draft Connection
Expand Down