diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d0df718b2..91e03b401 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,11 +15,14 @@ jobs: test: name: Publish to PyPI runs-on: ubuntu-latest + permissions: + id-token: write + contents: read steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python }} + python-version: "3.12" cache: "pip" cache-dependency-path: setup.py @@ -31,11 +34,12 @@ jobs: run: | python -m pytest - - name: Publish - env: - TWINE_NON_INTERACTIVE: true - TWINE_USERNAME: "__token__" - TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }} + - name: Build run: | + pip install --upgrade setuptools wheel python setup.py sdist bdist_wheel - twine upload dist/* --skip-existing + + - name: Publish + uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true diff --git a/setup.py b/setup.py index 8d4c7cc39..4a39e2051 100644 --- a/setup.py +++ b/setup.py @@ -33,9 +33,9 @@ "flake8", "pytest==8.1.1", "pytest-cov==2.8.1", - "six==1.13.0", + "six==1.16.0", "black==22.3.0", - "twine==4.0.2", + "twine==5.1.1", "requests==2.30.0", "urllib3==2.0.2", ], diff --git a/tests/test_sso.py b/tests/test_sso.py index 70649fd7b..6bec3bf67 100644 --- a/tests/test_sso.py +++ b/tests/test_sso.py @@ -424,6 +424,40 @@ def test_get_profile_and_token_returns_expected_workosprofile_object( assert profile_and_token.access_token == "01DY34ACQTM3B1CSX1YSZ8Z00D" assert profile_and_token.profile.to_dict() == mock_profile + def test_get_profile_and_token_from_id_token_returns_expected_workosprofile_object( + self, setup_with_client_id, mock_profile, mock_request_method + ): + response_dict = { + "profile": { + "object": "profile", + "id": mock_profile["id"], + "email": mock_profile["email"], + "first_name": mock_profile["first_name"], + "groups": mock_profile["groups"], + "organization_id": mock_profile["organization_id"], + "connection_id": mock_profile["connection_id"], + "connection_type": mock_profile["connection_type"], + "last_name": mock_profile["last_name"], + "idp_id": mock_profile["idp_id"], + "raw_attributes": { + "email": mock_profile["raw_attributes"]["email"], + "first_name": mock_profile["raw_attributes"]["first_name"], + "last_name": mock_profile["raw_attributes"]["last_name"], + "groups": mock_profile["raw_attributes"]["groups"], + }, + }, + "access_token": "01DY34ACQTM3B1CSX1YSZ8Z00D", + } + + mock_request_method("post", response_dict, 200) + + profile_and_token = self.sso.get_profile_and_token_from_id_token( + "eyJhbGciOiJSUzI1NiJ9.id.token", "org_01EHQMYV6MBK39QC5PZXHY59C3" + ) + + assert profile_and_token.access_token == "01DY34ACQTM3B1CSX1YSZ8Z00D" + assert profile_and_token.profile.to_dict() == mock_profile + def test_get_profile_and_token_without_first_name_or_last_name_returns_expected_workosprofile_object( self, setup_with_client_id, mock_magic_link_profile, mock_request_method ): diff --git a/workos/__about__.py b/workos/__about__.py index 4e19203ce..7664f6a00 100644 --- a/workos/__about__.py +++ b/workos/__about__.py @@ -12,7 +12,7 @@ __package_url__ = "https://github.com/workos-inc/workos-python" -__version__ = "4.15.0" +__version__ = "4.16.0" __author__ = "WorkOS" diff --git a/workos/sso.py b/workos/sso.py index cff8d9e35..d26cb2e8f 100644 --- a/workos/sso.py +++ b/workos/sso.py @@ -24,6 +24,8 @@ PROFILE_PATH = "sso/profile" OAUTH_GRANT_TYPE = "authorization_code" +TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange" +ID_TOKEN_SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id_token" RESPONSE_LIMIT = 10 @@ -162,6 +164,39 @@ def get_profile_and_token(self, code): return WorkOSProfileAndToken.construct_from_response(response) + def get_profile_and_token_from_id_token(self, id_token, organization_id): + """Exchange an externally issued OIDC ID token for a Profile and Token + + For flows where the user authenticates natively with the identity + provider (for example a mobile app using MSAL against Microsoft Entra) + and no browser redirect occurs. The ID token is exchanged for the same + WorkOS profile the authorization code flow would return. The connection + must have an ID token trust configured for the token's issuer and + audience. + + Args: + id_token (str): The OIDC ID token issued to the client. + organization_id (str): The organization whose connection the ID + token should be validated against. + + Returns: + WorkOSProfileAndToken: WorkOSProfileAndToken object representing the User + """ + params = { + "client_id": workos.client_id, + "client_secret": workos.api_key, + "grant_type": TOKEN_EXCHANGE_GRANT_TYPE, + "subject_token": id_token, + "subject_token_type": ID_TOKEN_SUBJECT_TOKEN_TYPE, + "organization_id": organization_id, + } + + response = self.request_helper.request( + TOKEN_PATH, method=REQUEST_METHOD_POST, params=params + ) + + return WorkOSProfileAndToken.construct_from_response(response) + def get_connection(self, connection): """Gets details for a single Connection