diff --git a/stytch/consumer/api/m2m.py b/stytch/consumer/api/m2m.py index 175f9cb..a56d6e6 100644 --- a/stytch/consumer/api/m2m.py +++ b/stytch/consumer/api/m2m.py @@ -139,6 +139,10 @@ def authenticate_token( if generic_claims is None: return None + # Session JWTs share the project's signing keys and must not pass as M2M tokens. + if "https://stytch.com/session" in generic_claims.untyped_claims: + return None + scope = generic_claims.untyped_claims[_scope_claim] scopes = [s for s in scope.split(" ") if len(s) > 0] required_scopes = required_scopes or [] @@ -195,6 +199,10 @@ async def authenticate_token_async( if generic_claims is None: return None + # Session JWTs share the project's signing keys and must not pass as M2M tokens. + if "https://stytch.com/session" in generic_claims.untyped_claims: + return None + scope = generic_claims.untyped_claims[_scope_claim] scopes = [s for s in scope.split(" ") if len(s) > 0] required_scopes = required_scopes or [] diff --git a/stytch/consumer/api/test/test_m2m_authenticate_token.py b/stytch/consumer/api/test/test_m2m_authenticate_token.py new file mode 100644 index 0000000..78ff7cb --- /dev/null +++ b/stytch/consumer/api/test/test_m2m_authenticate_token.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +import unittest +from unittest.mock import MagicMock, patch + +from stytch.consumer.api.m2m import M2M +from stytch.shared.jwt_helpers import GenericClaims + +FAKE_JWT = "fake.jwt.token" +FAKE_PROJECT_ID = "project-test-abc123" + +FAKE_GENERIC_CLAIMS = GenericClaims( + reserved_claims={"sub": "m2m-client-test-123", "exp": 9999999999}, + untyped_claims={"scope": "read:docs write:docs"}, +) + +SESSION_CLAIMS = GenericClaims( + reserved_claims={"sub": "user-test-123", "exp": 9999999999}, + untyped_claims={ + "scope": "read:docs write:docs", + "https://stytch.com/session": {"id": "session-test-123"}, + }, +) + + +def _make_m2m() -> M2M: + mock_api_base = MagicMock() + mock_api_base.base_url = "https://test.stytch.com/" + return M2M( + api_base=mock_api_base, + sync_client=MagicMock(), + async_client=MagicMock(), + jwks_client=MagicMock(), + project_id=FAKE_PROJECT_ID, + ) + + +class TestM2MAuthenticateToken(unittest.TestCase): + def setUp(self) -> None: + self.m2m = _make_m2m() + + @patch("stytch.consumer.api.m2m.jwt_helpers.authenticate_jwt_local") + def test_returns_none_for_invalid_jwt(self, mock_jwt) -> None: + mock_jwt.return_value = None + self.assertIsNone(self.m2m.authenticate_token(access_token=FAKE_JWT)) + + @patch("stytch.consumer.api.m2m.jwt_helpers.authenticate_jwt_local") + def test_returns_claims_for_valid_jwt(self, mock_jwt) -> None: + mock_jwt.return_value = FAKE_GENERIC_CLAIMS + result = self.m2m.authenticate_token(access_token=FAKE_JWT) + self.assertIsNotNone(result) + if result is not None: + self.assertEqual(result.client_id, "m2m-client-test-123") + self.assertEqual(result.scopes, ["read:docs", "write:docs"]) + + @patch("stytch.consumer.api.m2m.jwt_helpers.authenticate_jwt_local") + def test_returns_none_for_session_jwt(self, mock_jwt) -> None: + mock_jwt.return_value = SESSION_CLAIMS + self.assertIsNone(self.m2m.authenticate_token(access_token=FAKE_JWT)) + + +if __name__ == "__main__": + unittest.main() diff --git a/stytch/consumer/api/test/test_m2m_authenticate_token_async.py b/stytch/consumer/api/test/test_m2m_authenticate_token_async.py index e825133..c5a3b7e 100644 --- a/stytch/consumer/api/test/test_m2m_authenticate_token_async.py +++ b/stytch/consumer/api/test/test_m2m_authenticate_token_async.py @@ -16,6 +16,14 @@ untyped_claims={"scope": "read:docs write:docs"}, ) +SESSION_CLAIMS = GenericClaims( + reserved_claims={"sub": "user-test-123", "exp": 9999999999}, + untyped_claims={ + "scope": "read:docs write:docs", + "https://stytch.com/session": {"id": "session-test-123"}, + }, +) + def _make_m2m() -> M2M: mock_api_base = MagicMock() @@ -68,6 +76,15 @@ async def test_returns_none_when_required_scope_missing(self, mock_jwt) -> None: ) self.assertIsNone(result) + @patch( + "stytch.consumer.api.m2m.jwt_helpers.authenticate_jwt_local_async", + new_callable=AsyncMock, + ) + async def test_returns_none_for_session_jwt(self, mock_jwt) -> None: + mock_jwt.return_value = SESSION_CLAIMS + result = await self.m2m.authenticate_token_async(access_token=FAKE_JWT) + self.assertIsNone(result) + async def test_is_non_blocking_jwt_verification(self) -> None: DELAY = 0.1 N = 5 diff --git a/stytch/version.py b/stytch/version.py index 1609d49..63b4c27 100644 --- a/stytch/version.py +++ b/stytch/version.py @@ -1 +1 @@ -__version__ = "15.3.0" +__version__ = "15.3.1"