Quick Start
Get up and running with SVA OAuth in 5 minutes! This guide will walk you through integrating SVA OAuth into your Django application.
Prerequisites
- Python 3.8+
- Django 3.2+
- An SVA OAuth application (get credentials from your SVA provider)
Installation
Install the sva-oauth-client package:
pip install sva-oauth-client
Step 1: Add to INSTALLED_APPS
Add sva_oauth_client to your Django INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
# ... other apps
'django.contrib.sessions', # Required for session management
'sva_oauth_client',
]
Step 2: Add Middleware
Add the token refresh middleware to your MIDDLEWARE list:
# settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', # Must come before TokenRefreshMiddleware
# ... other middleware
'sva_oauth_client.middleware.TokenRefreshMiddleware',
]
Important: The TokenRefreshMiddleware must come after SessionMiddleware since it requires access to the session.
Step 3: Configure Settings
Add the required OAuth settings to your settings.py:
# settings.py
# Required Settings
SVA_OAUTH_BASE_URL = 'https://auth.getsva.com' # 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 = 'https://yourapp.com/oauth/callback/'
SVA_DATA_TOKEN_SECRET = 'your_data_token_secret' # Must match your SVA provider
# Optional Settings
SVA_OAUTH_SCOPES = 'openid email profile' # Default scopes
SVA_OAUTH_SUCCESS_REDIRECT = '/' # Redirect after successful login
SVA_OAUTH_ERROR_REDIRECT = '/' # Redirect on error
Step 4: Add URLs
Include the OAuth URLs in your main urls.py:
# urls.py
from django.urls import path, include
urlpatterns = [
# ... your other URLs
path('oauth/', include('sva_oauth_client.urls')),
]
This automatically provides:
/oauth/login/- Initiate OAuth flow/oauth/callback/- OAuth callback handler/oauth/logout/- Logout endpoint
Step 5: Create a Protected View
Create a view that uses the OAuth decorator:
# views.py
from django.shortcuts import render
from sva_oauth_client.decorators import sva_oauth_required
from sva_oauth_client.utils import get_sva_claims
@sva_oauth_required
def dashboard(request):
# Get user claims directly from the signed data_token (stateless!)
claims = get_sva_claims(request)
context = {
'email': claims.get('email'),
'name': claims.get('name'),
'all_claims': claims,
}
return render(request, 'dashboard.html', context)
Step 5: Add Login Link
Add a login link to your template:
<!-- templates/base.html -->
{% if not request.session.sva_oauth_access_token %}
<a href="{% url 'sva_oauth_client:login' %}">Sign In with SVA</a>
{% else %}
<a href="{% url 'sva_oauth_client:logout' %}">Logout</a>
{% endif %}
That's It! 🎉
Your Django app now has secure OAuth authentication with SVA! Here's what happens:
- User clicks "Sign In with SVA"
- User is redirected to SVA consent screen
- User approves requested scopes
- User is redirected back with tokens
- Your app can access user data from the signed token
Testing
-
Start your Django development server:
python manage.py runserver -
Visit your login URL (e.g.,
http://localhost:8000/oauth/login/) -
Complete the OAuth flow
-
Access your protected view to see user data
Next Steps
- Learn about Identity Blocks to request specific user data
- Explore Advanced Usage for more complex scenarios
- Read the API Reference for complete documentation
- Check out Security Best Practices for production deployment
Troubleshooting
If you encounter issues:
- ImportError: Make sure
sva-oauth-clientis installed:pip install sva-oauth-client - Connection Error: Verify
SVA_OAUTH_BASE_URLis correct - Token Error: Ensure
SVA_DATA_TOKEN_SECRETmatches your provider - Redirect Error: Check that
SVA_OAUTH_REDIRECT_URImatches your OAuth app configuration
See the Troubleshooting Guide for more help.