Commit bf8e0b949c0a48f06eae8f3d01a4618957b6d229

Refactor hardcoded config values out to a file
  • Diff rendering mode:
  • inline
  • side by side

swtr/sampleConf.py

1HOST='http://127.0.0.1'
2PORT=5000
3swtstoreURL='http://sweet/store/url'
4debug=True

swtr/sample_config.py

1# the URL pointing to the sweet store this application will sweet to
2swtstoreURL = 'http://sweet/store/url'
3# the app_id or client_id you have recieved when you registered this
4# application to swtstore
5app_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
8app_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'
12redirect_uri = 'http://yourapplication.domain'

swtr/server.py

22
3import flask3import flask
4from flask import session4from flask import session
5import conf
5import config
6import requests6import requests
7import json7import json
88
9app = flask.Flask(__name__)9app = flask.Flask(__name__)
10app.config['SECRET_KEY'] = config.secret_key
1011
11app.config['secret_key'] = "asdkasdiq2jedmaid0q2238uwadscksnc"
12app.secret_key = "asdkasdiq2jedmaid0q2238uwadscksnc"
1312
14appID = 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc'
15appSecret = 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ'
16
17@app.route('/', methods=['GET'])13@app.route('/', methods=['GET'])
18def index():14def index():
19 auth_tok = None15 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,
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/',
21 'redirect_uri': config.redirect_uri,
26 'grant_type': 'authorization_code',22 'grant_type': 'authorization_code',
27 'client_id': appID
23 'client_id': config.app_id
28 }24 }
29 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_tok29 print auth_tok
32 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)
3534
4141
42 print auth_tok42 print auth_tok
43 return flask.render_template('index.html',43 return flask.render_template('index.html',
44 access_token=auth_tok['access_token'],
45 refresh_token=auth_tok['refresh_token'],
46 url=flask.request.args.get('where'),
47 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'))
4848
49
50# if the app is run directly from command-line
51# assume its being run locally in a dev environment
49if __name__ == '__main__':52if __name__ == '__main__':
50 app.run(debug=conf.debug, host=conf.HOST, port=conf.PORT)
53 app.run(debug=True, host='0.0.0.0', port=5000)

swtr/static/js/swtmaker.js

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='+
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 }
237237
238 this.oauth = new Oauth({238 this.oauth = new Oauth({
239 app_id: 'YrYc9oMO7fT0avRUAtbRO1cLvoOUUI08BAuqOAJc',
240 app_secret: 'r9BIYjYOPotMQUOoI98DmH7Eu1M4zg6cMeLay7LOlSsrF1KhKZ',
241 endpoint: 'http://localhost:5001/oauth/authorize',
242 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. ');
343 }342 }
344 }343 }
345 });344 });

swtr/templates/index.html

5151
52 <script>52 <script>
53 window.swtr = window.swtr || {};53 window.swtr = window.swtr || {};
54 swtr.swtstoreURL = function() { return '{{ conf.swtstoreURL }}'; }
55 swtr.endpoints = {'get': '/api/sweets/q', 'post': '/api/sweets', 'auth':
56 '/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 };