Skip to content

Commit 99997bd

Browse files
one more gen
1 parent 0ee1973 commit 99997bd

File tree

4 files changed

+33
-111
lines changed

4 files changed

+33
-111
lines changed

stytch/b2b/api/idp.py

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
21
from __future__ import annotations
32

4-
from typing import Any, Dict, Optional, Union
3+
from typing import Any, Dict, Optional
54

65
import jwt
76

7+
from stytch.b2b.models.idp import AccessTokenJWTClaims, AccessTokenJWTResponse
88
from stytch.core.api_base import ApiBase
99
from stytch.core.http.client import AsyncClient, SyncClient
10-
from stytch.shared import jwt_helpers, rbac_local
10+
from stytch.shared import jwt_helpers
1111
from stytch.shared.policy_cache import PolicyCache
12-
from stytch.b2b.models.idp import AccessTokenJWTClaims, AccessTokenJWTResponse
12+
1313

1414
class IDP:
1515
def __init__(
@@ -27,7 +27,6 @@ def __init__(
2727
self.policy_cache = policy_cache
2828
self.jwks_client = jwks_client
2929
self.project_id = project_id
30-
3130

3231
# MANUAL(introspect_idp_access_token)(SERVICE_METHOD)
3332
# ADDIMPORT: from typing import Optional
@@ -37,10 +36,15 @@ def introspect_idp_access_token(
3736
access_token: str,
3837
client_id: str,
3938
client_secret: Optional[str] = None,
40-
grant_type: str = 'authorization_code',
41-
token_type_hint: str = 'access_token'
39+
grant_type: str = "authorization_code",
40+
token_type_hint: str = "access_token",
4241
) -> Optional[AccessTokenJWTClaims]:
43-
return self.introspect_idp_access_token_local(access_token, client_id) or self.introspect_idp_access_token_network(access_token, client_id, client_secret, grant_type, token_type_hint)
42+
return self.introspect_idp_access_token_local(
43+
access_token, client_id
44+
) or self.introspect_idp_access_token_network(
45+
access_token, client_id, client_secret, grant_type, token_type_hint
46+
)
47+
4448
# ENDMANUAL(introspect_idp_access_token)
4549

4650
# MANUAL(introspect_idp_access_token_network)(SERVICE_METHOD)
@@ -53,30 +57,30 @@ def introspect_idp_access_token_network(
5357
access_token: str,
5458
client_id: str,
5559
client_secret: Optional[str] = None,
56-
grant_type: str = 'authorization_code',
57-
token_type_hint: str = 'access_token'
60+
grant_type: str = "authorization_code",
61+
token_type_hint: str = "access_token",
5862
) -> Optional[AccessTokenJWTClaims]:
59-
headers: Dict[str, str] = {
60-
"Content-Type": "application/x-www-form-urlencoded"
61-
}
63+
headers: Dict[str, str] = {"Content-Type": "application/x-www-form-urlencoded"}
6264
data: Dict[str, Any] = {
6365
"token": access_token,
6466
"client_id": client_id,
6567
"grant_type": grant_type,
66-
"token_type_hint": token_type_hint
68+
"token_type_hint": token_type_hint,
6769
}
6870
if client_secret is not None:
6971
data["client_secret"] = client_secret
7072

71-
url = self.api_base.url_for(f"/v1/public/{self.project_id}/oauth2/introspect", data)
73+
url = self.api_base.url_for(
74+
f"/v1/public/{self.project_id}/oauth2/introspect", data
75+
)
7276
res = self.sync_client.postForm(url, data, headers)
73-
jwtResponse = AccessTokenJWTResponse.from_json(res.response.status_code, res.json)
77+
jwtResponse = AccessTokenJWTResponse.from_json(
78+
res.response.status_code, res.json
79+
)
7480
if not jwtResponse.active:
7581
return None
7682
return AccessTokenJWTClaims(
77-
subject=jwtResponse.sub,
78-
scopes=jwtResponse.scope,
79-
custom_claims=None
83+
subject=jwtResponse.sub, scopes=jwtResponse.scope, custom_claims=None
8084
)
8185

8286
# ENDMANUAL(introspect_idp_access_token_network)
@@ -96,7 +100,7 @@ def introspect_idp_access_token_local(
96100
jwks_client=self.jwks_client,
97101
jwt=access_token,
98102
custom_audience=client_id,
99-
custom_issuer=f"https://stytch.com/{self.project_id}"
103+
custom_issuer=f"https://stytch.com/{self.project_id}",
100104
)
101105
if generic_claims is None:
102106
return None
@@ -108,7 +112,7 @@ def introspect_idp_access_token_local(
108112
return AccessTokenJWTClaims(
109113
subject=generic_claims.reserved_claims["sub"],
110114
scopes=generic_claims.untyped_claims[_scope_claim],
111-
custom_claims=custom_claims
115+
custom_claims=custom_claims,
112116
)
113117

114-
# ENDMANUAL(introspect_idp_access_token_local)
118+
# ENDMANUAL(introspect_idp_access_token_local)

stytch/b2b/api/sessions.py

-85
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from stytch.core.http.client import AsyncClient, SyncClient
2828
from stytch.shared import jwt_helpers, rbac_local
2929
from stytch.shared.policy_cache import PolicyCache
30-
from stytch.b2b.models.idp import AccessTokenJWTClaims, AccessTokenJWTResponse
3130

3231

3332
class Sessions:
@@ -758,87 +757,3 @@ async def authenticate_jwt_local_async(
758757
return local_resp.member_session
759758

760759
# ENDMANUAL(authenticate_jwt_local)
761-
762-
# MANUAL(introspect_idp_access_token)(SERVICE_METHOD)
763-
# ADDIMPORT: from typing import Optional
764-
# ADDIMPORT: from stytch.b2b.models.idp import AccessTokenJWTResponse
765-
def introspect_idp_access_token(
766-
self,
767-
access_token: str,
768-
client_id: str,
769-
client_secret: Optional[str] = None,
770-
grant_type: str = 'authorization_code',
771-
token_type_hint: str = 'access_token'
772-
) -> Optional[AccessTokenJWTClaims]:
773-
return self.introspect_idp_access_token_local(access_token, client_id) or self.introspect_idp_access_token_network(access_token, client_id, client_secret, grant_type, token_type_hint)
774-
# ENDMANUAL(introspect_idp_access_token)
775-
776-
# MANUAL(introspect_idp_access_token_network)(SERVICE_METHOD)
777-
# ADDIMPORT: from typing import Optional
778-
# ADDIMPORT: from stytch.b2b.models.idp import AccessTokenJWTClaims, AccessTokenJWTResponse
779-
# ADDIMPORT: from stytch.shared import jwt_helpers
780-
# ADDIMPORT: from stytch.shared import rbac_local
781-
def introspect_idp_access_token_network(
782-
self,
783-
access_token: str,
784-
client_id: str,
785-
client_secret: Optional[str] = None,
786-
grant_type: str = 'authorization_code',
787-
token_type_hint: str = 'access_token'
788-
) -> Optional[AccessTokenJWTClaims]:
789-
headers: Dict[str, str] = {
790-
"Content-Type": "application/x-www-form-urlencoded"
791-
}
792-
data: Dict[str, Any] = {
793-
"token": access_token,
794-
"client_id": client_id,
795-
"grant_type": grant_type,
796-
"token_type_hint": token_type_hint
797-
}
798-
if client_secret is not None:
799-
data["client_secret"] = client_secret
800-
801-
url = self.api_base.url_for(f"/v1/public/{self.project_id}/oauth2/introspect", data)
802-
res = self.sync_client.postForm(url, data, headers)
803-
jwtResponse = AccessTokenJWTResponse.from_json(res.response.status_code, res.json)
804-
if not jwtResponse.active:
805-
return None
806-
return AccessTokenJWTClaims(
807-
subject=jwtResponse.sub,
808-
scopes=jwtResponse.scope,
809-
custom_claims=None
810-
)
811-
812-
# ENDMANUAL(introspect_idp_access_token_network)
813-
814-
# MANUAL(introspect_idp_access_token_local)(SERVICE_METHOD)
815-
# ADDIMPORT: from typing import Optional
816-
# ADDIMPORT: from stytch.b2b.models.sessions import AccessTokenJWTClaims
817-
# ADDIMPORT: from stytch.shared import jwt_helpers
818-
def introspect_idp_access_token_local(
819-
self,
820-
access_token: str,
821-
client_id: str,
822-
) -> Optional[AccessTokenJWTClaims]:
823-
_scope_claim = "scope"
824-
generic_claims = jwt_helpers.authenticate_jwt_local(
825-
project_id=self.project_id,
826-
jwks_client=self.jwks_client,
827-
jwt=access_token,
828-
custom_audience=client_id,
829-
custom_issuer=f"https://stytch.com/{self.project_id}"
830-
)
831-
if generic_claims is None:
832-
return None
833-
834-
custom_claims = {
835-
k: v for k, v in generic_claims.untyped_claims.items() if k != _scope_claim
836-
}
837-
838-
return AccessTokenJWTClaims(
839-
subject=generic_claims.reserved_claims["sub"],
840-
scopes=generic_claims.untyped_claims[_scope_claim],
841-
custom_claims=custom_claims
842-
)
843-
844-
# ENDMANUAL(introspect_idp_access_token_local)

stytch/b2b/models/idp.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from stytch.core.response_base import ResponseBase
2-
from typing import Any, Dict, List, Optional
1+
from typing import Any, Dict, Optional
32

43
import pydantic
54

5+
from stytch.core.response_base import ResponseBase
6+
7+
68
# MANUAL(AccessTokenJWTResponse)(TYPES)
79
# ADDIMPORT: from typing import Any, Dict, List, Optional
810
# ADDIMPORT: import pydantic
@@ -21,6 +23,7 @@ class AccessTokenJWTResponse(ResponseBase):
2123

2224
# ENDMANUAL(AccessTokenJWTResponse)
2325

26+
2427
# MANUAL(AccessTokenJWTClaims)(TYPES)
2528
# ADDIMPORT: from typing import Any, Dict, List, Optional
2629
# ADDIMPORT: import pydantic
@@ -37,4 +40,4 @@ class AccessTokenJWTClaims(pydantic.BaseModel):
3740
custom_claims: Optional[Dict[str, Any]] = None
3841

3942

40-
# ENDMANUAL(AccessTokenJWTClaims)
43+
# ENDMANUAL(AccessTokenJWTClaims)

stytch/core/http/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def post(
6565
final_headers.update(headers or {})
6666
resp = requests.post(url, json=json, headers=final_headers, auth=self.auth)
6767
return self._response_from_request(resp)
68-
68+
6969
def postForm(
7070
self,
7171
url: str,

0 commit comments

Comments
 (0)