Django 사용자 인증을 위한 Firebase 통합
Project description
django-firebase-auth
Quantit Django 프로젝트에서 Firebase Authentication을 쉽게 통합할 수 있는 패키지입니다. Client에서 Firebase에게서 발급 받은 token을 바탕으로 사용자를 인증하고, 사용자 정보를 동기화합니다.
특징
- Firebase ID 토큰 검증
- Django 사용자 자동 생성 및 동기화
- Global ID 지원하도록 BaseModel 클레스 제공
Requirements
- Python ^3.12
- Django >=5.1, <6.0
- Firebase Admin SDK >=6.3, <7.0
- GraphQL Core >=3.1, <3.3
설치
GitHub에서 직접 설치:
# pip 사용
pip install git+https://github.com/Quantit-Github/django-firebase-auth.git
# poetry 사용
poetry add git+https://github.com/Quantit-Github/django-firebase-auth.git
사용 방법
firebase admin sdk 설정(필수)
firebase admin sdk 설정이 필수적으로 필요합니다.
# settings.py
import firebase_admin
from firebase_admin import credentials
// ... existing code ...
cred = credentials.Certificate(f"path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
// ... existing code ...
앱 추가하기
앱 설정 파일에 아래 내용을 추가합니다.
# settings.py
// ... existing code ...
INSTALLED_APPS = [
...
"django_firebase_auth",
...
]
// ... existing code ...
middleware 설정
settings.py에 작성한 middleware를 등록합니다.
# settings.py
// ... existing code ...
MIDDLEWARE = [
...
"django_firebase_auth.auth.middleware.AuthenticationMiddleware",
...
]
// ... existing code ...
User 모델 설정
# settings.py
// ... existing code ...
AUTH_USER_MODEL = "django_firebase_auth.User"
// ... existing code ...
django_firebase_auth.User를 상속받은 커스텀 모델 역시 사용 가능합니다.
# models.py
from django_firebase_auth.models import User
class CustomUser(User):
pass
# settings.py
// ... existing code ...
AUTH_USER_MODEL = "your_app.CustomUser"
// ... existing code ...
설정 옵션
아래와 같은 configure을 지원합니다.
# settings.py
// ... existing code ...
GLOBAL_ID_SECRET_KEY = "your_secret_key" # 전역 ID 시크릿 키, 반드시 설정되어야 함(필수)
AUTHORIZATION_HEADER = "Authorization" # token이 포함된 헤더 이름, 기본값은 "Authorization"
AUTHORIZATION_HEADER_PREFIX = "Bearer" # token 앞에 붙는 접두사, 기본값은 "Bearer"
# 쿠키 설정
COOKIE_NAME = "firebase_auth_token" # 쿠키 이름, 기본값은 None. None일 경우 response에 쿠키를 추가하지 않음
COOKIE_MAX_AGE = 3600 # 쿠키 만료 시간, 기본값은 3600초(1시간)
COOKIE_HTTP_ONLY = True # 쿠키가 HTTP 전용인지 여부, 기본값은 True
COOKIE_SECURE = True # 쿠키가 HTTPS 전용인지 여부, 기본값은 True
COOKIE_SAMESITE = "Lax" # 쿠키의 SameSite 속성, 기본값은 "Lax"
# 사용자 자동 생성 여부
AUTO_CREATE_USER = True # 기본값은 False, 사용자 자동 생성 여부
// ... existing code ...
만일 AUTHORIZATION_HEADER를 따로 지정하시면서 CORS 설정이 되어 계시다면, CORS_ALLOW_HEADERS에 아래 값을 추가해주세요.
# settings.py
// ... existing code ...
CORS_ALLOW_HEADERS = [
...
AUTHORIZATION_HEADER,
...
]
// ... existing code ...
사용자 생성 override
AuthenticationMiddleware에서 create_user 메서드를 override하여 사용자 생성 로직을 커스텀할 수 있습니다.
# middleware.py
from django_firebase_auth.auth.middleware import AuthenticationMiddleware
// ... existing code ...
class CustomAuthenticationMiddleware(AuthenticationMiddleware):
def create_user(self, user_record: UserRecord):
...
// ... existing code ...
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 django_firebase_auth_sdk-0.1.3.tar.gz.
File metadata
- Download URL: django_firebase_auth_sdk-0.1.3.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4bda5ba00d9f8ea57552a588743957317c8d8b152ef3dc046542f76067738d5
|
|
| MD5 |
b1932a014c281b167d9475815e2fa29d
|
|
| BLAKE2b-256 |
c0592ee0de756203a5772ba434759009141e95ed0b19e756710ec472fb09d76b
|
File details
Details for the file django_firebase_auth_sdk-0.1.3-py3-none-any.whl.
File metadata
- Download URL: django_firebase_auth_sdk-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3771dffa28510bd737cb6d621d55a4572c915f30d4c635b3e75f7a2f9f866668
|
|
| MD5 |
d0d73a947b2a4c964f980edc0109f2eb
|
|
| BLAKE2b-256 |
bb8aa3ec9944115c5246ff351a022bfb1bd0eca0f34f55be4678f745210aec63
|