Django package for integrating SVA (Secure Vault Authentication) OAuth provider
Project description
SVA OAuth Client
A professional Django package for integrating SVA (Secure Vault Authentication) OAuth provider into your Django applications. This package provides a complete solution for authenticating users via SVA OAuth and retrieving identity blocks data from the consent screen.
Features
- ✅ Complete OAuth 2.0 Flow: Authorization Code Flow with PKCE support
- ✅ Easy Integration: Simple decorators and utilities for quick setup
- ✅ Identity Blocks: Retrieve all blocks data from consent screen
- ✅ Session Management: Automatic token storage and management
- ✅ Error Handling: Comprehensive error handling with user-friendly messages
- ✅ Django Integration: Seamless integration with Django views and templates
- ✅ Type Hints: Full type hint support for better IDE experience
Installation
pip install sva-oauth-client
Or install from source:
git clone https://github.com/getsva/sva-oauth-client.git
cd sva-oauth-client
pip install -e .
Quick Start
1. Add to INSTALLED_APPS
# settings.py
INSTALLED_APPS = [
# ... other apps
'sva_oauth_client',
]
2. Configure Settings
# settings.py
# SVA OAuth Configuration
SVA_OAUTH_BASE_URL = 'http://localhost:8000' # Your SVA OAuth provider URL
SVA_OAUTH_CLIENT_ID = 'your_client_id_here'
SVA_OAUTH_CLIENT_SECRET = 'your_client_secret_here'
SVA_OAUTH_REDIRECT_URI = 'http://localhost:8001/oauth/callback/'
SVA_DATA_TOKEN_SECRET = 'your_data_token_secret' # Must match SVA provider
SVA_DATA_TOKEN_ALGORITHM = 'HS256' # Default: HS256
# Optional: Request specific scopes (default: 'openid email profile')
SVA_OAUTH_SCOPES = 'openid email profile username name bio address social images pronoun dob skills hobby email phone pan_card crypto_wallet education employment professional_license aadhar driving_license voter_id passport'
# Optional: Custom redirect URLs
SVA_OAUTH_SUCCESS_REDIRECT = '/' # After successful login
SVA_OAUTH_ERROR_REDIRECT = '/' # On error
SVA_OAUTH_LOGOUT_REDIRECT = '/' # After logout
SVA_OAUTH_LOGIN_URL = '/oauth/login/' # Login URL
3. Add URLs
# urls.py
from django.urls import path, include
urlpatterns = [
# ... other URLs
path('oauth/', include('sva_oauth_client.urls')),
]
4. Use in Your Views
Basic Usage
from sva_oauth_client.decorators import sva_oauth_required
from sva_oauth_client.utils import get_blocks_data, get_userinfo
@sva_oauth_required
def my_view(request):
# User is authenticated with SVA OAuth
blocks_data = get_blocks_data(request.session)
userinfo = get_userinfo(request.session)
return render(request, 'my_template.html', {
'blocks': blocks_data,
'userinfo': userinfo,
})
Require Specific Blocks
from sva_oauth_client.decorators import sva_blocks_required
from sva_oauth_client.utils import get_blocks_data
@sva_blocks_required('email', 'name', 'phone')
def my_view(request):
# User has approved email, name, and phone blocks
blocks_data = get_blocks_data(request.session)
email = blocks_data.get('email')
name = blocks_data.get('name')
phone = blocks_data.get('phone')
return render(request, 'my_template.html', {
'email': email,
'name': name,
'phone': phone,
})
Manual Client Usage
from sva_oauth_client import SVAOAuthClient
# Initialize client
client = SVAOAuthClient(
base_url='http://localhost:8000',
client_id='your_client_id',
client_secret='your_client_secret',
redirect_uri='http://localhost:8001/oauth/callback/',
data_token_secret='your_data_token_secret',
)
# Get authorization URL
auth_url, code_verifier = client.get_authorization_url()
# Store code_verifier in session
request.session['code_verifier'] = code_verifier
# Redirect user to auth_url
# After callback, exchange code for tokens
tokens = client.exchange_code_for_tokens(
code=request.GET.get('code'),
code_verifier=request.session.get('code_verifier')
)
# Get blocks data
blocks_data = client.get_blocks_data(tokens['data_token'])
API Reference
SVAOAuthClient
Main OAuth client class.
Methods
-
get_authorization_url(state=None, code_verifier=None) -> tuple[str, str]- Generate authorization URL and code verifier
- Returns: (authorization_url, code_verifier)
-
exchange_code_for_tokens(code, code_verifier, state=None) -> dict- Exchange authorization code for tokens
- Returns: Dictionary with access_token, refresh_token, data_token, etc.
-
refresh_access_token(refresh_token) -> dict- Refresh access token using refresh token
-
get_userinfo(access_token) -> dict- Get user information from OAuth provider
-
decode_data_token(data_token) -> dict- Decode and verify data_token JWT
-
get_blocks_data(data_token) -> dict- Extract blocks data from data_token
Decorators
@sva_oauth_required
Require SVA OAuth authentication. Redirects to login if not authenticated.
@sva_oauth_required
def my_view(request):
# User is authenticated
pass
@sva_blocks_required(*blocks)
Require specific identity blocks. Redirects to login if blocks are missing.
@sva_blocks_required('email', 'name', 'phone')
def my_view(request):
# User has approved email, name, and phone
pass
Utility Functions
get_blocks_data(session) -> dict | None
Get blocks data from session.
get_userinfo(session) -> dict | None
Get userinfo from session or fetch from OAuth provider.
get_access_token(session) -> str | None
Get access token from session.
get_data_token(session) -> str | None
Get data token from session.
is_authenticated(session) -> bool
Check if user is authenticated with SVA OAuth.
clear_oauth_session(session) -> None
Clear all OAuth-related data from session.
Identity Blocks
The package supports all SVA identity blocks:
Core Identity
username- Usernamename- Full namebio- Bio/descriptionpronoun- Pronounsdob- Date of birth
Profile Information
images- Profile imagesskills- Skillshobby- Hobbies
Contact Information
address- Addresssocial- Social links
Verified Identity Blocks
email- Verified emailphone- Verified phonepan_card- PAN cardcrypto_wallet- Crypto walleteducation- Educationemployment- Employmentprofessional_license- Professional licenseaadhar- Aadhaar carddriving_license- Driving licensevoter_id- Voter IDpassport- Passport
Examples
Complete Example
# views.py
from django.shortcuts import render
from sva_oauth_client.decorators import sva_oauth_required
from sva_oauth_client.utils import get_blocks_data, get_userinfo
@sva_oauth_required
def dashboard(request):
blocks_data = get_blocks_data(request.session)
userinfo = get_userinfo(request.session)
context = {
'blocks': blocks_data,
'userinfo': userinfo,
'email': blocks_data.get('email') if blocks_data else None,
'name': blocks_data.get('name') if blocks_data else None,
}
return render(request, 'dashboard.html', context)
Template Example
<!-- dashboard.html -->
<h1>Welcome!</h1>
{% if blocks %}
<h2>Your Identity Blocks</h2>
<ul>
{% for key, value in blocks.items %}
<li><strong>{{ key }}:</strong> {{ value }}</li>
{% endfor %}
</ul>
{% endif %}
{% if userinfo %}
<h2>User Information</h2>
<p>Subject: {{ userinfo.sub }}</p>
<p>Email: {{ userinfo.email }}</p>
{% endif %}
Error Handling
The package provides comprehensive error handling:
from sva_oauth_client.client import SVAOAuthClient, SVATokenError, SVAAuthorizationError
try:
client = SVAOAuthClient(...)
tokens = client.exchange_code_for_tokens(code, code_verifier)
except SVATokenError as e:
# Handle token errors
print(f"Token error: {e}")
except SVAAuthorizationError as e:
# Handle authorization errors
print(f"Authorization error: {e}")
Security Best Practices
- Never expose secrets: Keep
SVA_OAUTH_CLIENT_SECRETandSVA_DATA_TOKEN_SECRETsecure - Use HTTPS in production: Always use HTTPS for OAuth redirects in production
- Validate state parameter: The package automatically validates state for CSRF protection
- Store tokens securely: Tokens are stored in Django session (server-side)
- Handle token expiration: Implement token refresh logic for long-lived sessions
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
Support
For issues and questions:
- GitHub Issues: https://github.com/getsva/sva-oauth-client/issues
- Email: support@getsva.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sva_oauth_client-1.0.0.tar.gz.
File metadata
- Download URL: sva_oauth_client-1.0.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c4ee5cc3b0a4e2e718578a823cbf0e05e113e0ec493175b2b1863dbff9c2866
|
|
| MD5 |
ac1c02e00b5b60cdd4c93b17b21c10d4
|
|
| BLAKE2b-256 |
fe05fd550bc8a0f250522077219ecade018cbc813717358ac782518931e650d6
|
File details
Details for the file sva_oauth_client-1.0.0-py3-none-any.whl.
File metadata
- Download URL: sva_oauth_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
765ff1b343cb5d7518ee4dc1daa9e103ffcad68d68d744aaef5b7cf44f8559bb
|
|
| MD5 |
6b3187ecf5cc5bae3499d012b98a8575
|
|
| BLAKE2b-256 |
14132a6d01ee84b1c7e7e6ea00b16a72b63e732632eac8bcaf2998438d3cb2e1
|