Commit bf8e0b949c0a48f06eae8f3d01a4618957b6d229

Refactor hardcoded config values out to a file
  
1HOST='http://127.0.0.1'
2PORT=5000
3swtstoreURL='http://sweet/store/url'
4debug=True
  
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'
  
22
33import flask
44from flask import session
5import conf
5import config
66import requests
77import json
88
99app = 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
1713@app.route('/', methods=['GET'])
1814def index():
1915 auth_tok = None
2016 if flask.request.args.get('code'):
2117 payload = {
2218 'scopes': 'email sweet',
23 'client_secret': appSecret,
19 'client_secret': config.app_secret,
2420 'code': flask.request.args.get('code'),
25 'redirect_uri': 'http://localhost:5000/',
21 'redirect_uri': config.redirect_uri,
2622 'grant_type': 'authorization_code',
27 'client_id': appID
23 'client_id': config.app_id
2824 }
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)
3028 auth_tok = json.loads(resp.text)
3129 print auth_tok
32 if auth_tok.has_key('error'):
30
31 if 'error' in auth_tok:
3332 print auth_tok['error']
3433 return flask.make_response(auth_tok['error'], 200)
3534
4141
4242 print auth_tok
4343 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
4952if __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)
  
2323 if(swtr.access_token) {
2424 $('#signinview').html('Signing you in..');
2525 $.ajax({
26 url: 'http://localhost:5001/api/users/me?access_token='+
26 url: swtr.swtstoreURL()+'/api/users/me?access_token='+
2727 swtr.access_token,
2828 success: function(data) {
29 console.log(data.username);
2930 swtr.appView.userLoggedIn(data.username);
31 },
32 error: function() {
33 $('#signinview').html('Error signing in! Please try again');
3034 }
3135 });
3236 }
236236 }
237237
238238 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,
243243 scopes: 'email,sweet'
244244 });
245245 },
339339 $('#signin-msg').html('Error signing in. Please check your username and password. ');
340340 }
341341 else {
342 $('#signin-msg').html('Error signin in. Please try again. ');
343342 }
344343 }
345344 });
  
5151
5252 <script>
5353 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 };
5762 swtr.access_token = '{{ access_token }}';
5863 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 }}';
5967 window.onload = function() {
6068 swtr.init();
6169 };