Skip to content
Draft
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
8 changes: 8 additions & 0 deletions stytch/consumer/api/m2m.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down Expand Up @@ -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 []
Expand Down
63 changes: 63 additions & 0 deletions stytch/consumer/api/test/test_m2m_authenticate_token.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions stytch/consumer/api/test/test_m2m_authenticate_token_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stytch/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "15.3.0"
__version__ = "15.3.1"
Loading