-
Notifications
You must be signed in to change notification settings - Fork 0
Volo be validate token logic added #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShyamSagothia
wants to merge
5
commits into
master
Choose a base branch
from
volo-be-validate-token-logic-added
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ec03aa
feat: Add Volopay backend client with token validation utility.
ShyamSagothia a7b4a58
added httpx library
ShyamSagothia b49acda
minor changes added
ShyamSagothia b6ded3f
Merge branch 'master' into volo-be-validate-token-logic-added
ShyamSagothia 87bc9f7
resolved pr comments
ShyamSagothia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| """ | ||
| HTTP client utilities for interacting with external/internal services. | ||
| """ | ||
|
|
||
| from .volopay_be import validate_token | ||
|
|
||
| __all__ = ["validate_token"] |
This file contains hidden or 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,109 @@ | ||
| """ | ||
| Volopay Backend (volo-be) HTTP client utilities. | ||
|
|
||
| This module provides shared utilities for microservices that need to interact | ||
| with the main volo-be backend, primarily for auth token validation. | ||
|
|
||
| Usage: | ||
| from vp_core.clients.volopay_be import validate_token | ||
|
|
||
| valid = await validate_token( | ||
| client="web", | ||
| access_token="abc123", | ||
| uid="user@example.com", | ||
| account="volopay", | ||
| volo_be_url="https://api.volopay.com" | ||
| ) | ||
| """ | ||
|
|
||
| import httpx | ||
|
|
||
|
|
||
| async def validate_token( | ||
| client: str, | ||
| access_token: str, | ||
| uid: str, | ||
| account: str, | ||
| volo_be_url: str, | ||
| x_feature: str | None = None, | ||
| ) -> bool: | ||
| """ | ||
| Validates a user's session token by calling volo-be's token validation endpoint. | ||
|
|
||
| This is used by satellite microservices (volo-agents, ocr-reader, etc.) to verify | ||
| that a frontend user's token is valid without maintaining their own user database. | ||
|
|
||
| Note: Environment-specific logic (e.g., skipping validation in test) should be | ||
| handled by the calling service, not here. This is a pure HTTP client function. | ||
|
|
||
| Args: | ||
| client: Client identifier (e.g., "web", "mobile") | ||
| access_token: User's session access token | ||
| uid: User identifier (typically email) | ||
| account: Account/organization identifier | ||
| volo_be_url: Base URL of volo-be (e.g., "https://api.volopay.com") | ||
| x_feature: Optional feature flag header | ||
|
|
||
| Returns: | ||
| True if the token is valid (volo-be returned 200), False otherwise. | ||
|
|
||
| Example: | ||
| >>> valid = await validate_token( | ||
| ... client="web", | ||
| ... access_token="eyJ0eXAi...", | ||
| ... uid="user@example.com", | ||
| ... account="volopay", | ||
| ... volo_be_url="https://api.volopay.com" | ||
| ... ) | ||
| >>> if valid: | ||
| ... # proceed with authenticated request | ||
| """ | ||
| headers = { | ||
| "client": client, | ||
| "access-token": access_token, | ||
| "uid": uid, | ||
| "account": account, | ||
| } | ||
|
|
||
| # Add optional x_feature header if provided | ||
| if x_feature: | ||
| headers["x_feature"] = x_feature | ||
|
|
||
| async with httpx.AsyncClient(base_url=volo_be_url, headers=headers) as http: | ||
| try: | ||
| response = await http.get("/api/v3/auth/user/validate_token") | ||
| return response.status_code == 200 | ||
| except httpx.HTTPError: | ||
| # Network errors, timeouts, etc. — treat as invalid token | ||
| return False | ||
|
|
||
|
|
||
| # Future: Two-layer auth builder | ||
| # When multiple microservices (volo-agents, ocr-reader, future services) all adopt | ||
| # the same two-layer auth pattern (shared secret + token validation fallback), | ||
| # consider adding a generic FastAPI dependency builder here: | ||
| # | ||
| # def create_two_layer_auth_dependency( | ||
| # service_name: str, # "agents", "ocr", etc. | ||
| # secret_env_var: str, # "VOLO_BE_AGENTS_SECRET" | ||
| # volo_be_url_env_var: str = "VOLO_BE_URL", | ||
| # env_var: str = "ENV" | ||
| # ) -> Callable: | ||
| # """ | ||
| # Creates a FastAPI Depends() function that enforces two-layer auth: | ||
| # 1. Checks volo_be_{service_name}_secret header against env var | ||
| # 2. Falls back to validate_token() if Layer 1 fails | ||
| # | ||
| # Returns: | ||
| # A FastAPI dependency (Depends-compatible callable) | ||
| # | ||
| # Example: | ||
| # from vp_core.clients import create_two_layer_auth_dependency | ||
| # | ||
| # AuthDep = create_two_layer_auth_dependency( | ||
| # service_name="agents", | ||
| # secret_env_var="VOLO_BE_AGENTS_SECRET" | ||
| # ) | ||
| # | ||
| # app.include_router(router, dependencies=[AuthDep]) | ||
| # """ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel it should change base url as well. based on the request u are getting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this we discussed, will pass this from FE