-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
335 lines (243 loc) · 8.81 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from __future__ import print_function
from flask import Flask, request, g, jsonify, current_app, render_template
import requests
import squareconnect
from squareconnect.rest import ApiException
from squareconnect.apis.locations_api import LocationsApi
from squareconnect.apis.o_auth_api import OAuthApi
from squareconnect.models.obtain_token_request import ObtainTokenRequest
import json
import urllib
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from enum import Enum
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
db = SQLAlchemy(app)
### NGROK
base_url = "https://278417fc.ngrok.io/"
#####################################
####### MISC CONSTSANTS ##########
#####################################
class Permission(Enum):
PAYMENTS_READ = "PAYMENTS_READ"
PAYMENTS_WRITE = "PAYMENTS_WRITE"
INVENTORY_READ = "INVENTORY_READ"
INVENTORY_WRITE = "INVENTORY_WRITE"
TIMECARDS_READ = "TIMECARDS_READ"
AVAILABLE_WEBHOOKS = {
"PAYMENT_UPDATED": {
"Type": "A charge was made or refunded through Square Point of Sale or the Transaction API.",
"Permission": Permission.PAYMENTS_READ.value,
},
"INVENTORY_UPDATED": {
"Type": "The inventory quantity for a catalog item was updated.",
"Permission": Permission.INVENTORY_READ.value,
},
"TIMECARD_UPDATED": {
"Type": "A timecard was created in the Square dashboard or an employee clocked in using Square Point of Sale.",
"Permission": Permission.TIMECARDS_READ.value,
},
}
#####################################
#####################################
#####################################
#################################
####### SQUARE AUTH ##########
#################################
location_id = "VKA3ZBDKC21TQ"
access_token = "EAAAECrvvArmS9YlaFw9q9UrDBvGTRokwReytSbjjjKVEIF5GaEI3417iLSQtDBq"
application_id = "sq0idp-e56RPKqT1rxul1sUO_9LHw"
application_secret = "sq0csp-34x7M-zMyS7UDNmNexHC26AgUzp6Povz3TQOmoizIGg"
oauth_api = OAuthApi()
_SQ_DOMAIN = "connect.squareup.com"
_SQ_AUTHZ_URL = "/oauth2/authorize"
tokens = {}
permissions = [perm.value for perm in Permission]
SUPPORTED_APIS = [
("list_employees", "v1/me/employees"),
("list_payments", "v1/{}/payments".format(location_id)),
("list_items", "v1/{}/items".format(location_id)),
]
#################################
#################################
#################################
##################
### MOCK AUTH ####
##################
USER_TOKEN = "EAAAEG_Bhmh-L9VEkxlWHecl1h-tnQ2i8URrm_AP-Au4vEITiv9tiPYd2MLjfnaK"
def getUserToken():
return USER_TOKEN
def setUserToken(token):
USER_TOKEN = token
@app.route("/set_token")
def set_token():
return render_template(
"set_token.html", url=base_url + "set_token2", curr_token=USER_TOKEN
)
@app.route("/set_token2", methods=["PATCH"])
def set_token2():
token = json.loads(request.data).get("token")
USER_TOKEN = token
return "OK"
##################
##################
##################
######################################
####### LOGIN MANAGEMENT ##########
######################################
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
results = User.query.filter_by(id=user_id)
if results.all():
return results.first()
else:
return None
######################################
######################################
######################################
############################
####### MODELS ##########
############################
class Token(db.Model):
__tablename__ = "Token"
id = db.Column(db.Integer, primary_key=True)
auth_token = db.Column(db.String)
def __repr__(self):
return "<Token(auth_token='%s')>" % (self.auth_token,)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
is_authenticated = db.Column(db.Boolean, default=False)
is_active = db.Column(db.Boolean, default=False)
is_anonymous = db.Column(db.Boolean, default=False)
def get_id(self):
return str(self.id).encode("utf-8").decode("utf-8")
def __repr__(self):
return "<User %r>" % self.username
############################
############################
############################
def getObtainTokenRequest():
r = requests.get("https://github.com/gitapi/user", auth=("user", "pass"))
#################################
########## ENDPOINTS ############
#################################
@app.route("/")
def home():
return render_template("main.html")
@app.route("/auth", methods=["GET", "POST"])
def auth():
perms = " ".join(permissions)
link = (
"https://"
+ _SQ_DOMAIN
+ _SQ_AUTHZ_URL
+ "?client_id="
+ application_id
+ "&scope="
+ perms
)
link2 = urllib.parse.urlparse(link).geturl()
return render_template("auth.html", url=link2, permissions=permissions)
@app.route("/login")
def login():
return render_template("login.html", tokens=tokens)
@app.route("/register")
def register():
return render_template(
"register.html", url=base_url + "register_backend", home=base_url
)
@app.route("/register_backend")
def register_backend():
username = request.data.get("username")
email = request.data.get("email")
try:
user = User(username=username, email=email)
db.session.add(user)
db.session.commit()
return "Registration successful!"
except:
return "Registration failed"
@app.route("/request_token", methods=["POST"])
def request_token():
data = json.loads(request.data)
print(data)
return "Hello, Salvador"
@app.route("/show_tokens")
def show_tokens():
tokens = Token.query.all()
return render_template("tokens.html", tokens=tokens)
@app.route("/s_location", methods=["GET", "POST"])
def location():
# create an instance of the Location API class
api_instance = LocationsApi()
# setup authorization
api_instance.api_client.configuration.access_token = access_token
try:
# ListLocations
api_response = api_instance.list_locations()
print(api_response.locations)
except ApiException as e:
print("Exception when calling LocationApi->list_locations: %s\n" % e)
return "Hello, Salvador"
@app.route("/settings")
def settings():
# CHECK FOR A TOKEN OR SOMETHING
return render_template("settings.html", webhooks=AVAILABLE_WEBHOOKS)
@app.route("/callback", methods=["GET", "POST"])
def callback():
# Extract the returned authorization code from the URL
print("-----------------------------------------------------------")
print(request.args.get("code"))
print("-----------------------------------------------------------")
authorization_code = request.args.get("code")
if authorization_code:
# Provide the code in a request to the Obtain Token endpoint
oauth_request_body = ObtainTokenRequest()
oauth_request_body.client_id = application_id
oauth_request_body.client_secret = application_secret
oauth_request_body.code = authorization_code
oauth_request_body.grant_type = "authorization_code"
response = oauth_api.obtain_token(oauth_request_body)
if response.access_token:
# Here, instead of printing the access token, your application server should store it securely
# and use it in subsequent requests to the Connect API on behalf of the merchant.
token_str = response.access_token
# print("Access token: " + token_str)
# tokens["access_token"] = token_str
token = Token(auth_token=token_str)
db.session.add(token)
db.session.commit()
# return "Authorization succeeded!"
return render_template("auth_success.html")
# The response from the Obtain Token endpoint did not include an access token. Something went wrong.
else:
return "Code exchange failed!"
# The request to the Redirect URL did not include an authorization code. Something went wrong.
else:
return "Authorization failed!"
@app.route("/api")
def api():
return render_template(
"api.html",
user_token=getUserToken(),
apis=SUPPORTED_APIS,
base_url="https://" + _SQ_DOMAIN + "/",
)
##################
# MOCK ENDPOINTS #
##################
@app.route("/mock_webhooks", methods=["PUT"])
def mock_webhooks():
# CHECK FOR A TOKEN OR SOMETHING
webhooks = json.loads(request.data).get("webhooks")
print("RECEIVED THE FOLLOWING WEBHOOK REQUESTS:")
for webhook in webhooks:
print(webhook)
return "SUCCESS"
if __name__ == "__main__":
app.run(debug=True)