initial commit
This commit is contained in:
3
server/app/admin/__init__.py
Normal file
3
server/app/admin/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from fastapi.security import HTTPBearer, OAuth2PasswordBearer
|
||||
|
||||
oauth2_http = HTTPBearer()
|
||||
17
server/app/admin/contact.py
Normal file
17
server/app/admin/contact.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
from os import getenv
|
||||
|
||||
HOST = "grapefruitswebsite@gmail.com"
|
||||
|
||||
|
||||
def send_email(subject: str, body: str) -> None:
|
||||
password = getenv("APP_PASSWORD")
|
||||
email = getenv("EMAIL")
|
||||
msg = MIMEText(body)
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = HOST
|
||||
smtp_server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
|
||||
smtp_server.login(HOST, password) # type: ignore
|
||||
smtp_server.sendmail(HOST, [email], msg.as_string()) # type: ignore
|
||||
smtp_server.quit()
|
||||
48
server/app/admin/images.py
Normal file
48
server/app/admin/images.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# Set your Cloudinary credentials
|
||||
# ==============================
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# Import the Cloudinary libraries
|
||||
# ==============================
|
||||
import cloudinary
|
||||
import cloudinary.api
|
||||
import cloudinary.uploader
|
||||
|
||||
# Set configuration parameter: return "https" URLs by setting secure=True
|
||||
# ==============================
|
||||
cloudinary.config(secure=True)
|
||||
|
||||
uploader = cloudinary.uploader
|
||||
|
||||
|
||||
class CloudinaryException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def delete_image(public_id: str) -> None:
|
||||
result = uploader.destroy(public_id)
|
||||
|
||||
if result.get("result") != "ok":
|
||||
raise CloudinaryException("Failed to delete image")
|
||||
|
||||
|
||||
def get_image_data(public_id: str) -> dict:
|
||||
data = cloudinary.api.resource(public_id)
|
||||
return data
|
||||
|
||||
|
||||
def get_image_url(public_id: str) -> str:
|
||||
url = cloudinary.utils.cloudinary_url(public_id)[0]
|
||||
if url is None:
|
||||
raise CloudinaryException("Failed to get image URL")
|
||||
return url
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
image_id = "coco_copy_jywbxm"
|
||||
27
server/app/admin/oauth_token.py
Normal file
27
server/app/admin/oauth_token.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from os import getenv
|
||||
|
||||
from fastapi.security.http import HTTPAuthorizationCredentials
|
||||
from google.auth import jwt
|
||||
from icecream import ic
|
||||
|
||||
|
||||
def _token_claims(token: HTTPAuthorizationCredentials) -> dict:
|
||||
aud = getenv("AUDIENCE")
|
||||
credentials = token.credentials
|
||||
claims = jwt.decode(credentials, aud, verify=False)
|
||||
if not claims:
|
||||
raise ValueError("Invalid token")
|
||||
if claims.get("aud") != aud:
|
||||
raise ValueError("Invalid audience")
|
||||
if claims.get("email_verified") is not True:
|
||||
raise ValueError("Email not verified")
|
||||
if not claims.get("email"):
|
||||
raise ValueError("Email not found in token")
|
||||
if not claims.get("sub"):
|
||||
raise ValueError("Sub not found in token")
|
||||
return claims
|
||||
|
||||
|
||||
def email_and_sub(token: HTTPAuthorizationCredentials) -> tuple[str, str]:
|
||||
claims = _token_claims(token)
|
||||
return claims["email"], claims["sub"]
|
||||
Reference in New Issue
Block a user