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

Commit

Permalink
Add endpoint for generating JWTs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jameswestman committed Mar 11, 2022
1 parent dedbdad commit e7422cf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Settings(BaseSettings):
google_client_secret: str = "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"
google_return_url: str = "http://localhost:3000/login/google"
cors_origins: str = "http://localhost:3000"
flat_manager_secret: str = "c2VjcmV0"


settings = Settings()
25 changes: 25 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import base64
from datetime import datetime, timedelta
from functools import lru_cache
from typing import List

import jwt
import sentry_sdk
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -212,6 +216,27 @@ def get_website_verification(appid: str):
return verification.get_website_verification(appid)


@app.post("/generate-download-token", status_code=200)
def get_download_token(appids: List[str]):
"""Generates a download token for the given app IDs."""

# TODO: Check the user has rights to download the given app IDs!

encoded = jwt.encode(
{
"sub": "download",
"exp": datetime.utcnow() + timedelta(hours=24),
"prefixes": appids,
},
base64.b64decode(config.settings.flat_manager_secret),
algorithm="HS256",
)

return {
"token": encoded,
}


def sort_ids_by_downloads(ids):
if len(ids) <= 1:
return ids
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ itsdangerous = "^2.1"
PyGithub = "^1.55"
vcrpy = "^4.1.1"
python-gitlab = "^3.1"
PyJWT = "^2.3.0"

[tool.poetry.dev-dependencies]
black = "^22.1"
Expand Down

0 comments on commit e7422cf

Please sign in to comment.