-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved UserSession and UserProfile classes to mojang.classes
- Loading branch information
Showing
8 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import requests | ||
import datetime as dt | ||
|
||
from ._urls import URLs | ||
from ._structures import NameChange, Skin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .profile import user | ||
from .session import connect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import base64 | ||
import json | ||
from dataclasses import dataclass, field | ||
from typing import List | ||
|
||
import requests | ||
from uuid import UUID | ||
|
||
from ..account import get_uuid, names | ||
from ..account._structures import Cape, NameInfoList, Skin | ||
|
||
def user(uuid_name: str): | ||
return UserProfile.create(uuid_name) | ||
|
||
@dataclass | ||
class UserProfile: | ||
name: str = field() | ||
uuid: str = field() | ||
is_legacy: bool = field() | ||
is_demo: bool = field() | ||
names: NameInfoList = field() | ||
skin: Skin = field() | ||
cape: Cape = field() | ||
|
||
@staticmethod | ||
def create(uuid: str): | ||
try: | ||
UUID(uuid) | ||
except: | ||
uuid = get_uuid(uuid).uuid | ||
|
||
# Fetch profile | ||
response = requests.get('https://sessionserver.mojang.com/session/minecraft/profile/{}'.format(uuid)) | ||
user_data = response.json() | ||
data = json.loads(base64.b64decode(user_data['properties'][0]['value'])) | ||
|
||
skin_data = data['textures']['SKIN'] | ||
skin = Skin(skin_data['url'], skin_data.get('metadata', {}).get('model', 'classic')) | ||
cape = Cape(data['textures']['CAPE']['url']) if 'CAPE' in data['textures'] else None | ||
|
||
# Get name history | ||
name_history = names(uuid) | ||
|
||
return UserProfile( | ||
name=user_data['name'], | ||
uuid=uuid, | ||
is_legacy=user_data.get('legacy', False), | ||
is_demo=user_data.get('demo', False), | ||
names=name_history, | ||
skin=skin, | ||
cape=cape | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import datetime as dt | ||
from dataclasses import dataclass, field | ||
|
||
from ..account import session | ||
from ..account.auth import security, yggdrasil | ||
from .profile import UserProfile | ||
|
||
def connect(username: str, password: str, client_token: str = None): | ||
auth = yggdrasil.authenticate(username, password, client_token) | ||
return UserSession(auth.access_token, auth.client_token) | ||
|
||
@dataclass(init=False) | ||
class UserSession(UserProfile): | ||
created_at: dt.datetime = field() | ||
name_change_allowed: bool = field() | ||
|
||
def __init__(self, access_token: str, client_token: str): | ||
self.__access_token = access_token | ||
self.__client_token = client_token | ||
|
||
self.refresh() | ||
|
||
def refresh(self): | ||
auth = yggdrasil.refresh(self.__access_token, self.__client_token) | ||
|
||
# Update tokens | ||
self.__access_token = auth.access_token | ||
self.__client_token = auth.client_token | ||
|
||
# Update info | ||
self.uuid = auth.uuid | ||
self.name = auth.name | ||
self.is_demo = auth.demo | ||
self.is_legacy = auth.legacy | ||
|
||
# Fetch other data | ||
self._fetch_data() | ||
|
||
def _fetch_data(self): | ||
# Load profile | ||
profile = super().create(self.uuid) | ||
self.names = profile.names | ||
self.skin = profile.skin | ||
self.cape = profile.cape | ||
del profile | ||
|
||
# Load name change | ||
name_change = session.get_user_name_change(self.__access_token) | ||
self.name_change_allowed = name_change.allowed | ||
self.created_at = name_change.created_at | ||
|
||
def close(self): | ||
yggdrasil.invalidate(self.__access_token, self.__client_token) | ||
self.__access_token = None | ||
self.__client_token = None | ||
|
||
|
||
# Security | ||
@property | ||
def secure(self): | ||
return security.check_ip(self.__access_token) | ||
|
||
@property | ||
def challenges(self): | ||
return security.get_challenges(self.__access_token) | ||
|
||
def verify(self, answers: list): | ||
return security.verify_ip(self.__access_token, answers) | ||
|
||
|
||
# Name | ||
def change_name(self, name: str): | ||
session.change_user_name(self.__access_token, name) | ||
self._fetch_data() | ||
|
||
# Skin | ||
def change_skin(self, path: str, variant='classic'): | ||
session.change_user_skin(self.__access_token, path, variant) | ||
self._fetch_data() | ||
|
||
def reset_skin(self): | ||
session.reset_user_skin(self.__access_token, self.uuid) | ||
self._fetch_data() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .proto import query, rcon, slp |
Empty file.