Skip to main content

Authentication UI for DRF Spectacular with AWS Cognito support

Project description

DRF Spectacular Auth

๐Ÿ” Authentication UI for DRF Spectacular with AWS Cognito support

A Django package that adds a beautiful authentication panel to your DRF Spectacular (Swagger UI) documentation, with built-in support for AWS Cognito and extensible authentication providers.

โœจ Features

  • ๐ŸŽจ Beautiful UI: Clean, modern authentication panel that integrates seamlessly with Swagger UI
  • ๐Ÿ” AWS Cognito Support: Built-in integration with AWS Cognito User Pools
  • ๐Ÿ“‹ Token Management: Easy token copying with clipboard integration and manual fallback
  • ๐ŸŽฏ Auto Authorization: Automatically populates Swagger UI authorization headers
  • ๐ŸŽจ Customizable: Flexible theming and positioning options
  • ๐ŸŒ i18n Ready: Multi-language support (Korean, English, Japanese)
  • ๐Ÿ”ง Extensible: Plugin system for additional authentication providers
  • ๐Ÿ“ฆ Easy Integration: Minimal setup with sensible defaults

๐Ÿš€ Quick Start

Installation

pip install drf-spectacular-auth

Basic Setup

  1. Add to your Django settings:
INSTALLED_APPS = [
    'drf_spectacular_auth',  # Add 'drf_spectacular_auth' before 'drf_spectacular'
    'drf_spectacular',
    # ... your other apps
]

DRF_SPECTACULAR_AUTH = {
    'COGNITO_REGION': 'your-aws-region',
    'COGNITO_CLIENT_ID': 'your-cognito-client-id',
    'COGNITO_CLIENT_SECRET': 'your-client-secret',  # Private client์ธ ๊ฒฝ์šฐ์—๋งŒ ํ•„์š”
}
  1. Update your URLs:
from drf_spectacular_auth.views import SpectacularAuthSwaggerView

