Skip to content

Commit

Permalink
added a lot more backend features and completed a form for adding a s…
Browse files Browse the repository at this point in the history
…ociety
  • Loading branch information
AmeerArsala committed Jun 7, 2024
1 parent 5585a33 commit 319f7d0
Show file tree
Hide file tree
Showing 17 changed files with 804 additions and 62 deletions.
82 changes: 41 additions & 41 deletions app/core/api_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from app.lib.utils import cryptography

import pandas as pd


dotenv.load_dotenv()

Expand Down Expand Up @@ -48,86 +50,84 @@ def get_all_user_api_keys() -> List[str]:

results: List[Dict] = response["result"]

api_keys: List[str] = [result["name"] for result in results]
keys: List[str] = [result["name"] for result in results]

# Filter out the emails from the keys
keys_series = pd.Series(keys)

# Filter the series to exclude strings containing '@'
api_keys: List[str] = keys_series[~keys_series.str.contains("@")].tolist()

return api_keys


def read_api_key_from_email(email: str):
# The KV is str -> str
def _read_key(key_name: str) -> str:
account_id: str = CLOUDFLARE_ACCOUNT_ID
namespace_id: str = CLOUDFLARE_KV_NAMESPACE_ID
key_name: str = email

headers = {"Authorization": "Bearer undefined", "Content-Type": "application/json"}

# Get response from api
response = requests.get(
response: str = requests.get(
f"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/values/{key_name}",
headers=headers,
)

# if isinstance(response, dict) and response["success"] is False:
# return None
return response

api_key: str = response

def read_api_key_from_email(email: str):
api_key: str = _read_key(email)

return api_key


def read_email_from_api_key(api_key: str):
account_id: str = CLOUDFLARE_ACCOUNT_ID
namespace_id: str = CLOUDFLARE_KV_NAMESPACE_ID
key_name: str = api_key

headers = {"Authorization": "Bearer undefined", "Content-Type": "application/json"}

# Get response from api
response = requests.get(
f"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/values/{key_name}",
headers=headers,
)

# if isinstance(response, dict) and response["success"] is False:
# return None

user_email: str = response
user_email: str = _read_key(api_key)

return user_email


# user_id is hopefully a string
def create_api_key(user_email: str) -> str:
def create_api_key(user_email: str, expiration_ttl: int = -1) -> str:
# Generate it first
api_key: str = cryptography.generate_api_key()

# Put it in the KV DB
account_id: str = CLOUDFLARE_ACCOUNT_ID
namespace_id: str = CLOUDFLARE_KV_NAMESPACE_ID

headers = {"Authorization": "Bearer undefined", "Content-Type": "application/json"}
def make_url(key_name: str) -> str:
return f"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/values/{key_name}"

# Forward mapping: [email -> api_key]
query_params = {"base64": False, "key": user_email, "value": api_key}
headers = {
"Content-Type": "multipart/form-data",
"Authorization": "Bearer undefined",
}

response = requests.put(
f"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/bulk",
headers=headers,
params=query_params,
)
def make_request(key: str, val, expiration_ttl: int = -1):
url: str = make_url(key_name=key)

body = {"metadata": {}, "value": val}

if expiration_ttl != -1:
body["expiration_ttl"] = expiration_ttl

response = requests.request("PUT", url, headers=headers, json=body)
return response

# Forward mapping: [user_email -> api_key]
(K, V) = (user_email, api_key)
response = make_request(K, V, expiration_ttl=expiration_ttl)

# Print results of call
print("Forward Mapping:")
print(response.status_code)
print(response.text)

# Reverse Mapping: [api_key -> email]
query_params = {"base64": False, "key": api_key, "value": user_email}

response = requests.put(
f"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/bulk",
headers=headers,
params=query_params,
)
# Reverse Mapping: [api_key -> user_email]
(K, V) = (api_key, user_email)
response = make_request(K, V, expiration_ttl=expiration_ttl)

# Print results of call
print("Reverse Mapping:")
Expand Down
21 changes: 20 additions & 1 deletion app/core/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,29 @@ async def create_api_key(request: Request) -> str:
user_details: UserDetails = UserDetails(**user_client.get_user_details())

# user_auth_id: str = user_details.id
expiration_ttl = request.query_params.get("expiration_ttl_seconds")

# 'int'ify it
if expiration_ttl is None:
expiration_ttl = -1
else:
expiration_ttl = int(expiration_ttl)

# Make api key and store it
api_key: str = api_auth.create_api_key(user_details.email)
api_key: str = api_auth.create_api_key(
user_details.email, expiration_ttl=expiration_ttl
)

return api_key


# using post for the additional security
@router.post("/get_apikey")
async def get_api_key(request: Request) -> str:
user_client = user_kinde_client(request.url)
user_details: UserDetails = UserDetails(**user_client.get_user_details())

api_key: str = api_auth.read_api_key_from_email(user_details.email)
return api_key


Expand Down
2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
"typescript": "^5.4.5"
},
"devDependencies": {
"@melt-ui/pp": "^0.3.2",
"@melt-ui/svelte": "^0.81.0",
"flowbite": "^2.3.0",
"flowbite-svelte": "^0.46.1",
"svelte-headless-table": "^0.18.2"
Expand Down
38 changes: 32 additions & 6 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import CreateCommunityForm from "./CreateCommunityForm.svelte";
import * as Dialog from "$lib/components/ui/dialog/index.js";
import { Button } from "$lib/components/ui/button/index.js";
</script>

<Dialog.Content>
<CreateCommunityForm childIsCloseButton={true}>
<Dialog.Close>
<Button variant="outline">Cancel</Button>
</Dialog.Close>
</CreateCommunityForm>
</Dialog.Content>
Loading

0 comments on commit 319f7d0

Please sign in to comment.