sophi-app-internal
Python Package
Overview
sophi-app-internal is a Python package designed to facilitate secure and authenticated HTTP request handling, particularly in environments where JWT (JSON Web Token) authentication is used. The package includes utilities for validating JWTs, fetching public keys, constructing HTTP request objects with robust header and parameter management, and interacting with Azure Cosmos DB.
Features
- JWT validation and verification using Auth0.
- Secure fetching of JSON Web Key Sets (JWKS).
- Construction and management of HTTP request objects with headers, parameters, and body handling.
- Azure Cosmos DB client for querying and managing documents.
Installation
To install the sophi-app-internal package, use the following command:
pip install sophi-app-internal
Token Validator Usage
Setting Up Variables
Before using the package, ensure you have set up the necessary environment variables:
- AUTH0_DOMAIN: Your Auth0 domain, e.g.
<tenant>.us.auth0.com
. - AUTH0_AUDIENCE: The expected token audience.
Note: client ID and client secret are not required to validate tokens
You can set these variables in your environment or pass them directly when initializing the Auth0ManagementApiClient
.
Note: You need to register your API on Auth0 before getting a valid audience value.
Example Code
Here's an example of how to use the token_validator
method:
from sophi_app_internal.integrations.auth0.management_api import Auth0ManagementApiClient
auth0_client = Auth0ManagementApiClient()
token = "<your token>"
try:
claims = auth0_client.token_validator(token)
user_id = claims['sub']
print(user_id)
except Exception as e:
print(e)
Cosmos DB Client Usage
Setting Up Variables
Before using the Cosmos DB client, ensure you have set up the necessary variables:
- COSMOS_CONNECTION_STRING: Your Azure - Cosmos DB connection string.
- DB_NAME: The name of your database.
- CONTAINER_NAME: The name of your container.
You can set these variables in your environment:
import os
connection_string = os.getenv("COSMOS_CONNECTION_STRING")
db_name = "your_db_name"
container_name = "your_container_name"
Example Code
Using CosmosContainerClient
Here's an example of how to use the CosmosContainerClient to query and manage documents in Azure Cosmos DB.
import os
import logging
from sophi_app_internal import CosmosContainerClient
logging.basicConfig(level=logging.INFO)
connection_string = os.getenv("COSMOS_CONNECTION_STRING")
db_name = "your_db_name"
container_name = "your_container_name"
client = CosmosContainerClient(connection_string, db_name, container_name)
query = "SELECT * FROM c"
user_id = "1234"
partition_key = [user_id]
documents = client.query_cosmosdb_container(query, partition_key)
print(documents)
new_db_name = "another_db_name"
new_container_name = "another_container_name"
client.set_database_and_container(new_db_name, new_container_name)
documents = client.query_cosmosdb_container(query)
print(documents)
document = {
"id": "1",
"name": "Sample Document",
"description": "This is a sample document."
}
client.upsert_cosmosdb_document(document)