-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
195 lines (166 loc) · 7.17 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from flask import Flask, request, session, g, redirect, url_for, render_template, jsonify
from flask.ext.login import LoginManager, UserMixin, login_required, current_user
import requests
import json
import os
from flask.ext.cors import CORS
from pymongo import MongoClient
from OpenSSL import SSL
from flask.ext.bcrypt import Bcrypt
def connect():
# Temporary !!
connection = MongoClient("ds023118.mlab.com",23118)
handle = connection["users"]
handle.authenticate("admin","1234")# .......
return handle
app = Flask(__name__)
CORS(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)
db = connect()
app.config.update(
DEBUG=True,
SECRET_KEY='SuperDuperSecretKey!',
BCRYPT_LOG_ROUNDS = 12
)
# user class
#----------------------------------------------------------------------------------------------------
class User(UserMixin):
def __init__(self, username):
data = db.collection.find({"user": username})[0]
self.user = data['user']
self.password_hash = data['password']
self.data = data['data']
def update(self,tempdata):
#add each item in new list to orignal list, ignore duplicates
db.collection.update(
{"user": self.user},
{
"$addToSet":
{
"data":
{
"$each": tempdata['data']
}
}
}
)
self.data = db.collection.find({"user": self.user})[0]['data']
@classmethod
def get(cls,uid):
if db.collection.find({"user": uid}).limit(1).count() != 0:
return db.collection.find({"user": uid})[0]
#----------------------------------------------------------------------------------------------------
# authenticates requests
#----------------------------------------------------------------------------------------------------
@login_manager.request_loader
def load_user(request):
token = request.headers.get('Authorization')
if token is None:
token = request.args.get('token')
if token is not None:
username,password = token.split(":") #edit this
user_entry = User.get(username)
if (user_entry is not None):
user = User(user_entry['user'])
if bcrypt.check_password_hash(user.password_hash, password) == True:
return user
return None
#----------------------------------------------------------------------------------------------------
# basic endpoints
#----------------------------------------------------------------------------------------------------
@app.route("/")
def hello():
#can be used to check connection status with api
return jsonify({'status':'success'})
@app.route("/api/")
@login_required
def status():
#can be used to check login status
return jsonify({'status':'success','msg':'Currently logged in as '+ current_user.user})
#----------------------------------------------------------------------------------------------------
# endpoint for retriving user data from db
#----------------------------------------------------------------------------------------------------
@app.route("/api/get-user-data/" , methods=['GET','POST'])
@login_required
def recieve():
#retrive specific user data from cloud db
if current_user.is_anonymous == True:
return jsonify({'status':'Error','msg':'Authentication failed !!'})
try:
if request.method == 'GET':
return jsonify({
'user' : current_user.user,
'data' : current_user.data
})
else:
return jsonify({'status':'Error','msg':'That method is not allowed !!'})
except Exception as e:
return jsonify({'status':'Error','msg':'An exception has occured !! - ' + e.message})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
#----------------------------------------------------------------------------------------------------
# endpoint for storing user data in db
#----------------------------------------------------------------------------------------------------
@app.route('/api/post-user-data/', methods=['GET','POST'])
@login_required
def send():
#send data to cloud db for storage
if current_user.is_anonymous == True:
return jsonify({'status':'Error','msg':'Authentication failed !!'})
try:
if request.method == 'POST':
tempdata = json.loads(request.data)
current_user.update(tempdata)
return jsonify({'status':'success','msg':'updated-user'})
else:
return jsonify({'status':'Error','msg':'That method is not allowed !!'})
except Exception as e:
return jsonify({'status':'Error','msg':'An exception has occured !! - ' + e.message})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
#----------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------
@app.route('/api/create-user/', methods=['GET','POST'])
def create():
try:
if request.method == 'POST':
tempdata = json.loads(request.data)
if db.collection.find({"user": tempdata['user']}).limit(1).count() != 0:
#user already exists
return jsonify({'status':'Error','msg':'That user already exists !!'})
else:
db.collection.insert(
{
'user':tempdata['user'],
'password': bcrypt.generate_password_hash(tempdata['password'],12),
'data':[]
}
)
return jsonify({'status':'success','msg':'created-user'})
else:
return jsonify({'status':'Error','msg':'That method is not allowed !!'})
except Exception as e:
return jsonify({'status':'Error','msg':'An exception has occured !! - ' + e.message})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
#----------------------------------------------------------------------------------------------------
#main function
#----------------------------------------------------------------------------------------------------
if __name__ == "__main__":
# app.config["SECRET_KEY"] = "SuperDuperSecretKey!"
port = int(os.environ.get('PORT', 5000))
app.run(debug=True,port=port,host='0.0.0.0',ssl_context=('key.crt','key.key'))