Created
May 10, 2021 02:57
-
-
Save crissilvaeng/fd12bc7bd5b3c7075dc6af31191042e5 to your computer and use it in GitHub Desktop.
Flask with Parse Server JWT Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from functools import wraps | |
from flask import Flask, request | |
PARSE_SERVER_URL = os.environ['PARSE_SERVER_URL'] | |
PARSE_APPLICATION_ID = os.environ['PARSE_SERVER_URL'] | |
app = Flask(__name__) | |
def auth_required(f): | |
@wraps(f) | |
def decorated_function(*args, **kwargs): | |
session_token = request.headers.get('X-Parse-Session-Token') | |
headers = { 'X-Parse-Application-Id': PARSE_APPLICATION_ID, 'X-Parse-Session-Token': session_token } | |
response = requests.get(f'{PARSE_SERVER_URL}/users/me', headers=headers) | |
if response.status_code == 200: | |
return f(*args, **kwargs) | |
return '403 Forbidden', 403 | |
return decorated_function | |
@app.route('/') | |
@auth_required | |
def tournament_list(): | |
return 'Hello World!' | |
@app.route('/healthcheck') | |
def healthcheck(): | |
return 'Ok', 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment