Skip to content

Commit

Permalink
Documentation for mojang.api.auth.security
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed Mar 24, 2021
1 parent 609ce40 commit f0e5116
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ verify_ssl = true
mkdocs = "*"
mkdocs-material = "*"
mkdocstrings = "*"
pygments = "*"

[packages]
requests = "*"
Expand Down
4 changes: 2 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions docs/PyMojang/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Mojang Security
===

::: mojang.api.auth.security
handler: python
selection:
members:
- is_user_ip_secure
- get_user_challenges
- verify_user_ip
rendering:
show_root_heading: true
show_source: true
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ theme:

markdown_extensions:
- admonition
- pymdownx.highlight
- pymdownx.superfences

plugins:
- search
Expand All @@ -25,7 +27,7 @@ nav:

- PyMojang:
- 'mojang.api.auth.yggdrasil': 'PyMojang/yggdrasil.md'
- 'mojang.api.auth.security': 'PyMojang/secuity.md'
- 'mojang.api.auth.security': 'PyMojang/security.md'
- 'mojang.api.base': 'PyMojang/base.md'
- 'mojang.api.session': 'PyMojang/session.md'
- 'mojang.profile.UserProfile': 'PyMojang/UserProfile.md'
Expand Down
60 changes: 60 additions & 0 deletions mojang/api/auth/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@


def is_user_ip_secure(access_token: str) -> bool:
"""Check if authenticated user IP is secure
Args:
access_token (str): The session's access token
Returns:
True if IP is secure else False
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
"""
try:
web.auth_request('get', SECURITY_CHECK, access_token, exceptions=(PayloadError, Unauthorized, IPNotSecured))
except IPNotSecured:
Expand All @@ -12,6 +24,30 @@ def is_user_ip_secure(access_token: str) -> bool:
return True

def get_user_challenges(access_token: str) -> list:
"""Return a list of challenges to verify IP
Args:
access_token (str): The session's access token
Returns:
A list of tuples, each one contains the answer's id and the
question
Example:
Example of challenges
```python
[
(123, "What is your favorite pet's name?"),
(456, "What is your favorite movie?"),
(789, "What is your favorite author's last name?")
]
```
Raises:
Unauthorized: If access token is invalid
PayloadError: If access token is not formated correctly
"""
data = web.auth_request('get', SECURITY_CHALLENGES, access_token, exceptions=(PayloadError, Unauthorized))
challenges = []
if data:
Expand All @@ -22,6 +58,30 @@ def get_user_challenges(access_token: str) -> list:
return challenges

def verify_user_ip(access_token: str, answers: list) -> bool:
"""Verify IP with the given answers
Args:
access_token (str): The session's access token
answers (list): The answers to the question
Example:
```python
answers = [
(123, "foo"),
(456, "bar"),
(789, "baz")
]
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
"""
formatted_answers = []
for answer in answers:
formatted_answers.append({
Expand Down

0 comments on commit f0e5116

Please sign in to comment.