From bf8e0b949c0a48f06eae8f3d01a4618957b6d229 Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Thu, 29 May 2014 17:24:05 +0530 Subject: [PATCH] Refactor hardcoded config values out to a file --- swtr/sampleConf.py | 4 ---- swtr/sample_config.py | 12 ++++++++++++ swtr/server.py | 34 ++++++++++++++++++---------------- swtr/static/js/swtmaker.js | 15 +++++++++------ swtr/templates/index.html | 14 +++++++++++--- 5 files changed, 50 insertions(+), 29 deletions(-) delete mode 100644 swtr/sampleConf.py create mode 100644 swtr/sample_config.py diff --git a/swtr/sampleConf.py b/swtr/sampleConf.py deleted file mode 100644 index cf78fbf..0000000 --- a/swtr/sampleConf.py +++ /dev/null @@ -1,4 +0,0 @@ -HOST='http://127.0.0.1' -PORT=5000 -swtstoreURL='http://sweet/store/url' -debug=True diff --git a/swtr/sample_config.py b/swtr/sample_config.py new file mode 100644 index 0000000..27833a9 --- /dev/null +++ b/swtr/sample_config.py @@ -0,0 +1,12 @@ +# the URL pointing to the sweet store this application will sweet to +swtstoreURL = 'http://sweet/store/url' +# the app_id or client_id you have recieved when you registered this +# application to swtstore +app_id = 'the app_id or client_id' +# the app_secret or client_secret you have recieved when you registered this +# application to swtstore +app_secret = 'the app_secret or client_secret' +# the URL at which your application is hosted +# when you are deploying the app locally, by default this should be +#redirect_uri = 'http://localhost:5000' +redirect_uri = 'http://yourapplication.domain' diff --git a/swtr/server.py b/swtr/server.py index dddf415..55c8724 100644 --- a/swtr/server.py +++ b/swtr/server.py @@ -2,17 +2,13 @@ import flask from flask import session -import conf +import config import requests import json app = flask.Flask(__name__) +app.config['SECRET_KEY'] = config.secret_key -app.config['secret_key'] = "asdkasdiq2jedmaid0q2238uwadscksnc" -app.secret_key = "asdkasdiq2jedmaid0q2238uwadscksnc" - -appID = 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc' -appSecret = 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ' @app.route('/', methods=['GET']) def index(): @@ -20,16 +16,19 @@ def index(): if flask.request.args.get('code'): payload = { 'scopes': 'email sweet', - 'client_secret': appSecret, + 'client_secret': config.app_secret, 'code': flask.request.args.get('code'), - 'redirect_uri': 'http://localhost:5000/', + 'redirect_uri': config.redirect_uri, 'grant_type': 'authorization_code', - 'client_id': appID + 'client_id': config.app_id } - resp = requests.post('http://localhost:5001/oauth/token', data=payload) + # token exchange endpoint + oauth_token_x_endpoint = config.swtstoreURL + '/oauth/token' + resp = requests.post(oauth_token_x_endpoint, data=payload) auth_tok = json.loads(resp.text) print auth_tok - if auth_tok.has_key('error'): + + if 'error' in auth_tok: print auth_tok['error'] return flask.make_response(auth_tok['error'], 200) @@ -42,10 +41,13 @@ def index(): print auth_tok return flask.render_template('index.html', - access_token=auth_tok['access_token'], - refresh_token=auth_tok['refresh_token'], - url=flask.request.args.get('where'), - conf=conf) + access_token=auth_tok['access_token'], + refresh_token=auth_tok['refresh_token'], + config=config, + url=flask.request.args.get('where')) + +# if the app is run directly from command-line +# assume its being run locally in a dev environment if __name__ == '__main__': - app.run(debug=conf.debug, host=conf.HOST, port=conf.PORT) + app.run(debug=True, host='0.0.0.0', port=5000) diff --git a/swtr/static/js/swtmaker.js b/swtr/static/js/swtmaker.js index 1b249d0..d1147fd 100644 --- a/swtr/static/js/swtmaker.js +++ b/swtr/static/js/swtmaker.js @@ -23,10 +23,14 @@ if(swtr.access_token) { $('#signinview').html('Signing you in..'); $.ajax({ - url: 'http://localhost:5001/api/users/me?access_token='+ + url: swtr.swtstoreURL()+'/api/users/me?access_token='+ swtr.access_token, success: function(data) { + console.log(data.username); swtr.appView.userLoggedIn(data.username); + }, + error: function() { + $('#signinview').html('Error signing in! Please try again'); } }); } @@ -232,10 +236,10 @@ } this.oauth = new Oauth({ - app_id: 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc', - app_secret: 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ', - endpoint: 'http://localhost:5001/oauth/authorize', - redirect_uri: 'http://localhost:5000/', + app_id: swtr.app_id, + app_secret: swtr.app_secret, + endpoint: swtr.swtstoreURL() + swtr.endpoints.auth, + redirect_uri: swtr.oauth_redirect_uri, scopes: 'email,sweet' }); }, @@ -335,7 +339,6 @@ $('#signin-msg').html('Error signing in. Please check your username and password. '); } else { - $('#signin-msg').html('Error signin in. Please try again. '); } } }); diff --git a/swtr/templates/index.html b/swtr/templates/index.html index efe79f4..0345b96 100644 --- a/swtr/templates/index.html +++ b/swtr/templates/index.html @@ -51,11 +51,19 @@