Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit 8f915e6

Browse files
committed
Add endpoint for generating JWTs
These tokens are provided to flat-manager to authorize the user to download an app. In the future, this enpoint should first check whether the user has paid for the app if necessary.
1 parent 19e32f0 commit 8f915e6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

app/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Settings(BaseSettings):
3636
google_client_secret: str = "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"
3737
google_return_url: str = "http://localhost:3000/login/google"
3838
cors_origins: str = "http://localhost:3000"
39+
flat_manager_secret: str = "c2VjcmV0"
3940

4041

4142
settings = Settings()

app/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import base64
2+
from datetime import datetime, timedelta
13
from functools import lru_cache
4+
from typing import List
25

6+
import jwt
37
import sentry_sdk
48
from fastapi import FastAPI, Response
59
from fastapi.middleware.cors import CORSMiddleware
@@ -199,6 +203,27 @@ def get_summary(appid: str, response: Response):
199203
return None
200204

201205

206+
@app.post("/generate-download-token", status_code=200)
207+
def get_download_token(appids: List[str]):
208+
"""Generates a download token for the given app IDs."""
209+
210+
# TODO: Check the user has rights to download the given app IDs!
211+
212+
encoded = jwt.encode(
213+
{
214+
"sub": "download",
215+
"exp": datetime.utcnow() + timedelta(hours=24),
216+
"prefixes": appids,
217+
},
218+
base64.b64decode(config.settings.flat_manager_secret),
219+
algorithm="HS256",
220+
)
221+
222+
return {
223+
"token": encoded,
224+
}
225+
226+
202227
def sort_ids_by_downloads(ids):
203228
if len(ids) <= 1:
204229
return ids

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ itsdangerous = "^2.1"
2323
PyGithub = "^1.55"
2424
vcrpy = "^4.1.1"
2525
python-gitlab = "^3.1"
26+
PyJWT = "^2.3.0"
2627

2728
[tool.poetry.dev-dependencies]
2829
black = "^22.1"

0 commit comments

Comments
 (0)