-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
langchain_permit/examples/demo_scripts/demo_jwt_validation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import asyncio | ||
|
||
from langchain_permit.tools import LangchainJWTValidationTool | ||
|
||
# Load your JWT token from environment variables (e.g., .env) | ||
TEST_JWT_TOKEN = os.getenv("TEST_JWT_TOKEN") | ||
JWKS_URL = os.getenv("JWKS_URL", "") | ||
|
||
async def main(): | ||
|
||
print("Test Token JWt =====>", JWKS_URL) | ||
# 1. Initialize the JWT validation tool | ||
jwt_validator = LangchainJWTValidationTool( | ||
jwks_url=JWKS_URL | ||
) | ||
|
||
# 2. Validate the token | ||
try: | ||
# _arun calls the async JWT validation logic | ||
claims = await jwt_validator._arun(TEST_JWT_TOKEN) | ||
print("✅ Token validated successfully!") | ||
print("Decoded Claims:", claims) | ||
except Exception as e: | ||
print("❌ Token validation failed:", str(e)) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
52 changes: 52 additions & 0 deletions
52
langchain_permit/examples/demo_scripts/demo_permissions_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# examples/demo_permissions_check.py | ||
|
||
import os | ||
import asyncio | ||
|
||
from permit import Permit | ||
from langchain_permit.tools import LangchainPermissionsCheckTool | ||
|
||
PERMIT_API_KEY = os.getenv("PERMIT_API_KEY", "") | ||
PERMIT_PDP_URL = os.getenv("PERMIT_PDP_URL", "") | ||
DEFAULT_ACTION = "read" | ||
RESOURCE_TYPE = "Document" | ||
|
||
async def main(): | ||
# 1. Create a Permit client | ||
permit_client = Permit( | ||
token=PERMIT_API_KEY, | ||
pdp=PERMIT_PDP_URL | ||
) | ||
|
||
# 2. Initialize the permission-check tool | ||
permissions_checker = LangchainPermissionsCheckTool( | ||
name="permission_check", | ||
description="Checks if a user can read a document", | ||
permit=permit_client, | ||
) | ||
|
||
# 3. Mock a user object and resource | ||
user = { | ||
"key": "user-123", | ||
"firstName": "Harry", | ||
"attributes": {"role": "basic_user"} | ||
} | ||
resource = { | ||
"type": RESOURCE_TYPE, | ||
"key": "doc123", | ||
"tenant": "techcorp" | ||
} | ||
|
||
# 4. Use the async _arun to avoid nested event loops | ||
try: | ||
allowed_result = await permissions_checker._arun( | ||
user=user, | ||
action=DEFAULT_ACTION, | ||
resource=resource | ||
) | ||
print(f"✅ Permission check result: {allowed_result}") | ||
except Exception as e: | ||
print(f"❌ Permission check failed: {e}") | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Oops, something went wrong.