initial commit
This commit is contained in:
commit
f4abef1ed6
3
.env.example
Normal file
3
.env.example
Normal file
@ -0,0 +1,3 @@
|
||||
MONZO_CLIENT_ID=oauth2client_xxx
|
||||
MONZO_CLIENT_SECRET=mnzconf.base64-string
|
||||
MONZO_CALLBACK_URI=http://localhost:8000/callback
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.env
|
||||
.env.public
|
||||
.env.private
|
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@ -0,0 +1,14 @@
|
||||
FROM ghcr.io/astral-sh/uv:alpine
|
||||
|
||||
RUN uv venv /code/.venv
|
||||
WORKDIR /code
|
||||
|
||||
COPY ./requirements.txt /code/requirements.txt
|
||||
|
||||
RUN uv pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
||||
|
||||
COPY ./app /code/app
|
||||
|
||||
# If running behind a proxy like Nginx or Traefik add --proxy-headers
|
||||
# CMD ["uv", "run", "fastapi", "run", "app/main.py", "--port", "80", "--proxy-headers"]
|
||||
CMD ["uv", "run", "fastapi", "run", "app/main.py", "--port", "80"]
|
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
50
app/main.py
Normal file
50
app/main.py
Normal file
@ -0,0 +1,50 @@
|
||||
from collections import OrderedDict
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import RedirectResponse
|
||||
from uuid import uuid4
|
||||
from urllib.parse import urlencode
|
||||
from typing import Union
|
||||
import requests
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
client_id = os.environ['MONZO_CLIENT_ID']
|
||||
client_secret = os.environ['MONZO_CLIENT_SECRET']
|
||||
callback_uri = os.environ['MONZO_CALLBACK_URI']
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/")
|
||||
def read_root(): return {"version": "v0.0.4"}
|
||||
|
||||
|
||||
@app.get("/redirect", response_class=RedirectResponse)
|
||||
def read_redirect():
|
||||
state = uuid4()
|
||||
# TODO: store state in a cookie to check it later
|
||||
query = urlencode(OrderedDict(
|
||||
client_id=client_id,
|
||||
redirect_uri=callback_uri,
|
||||
state=state,
|
||||
response_type="code",
|
||||
))
|
||||
return f"https://auth.monzo.com/?{query}"
|
||||
|
||||
|
||||
@app.get("/callback")
|
||||
def read_callback(code: str, state: str):
|
||||
# TODO: check the state with the user's cookie
|
||||
data = {
|
||||
'grant_type': 'authorization_code',
|
||||
'client_id': client_id,
|
||||
'client_secret': client_secret,
|
||||
'redirect_uri': callback_uri,
|
||||
'code': code,
|
||||
}
|
||||
response = requests.post("https://api.monzo.com/oauth2/token", data=data)
|
||||
return response.json()
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
def read_item(item_id: int, q: Union[str, None] = None):
|
||||
return {"item_id": item_id, "q": q}
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
fastapi[standard]>=0.113.0,<0.114.0
|
||||
pydantic>=2.7.0,<3.0.0
|
||||
requests>=2.32.3
|
Loading…
x
Reference in New Issue
Block a user