Skip to content

Commit 3ade83d

Browse files
committed
Initial project setup
0 parents  commit 3ade83d

File tree

11 files changed

+224
-0
lines changed

11 files changed

+224
-0
lines changed

.dockerignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
venv/
2+
.flake8
3+
.mypy_cache
4+
.envs
5+
6+
.idea
7+
*.lock
8+
.DS_Store
9+
**/__pycache__/
10+
.envs/
11+
celery-beat-schedule
12+
celerybeat-schedule
13+
*.pid

.env.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
KEYCLOAK_SERVER_URL=https://sample.com
2+
KEYCLOAK_CLIENT_ID=sample
3+
KEYCLOAK_REALM_NAME=sample
4+
KEYCLOAK_CLIENT_SECRET=sample
5+
KEYCLOAK_ADMIN_CLIENT_SECRET=sample
6+
KEYCLOAK_CALLBACK_URI=http://sample.com/callback

.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
*.pyc
2+
*.pyo
3+
*.pyd
4+
5+
# C extensions
6+
*.so
7+
8+
# Packages
9+
__pycache__
10+
*.py[cod]
11+
*$py.class
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
.venv/
30+
venv/
31+
.env
32+
33+
# Python debug
34+
pdb/
35+
.ipynb_checkpoints
36+
37+
# pyenv
38+
.python-version
39+
40+
# celery
41+
celerybeat-schedule*
42+
celerybeat.pid
43+
44+
# Flask stuff
45+
instance/
46+
.webassets-cache
47+
48+
# Intellij IDEA specifics
49+
.idea/
50+
.fleet/
51+
52+
# Qt related
53+
*.log
54+
*.log.*
55+
*.swo
56+
*.swp
57+
.DS_Store
58+
.AppleDouble
59+
.LSOverride
60+
+47

.pre-commit-config.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
exclude: 'docs|node_modules|migrations|.git|.tox|.mypy_cache|frontend'
2+
default_stages: [commit]
3+
fail_fast: true
4+
5+
repos:
6+
- repo: https://github.com/pre-commit/pre-commit-hooks
7+
rev: v4.5.0
8+
hooks:
9+
- id: trailing-whitespace
10+
- id: end-of-file-fixer
11+
- id: check-yaml
12+
- id: requirements-txt-fixer
13+
- id: check-merge-conflict
14+
- repo: https://github.com/myint/autoflake
15+
rev: v1.4
16+
hooks:
17+
- id: autoflake
18+
args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variables', '--ignore-init-module-imports']
19+
- repo: https://github.com/psf/black
20+
rev: 23.11.0
21+
hooks:
22+
- id: black
23+
- repo: https://github.com/PyCQA/bandit
24+
rev: '1.7.5'
25+
hooks:
26+
- id: bandit

app/__init__.py

Whitespace-only changes.

app/api/endpoints.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastapi import APIRouter, Depends
2+
from fastapi_keycloak import OIDCUser
3+
4+
from app.keycloak import idp
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/")
10+
def home():
11+
"""Public home page."""
12+
return {"message": "Hello World! This is a public endpoint"}
13+
14+
15+
@router.get("/me/")
16+
def my_user(user: OIDCUser = Depends(idp.get_current_user())):
17+
"""Shows the logged-in user's info. No specified role required."""
18+
return user
19+
20+
21+
@router.get("/admin/")
22+
def my_admin(user: OIDCUser = Depends(idp.get_current_user(required_roles=["admin"]))):
23+
"""Endpoint specific for admins."""
24+
return {"message": "Hello admin! This is an admin-only endpoint"}

app/api/routers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from fastapi import APIRouter
2+
3+
from app import api
4+
5+
api_router_v1 = APIRouter()
6+
api_router_v1.include_router(api.router)

app/config/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pydantic import BaseModel
2+
from pydantic_settings import BaseSettings, SettingsConfigDict
3+
4+
5+
class KeycloakSettings(BaseModel):
6+
server_url: str
7+
client_id: str
8+
client_secret: str
9+
realm_name: str
10+
admin_client_secret: str
11+
callback_uri: str
12+
13+
14+
class Settings(BaseSettings):
15+
model_config = SettingsConfigDict(env_file=".env", extra='ignore')
16+
17+
keycloak_server_url: str
18+
keycloak_client_id: str
19+
keycloak_client_secret: str
20+
keycloak_realm_name: str
21+
keycloak_admin_client_secret: str
22+
keycloak_callback_uri: str
23+
24+
25+
settings = Settings()

app/config/keycloak.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi_keycloak import FastAPIKeycloak
2+
3+
from app.config.base import settings
4+
5+
idp = FastAPIKeycloak(
6+
server_url=settings.keycloak_server_url,
7+
client_id=settings.keycloak_client_id,
8+
client_secret=settings.keycloak_client_secret,
9+
admin_client_secret=settings.keycloak_admin_client_secret,
10+
realm=settings.keycloak_realm_name,
11+
callback_uri=settings.keycloak_callback_uri
12+
)

app/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from fastapi import FastAPI
2+
3+
from app.api.routers import api_router_v1
4+
5+
app = FastAPI()
6+
app.swagger_ui_init_oauth = {
7+
"usePkceWithAuthorizationCodeGrant": True,
8+
}
9+
10+
app.include_router(api_router_v1)

0 commit comments

Comments
 (0)