Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fastapi.middleware.cors import CORSMiddleware
- from fastapi.staticfiles import StaticFiles
- from fastapi import FastAPI
- from main import app
- import pandas
- import json
- import keyring
- import string
- import random
- app = FastAPI()
- app.add_middleware(CORSMiddleware, allow_origins=["*"])
- # --------------------------- User Accounts ----------------------------------------------------------
- def checkpass(username, password):
- return password==keyring.get_password('Users', username)
- def setpass(username, password):
- keyring.set_password('Users', username, password)
- def checkAuth(username, auth):
- return auth==keyring.get_password('Auth', username)
- @app.get("/login/change")
- async def read_item(username, oldpassword, newpassword):
- if checkpass(username,oldpassword):
- setpass(username,newpassword)
- return {'valid':True,'alert':"Password changed successfully"}
- else:
- return {'valid':False,'alert':"Existing password is incorrect"}
- @app.get("/login/create")
- async def read_item(username, password):
- if not keyring.get_password('Users',username) is None:
- return {'alert':"Account already exists",'valid':False}
- else:
- setpass(username,password)
- return {'alert':"Account created successfully",'valid':True}
- @app.get("/login/check")
- async def read_item(username, password):
- if checkpass(username, password):
- auth = ''.join(random.choices(string.ascii_uppercase + string.digits, k=15))
- keyring.set_password('Auth', username,auth)
- return {'valid':True, 'auth':auth}
- else:
- return {'valid':False,'alert':"The password is incorrect"}
- @app.get("/login/Auth")
- async def read_item(username, auth):
- return {'valid':checkAuth(username,auth)}
- # --------------------------- User Accounts ----------------------------------------------------------
- # --------------------------- Bookmark Utility -------------------------------------------------------
- @app.get("/Utilities/Bookmark/read")
- async def read_item(username, auth):
- if checkAuth(username, auth):
- top = keyring.get_password('/Utilities/Bookmark', username+"#top")
- bottom = keyring.get_password('/Utilities/Bookmark', username+"#bottom")
- return {'valid':True,'top':top,'bottom':bottom}
- else:
- return {'valid':False}
- @app.get("/Utilities/Bookmark/write")
- async def read_item(username, auth,top,bottom):
- if checkAuth(username, auth):
- top = keyring.set_password('/Utilities/Bookmark', username+"#top",top)
- bottom = keyring.set_password('/Utilities/Bookmark', username+"#bottom",bottom)
- return {'valid':True}
- else:
- return {'valid':False}
- # --------------------------- Bookmark Utility -------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement