Commit bf8e0b949c0a48f06eae8f3d01a4618957b6d229
Refactor hardcoded config values out to a file
| | | | 1 | HOST='http://127.0.0.1' | | HOST='http://127.0.0.1' |
---|
2 | PORT=5000 | | PORT=5000 |
---|
3 | swtstoreURL='http://sweet/store/url' | | swtstoreURL='http://sweet/store/url' |
---|
4 | debug=True | | debug=True |
---|
| | | | | | 1 | # the URL pointing to the sweet store this application will sweet to |
---|
| | 2 | swtstoreURL = 'http://sweet/store/url' |
---|
| | 3 | # the app_id or client_id you have recieved when you registered this |
---|
| | 4 | # application to swtstore |
---|
| | 5 | app_id = 'the app_id or client_id' |
---|
| | 6 | # the app_secret or client_secret you have recieved when you registered this |
---|
| | 7 | # application to swtstore |
---|
| | 8 | app_secret = 'the app_secret or client_secret' |
---|
| | 9 | # the URL at which your application is hosted |
---|
| | 10 | # when you are deploying the app locally, by default this should be |
---|
| | 11 | #redirect_uri = 'http://localhost:5000' |
---|
| | 12 | redirect_uri = 'http://yourapplication.domain' |
---|
| | | | 2 | | 2 | |
---|
3 | import flask | 3 | import flask |
---|
4 | from flask import session | 4 | from flask import session |
---|
5 | import conf | | import conf |
---|
| | 5 | import config | 6 | import requests | 6 | import requests |
---|
7 | import json | 7 | import json |
---|
8 | | 8 | |
---|
9 | app = flask.Flask(__name__) | 9 | app = flask.Flask(__name__) |
---|
| | 10 | app.config['SECRET_KEY'] = config.secret_key |
---|
10 | | 11 | |
---|
11 | app.config['secret_key'] = "asdkasdiq2jedmaid0q2238uwadscksnc" | | app.config['secret_key'] = "asdkasdiq2jedmaid0q2238uwadscksnc" |
---|
12 | app.secret_key = "asdkasdiq2jedmaid0q2238uwadscksnc" | | app.secret_key = "asdkasdiq2jedmaid0q2238uwadscksnc" |
---|
13 | | 12 | |
---|
14 | appID = 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc' | | appID = 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc' |
---|
15 | appSecret = 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ' | | appSecret = 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ' |
---|
16 | | | |
---|
17 | @app.route('/', methods=['GET']) | 13 | @app.route('/', methods=['GET']) |
---|
18 | def index(): | 14 | def index(): |
---|
19 | auth_tok = None | 15 | auth_tok = None |
---|
20 | if flask.request.args.get('code'): | 16 | if flask.request.args.get('code'): |
---|
21 | payload = { | 17 | payload = { |
---|
22 | 'scopes': 'email sweet', | 18 | 'scopes': 'email sweet', |
---|
23 | 'client_secret': appSecret, | | 'client_secret': appSecret, |
---|
| | 19 | 'client_secret': config.app_secret, | 24 | 'code': flask.request.args.get('code'), | 20 | 'code': flask.request.args.get('code'), |
---|
25 | 'redirect_uri': 'http://localhost:5000/', | | 'redirect_uri': 'http://localhost:5000/', |
---|
| | 21 | 'redirect_uri': config.redirect_uri, | 26 | 'grant_type': 'authorization_code', | 22 | 'grant_type': 'authorization_code', |
---|
27 | 'client_id': appID | | 'client_id': appID |
---|
| | 23 | 'client_id': config.app_id | 28 | } | 24 | } |
---|
29 | resp = requests.post('http://localhost:5001/oauth/token', data=payload) | | resp = requests.post('http://localhost:5001/oauth/token', data=payload) |
---|
| | 25 | # token exchange endpoint | | | 26 | oauth_token_x_endpoint = config.swtstoreURL + '/oauth/token' |
---|
| | 27 | resp = requests.post(oauth_token_x_endpoint, data=payload) |
---|
30 | auth_tok = json.loads(resp.text) | 28 | auth_tok = json.loads(resp.text) |
---|
31 | print auth_tok | 29 | print auth_tok |
---|
32 | if auth_tok.has_key('error'): | | if auth_tok.has_key('error'): |
---|
| | 30 | | | | 31 | if 'error' in auth_tok: |
---|
33 | print auth_tok['error'] | 32 | print auth_tok['error'] |
---|
34 | return flask.make_response(auth_tok['error'], 200) | 33 | return flask.make_response(auth_tok['error'], 200) |
---|
35 | | 34 | |
---|
… | | … | |
---|
41 | | 41 | |
---|
42 | print auth_tok | 42 | print auth_tok |
---|
43 | return flask.render_template('index.html', | 43 | return flask.render_template('index.html', |
---|
44 | access_token=auth_tok['access_token'], | | access_token=auth_tok['access_token'], |
---|
45 | refresh_token=auth_tok['refresh_token'], | | refresh_token=auth_tok['refresh_token'], |
---|
46 | url=flask.request.args.get('where'), | | url=flask.request.args.get('where'), |
---|
47 | conf=conf) | | conf=conf) |
---|
| | 44 | access_token=auth_tok['access_token'], | | | 45 | refresh_token=auth_tok['refresh_token'], |
---|
| | 46 | config=config, |
---|
| | 47 | url=flask.request.args.get('where')) |
---|
48 | | 48 | |
---|
| | 49 | |
---|
| | 50 | # if the app is run directly from command-line |
---|
| | 51 | # assume its being run locally in a dev environment |
---|
49 | if __name__ == '__main__': | 52 | if __name__ == '__main__': |
---|
50 | app.run(debug=conf.debug, host=conf.HOST, port=conf.PORT) | | app.run(debug=conf.debug, host=conf.HOST, port=conf.PORT) |
---|
| | 53 | app.run(debug=True, host='0.0.0.0', port=5000) |
| | | | 23 | if(swtr.access_token) { | 23 | if(swtr.access_token) { |
---|
24 | $('#signinview').html('Signing you in..'); | 24 | $('#signinview').html('Signing you in..'); |
---|
25 | $.ajax({ | 25 | $.ajax({ |
---|
26 | url: 'http://localhost:5001/api/users/me?access_token='+ | | url: 'http://localhost:5001/api/users/me?access_token='+ |
---|
| | 26 | url: swtr.swtstoreURL()+'/api/users/me?access_token='+ | 27 | swtr.access_token, | 27 | swtr.access_token, |
---|
28 | success: function(data) { | 28 | success: function(data) { |
---|
| | 29 | console.log(data.username); |
---|
29 | swtr.appView.userLoggedIn(data.username); | 30 | swtr.appView.userLoggedIn(data.username); |
---|
| | 31 | }, |
---|
| | 32 | error: function() { |
---|
| | 33 | $('#signinview').html('Error signing in! Please try again'); |
---|
30 | } | 34 | } |
---|
31 | }); | 35 | }); |
---|
32 | } | 36 | } |
---|
… | | … | |
---|
236 | } | 236 | } |
---|
237 | | 237 | |
---|
238 | this.oauth = new Oauth({ | 238 | this.oauth = new Oauth({ |
---|
239 | app_id: 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc', | | app_id: 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc', |
---|
240 | app_secret: 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ', | | app_secret: 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ', |
---|
241 | endpoint: 'http://localhost:5001/oauth/authorize', | | endpoint: 'http://localhost:5001/oauth/authorize', |
---|
242 | redirect_uri: 'http://localhost:5000/', | | redirect_uri: 'http://localhost:5000/', |
---|
| | 239 | app_id: swtr.app_id, | | | 240 | app_secret: swtr.app_secret, |
---|
| | 241 | endpoint: swtr.swtstoreURL() + swtr.endpoints.auth, |
---|
| | 242 | redirect_uri: swtr.oauth_redirect_uri, |
---|
243 | scopes: 'email,sweet' | 243 | scopes: 'email,sweet' |
---|
244 | }); | 244 | }); |
---|
245 | }, | 245 | }, |
---|
… | | … | |
---|
339 | $('#signin-msg').html('Error signing in. Please check your username and password. '); | 339 | $('#signin-msg').html('Error signing in. Please check your username and password. '); |
---|
340 | } | 340 | } |
---|
341 | else { | 341 | else { |
---|
342 | $('#signin-msg').html('Error signin in. Please try again. '); | | $('#signin-msg').html('Error signin in. Please try again. '); |
---|
343 | } | 342 | } |
---|
344 | } | 343 | } |
---|
345 | }); | 344 | }); |
---|
| | | | 51 | | 51 | |
---|
52 | <script> | 52 | <script> |
---|
53 | window.swtr = window.swtr || {}; | 53 | window.swtr = window.swtr || {}; |
---|
54 | swtr.swtstoreURL = function() { return '{{ conf.swtstoreURL }}'; } | | swtr.swtstoreURL = function() { return '{{ conf.swtstoreURL }}'; } |
---|
55 | swtr.endpoints = {'get': '/api/sweets/q', 'post': '/api/sweets', 'auth': | | swtr.endpoints = {'get': '/api/sweets/q', 'post': '/api/sweets', 'auth': |
---|
56 | '/authenticate', 'login': '/auth/login', 'logout': '/auth/logout'}; | | '/authenticate', 'login': '/auth/login', 'logout': '/auth/logout'}; |
---|
| | 54 | swtr.swtstoreURL = function() { return '{{ config.swtstoreURL }}'; } | | | 55 | swtr.endpoints = { |
---|
| | 56 | 'get': '/api/sweets/q', |
---|
| | 57 | 'post': '/api/sweets', |
---|
| | 58 | 'auth': '/oauth/authorize', |
---|
| | 59 | 'login': '/auth/login', |
---|
| | 60 | 'logout': '/auth/logout' |
---|
| | 61 | }; |
---|
57 | swtr.access_token = '{{ access_token }}'; | 62 | swtr.access_token = '{{ access_token }}'; |
---|
58 | swtr.refresh_token = '{{ refresh_token }}'; | 63 | swtr.refresh_token = '{{ refresh_token }}'; |
---|
| | 64 | swtr.app_id = '{{ config.app_id }}'; |
---|
| | 65 | swtr.app_secret = '{{ config.app_secret }}'; |
---|
| | 66 | swtr.oauth_redirect_uri = '{{ config.redirect_uri }}'; |
---|
59 | window.onload = function() { | 67 | window.onload = function() { |
---|
60 | swtr.init(); | 68 | swtr.init(); |
---|
61 | }; | 69 | }; |
---|