Skip to content

Commit

Permalink
Add token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
f1nality committed Dec 25, 2018
1 parent 7509f5e commit b197cc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions jet_bridge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
23 changes: 23 additions & 0 deletions jet_bridge/utils/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b197cc2

Please sign in to comment.