Commit df78035ac223d3a9ba67866bfd6f5cbf1792ec6c
- Diff rendering mode:
- inline
- side by side
swtr.py
(26 / 5)
  | |||
25 | 25 | PASSWORD = 'default' | |
26 | 26 | DB_PORT = 27017 | |
27 | 27 | DB_HOST = 'localhost' | |
28 | URL = "http://localhost:5000" | ||
28 | URL = "http://localhost:5001" | ||
29 | 29 | # create our little application :) | |
30 | 30 | app = Flask(__name__) | |
31 | 31 | app.config.from_object(__name__) | |
… | … | ||
39 | 39 | g.collection = db[app.config["COLLECTION_NAME"]] | |
40 | 40 | ||
41 | 41 | ||
42 | |||
43 | 42 | @app.teardown_request | |
44 | 43 | def close_db(exception): | |
45 | 44 | g.connection.disconnect() | |
… | … | ||
137 | 137 | ||
138 | 138 | @app.route('/serveUser') | |
139 | 139 | def serveUser(): | |
140 | session['key'] = conf.SECRET_KEY | ||
141 | return render_template('user.html') | ||
140 | if "logged_in" in session: | ||
141 | print session["logged_in"] | ||
142 | session['key'] = conf.SECRET_KEY | ||
143 | return render_template('user.html') | ||
144 | else: | ||
145 | return render_template('login.html', error=None) | ||
142 | 146 | ||
143 | |||
144 | 147 | @app.route('/user', methods=['POST', "GET"]) | |
145 | 148 | def user(): | |
146 | 149 | if request.method == 'POST': | |
… | … | ||
159 | 159 | for user in collection.find(): | |
160 | 160 | users.append(user['user']) | |
161 | 161 | return render_template("users.html", users=users) | |
162 | |||
163 | |||
164 | @app.route('/authenticate', methods=['POST','GET']) | ||
165 | def authenticate(): | ||
166 | if request.method == "POST": | ||
167 | response = make_response() | ||
168 | db = g.connection[app.config['DATABASE']] | ||
169 | collection = db['sweet_users'] | ||
170 | for i in collection.find(): | ||
171 | if i['user'] == request.form['user'] and i['key'] == request.form['hash']: | ||
172 | response.status_code = 200 | ||
173 | response.headers['Access-Control-Allow-Origin'] = '*' | ||
174 | return response | ||
175 | else: | ||
176 | response.status_code = 403 | ||
177 | response.headers['Access-Control-Allow-Origin'] = '*' | ||
178 | return response | ||
179 | elif request.method == "GET": | ||
180 | return app.send_static_file("sweet-authenticate.js") | ||
162 | 181 | ||
163 | 182 | ||
164 | 183 | def make_list(res): |
templates/user.html
(2 / 3)
  | |||
1 | 1 | <!doctype html> | |
2 | 2 | <head> | |
3 | <script src="{{ url_for('static', filename='sjcl.js') }}" type="text/javascript"></script> | ||
3 | <!-- <script src="{{ url_for('static', filename='sjcl.js') }}" type="text/javascript"></script> --> | ||
4 | 4 | <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
5 | 5 | <link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}"> | |
6 | 6 | <style type="text/css"> | |
… | … | ||
51 | 51 | { | |
52 | 52 | key = {{ session.key| tojson| safe }} | |
53 | 53 | data = $('#password').val(); | |
54 | var hash = sjcl.encrypt(key,data); | ||
55 | $.post({{ url_for('user')| tojson| safe }}, {'user':$("#user").val(), "key":hash}, function(data){ | ||
54 | $.post({{ url_for('user')| tojson| safe }}, {'user':$("#user").val(), "key":data}, function(data){ | ||
56 | 55 | /*TODO: In the UI flash a "check mark", to indicate success.*/ | |
57 | 56 | console.log("Success"); | |
58 | 57 | }); |