|
|
|
@ -3,7 +3,8 @@ from datetime import datetime |
|
|
|
|
from quarry.net.server import ServerFactory, ServerProtocol |
|
|
|
|
|
|
|
|
|
from .database import AccountLinkToken, MinecraftAccount, DiscordAccount |
|
|
|
|
from . import database_session |
|
|
|
|
|
|
|
|
|
DATABASE_SESSION = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AuthProtocol(ServerProtocol): |
|
|
|
@ -32,46 +33,42 @@ class AuthProtocol(ServerProtocol): |
|
|
|
|
|
|
|
|
|
self.logger.info("[AUTH SERVER] %s (%s) connected to address %s:%s", |
|
|
|
|
display_name, uuid, ip_addr, connect_port) |
|
|
|
|
try: |
|
|
|
|
connection_token = ip_addr.split(".")[0] |
|
|
|
|
session = database_session.get_session() |
|
|
|
|
token = session.query(AccountLinkToken).filter_by(token=connection_token).first() |
|
|
|
|
if not token: |
|
|
|
|
self.close("You have connected with an invalid token!") |
|
|
|
|
session.close() |
|
|
|
|
return |
|
|
|
|
discord_account = session.query(DiscordAccount).filter_by(link_token_id=token.id).first() |
|
|
|
|
if not discord_account: |
|
|
|
|
self.close("You have connected with an invalid token!") |
|
|
|
|
session.close() |
|
|
|
|
return |
|
|
|
|
if datetime.utcnow() < token.expiry: |
|
|
|
|
# Check if they already have a linked account and are re-linking |
|
|
|
|
if discord_account.minecraft_account_id is not None: |
|
|
|
|
existing_account = session.query(MinecraftAccount).filter_by( |
|
|
|
|
id=discord_account.minecraft_account_id).first() |
|
|
|
|
self.logger.info("unlinking existing %s account and replacing it with %s", |
|
|
|
|
existing_account.minecraft_uuid, str(uuid)) |
|
|
|
|
session.delete(existing_account) |
|
|
|
|
mc_account = MinecraftAccount(str(uuid), discord_account.id) |
|
|
|
|
discord_account.minecraft_account = mc_account |
|
|
|
|
session.add(mc_account) |
|
|
|
|
session.delete(token) |
|
|
|
|
session.commit() |
|
|
|
|
session.close() |
|
|
|
|
self.close("Your minecraft account has successfully been linked to your discord account!") |
|
|
|
|
return |
|
|
|
|
else: |
|
|
|
|
session.delete(token) |
|
|
|
|
session.commit() |
|
|
|
|
session.close() |
|
|
|
|
self.close("You have connected with an expired token! " |
|
|
|
|
"Please run the mc!register command again to get a new token.") |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
self.logger.error(e) |
|
|
|
|
connection_token = ip_addr.split(".")[0] |
|
|
|
|
session = DATABASE_SESSION.get_session() |
|
|
|
|
token = session.query(AccountLinkToken).filter_by(token=connection_token).first() |
|
|
|
|
if not token: |
|
|
|
|
self.close("You have connected with an invalid token!") |
|
|
|
|
session.close() |
|
|
|
|
return |
|
|
|
|
discord_account = session.query(DiscordAccount).filter_by(link_token_id=token.id).first() |
|
|
|
|
if not discord_account: |
|
|
|
|
self.close("You have connected with an invalid token!") |
|
|
|
|
session.close() |
|
|
|
|
return |
|
|
|
|
if datetime.utcnow() < token.expiry: |
|
|
|
|
# Check if they already have a linked account and are re-linking |
|
|
|
|
if discord_account.minecraft_account_id is not None: |
|
|
|
|
existing_account = session.query(MinecraftAccount).filter_by( |
|
|
|
|
id=discord_account.minecraft_account_id).first() |
|
|
|
|
self.logger.info("unlinking existing %s account and replacing it with %s", |
|
|
|
|
existing_account.minecraft_uuid, str(uuid)) |
|
|
|
|
session.delete(existing_account) |
|
|
|
|
mc_account = MinecraftAccount(str(uuid), discord_account.id) |
|
|
|
|
discord_account.minecraft_account = mc_account |
|
|
|
|
session.add(mc_account) |
|
|
|
|
session.delete(token) |
|
|
|
|
session.commit() |
|
|
|
|
session.close() |
|
|
|
|
self.close("Your minecraft account has successfully been linked to your discord account!") |
|
|
|
|
return |
|
|
|
|
else: |
|
|
|
|
session.delete(token) |
|
|
|
|
session.commit() |
|
|
|
|
session.close() |
|
|
|
|
self.close("You have connected with an expired token! " |
|
|
|
|
"Please run the mc!register command again to get a new token.") |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
# Kick the player. |
|
|
|
|
self.close("This shouldn't happen!") |
|
|
|
|