Skip to content

Commit 4ae8980

Browse files
committed
doc: readme in example
1 parent e14b485 commit 4ae8980

File tree

11 files changed

+965
-2263
lines changed

11 files changed

+965
-2263
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
uses: actions/setup-python@v1
1818
with:
1919
python-version: ${{ matrix.python-version }}
20-
20+
2121
- name: Install Task
2222
uses: arduino/setup-task@v1
2323

@@ -38,9 +38,6 @@ jobs:
3838
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
3939
run: poetry install --no-interaction --no-root
4040

41-
- name: Install library
42-
run: poetry install --no-interaction
43-
4441
- name: Run CI
4542
run: task ci
4643

example/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# syntax=docker/dockerfile:experimental
2+
FROM python:3.8.8
3+
4+
RUN apt-get update \
5+
&& apt-get install -y \
6+
gcc \
7+
ffmpeg \
8+
libsm6 \
9+
libxext6 \
10+
git \
11+
curl
12+
13+
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
14+
15+
WORKDIR /app
16+
COPY pyproject.toml .
17+
COPY poetry.lock .
18+
19+
RUN ~/.poetry/bin/poetry install
20+
21+
COPY app app
22+
23+
EXPOSE 8000
24+
25+
CMD ["/root/.poetry/bin/poetry", "run", "python", "-m", "app.main", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]

example/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example with keycloak
2+
3+
## Setup your realm
4+
5+
1. Start up keycloak with `docker-compose up` (the fastapi app will crash since
6+
we do not have a realm yet).
7+
2. Log into keycloak at http://localhost:8080 with username/password `admin/admin`.
8+
3. Create a realm `my-realm`. This will set your `openid_connect_url` to `http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration`
9+
and your issuer to `http://localhost:8080/auth/realms/my-realm`.
10+
4. Allow implicit flow (in order for login in interactive docs to work).
11+
5. Create a user and add credentials (password).
12+
13+
## Login into docs with your credentials
14+
15+
1. Kill app and then restart with `docker-compose up`.
16+
2. Go to `http://localhost:8000/docs` and login with your credentials by
17+
clicking `authorize` in the top right corner.

example/app/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pydantic import BaseSettings
2+
from pydantic import Field
3+
4+
5+
class Config(BaseSettings):
6+
openid_connect_url: str = Field(..., env="AUTH_OPENID_CONNECT_URL")
7+
issuer: str = Field(..., env="AUTH_ISSUER")
8+
client_id: str = Field(..., env="AUTH_CLIENT_ID")
9+
10+
11+
config = Config()

example/app/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
import uvicorn
4+
from app.config import config
45
from fastapi import Depends
56
from fastapi import FastAPI
67
from fastapi import Security
@@ -12,9 +13,9 @@
1213
from fastapi_third_party_auth import KeycloakIDToken
1314

1415
auth = Auth(
15-
openid_connect_url="http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration",
16-
issuer="http://localhost:8080/auth/realms/my-realm", # optional, verification only
17-
client_id="my-client", # optional, verification only
16+
openid_connect_url=config.openid_connect_url,
17+
issuer=config.issuer, # optional, verification only
18+
client_id=config.client_id, # optional, verification only
1819
scopes=["email"], # optional, verification only
1920
idtoken_model=KeycloakIDToken, # optional, verification only
2021
)

example/docker-compose.yml

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
version: '3'
1+
version: "3"
22

33
services:
4-
5-
# test-fastapi-keycloak:
6-
# build:
7-
# context: .
8-
# dockerfile: Dockerfile
9-
# restart: always
10-
# depends_on:
11-
# - keycloak
12-
# # keycloak:
13-
# # condition: service_healthy
14-
# network_mode: host
4+
test-fastapi-keycloak:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
depends_on:
9+
keycloak:
10+
condition: service_healthy
11+
network_mode: host
1512

1613
keycloak:
1714
image: jboss/keycloak:15.0.2
@@ -26,16 +23,20 @@ services:
2623
- DB_PASSWORD=password
2724
- KEYCLOAK_USER=admin
2825
- KEYCLOAK_PASSWORD=admin
29-
- KEYCLOAK_IMPORT=/tmp/my-realm-export.json
3026
ports:
3127
- 8080:8080
3228
depends_on:
3329
- keycloak-postgres
34-
# healthcheck:
35-
# test: ["CMD", "curl", "-f", "http://keycloak:8080"]
36-
# interval: 10s
37-
# timeout: 10s
38-
# retries: 2
30+
environment:
31+
- AUTH_OPENID_CONNECT_URL=http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration
32+
- AUTH_ISSUER=http://localhost:8080/auth/realms/my-realm
33+
- AUTH_CLIENT_ID=my-client
34+
healthcheck:
35+
test: "curl http://localhost:8080"
36+
interval: 30s
37+
timeout: 30s
38+
retries: 3
39+
start_period: 2m
3940

4041
keycloak-postgres:
4142
image: postgres:13.4-alpine3.14

0 commit comments

Comments
 (0)