Fixed to easily realize multifunctional login / logout with Python / Flask using Auth0

Reference site

Easily implement multifunctional login / logout with Python / Flask using Auth0 https://blanktar.jp/blog/2017/11/python-flask-auth0.html

There was a part of the above site that didn't work, so I fixed it.

Modification place

from jwt.algorithms import RSAAlgorithm

@app.route('/callback')
def auth_callback():
	#Get the information given by Auth0.
	resp = auth0.authorized_response()
	if resp is None:
		return 'nothing data', 403

	#Get the information to check the signature.
	jwks = json.loads(urllib.request.urlopen("https://"+AUTH0_DOMAIN+"/.well-known/jwks.json").read())
	
    #Decrypt JWT format data to get information about the user.
	#By the way, I'm verifying that the signature is correct.
	try:
		payload = jwt.decode(resp['id_token'], RSAAlgorithm.from_jwk( json.dumps(jwks['keys'][0])), audience=AUTH0_CLIENT_ID, algorithms='RS256')
	except Exception as e:
		print(e)
		return 'something wrong', 403  #The signature is strange.

	#Save user data in cookie using Flask Session.
	flask.session['profile'] = {
		'id': payload['sub'],
		'name': payload['name'],
		'picture': payload['picture'],
	}

	#Skip to my page.
	return flask.redirect(flask.url_for('mypage'))

Recommended Posts

Fixed to easily realize multifunctional login / logout with Python / Flask using Auth0
Easily post to twitter with Python 3
How to upload with Heroku, Flask, Python, Git (4)
Easily realize microservices with Cloud Run x Flask
Flow to complete Slack authentication with Flask (Python)
I made a login / logout process using Python Bottle.
How to upload with Heroku, Flask, Python, Git (Part 3)
Easy way to scrape with python using Google Colab
Try to make RESTful API with MVC using Flask 1.0.2
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Easily beep with python
Programming with Python Flask
screen and split screen with python and ssh login to remote server
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
How to update FC2 blog etc. using XMLRPC with python
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Connect to BigQuery with Python
Easily serverless with Python with chalice
Using Quaternion with Python ~ numpy-quaternion ~
Login to website in Python
Connect to Wikipedia with Python
Post to slack with Python 3
[Python] Using OpenCV with Python (Basic)
Post to Twitter using Python
Start to Selenium using python
Switch python to 2.7 with alternatives
Write to csv with Python
Web application with Python + Flask ② ③
Using OpenCV with Python @Mac
Web application with Python + Flask ④
Send using Python with Gmail
I made a library to easily read config files with Python
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Python: Introduction to Flask: Creating a number identification app using MNIST
I tried to make a todo application using bottle with python
When you want to send an object with requests using flask
I tried to easily detect facial landmarks with python and dlib