Skip to content

Commit b909ba7

Browse files
committed
Added documentation
1 parent c69ef52 commit b909ba7

File tree

10 files changed

+121
-13
lines changed

10 files changed

+121
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[![Documentation Status](https://readthedocs.org/projects/pymojang/badge/?version=latest)](https://pymojang.readthedocs.io/en/latest/?badge=latest)
2-
![PyPI - Downloads](https://img.shields.io/pypi/dm/pymojang)
3-
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pymojang)
1+
![Read the Docs](https://img.shields.io/readthedocs/pymojang?style=flat-square)
2+
![PyPI - Downloads](https://img.shields.io/pypi/dm/pymojang?style=flat-square)
3+
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pymojang?style=flat-square)
44

55
# Pymojang
66
Pymojang is a full wrapper arround de [Mojang API](https://wiki.vg/Mojang_API) and [Mojang Authentication API](https://wiki.vg/Authentication).

docs/account/auth_api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
!!! note "Only Mojang Accounts"
3+
The following functions only works for Mojang accounts, for Microsoft accounts
4+
views [this](./microsoft/auth_api.md)
5+
26
::: mojang.account.auth.yggdrasil
37
handler: python
48
rendering:

docs/account/auth_connect.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Getting a user profile is great, but you can also connect to your account and re
22

33
Both [`connect`][mojang.account.ext.session.connect] and [`UserSession`][mojang.account.ext.session.UserSession] use more low-level functions, you can view them in the following sections: [`Auth API`](./auth_api.md), [`Security API`](./auth_security.md) and [`Session API`](./auth_session.md).
44

5+
!!! note "Microsoft Users"
6+
The following function works for Mojang account, for Microsoft accounts view
7+
[this](./microsoft/connect.md)
58

69
::: mojang.account.ext.session
710
handler: python

docs/account/auth_security.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
!!! note "Only Mojang Accounts"
3+
The following functions only works for Mojang accounts
4+
25
::: mojang.account.auth.security
36
handler: python
47
rendering:

docs/account/microsoft/auth_api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
!!! note "Only Microsoft Accounts"
3+
The following functions only works for Microsoft accounts, for Mojang accounts
4+
views [this](../auth_api.md)
5+
6+
::: mojang.account.auth.microsoft
7+
handler: python
8+
rendering:
9+
show_source: false
10+
show_root_toc_entry: false

docs/account/microsoft/connect.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
If you have a migrated account, you can't use the [`connect`][mojang.account.ext.session.connect] function. You will need to use the [`OAuth flow provided by Microsoft`](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow).
2+
3+
To do so you will first need to create an [`Microsoft Azure App`](https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app) to get your **client id** and **client secret**. Once you have those credentials, you can call the [`microsoft_app`][mojang.account.ext.microsoft.microsoft_app] function with your credentials and it will create a [`MicrosotApp`][mojang.account.ext.microsoft.MicrosoftApp] for you.
4+
5+
Now to authenticate a user, he will need to visit the [`authorization_url`][mojang.account.ext.microsoft.MicrosoftApp.authorization_url] and grant access to your app, he will then be redirected to an url with a **code** parameter. The value of this parameter can the be used when calling the function [`authenticate`][mojang.account.ext.microsoft.MicrosoftApp.authenticate] that will return the same [`UserSession`][mojang.account.ext.session.UserSession] object as the [`connect`][mojang.account.ext.session.connect] function.
6+
7+
8+
::: mojang.account.ext.microsoft
9+
handler: python
10+
rendering:
11+
show_source: false
12+
show_root_toc_entry: false
13+
selection:
14+
members:
15+
- microsoft_app
16+
17+
::: mojang.account.ext.microsoft
18+
handler: python
19+
rendering:
20+
show_source: false
21+
show_root_toc_entry: false
22+
selection:
23+
members:
24+
- MicrosoftApp

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PyMojang is a full wrapper of the [Mojang API](https://wiki.vg/Mojang_API) and [Mojang Authentication API](https://wiki.vg/Authentication).
2+
It also support the [Microsoft Authentication Scheme](https://wiki.vg/Microsoft_Authentication_Scheme).
23

34
You can retrieve basic user information such as their ***uuid***, ***skin***, ***cape***,...
45

mkdocs.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ nav:
4949
- Check Status: 'check_status.md'
5050
- Account:
5151
- Retrieve user information: 'account/retrieve_user_info.md'
52-
- Authenticated User:
53-
- Connect: 'account/auth_connect.md'
52+
- Authenticate:
53+
- Mojang Account: 'account/auth_connect.md'
54+
- Microsoft Account: 'account/microsoft/connect.md'
55+
- API:
5456
- Auth API: 'account/auth_api.md'
57+
- Microsoft Auth API: 'account/microsoft/auth_api.md'
5558
- Security API: 'account/auth_security.md'
5659
- Session API: 'account/auth_session.md'
5760
- Minecraft:

mojang/account/auth/microsoft.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Tuple
12
import requests
23
from mojang.exceptions import *
34

@@ -9,11 +10,12 @@
910
}
1011

1112

12-
def authenticate_xbl(auth_token: str) -> tuple:
13-
"""Authenticate with Xbox Live
13+
def authenticate_xbl(auth_token: str) -> Tuple[str, str]:
14+
"""Authenticate with Xbox Live using the Microsoft access token
15+
received after the OAuth authentication
1416
1517
Args:
16-
auth_token (str): The access token received from the authentication with Microsoft
18+
auth_token (str): The access token
1719
1820
Returns:
1921
A tuple containing the Xbox Live token and user hash
@@ -36,8 +38,8 @@ def authenticate_xbl(auth_token: str) -> tuple:
3638
data = handle_response(response, XboxLiveAuthenticationError)
3739
return data['Token'], data['DisplayClaims']['xui'][0]['uhs']
3840

39-
def authenticate_xsts(xbl_token: str) -> tuple:
40-
"""Retrieve the XSTS Token
41+
def authenticate_xsts(xbl_token: str) -> Tuple[str, str]:
42+
"""Retrieve the XSTS Token using the Xbox Live token
4143
4244
Args:
4345
xbl_token (str): The Xbox Live token
@@ -62,8 +64,9 @@ def authenticate_xsts(xbl_token: str) -> tuple:
6264
data = handle_response(response, XboxLiveAuthenticationError)
6365
return data['Token'], data['DisplayClaims']['xui'][0]['uhs']
6466

65-
def authenticate_minecraft(userhash: str, xsts_token: str):
66-
"""Login to minecraft using Xbox Live
67+
def authenticate_minecraft(userhash: str, xsts_token: str) -> str:
68+
"""Retrieve the Minecraft access token loging in using Xbox Live
69+
6770
Args:
6871
user_hash (str): The user hash from Xbox Live
6972
xbl_token (str): The XSTS Token from Xbox Live
@@ -74,6 +77,18 @@ def authenticate_minecraft(userhash: str, xsts_token: str):
7477
Raises:
7578
XboxLiveInvalidUserHash: If the user hash is invalid
7679
Unauthorized: If the XSTS token is invalid
80+
81+
Example:
82+
83+
```python
84+
from mojang.account.auth import microsoft
85+
86+
ACCESS_TOKEN = '....' # Access token from Microsoft
87+
88+
xbl_token, _ = microsoft.authenticate_xbl(ACCESS_TOKEN)
89+
xsts_token, userhash = microsoft.authenticate_xsts(xbl_token)
90+
mc_access_token = microsoft.authenticate_minecraft(userhash, xsts_token)
91+
```
7792
"""
7893

7994
data = {"identityToken": f"XBL3.0 x={userhash};{xsts_token}"}

mojang/account/ext/microsoft.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,66 @@
55

66
_DEFAULT_SCOPES = ['XboxLive.signin']
77

8-
def microsoft_app(client_id: str, client_secret: str, redirect_uri: str = 'http://example.com'):
8+
def microsoft_app(client_id: str, client_secret: str, redirect_uri: str = 'http://example.com') -> 'MicrosoftApp':
9+
"""It create an instance of [`MicrosoftApp`][mojang.account.ext.microsoft.MicrosoftApp] with the client id and client secret.
10+
This app can then be used to get a [`UserSession`][mojang.account.ext.session.UserSession] like
11+
[`connect`][mojang.account.ext.session.connect] for Microsoft users.
12+
13+
Args:
14+
client_id (str): Your Azure App client id
15+
client_secret (str): Your Azure App client secret
16+
redirect_uri (str, optional): The default redirect uri for your app
17+
18+
Returns:
19+
An instance of the MicrosoftApp
20+
21+
Examples:
22+
23+
```python
24+
import mojang
25+
26+
CLIENT_ID = ... # Your Azure App client id
27+
CLIENT_SECRET = ... # Your Azure App client secret
28+
29+
app = mojang.microsoft_app(CLIENT_ID, CLIENT_SECRET)
30+
31+
# To authenticate users, you first need them to visit the url
32+
# returned by the function `app.authorization_url`
33+
url = app.authorization_url()
34+
35+
# Once they will have granted access to their account to your app,
36+
# they will be redirect to the uri you choose with a `code` parameter
37+
# http://example.com?code=...
38+
# You can use this code to authenticate the user
39+
code = ...
40+
user = app.authenticate(code)
41+
```
42+
"""
943
client = msal.ClientApplication(client_id, client_credential=client_secret, authority='https://login.microsoftonline.com/consumers')
1044
return MicrosoftApp(client, redirect_uri)
1145

1246

1347
class MicrosoftApp:
48+
"""This class allows you to authenticate Microsoft users to Minecraft"""
1449

1550
def __init__(self, client: msal.ClientApplication, redirect_uri: str):
1651
self.__client = client
1752
self.__redirect_uri = redirect_uri
1853

1954
def authorization_url(self, redirect_uri: str = None) -> str:
55+
"""Returns the authorization url for Microsfot OAuth"""
2056
return self.__client.get_authorization_request_url(scopes=_DEFAULT_SCOPES, redirect_uri=(redirect_uri or self.__redirect_uri))
2157

2258
def authenticate(self, auth_code: str, redirect_uri: str = None) -> 'UserSession':
59+
"""Authenticate a user with the auth code
60+
61+
Args:
62+
auth_code (str): The auth code from the redirect
63+
redirect_uri (str, optional): The redirect uri for your app
64+
65+
Returns:
66+
An instance of UserSession
67+
"""
2368
response = self.__client.acquire_token_by_authorization_code(auth_code, scopes=_DEFAULT_SCOPES, redirect_uri=(redirect_uri or self.__redirect_uri))
2469
xbl_token, userhash = microsoft.authenticate_xbl(response['access_token'])
2570
xsts_token, userhash = microsoft.authenticate_xsts(xbl_token)

0 commit comments

Comments
 (0)