urlpatterns = [
    path('api/auth/', include('drf_spectacular_auth.urls')),  # Authentication endpoints
    path('api/docs/', SpectacularAuthSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    # ... your other urls
]
  1. That's it! ๐ŸŽ‰ Your Swagger UI now has an authentication panel.

๐Ÿ“ Examples

์™„์ „ํ•œ ์‚ฌ์šฉ๋ฒ• ์˜ˆ์‹œ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด examples/ ํด๋”๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”:

  • basic_usage/ - ๊ธฐ๋ณธ์ ์ธ Django + DRF + AWS Cognito ํ†ตํ•ฉ ์˜ˆ์‹œ
  • cognito_integration/ - AWS Cognito ๊ณ ๊ธ‰ ์„ค์ • ์˜ˆ์‹œ (์ค€๋น„ ์ค‘)
  • custom_theming/ - ์‚ฌ์šฉ์ž ์ •์˜ ํ…Œ๋งˆ ์ ์šฉ ์˜ˆ์‹œ (์ค€๋น„ ์ค‘)
  • hooks_example/ - ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ ํ›… ์‚ฌ์šฉ๋ฒ• ์˜ˆ์‹œ (์ค€๋น„ ์ค‘)

๋น ๋ฅธ ํ…Œ์ŠคํŠธ

cd examples/basic_usage
pip install -r requirements.txt
python manage.py migrate
python manage.py runserver

๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:8000/docs/์— ์ ‘์†ํ•˜์—ฌ ์ธ์ฆ์ด ํ†ตํ•ฉ๋œ Swagger UI๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โš™๏ธ Configuration

AWS Cognito Client Types

Public Client (๊ธฐ๋ณธ):

  • Client Secret์ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ
  • COGNITO_CLIENT_SECRET ์„ค์ • ๋ถˆํ•„์š”
  • ๋Œ€๋ถ€๋ถ„์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉ

Private Client (๋ณด์•ˆ ๊ฐ•ํ™”):

  • Client Secret ํ•„์š”
  • COGNITO_CLIENT_SECRET ์„ค์ • ํ•„์ˆ˜
  • SECRET_HASH ์ž๋™ ๊ณ„์‚ฐ ๋ฐ ์ ์šฉ
# Public Client (๊ธฐ๋ณธ)
DRF_SPECTACULAR_AUTH = {
    'COGNITO_REGION': 'ap-northeast-2',
    'COGNITO_CLIENT_ID': 'your-public-client-id',
}

# Private Client (๋ณด์•ˆ ๊ฐ•ํ™”)
DRF_SPECTACULAR_AUTH = {
    'COGNITO_REGION': 'ap-northeast-2',
    'COGNITO_CLIENT_ID': 'your-private-client-id',
    'COGNITO_CLIENT_SECRET': os.getenv('COGNITO_CLIENT_SECRET'),  # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๊ถŒ์žฅ
}

Full Configuration Options

DRF_SPECTACULAR_AUTH = {
    # AWS Cognito Settings
    'COGNITO_REGION': 'ap-northeast-2',
    'COGNITO_CLIENT_ID': 'your-client-id',
    'COGNITO_CLIENT_SECRET': None,  # Private client์ธ ๊ฒฝ์šฐ์—๋งŒ ์„ค์ • (ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๊ถŒ์žฅ)
    
    # API Endpoints
    'LOGIN_ENDPOINT': '/api/auth/login/',
    'LOGOUT_ENDPOINT': '/api/auth/logout/',
    
    # UI Settings
    'PANEL_POSITION': 'top-right',  # top-left, top-right, bottom-left, bottom-right
    'PANEL_STYLE': 'floating',      # floating, embedded
    'AUTO_AUTHORIZE': True,         # Auto-fill authorization headers
    'SHOW_COPY_BUTTON': True,       # Show token copy button
    'SHOW_USER_INFO': True,         # Show user email in panel
    
    # Theming
    'THEME': {
        'PRIMARY_COLOR': '#61affe',
        'SUCCESS_COLOR': '#28a745',
        'ERROR_COLOR': '#dc3545',
        'BACKGROUND_COLOR': '#ffffff',
        'BORDER_RADIUS': '8px',
        'SHADOW': '0 2px 10px rgba(0,0,0,0.1)',
    },
    
    # Localization
    'DEFAULT_LANGUAGE': 'ko',
    'SUPPORTED_LANGUAGES': ['ko', 'en', 'ja'],
    
    # Security
    'TOKEN_STORAGE': 'localStorage',  # localStorage, sessionStorage
    'CSRF_PROTECTION': True,
    
    # Extensibility
    'CUSTOM_AUTH_PROVIDERS': [],
    'HOOKS': {
        'PRE_LOGIN': None,
        'POST_LOGIN': None,
        'PRE_LOGOUT': None,
        'POST_LOGOUT': None,
    }
}

๐ŸŽจ Customization

Custom Authentication Provider

from drf_spectacular_auth.providers.base import AuthProvider

class CustomAuthProvider(AuthProvider):
    def authenticate(self, credentials):
        # Your custom authentication logic
        return {
            'access_token': 'your-token',
            'user': {'email': 'user@example.com'}
        }
    
    def get_user_info(self, token):
        # Get user information from token
        return {'email': 'user@example.com'}

# Register your provider
DRF_SPECTACULAR_AUTH = {
    'CUSTOM_AUTH_PROVIDERS': [
        'path.to.your.CustomAuthProvider'
    ]
}

Custom Templates

DRF_SPECTACULAR_AUTH = {
    'CUSTOM_TEMPLATES': {
        'auth_panel': 'your_app/custom_auth_panel.html',
        'login_form': 'your_app/custom_login_form.html',
    }
}

๐Ÿ”ง Development

Local Development

git clone https://github.com/yourusername/drf-spectacular-auth.git
cd drf-spectacular-auth
pip install -e ".[dev]"

Running Tests

pytest
pytest --cov=drf_spectacular_auth

Code Quality

black .
isort .
flake8

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“š Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_spectacular_auth-1.0.6.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_spectacular_auth-1.0.6-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file drf_spectacular_auth-1.0.6.tar.gz.

File metadata

  • Download URL: drf_spectacular_auth-1.0.6.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for drf_spectacular_auth-1.0.6.tar.gz
Algorithm Hash digest
SHA256 ed9d36c4264277239bcf0eb3820c7c6a558ebe3969abc46da233557567652e38
MD5 cb44c4a3668c7d8340be83dcce58dce6
BLAKE2b-256 ad3d651f87b813241a3302d9c4b3df42844745db7a718e12031fe0030e2a7005

See more details on using hashes here.

File details

Details for the file drf_spectacular_auth-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_spectacular_auth-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 200ea4016ad3255ff3948d5e8579c28a56eee7ca6a86bba2b492278ced6e7d74
MD5 73555418a2e682eae0869cd417100e3e
BLAKE2b-256 3fab231f64f38d0f0694459be9138d649f6312121a379e4dd69c2ed5afb9393c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page