From 51c0757d59c8996b3ae138dc79436c37ede6058e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Mon, 18 Dec 2017 00:53:20 +0100 Subject: [PATCH] Remove Flask. --- openlobby/core/auth.py | 30 ------------------------------ openlobby/core/mutations.py | 3 ++- openlobby/core/utils.py | 5 +++-- openlobby/server.py | 26 -------------------------- requirements.txt | 2 -- 5 files changed, 5 insertions(+), 61 deletions(-) delete mode 100644 openlobby/server.py diff --git a/openlobby/core/auth.py b/openlobby/core/auth.py index 6596f41..5081865 100644 --- a/openlobby/core/auth.py +++ b/openlobby/core/auth.py @@ -1,10 +1,7 @@ from django.conf import settings import json import jwt -import re import time -from flask import g, request -from flask_graphql import GraphQLView def get_login_attempt_expiration_time(): @@ -32,30 +29,3 @@ def parse_access_token(token): def graphql_error_response(message, code=400): error = {'message': message} return json.dumps({'errors': [error]}), code, {'Content-Type': 'application/json'} - - -class AuthGraphQLView(GraphQLView): - """ - GraphQLView which sets session_id into 'g' if authorization token is - provided in Authorization header. - """ - - def dispatch_request(self): - session_id = None - auth_header = request.headers.get('Authorization') - if auth_header is not None: - m = re.match(r'Bearer (?P<token>.+)', auth_header) - if m: - token = m.group('token') - else: - return graphql_error_response('Wrong Authorization header. Expected: "Bearer <token>"') - - try: - session_id = parse_access_token(token) - except jwt.InvalidTokenError: - session_id = None - except Exception: - return graphql_error_response('Wrong Authorization token.', 401) - - g.session_id = session_id - return super(AuthGraphQLView, self).dispatch_request() diff --git a/openlobby/core/mutations.py b/openlobby/core/mutations.py index a6b6086..63ca820 100644 --- a/openlobby/core/mutations.py +++ b/openlobby/core/mutations.py @@ -1,5 +1,4 @@ import arrow -from flask import g import graphene from graphene import relay from graphene.types.datetime import DateTime @@ -201,6 +200,8 @@ class Logout(relay.ClientIDMutation): if viewer is None: raise Exception('User must be logged in to perform this mutation.') + # TODO + raise NotImplementedError() session_id = g.get('session_id') session = SessionDoc.get(session_id, using=info.context['es'], index=info.context['index']) session.delete(using=info.context['es'], index=info.context['index']) diff --git a/openlobby/core/utils.py b/openlobby/core/utils.py index e26871b..e4dc0fd 100644 --- a/openlobby/core/utils.py +++ b/openlobby/core/utils.py @@ -1,11 +1,12 @@ -from flask import g - from .documents import SessionDoc from .types import User def get_viewer(info): """Resolves actual viewer and caches it into 'g'.""" + # TODO + raise NotImplementedError + if not hasattr(g, 'viewer'): session_id = g.get('session_id', None) if session_id is None: diff --git a/openlobby/server.py b/openlobby/server.py deleted file mode 100644 index e35d947..0000000 --- a/openlobby/server.py +++ /dev/null @@ -1,26 +0,0 @@ -import os -from flask import Flask -from elasticsearch import Elasticsearch - -from .auth import AuthGraphQLView -from .management import bootstrap_es -from .schema import schema -from .settings import ES_INDEX - - -app = Flask(__name__) - - -es_dsn = os.environ.get('ELASTICSEARCH_DSN', 'http://localhost:9200') -es_client = Elasticsearch(es_dsn) - -bootstrap_es(es_client, ES_INDEX) - - -@app.route('/') -def hello(): - return 'Open Lobby Server\n\nAPI is at: /graphql', 200, {'Content-Type': 'text/plain; charset=utf-8'} - - -app.add_url_rule('/graphql', view_func=AuthGraphQLView.as_view( - 'graphql', schema=schema, graphiql=True, context={'es': es_client, 'index': ES_INDEX})) diff --git a/requirements.txt b/requirements.txt index 47d9118..e363692 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,6 @@ Django>=2,<2.1 graphene>=2.0,<3.0 graphene-django>=2.0,<3.0 -flask>=0.12,<0.13 -flask_graphQL>=1.4,<1.5 elasticsearch-dsl>=5.3.0,<6.0.0 pytest>=3.2.3,<3.3.0 pytest-django>=3.1.2,<3.2 -- GitLab