Skip to content

Commit

Permalink
Microsoft Authentication example with Flask
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucino772 committed Sep 16, 2021
1 parent d2ce93b commit 1419595
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/microsoft_flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os

import mojang
import mojang.exceptions
from dotenv import load_dotenv
from flask import Flask, jsonify, redirect, request

load_dotenv()

app = Flask(__name__)
microsoft_app = mojang.microsoft_app(os.getenv('CLIENT_ID'), os.getenv('CLIENT_SECRET'), 'http://localhost:3000')

def _sess_to_json(sess):
return {
'name': sess.name,
'uuid': sess.uuid,
'is_legacy': sess.is_legacy,
'is_demo': sess.is_demo,
'names': [{'name': name[0], 'changed_to_at': name[1]} for name in sess.names],
'skin': {
'url': sess.skin.source,
'variant': sess.skin.variant
},
'cape': {
'url': sess.cape.source
},
'created_at': sess.created_at,
'can_change_name': sess.name_change_allowed
}


@app.route('/')
def index():
if request.args.get('code', False):
try:
sess = microsoft_app.authenticate(request.args['code'])
return jsonify(_sess_to_json(sess))
except mojang.exceptions.MicrosoftInvalidGrant:
pass

return redirect(microsoft_app.authorization_url())


if __name__ == '__main__':
app.run(debug=True, port=3000)
22 changes: 22 additions & 0 deletions examples/microsoft_flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.5
click==8.0.1
colorama==0.4.4
cryptography==3.4.8
decorator==5.1.0
Flask==2.0.1
idna==3.2
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
msal==1.14.0
pycparser==2.20
PyJWT==2.1.0
pymojang==1.3.0
python-dotenv==0.19.0
requests==2.26.0
six==1.16.0
urllib3==1.26.6
validators==0.18.2
Werkzeug==2.0.1

0 comments on commit 1419595

Please sign in to comment.