Installation
This guide covers installing and setting up the sva-oauth-client package for Django.
Requirements
- Python: 3.8 or higher
- Django: 3.2 or higher
- Django Sessions: Required for token storage
Installation Methods
Install from PyPI (Recommended)
pip install sva-oauth-client
Install from Source
git clone https://github.com/getsva/sva-oauth-client.git
cd sva-oauth-client
pip install -e .
Install Specific Version
pip install sva-oauth-client==1.0.0
Django Setup
1. Add to INSTALLED_APPS
Add sva_oauth_client to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions', # Required!
'django.contrib.messages',
'django.contrib.staticfiles',
# ... your other apps
'sva_oauth_client',
]
Important: django.contrib.sessions must be in INSTALLED_APPS for session management.
2. Configure Session Backend
Ensure sessions are configured in your settings.py:
# settings.py
# Session configuration (usually already configured)
SESSION_ENGINE = 'django.contrib.sessions.backends.db' # or 'cache' or 'cached_db'
SESSION_COOKIE_HTTPONLY = True # Recommended for security
SESSION_COOKIE_SECURE = True # Use True in production with HTTPS
SESSION_COOKIE_SAMESITE = 'Lax' # CSRF protection
3. Run Migrations
If you're using database sessions, run migrations:
python manage.py migrate
4. Add Middleware
Add the token refresh middleware to your MIDDLEWARE:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', # Must be before TokenRefreshMiddleware
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# ... your other middleware
'sva_oauth_client.middleware.TokenRefreshMiddleware', # Add this
]
Critical: TokenRefreshMiddleware must come after SessionMiddleware.
5. Add URLs
Include the OAuth URLs in your main urls.py:
# urls.py
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# ... your other URLs
path('oauth/', include('sva_oauth_client.urls')),
]
Verify Installation
Create a simple test view to verify the installation:
# views.py
from django.http import JsonResponse
from sva_oauth_client.utils import is_authenticated
def test_view(request):
return JsonResponse({
'installed': True,
'authenticated': is_authenticated(request.session),
})
Add to your URLs:
urlpatterns = [
# ...
path('test/', test_view, name='test'),
]
Visit http://localhost:8000/test/ - you should see:
{
"installed": true,
"authenticated": false
}
Next Steps
After installation, proceed to Configuration to set up your OAuth credentials.