LawMixer

s

Apr 2nd, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import secrets, discordoauth2, os, rblxopencloud, firebase_admin
  2.  
  3. from dotenv import load_dotenv
  4. load_dotenv()
  5. from flask import Flask, request, redirect, jsonify
  6. from discordoauth2 import Client, AccessToken
  7. from rblxopencloud import OAuth2App
  8. from firebase_admin import db, credentials
  9.  
  10.  
  11. app = Flask(__name__)
  12.  
  13.  
  14. DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
  15. DISCORD_CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
  16. DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
  17. DISCORD_REDIRECT = os.getenv("DISCORD_REDIRECT")
  18.  
  19. ROBLOX_APP_ID = os.getenv("ROBLOX_APP_ID")
  20. ROBLOX_APP_SECRET = os.getenv("ROBLOX_APP_SECRET")
  21. ROBLOX_APP_REDIRECT = os.getenv("ROBLOX_APP_REDIRECT")
  22.  
  23. # NOTE: this is just an example function, and may not be the same in production
  24.  
  25. rbx_client = OAuth2App(id=ROBLOX_APP_ID, secret=ROBLOX_APP_SECRET, redirect_uri=ROBLOX_APP_REDIRECT)
  26. dsc_client = Client(id=DISCORD_CLIENT_ID, secret=DISCORD_CLIENT_SECRET, redirect=DISCORD_REDIRECT, bot_token=DISCORD_TOKEN)
  27. discord_access_cache = {}
  28.  
  29.  
  30. def fetch_roblox_id_from_discord_id(discord_id: int):
  31. users = db.reference("/Users")
  32.  
  33. for info in users.get():
  34. if int(users.child(info).get()["discord_id"]) == int(discord_id):
  35. return users.child(info).get()
  36.  
  37. return None
  38.  
  39. @app.route("/callback/discord")
  40. def api_oauth_callback_discord():
  41. access = dsc_client.client_credentails_grant(["identify", "guilds.members.read"])
  42.  
  43. state = secrets.token_urlsafe(16)
  44. discord_access_cache[state] = access.token
  45.  
  46. print(access.token)
  47.  
  48. return redirect(rbx_client.generate_uri(["openid", "profile"], state=state))
  49.  
  50. @app.route("/callback/roblox")
  51. def api_oauth_callback_roblox():
  52. access = rbx_client.exchange_code(code=request.args["code"])
  53.  
  54. robloxInfo = access.fetch_userinfo()
  55.  
  56. cachedUsername = robloxInfo.username
  57. robloxId = robloxInfo.id
  58.  
  59. discord_access = dsc_client.from_access_token(discord_access_cache[request.args.get("state")])
  60. discord_user = discord_access.fetch_identify()
  61.  
  62.  
  63. userInfo = fetch_roblox_id_from_discord_id(discord_user["id"])
  64.  
  65. print(robloxId, cachedUsername, discord_user["id"])
  66. print(userInfo)
  67. if userInfo == None:
  68. db.reference("/Users").update({
  69. robloxId: {
  70. "cachedUsername": cachedUsername,
  71. "roblox_id": robloxId,
  72. "discord_id": discord_user["id"]
  73. }
  74. })
  75.  
  76. return "You are verified with Orosius Authorization."
  77.  
  78.  
  79. return "You are already verified with Orosius Authorization."
  80.  
  81. app.run(host="192.168.48.2", port=2938)
Advertisement
Add Comment
Please, Sign In to add comment