From e17c1760d44d888f3699fa92b039c0516a6d33df Mon Sep 17 00:00:00 2001 From: John Date: Fri, 15 Sep 2023 13:12:06 -0400 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ api.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ generate-key.sh | 5 +++++ start.sh | 3 +++ 4 files changed, 64 insertions(+) create mode 100644 .gitignore create mode 100644 api.py create mode 100644 generate-key.sh create mode 100644 start.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff32762 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +keys.yaml +badges.yaml \ No newline at end of file diff --git a/api.py b/api.py new file mode 100644 index 0000000..7b39736 --- /dev/null +++ b/api.py @@ -0,0 +1,54 @@ +from fastapi import FastAPI, HTTPException, Security, status +from fastapi.security import APIKeyHeader +from fastapi.responses import RedirectResponse +import yaml + +app = FastAPI() + +api_key_header = APIKeyHeader(name="X-API-Key") + +with open("keys.yaml") as f: + api_keys = yaml.safe_load(f) + +try: + with open("badges.yaml") as f: + badge_dict = yaml.safe_load(f) +except FileNotFoundError: + badge_dict = dict() + + +def get_api_key(api_key_header: str = Security(api_key_header)) -> str: + if api_key_header in api_keys: + return api_key_header + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid or missing API Key", + ) + + +@app.get("/") +async def root(): + return + + +@app.get("/set_badge/") +async def set_badge(repo: str, new_badge: str, api_key: str = Security(get_api_key)): + if api_key.rsplit("-", 1)[0] != repo: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="No permissions for repo", + ) + + badge_dict[repo] = new_badge + with open("badges.yaml", "w") as f: + yaml.safe_dump(badge_dict, f) + + +@app.get("/get_badge/") +async def get_badge(repo: str): + if repo not in badge_dict.keys(): + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Repo not found", + ) + return RedirectResponse("https://img.shields.io/badge/" + badge_dict[repo]) diff --git a/generate-key.sh b/generate-key.sh new file mode 100644 index 0000000..2defff5 --- /dev/null +++ b/generate-key.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "Enter repo: " +read -r +echo "- $REPLY-$(openssl rand -hex 20)" >> keys.yaml \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..4d7e574 --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +uvicorn api:app --reload \ No newline at end of file