Skip to content

Commit

Permalink
Added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed May 8, 2021
1 parent 64119c6 commit 67579b9
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 28 deletions.
53 changes: 37 additions & 16 deletions mojang/account/auth/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def check_ip(access_token: str) -> bool:
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
Example:
```python
from mojang.account.auth import security
checked = security.check_ip('ACCESS_TOKEN')
print(checked)
```
```
True
```
"""
response = requests.get(URLs.verify_ip(), auth=BearerAuth(access_token))
try:
Expand All @@ -37,20 +49,26 @@ def get_challenges(access_token: str) -> List[ChallengeInfo]:
Returns:
A list of ChallengeInfo
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
Example:
Example of challenges
```python
from mojang.account.auth import security
challenges = security.get_challenges('ACCESS_TOKEN')
print(challenges)
```
```bash
[
(123, "What is your favorite pet's name?"),
(456, "What is your favorite movie?"),
(789, "What is your favorite author's last name?")
ChallengeInfo(id=123, challenge="What is your favorite pet's name?"),
ChallengeInfo(id=456, challenge="What is your favorite movie?"),
ChallengeInfo(id=589, challenge="What is your favorite author's last name?")
]
```
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
"""
response = requests.get(URLs.get_challenges(), auth=BearerAuth(access_token))
data = handle_response(response, PayloadError, Unauthorized)
Expand All @@ -67,24 +85,27 @@ def verify_ip(access_token: str, answers: list) -> bool:
Args:
access_token (str): The session's access token
answers (list): The answers to the question
Returns:
True if IP is secure else False
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
Example:
```python
from mojang.account.auth import security
answers = [
(123, "foo"),
(456, "bar"),
(789, "baz")
]
security.verify_user_ip(ACCESS_TOKEN, answers)
security.verify_user_ip('ACCESS_TOKEN', answers)
```
Returns:
True if IP is secure else False
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
"""
answers = list(map(lambda a: {'id': a[0], 'answer': a[1]}, answers))
response = requests.post(URLs.verify_ip(), auth=BearerAuth(access_token), json=answers)
Expand Down
48 changes: 48 additions & 0 deletions mojang/account/auth/yggdrasil.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def authenticate(username: str, password: str, client_token: Optional[str] = Non
Raises:
CredentialsError: If username and password are invalid
PayloadError: If credentials are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
auth_info = yggdrasil.authenticate('USERNAME_OR_EMAIL','PASSWORD')
print(auth_info)
```
```
AuthenticationInfo(access_token='ACCESS_TOKEN', client_token='CLIENT_TOKEN', uuid='...', name='...', legacy=False, demo=False)
```
"""
payload = {
'username': username,
Expand Down Expand Up @@ -57,6 +69,18 @@ def refresh(access_token: str, client_token: str) -> AuthenticationInfo:
Raises:
TokenError: If client token is not the one used to generate the access token
PayloadError: If the tokens are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
refresh_info = yggdrasil.refresh('CURRENT_ACCESS_TOKEN','CLIENT_TOKEN')
print(refresh_info)
```
```
AuthenticationInfo(access_token='NEW_ACCESS_TOKEN', client_token='CLIENT_TOKEN', uuid='...', name='...', legacy=False, demo=False)
```
"""
payload = {
'accessToken': access_token,
Expand Down Expand Up @@ -85,6 +109,14 @@ def validate(access_token: str, client_token: str):
Raises:
TokenError: If client token is not the one used to generate the access token
PayloadError: If the tokens are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
yggdrasil.validate('CURRENT_ACCESS_TOKEN','CLIENT_TOKEN')
```
"""
payload = {
'accessToken': access_token,
Expand All @@ -103,6 +135,14 @@ def signout(username: str, password: str):
Raises:
CredentialsError: If username and password are invalid
PayloadError: If credentials are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
yggdrasil.signout('USERNAME_OR_EMAIL','PASSWORD')
```
"""
payload = {
'username': username,
Expand All @@ -121,6 +161,14 @@ def invalidate(access_token: str, client_token: str):
Raises:
TokenError: If client token is not the one used to generate the access token
PayloadError: If the tokens are not formated correctly
Example:
```python
from mojang.account.auth import yggdrasil
yggdrasil.invalidate('CURRENT_ACCESS_TOKEN','CLIENT_TOKEN')
```
"""
payload = {
'accessToken': access_token,
Expand Down
72 changes: 72 additions & 0 deletions mojang/account/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ def status() -> StatusCheck:
Returns:
StatusCheck
Example:
```python
import mojang
service_status = mojang.status().get('minecraft.net')
print(service_status)
```
```bash
ServiceStatus(name='minecraft.net', status='green')
```
"""
response = requests.get(URLs.status_check())
data = handle_response(response)
Expand All @@ -34,6 +46,17 @@ def get_uuid(username: str) -> UUIDInfo:
Returns:
UUIDInfo
Example:
```python
import mojang
uuid_info = mojang.get_uuid('Notch')
print(uuid_info)
```
```
UUIDInfo(name='Notch', uuid='069a79f444e94726a5befca90e38aaf5', legacy=False, demo=False)
```
"""
response = requests.get(URLs.uuid(username))
data = handle_response(response)
Expand All @@ -54,6 +77,20 @@ def get_uuids(usernames: list) -> List[UUIDInfo]:
Returns:
A list of UUIDInfo
Example:
```python
import mojang
uuids_info = mojang.get_uuids(['Notch', '_jeb'])
print(uuids_info)
```
```
[
UUIDInfo(name='Notch', uuid='069a79f444e94726a5befca90e38aaf5', legacy=False, demo=False),
UUIDInfo(name='_jeb', uuid='45f50155c09f4fdcb5cee30af2ebd1f0', legacy=False, demo=False)
]
```
"""
usernames = list(map(lambda u: u.lower(), usernames))
_uuids = [None]*len(usernames)
Expand All @@ -77,6 +114,21 @@ def names(uuid: str) -> NameInfoList:
Returns:
NameInfoList
Example:
```python
import mojang
name_history = mojang.names('65a8dd127668422e99c2383a07656f7a')
print(name_history)
```
```
(
NameInfo(name='piewdipie', changed_to_at=None),
NameInfo(name='KOtMotros', changed_to_at=datetime.datetime(2020, 3, 4, 17, 45, 26))
)
```
"""
response = requests.get(URLs.name_history(uuid))
data = handle_response(response)
Expand All @@ -98,6 +150,26 @@ def user(uuid: str) -> UserProfile:
Returns:
UserProfile
Example:
```python
import mojang
profile = mojang.user('069a79f444e94726a5befca90e38aaf5')
print(profile)
```
```
UserProfile(
name='Notch',
uuid='069a79f444e94726a5befca90e38aaf5',
is_legacy=False,
is_demo=False,
names=(NameInfo(name='Notch', changed_to_at=None),),
skin=Skin(source='...', variant='classic'),
cape=None
)
```
"""
response = requests.get(URLs.profile(uuid))
data = handle_response(response)
Expand Down

0 comments on commit 67579b9

Please sign in to comment.