cognee/cognee/get_token.py
Igor Ilic 7bf30f7373
fix: Cognee backend fixes (#659)
<!-- .github/pull_request_template.md -->

## Description
Cognee backend fixes

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Improved handling of `tenant_id` in JWT payload for enhanced type
safety.
- Unique identifier generation for datasets now considers the owner ID,
allowing for multiple users to share the same dataset name.

- **Bug Fixes**
- Disabled user role permissions in the permission check logic
temporarily during a rework.

- **Refactor**
  - Simplified dependencies by removing unnecessary model imports.
- Updated parameter name from `tenant` to `tenant_id` for clarity in JWT
creation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-03-20 21:51:35 +01:00

23 lines
634 B
Python

import jwt
import os
import datetime
SECRET_KEY = os.getenv("FASTAPI_USERS_JWT_SECRET", "super_secret")
def create_jwt(user_id: str, tenant_id: str, roles: list[str]):
payload = {
"user_id": user_id,
"tenant_id": tenant_id,
"roles": roles,
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1), # 1 hour expiry
}
return jwt.encode(payload, SECRET_KEY, algorithm="HS256")
if __name__ == "__main__":
# Example token generation
token = create_jwt(
"6763554c-91bd-432c-aba8-d42cd72ed659", "4523544d-82bd-432c-aca7-d42cd72ed651", ["admin"]
)
print(token)