-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example callback and related changes
- Loading branch information
Showing
19 changed files
with
444 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ __pycache__/ | |
node_modules/ | ||
sandbox/ | ||
htmlcov/ | ||
.ipynb_checkpoints | ||
|
||
# venv related | ||
bin/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Use a Python image with uv pre-installed | ||
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim | ||
|
||
# Enable bytecode compilation | ||
ENV UV_COMPILE_BYTECODE=1 | ||
|
||
# Copy from the cache instead of linking since it's a mounted volume | ||
ENV UV_LINK_MODE=copy | ||
|
||
# copy project into container | ||
COPY . /code | ||
|
||
# install Python packages | ||
RUN \ | ||
uv venv &&\ | ||
uv pip install --no-cache-dir --upgrade -e /code &&\ | ||
uv pip install --no-cache-dir --upgrade -e /code/examples/edutap_wallet_google_example_callback | ||
|
||
# Place executables in the environment at the front of the path | ||
ENV PATH="/.venv/bin:$PATH" | ||
|
||
# Create a directory for logs | ||
RUN mkdir /logs | ||
ENV EDUTAP_WALLET_GOOGLE_EXAMPLE_CALLBACK_LOG_FILE=/logs/callback.log | ||
|
||
# set working directory | ||
WORKDIR /code/examples/edutap_wallet_google_example_callback | ||
|
||
# run fastapi | ||
CMD ["fastapi", "run", "edutap_wallet_google_example_callback.py", "--host", "0.0.0.0", "--port", "8080", "--proxy-headers"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Example callback service for Google | ||
|
||
This service logs received data to a file and to stdout. | ||
|
||
It logs space separated: | ||
- `class_id` | ||
- `object_id` | ||
- `event_type` | ||
- `exp_time_millis` | ||
- `count` | ||
- `nonce` | ||
|
||
# Configuration environment | ||
|
||
Environment variables are used for configuration. | ||
|
||
- `EDUTAP_WALLET_GOOGLE_EXAMPLE_CALLBACK_LOG_FILE` | ||
|
||
The name and location of the log file. | ||
Default is relative to current working directory: `./callback_log.txt`. | ||
|
||
From `edutap.wallet_google`: | ||
|
||
- `EDUTAP_WALLET_GOOGLE_HANDLER_PREFIX_CALLBACK` | ||
|
||
The path prefix of the callback in the browser. | ||
Default: Empty string (no prefix) | ||
|
||
- `EDUTAP_WALLET_GOOGLE_HANDLER_CALLBACK_VERIFY_SIGNATURE` | ||
|
||
Whether to verify the signature (`1`) in the callback or not (`0`). | ||
Default: `1` | ||
|
||
|
||
## Local usage (dev) | ||
|
||
Installation (execute in this folder) | ||
|
||
```shell | ||
uv venv | ||
uv pip install -r requirements.txt | ||
source .venv/bin/activate | ||
``` | ||
|
||
Run with | ||
```shell | ||
fastapi dev edutap_wallet_google_example_callback.py | ||
``` | ||
|
||
## As Docker container | ||
|
||
### Build image and check container | ||
|
||
In the root of the repository (in `../..` relative to the location of this `README.md`), run: | ||
|
||
```shell | ||
docker buildx build --progress=plain --no-cache -f examples/edutap_wallet_google_example_callback/Dockerfile -t edutap_wallet_google_example_callback . | ||
``` | ||
|
||
Then run the container interactive to verify its working: | ||
```shell | ||
docker run -it edutap_wallet_google_example_callback | ||
``` | ||
Watch out for errors. Stop with Ctrl-c. | ||
|
||
|
||
### Run on a server | ||
|
||
To get actual callbacks from Google the application has to be accessible from the internet and it need to serve on `https` with a valid TLS certificate. | ||
No self-signed certificates are allowed! | ||
|
||
A kind of simple way to get an environment up and running is with Docker Swarm, Traefik Web-Proxy with Lets-Encrypt and our container running in there. | ||
Since this is out of scope of this README we point the dear reader to the tutorial website [Docker Swarm Rocks](https://dockerswarm.rocks/traefik/). | ||
The following examples are meant to run in such a cluster, or to be adapted to different environment. | ||
We hope you get the idea. | ||
|
||
There is an example swarm deployment in here in `swarm.yml`. | ||
It can be deployed on the cluster. | ||
The public domain is configured using the environment variable `EDUTAP_WALLET_GOOGLE_EXAMPLE_DOMAIN`. | ||
A TLS certificate will be issued automatically using Lets Encrypt. | ||
|
||
```shell | ||
docker stack deploy swarm.yml -c swarm.yml edutap_wallet_google_example_callback | ||
``` |
35 changes: 35 additions & 0 deletions
35
examples/edutap_wallet_google_example_callback/edutap_wallet_google_example_callback.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from edutap.wallet_google.handlers.fastapi import router_callback | ||
from fastapi import FastAPI | ||
from fastapi.logger import logger | ||
|
||
import os | ||
import pathlib | ||
|
||
|
||
app = FastAPI() | ||
app.include_router(router_callback) | ||
|
||
|
||
class LoggingCallbackHandler: | ||
""" | ||
Implementation of edutap.wallet_google.protocols.CallbackHandler | ||
""" | ||
|
||
async def handle( | ||
self, | ||
class_id: str, | ||
object_id: str, | ||
event_type: str, | ||
exp_time_millis: int, | ||
count: int, | ||
nonce: str, | ||
) -> None: | ||
pathlib.Path( | ||
os.environ.get( | ||
"EDUTAP_WALLET_GOOGLE_EXAMPLE_CALLBACK_LOG_FILE", "./callback_log.txt" | ||
) | ||
) | ||
line = f'"{class_id}", "{object_id}", "{event_type}", "{exp_time_millis}", "{count}", "{nonce}"\n' | ||
logger.info(line) | ||
with open("callback_log.txt", "a") as file: | ||
file.write(line) |
10 changes: 10 additions & 0 deletions
10
examples/edutap_wallet_google_example_callback/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[project] | ||
name = "edutap_wallet_google_example_callback" | ||
version = "1.0" | ||
dependencies = [ | ||
"edutap.wallet_google[fastapi]", | ||
"fastapi[standard]", | ||
] | ||
|
||
[project.entry-points.'edutap.wallet_google.plugins'] | ||
CallbackHandler = 'edutap_wallet_google_example_callback:LoggingCallbackHandler' |
2 changes: 2 additions & 0 deletions
2
examples/edutap_wallet_google_example_callback/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-e ../.. | ||
-e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
version: '3.7' | ||
|
||
volumes: | ||
logs: | ||
driver_opts: | ||
type: none | ||
device: /data/wallet_google_example_callback | ||
o: bind | ||
|
||
networks: | ||
traefik-public: | ||
external: true | ||
driver: overlay | ||
|
||
services: | ||
callback: | ||
image: 'edutap_wallet_google_example_callback:latest' | ||
volumes: | ||
- logs:/logs | ||
networks: | ||
- traefik-public | ||
environment: | ||
EDUTAP_WALLET_GOOGLE_HANDLER_CALLBACK_VERIFY_SIGNATURE: "0" | ||
deploy: | ||
replicas: 1 | ||
resources: | ||
limits: | ||
cpus: '1' | ||
memory: 128M | ||
labels: | ||
- traefik.enable=true | ||
- traefik.docker.network=traefik-public | ||
- traefik.constraint-label=traefik-public | ||
# SERVICE | ||
- traefik.http.services.wallet_google_example_callback.loadbalancer.server.port=8080 | ||
# DOMAIN TLS | ||
- traefik.http.routers.wallet_google_example_callback-domain.rule=Host(`${EDUTAP_WALLET_GOOGLE_EXAMPLE_DOMAIN?Unset}`) | ||
- traefik.http.routers.wallet_google_example_callback-domain.entrypoints=https | ||
- traefik.http.routers.wallet_google_example_callback-domain.tls=true | ||
- traefik.http.routers.wallet_google_example_callback-domain.tls.certresolver=le | ||
- traefik.http.routers.wallet_google_example_callback-domain.service=wallet_google_example_callback | ||
- traefik.http.routers.wallet_google_example_callback-domain.middlewares=gzip | ||
# DOMAIN insecure | ||
- traefik.http.routers.wallet_google_example_callback-domain-ins.rule=Host(`${EDUTAP_WALLET_GOOGLE_EXAMPLE_DOMAIN?Unset}`) | ||
- traefik.http.routers.wallet_google_example_callback-domain-ins.entrypoints=http | ||
- traefik.http.routers.wallet_google_example_callback-domain-ins.service=wallet_google_example_callback | ||
- traefik.http.routers.wallet_google_example_callback-domain-ins.middlewares=gzip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.