Swaguard
Swaguard는 Django와 FastAPI 애플리케이션에서 Swagger/OpenAPI UI를 보호하기 위한 Python 라이브러리입니다. 인증된 사용자만 API 문서에 접근할 수 있도록 합니다.
특징
- Swagger UI(/docs), ReDoc(/redoc) 및 OpenAPI 스키마(/openapi.json) 엔드포인트 보호
- 간단한 HTTP-only 쿠키 기반 인증
- 설정 가능한 쿠키 만료 시간 (기본 60분)
- 환경 변수 또는 YAML 파일을 통한 설정
- FastAPI 애플리케이션 지원 (현재 구현)
- Django 애플리케이션 지원 (향후 구현 예정)
설치
pip install swaguard
또는 소스에서 직접 설치:
git clone https://github.com/riseryan89/swaguard.git
cd swaguard
pip install -e .
사용 방법
FastAPI에서 사용하기
- 미들웨어를 사용하여 모든 Swagger 관련 경로 보호:
from fastapi import FastAPI
from swaguard import SwaguardMiddleware, create_login_router, create_user
# 사용자 생성
create_user("admin", "password")
app = FastAPI()
# Swaguard 미들웨어 등록
app.add_middleware(SwaguardMiddleware)
# 로그인 라우터 등록
login_router = create_login_router()
app.include_router(login_router)
- 종속성 주입을 사용하여 특정 엔드포인트 보호:
from fastapi import FastAPI, Depends
from swaguard import verify_swagger_cookie
app = FastAPI()
@app.get("/api/protected")
async def protected_endpoint(username: str = Depends(verify_swagger_cookie)):
return {"message": f"Hello, {username}!"}
설정
Swaguard는 다음과 같은 방법으로 설정할 수 있습니다:
환경 변수
# 쿠키 설정
export SWAGUARD_COOKIE_NAME="swaguard_auth"
export SWAGUARD_COOKIE_EXPIRE_MINUTES="60"
export SWAGUARD_COOKIE_SECURE="true"
export SWAGUARD_COOKIE_HTTPONLY="true"
export SWAGUARD_COOKIE_SAMESITE="lax"
# 경로 설정
export SWAGUARD_LOGIN_PATH="/swaguard/login"
export SWAGUARD_LOGOUT_PATH="/swaguard/logout"
# 사용자 설정 (개발 환경에서만 사용 권장)
export SWAGUARD_USERS="admin:password,user:userpass"
# 비밀 키 설정
export SWAGUARD_SECRET_KEY="your-secret-key"
YAML 설정 파일
# swaguard_config.yaml
cookie_name: swaguard_auth
cookie_expire_minutes: 60
cookie_secure: true
cookie_httponly: true
cookie_samesite: lax
login_path: /swaguard/login
logout_path: /swaguard/logout
protected_paths:
- /docs
- /redoc
- /openapi.json
users:
admin: $2b$12$... # 해시된 비밀번호
예제
예제 폴더에 샘플 애플리케이션이 포함되어 있습니다:
examples/fastapi_example.py: FastAPI를 사용한 예제
예제 실행:
cd examples
python fastapi_example.py
그런 다음 브라우저에서 http://localhost:/docs 에 접속하면 로그인 페이지로 리다이렉트됩니다.
라이선스
MIT
컨트리뷰션
Pull Request 및 이슈 제출을 환영합니다. 주요 변경 사항에 대해서는 먼저 이슈를 통해 논의해 주세요.
Metadata
Release files for swaguard 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| swaguard-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for swaguard-0.1.0-py3-none-any.whl |
Release files / swaguard-0.1.0-py3-none-any.whl
| Download URL | swaguard-0.1.0-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b037ca85332050d434c5050cc8597fbccd6ca4ebc9910b20be25994cbea6cdd
|
|
BLAKE2b-256 checksum How to use checksums |
4e7fd2c9b6ab19624994a75cbfc6b3674d603455dc1245a736b3eec326952643
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for swaguard 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.