Advertisement
Guest User

Untitled

a guest
Feb 8th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. from fastapi.middleware.cors import CORSMiddleware
  2. from fastapi.staticfiles import StaticFiles
  3. from fastapi import FastAPI
  4. from main import app
  5. import pandas
  6. import json
  7. import keyring
  8. import string
  9. import random
  10. app = FastAPI()
  11. app.add_middleware(CORSMiddleware, allow_origins=["*"])
  12.  
  13. # --------------------------- User Accounts ----------------------------------------------------------
  14. def checkpass(username, password):
  15. return password==keyring.get_password('Users', username)
  16.  
  17. def setpass(username, password):
  18. keyring.set_password('Users', username, password)
  19.  
  20. def checkAuth(username, auth):
  21. return auth==keyring.get_password('Auth', username)
  22.  
  23. @app.get("/login/change")
  24. async def read_item(username, oldpassword, newpassword):
  25. if checkpass(username,oldpassword):
  26. setpass(username,newpassword)
  27. return {'valid':True,'alert':"Password changed successfully"}
  28. else:
  29. return {'valid':False,'alert':"Existing password is incorrect"}
  30.  
  31. @app.get("/login/create")
  32. async def read_item(username, password):
  33. if not keyring.get_password('Users',username) is None:
  34. return {'alert':"Account already exists",'valid':False}
  35. else:
  36. setpass(username,password)
  37. return {'alert':"Account created successfully",'valid':True}
  38.  
  39. @app.get("/login/check")
  40. async def read_item(username, password):
  41. if checkpass(username, password):
  42. auth = ''.join(random.choices(string.ascii_uppercase + string.digits, k=15))
  43. keyring.set_password('Auth', username,auth)
  44. return {'valid':True, 'auth':auth}
  45. else:
  46. return {'valid':False,'alert':"The password is incorrect"}
  47.  
  48. @app.get("/login/Auth")
  49. async def read_item(username, auth):
  50. return {'valid':checkAuth(username,auth)}
  51.  
  52. # --------------------------- User Accounts ----------------------------------------------------------
  53. # --------------------------- Bookmark Utility -------------------------------------------------------
  54.  
  55. @app.get("/Utilities/Bookmark/read")
  56. async def read_item(username, auth):
  57. if checkAuth(username, auth):
  58. top = keyring.get_password('/Utilities/Bookmark', username+"#top")
  59. bottom = keyring.get_password('/Utilities/Bookmark', username+"#bottom")
  60. return {'valid':True,'top':top,'bottom':bottom}
  61. else:
  62. return {'valid':False}
  63.  
  64. @app.get("/Utilities/Bookmark/write")
  65. async def read_item(username, auth,top,bottom):
  66. if checkAuth(username, auth):
  67. top = keyring.set_password('/Utilities/Bookmark', username+"#top",top)
  68. bottom = keyring.set_password('/Utilities/Bookmark', username+"#bottom",bottom)
  69. return {'valid':True}
  70. else:
  71. return {'valid':False}
  72.  
  73. # --------------------------- Bookmark Utility -------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement