Skip to content

Commit

Permalink
Fix python code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
bcbogdan committed Oct 25, 2024
1 parent 03f8163 commit 1ed8a26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
14 changes: 7 additions & 7 deletions v2/microservice_auth/client-credentials.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ func ValidateToken(token string) bool {
You can use the [PyJWT](https://github.com/jpadilla/pyjwt) library to verify the token.

```python
from typing import Optional, List
import requests
import jwt
from jwt import PyJWKClient

def validate_token(token):
def validate_token(token: str) -> bool:
api_domain = "^{form_apiDomain}"
api_base_path = "^{form_apiBasePath}"
audience = "<AUDIENCE>"
Expand All @@ -364,7 +365,7 @@ def validate_token(token):
jwks_client = PyJWKClient(jwks_url)

try:
signing_key = jwks_client.get_signing_keys_from_jwt(token)
signing_key = jwks_client.get_signing_key_from_jwt(token)
decoded = jwt.decode(
token,
signing_key.key,
Expand All @@ -373,14 +374,13 @@ def validate_token(token):
options={"require": ["stt", "scp"]}
)

sst = decoded.get('sst', None)
stt: Optional[int] = decoded.get('stt')
if stt != 1:
return False


scopes = decoded.get('scp', [])
if required_scope not in scopes:
return False
scopes: List[str] = decoded.get('scp', [])
if not isinstance(scopes, list) or required_scope not in scopes:
return False

return True
except Exception as e:
Expand Down
13 changes: 7 additions & 6 deletions v2/unified-login/customizations/verify-tokens.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ func ValidateToken(token string) bool {
You can use the [PyJWT](https://github.com/jpadilla/pyjwt) library to verify the token.

```python
from typing import Optional, List
import requests
import jwt
from jwt import PyJWKClient

def validate_token(token):
def validate_token(token: str) -> bool:
api_domain = "^{form_apiDomain}"
api_base_path = "^{form_apiBasePath}"
client_id = "<CLIENT_ID>"
Expand All @@ -171,7 +172,7 @@ def validate_token(token):
jwks_client = PyJWKClient(jwks_url)

try:
signing_key = jwks_client.get_signing_keys_from_jwt(token)
signing_key = jwks_client.get_signing_key_from_jwt(token)
decoded = jwt.decode(
token,
signing_key.key,
Expand All @@ -180,17 +181,17 @@ def validate_token(token):
options={"require": ["stt", "client_id", "scp"]}
)

sst = decoded.get('sst', None)
stt: Optional[int] = decoded.get('stt')
if stt != 1:
return False

token_client_id = decoded.get('client_id', None)
token_client_id: Optional[str] = decoded.get('client_id', None)
if client_id != token_client_id:
return False


scopes = decoded.get('scp', [])
if required_scope not in scopes:
scopes: List[str] = decoded.get('scp', [])
if not isinstance(scopes, list) or required_scope not in scopes:
return False

return True
Expand Down

0 comments on commit 1ed8a26

Please sign in to comment.