From 373f3f95d063e9695f5eeada10da64c0564efa5f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 5 Aug 2018 17:22:47 +0300 Subject: Fix: getting token --- ickweb/app.py | 26 +++++++++++++++++--------- run | 9 ++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/ickweb/app.py b/ickweb/app.py index dda2928..d8a79ae 100644 --- a/ickweb/app.py +++ b/ickweb/app.py @@ -1,4 +1,6 @@ import json +import logging +import logging.handlers import urllib import bottle @@ -10,6 +12,9 @@ client_id = 'ickweb' COOKIE = 'ickweb-session' +handler = logging.handlers.SysLogHandler() +logging.basicConfig(handlers=[handler]) + def create_app(our_url, controller, client_secret): app = bottle.Bottle() api = API(controller) @@ -71,7 +76,7 @@ def create_app(our_url, controller, client_secret): headers = { 'Location': url, } - print('/login: redirect to', url) + print('params:', params) return bottle.HTTPResponse(status=302, headers=headers) @app.route('/web/projects') @@ -100,21 +105,24 @@ def create_app(our_url, controller, client_secret): @app.route('/web/callback') def callback(): - print('/callback called') + logging.debug('/callback called') code = bottle.request.query['code'] - print('code:', repr(code)) - token_url = '{}/token'.format(controller) + print('code:', code) + + path = '/token' params = { 'grant_type': 'authorization_code', 'code': code, } auth = (client_id, client_secret) - print('requesting token') - r = api.POST(token_url, params, auth) - print('r:', repr(r)) + + print('requesting token using code') + r = api.POST(path, params, auth) + obj = r.json() token = obj['access_token'] - print('token:', token) + print('got access token:', token) + bottle.response.set_cookie(COOKIE, token) bottle.redirect('/web') @@ -149,6 +157,7 @@ class API: def POST(self, path, params, auth): url = self.url(path) + print('POST: url:', url) headers = {} token = self.get_token() if token is not None: @@ -171,7 +180,6 @@ class API: def get_project(self, name): url = self.url('/projects', name) - print('get_project: url:', url) headers = { 'Authorization': 'Bearer {}'.format(self.get_token()) } diff --git a/run b/run index c252f99..2442b0f 100755 --- a/run +++ b/run @@ -6,11 +6,14 @@ import ickweb controller = sys.argv[1] client_secret_filename = sys.argv[2] -port = int(sys.argv[3]) - with open(client_secret_filename) as f: client_secret = f.readline().strip() -our_url = '{}/web'.format(controller) +port = int(sys.argv[3]) +if sys.argv[4] == 'debug': + our_url = 'http://localhost:{}/web'.format(port) +else: + our_url = '{}/web'.format(controller) + app = ickweb.create_app(our_url, controller, client_secret) app.run(host='localhost', port=port) -- cgit v1.2.1