From b197cc263843707866affe556884b1079a2ee604 Mon Sep 17 00:00:00 2001 From: Denis K Date: Wed, 26 Dec 2018 01:38:28 +0300 Subject: [PATCH] Add token validation --- jet_bridge/__main__.py | 11 +++++++++-- jet_bridge/utils/backend.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/jet_bridge/__main__.py b/jet_bridge/__main__.py index 1ca6c8cd..d9e96f65 100644 --- a/jet_bridge/__main__.py +++ b/jet_bridge/__main__.py @@ -6,6 +6,7 @@ from jet_bridge import settings, VERSION from jet_bridge.router import Router +from jet_bridge.utils.backend import is_token_activated from jet_bridge.views.api import ApiHandler from jet_bridge.views.main import MainHandler from jet_bridge.views.message import MessageHandler @@ -35,11 +36,17 @@ def make_app(): def main(): app = make_app() app.listen(settings.PORT, settings.ADDRESS) + address = 'localhost' if settings.ADDRESS == '0.0.0.0' else settings.ADDRESS + url = 'http://{}:{}/'.format(address, settings.PORT) print(datetime.now().strftime('%B %d, %Y - %H:%M:%S %Z')) print('Jet Bridge version {}'.format(VERSION)) - print('Starting server at http://{}:{}/'.format(settings.ADDRESS, settings.PORT)) - print('Quit the server with CONTROL-C.') + print('Starting server at {}'.format(url)) + print('Quit the server with CONTROL-C') + + if not is_token_activated(): + print('[!] Your server token is not activated') + print('[!] Go to {}register/ to activate'.format(url)) tornado.ioloop.IOLoop.current().start() diff --git a/jet_bridge/utils/backend.py b/jet_bridge/utils/backend.py index f89821fe..15f6812d 100644 --- a/jet_bridge/utils/backend.py +++ b/jet_bridge/utils/backend.py @@ -37,6 +37,29 @@ def register_token(): return token, True +def is_token_activated(): + session = Session() + token = session.query(Token).first() + + if not token: + return False + + url = api_method_url('project_tokens/{}/'.format(token.token)) + headers = { + 'User-Agent': 'Jet Django' + } + + r = requests.request('GET', url, headers=headers) + success = 200 <= r.status_code < 300 + + if not success: + return False + + result = r.json() + + return bool(result.get('activated')) + + def reset_token(): session = Session() session.query(Token).delete()