forked from sudobob/wild_apricot_python_flask_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wautils.py
executable file
·529 lines (412 loc) · 14.5 KB
/
wautils.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python3
"""
wautils
A set of web-based tools for Wild Apricot Integration
o Accepts your Wild Apricot Credentials via Wild Apricot OAuth
o Determines if you are have Wild Apricot admin credentials
o Give you further access only if you have admin credentials
"""
usage_mesg = """
usage:
wautils [--debug]
Start up wautils web server
"""
from flask import Flask, redirect, url_for, render_template, flash, g, request, send_file, abort, send_from_directory
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user, current_user, login_required
from flask_bootstrap import Bootstrap
from flask_restful import Resource as FlaskRestResource
from flask_restful import reqparse as FlaskRestReqparse
from flask_restful import Api as FlaskRestAPI
from flask_restful import request as FlaskRestRequest
from flask_migrate import Migrate
import WaApi
import urllib
import os,sys,requests
from dotenv import load_dotenv
from oauth import OAuthSignIn
import getopt
import json
import random
import csv
import re
pos_ran_chars = 'abcdefghijknpqrstuvwxyz23456789'
import pprint # for debugging
ex_code_fail = 1 # used with sys.exit()
ex_code_success = 0
# get keys and config info from .env
load_dotenv()
wa_uri_prefix = "https://api.wildapricot.org/v2.2/"
wa_uri_prefix_accounts = wa_uri_prefix + "Accounts/"
app = Flask(__name__)
app.secret_key = os.environ['FLASK_SECRET_KEY']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['OAUTH_CREDENTIALS'] = {
'wildapricot' : {
'account' : os.environ['WA_ACCOUNT'],
'id' : os.environ['OAUTH_ID'],
'api_key' : os.environ['OAUTH_API_KEY'],
'secret' : os.environ['OAUTH_SECRET']
}
}
# rest api support
restapi = FlaskRestAPI(app)
# bootstrap framework support
Bootstrap(app)
# allow cross-site origin
CORS(app)
# tell bootstrap NOT to fetch from CDNs
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
# login manager setup
lm = LoginManager(app)
lm.login_view = 'index'
# database setup
db = SQLAlchemy(app)
migrate = Migrate(app,db)
# for debugging
pp = pprint.PrettyPrinter(stream=sys.stderr)
def wapi_init():
# setup the WA API
creds = app.config['OAUTH_CREDENTIALS']['wildapricot']
wapi = WaApi.WaApiClient(creds['id'],creds['secret'])
wapi.authenticate_with_apikey(creds['api_key'])
return wapi,creds
class User(UserMixin, db.Model):
# define table entry in db for logged-in user
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key = True)
first_name = db.Column(db.String(64), nullable = False)
last_name = db.Column(db.String(64), nullable = False)
email = db.Column(db.String(64), nullable = True)
token = db.Column(db.String(64), nullable = True)
#wa_admin = db.Column(db.Boolean, nullable = True)
@lm.user_loader
def load_user(id):
# required by flask_login
return User.query.get(int(id))
def is_account_admin(waco):
if waco['IsAccountAdministrator']:
return True
else:
return False
def has_wautils_signoff(waco):
sos = [] # signoffs
for fv in waco['FieldValues']:
if fv['FieldName'] == "NL Signoffs and Categories":
sos = fv
if len(sos):
for so in sos['Value']:
if so['Label'] == '[nlgroup] wautils':
return True
return False
def before_authenticated_request():
if current_user.is_anonymous:
abort(401)
# retrieve users credentials
wapi,creds = wapi_init()
wac = wapi.execute_request_raw( wa_uri_prefix_accounts + creds['account'] + "/contacts/" + str(current_user.id))
waco = json.loads(wac.read().decode('utf-8'))
g.wa_me = waco
g.is_wa_admin = is_account_admin(waco)
g.is_wa_signoffer = has_wautils_signoff(waco)
g.wa_url = os.getenv('WA_SITE_URL')
g.formbot_url = os.getenv('FORMBOT_URL')
@app.errorhandler(401)
def unauthorized_handler(error):
return render_template('unauthorized.html'), 401
@app.route('/')
def index():
before_authenticated_request()
return render_template('index.html')
@app.route('/signoffs')
@login_required
def signoffs():
before_authenticated_request()
return render_template('signoffs.html')
@app.route('/tool_signoffs')
@login_required
def tool_signoffs():
before_authenticated_request()
return render_template('tool_signoffs.html')
@app.route('/events')
@login_required
def events():
before_authenticated_request()
return render_template('events.html')
@app.route('/dump_events')
@login_required
def dump_events():
if current_user.is_anonymous:
abort(401)
wapi,creds = wapi_init()
resp = wapi.execute_request_raw( wa_uri_prefix_accounts + creds['account'] + "/events/")
events = json.loads(resp.read().decode())
ran_chars = ''.join(random.choice(pos_ran_chars) for _ in range (10))
event_file_name= '/tmp/events_' + ran_chars + '.csv'
event_file = open(event_file_name,'w')
event_file_csv = csv.writer(event_file)
ar = []
ar.append( 'AccessLevel' )
ar.append( 'CheckedInAttendeesNumber')
ar.append( 'ConfirmedRegistrationsCount')
ar.append( 'EndDate')
ar.append( 'EndTimeSpecified')
ar.append( 'EventType')
ar.append( 'HasEnabledRegistrationTypes')
ar.append( 'Id')
ar.append( 'Location')
ar.append( 'Name')
ar.append( 'PendingRegistrationsCount')
ar.append( 'RegistrationEnabled')
ar.append( 'RegistrationsLimit')
ar.append( 'StartDate')
ar.append( 'StartTimeSpecified')
ar.append( 'Tags')
ar.append( 'Url')
event_file_csv.writerow(ar)
for ev in events['Events']:
ar = []
ar.append(ev[ 'AccessLevel' ])
ar.append(ev[ 'CheckedInAttendeesNumber'])
ar.append(ev[ 'ConfirmedRegistrationsCount'])
ar.append(ev[ 'EndDate'])
ar.append(ev[ 'EndTimeSpecified'])
ar.append(ev[ 'EventType'])
ar.append(ev[ 'HasEnabledRegistrationTypes'])
ar.append(ev[ 'Id'])
ar.append(ev[ 'Location'])
ar.append(ev[ 'Name'])
ar.append(ev[ 'PendingRegistrationsCount'])
ar.append(ev[ 'RegistrationEnabled'])
ar.append(ev[ 'RegistrationsLimit'])
ar.append(ev[ 'StartDate'])
ar.append(ev[ 'StartTimeSpecified'])
ar.append(ev[ 'Tags'])
ar.append(ev[ 'Url'])
event_file_csv.writerow(ar)
event_file.close()
return send_file(event_file_name,as_attachment=True,attachment_filename='events.csv')
"""
{'AccessLevel': 'Public',
'CheckedInAttendeesNumber': 0,
'ConfirmedRegistrationsCount': 0,
'EndDate': '2021-04-01T00:00:00-04:00',
'EndTimeSpecified': False,
'EventType': 'Regular',
'HasEnabledRegistrationTypes': True,
'Id': 4053381,
'Location': '',
'Name': 'Adams Test',
'PendingRegistrationsCount': 0,
'RegistrationEnabled': True,
'RegistrationsLimit': None,
'StartDate': '2021-04-01T00:00:00-04:00',
'StartTimeSpecified': False,
'Tags': [],
'Url': 'https://api.wildapricot.org/v2.1/accounts/335649/Events/4053381'}
"""
"""
swipe_file = open('/tmp/events.csv','w')
swipe_file_csv = csv.writer(swipe_file)
q = BadgeSwipe.query.order_by(desc(BadgeSwipe.timestamp))
for r in q:
dat = r.to_dict()
ar = []
ar.append(dat['timestamp'])
ar.append(dat['user_name'])
if '.' in dat['card_id']:
# put leading zero back into card id
(fac,id) = dat['card_id'].split('.')
id = '0' + id if len(id) == 4 else id
id = '00' + id if len(id) == 3 else id
id = '000' + id if len(id) == 2 else id
id = '0000' + id if len(id) == 1 else id
dat['card_id'] = fac + id
ar.append(dat['card_id'])
swipe_file_csv.writerow(ar)
swipe_file.close()
return send_file('/tmp/swipes.csv',as_attachment=True,attachment_filename='swipes.csv')
"""
@app.route('/members')
@login_required
def members():
before_authenticated_request()
return render_template('members.html')
@app.route('/utils')
@login_required
def utils():
before_authenticated_request()
return render_template('utils.html')
@app.route('/logout/<provider>')
@login_required
def logout(provider):
if not current_user.is_anonymous:
# if user is logged in..
# first tell oauth who will give us a nonce token
payload = {
'token' : current_user.token,
'email' : current_user.email,
'redirectUrl' : request.environ['HTTP_REFERER']
}
rq = requests.post(os.environ['WA_DEAUTHORIZE_URL'], json = payload)
if rq.reason == 'OK':
# then call WA's logout url with that nonce, and it'll really log them out
logout_user()
url = os.environ['WA_LOGOUT_URL'] + '?nonce=' + rq.json()['nonce']
return redirect(url)
else:
logout_user()
return redirect(url_for('index'))
# otherwise just repaint the top page
logout_user()
return redirect(url_for('index'))
@app.route('/authorize/<provider>')
def oauth_authorize(provider):
if not current_user.is_anonymous:
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
return oauth.authorize()
@app.route('/callback/<provider>')
def oauth_callback(provider):
# oauth calls us once we've been granted a token
if not current_user.is_anonymous:
# not logged in
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
me,oauth_session = oauth.callback()
if not('Email' in me):
flash("ERROR oauth_callback(): " + me['Message'],'error')
return redirect(url_for('index'))
sys.stderr.write("oauth_callback()\n")
# is this user in the DB ?
user = User.query.filter_by(email=me['Email']).first()
if not user:
# if not, add them
user = User(
first_name = me['FirstName'],
last_name = me['LastName'],
email = me['Email'],
id = me['Id'],
#wa_admin = me['IsAccountAdministrator'],
token = oauth_session.access_token
)
db.session.add(user)
db.session.commit()
else:
user.token = oauth_session.access_token
db.session.commit()
# officially login them into flask_login system
login_user(user, True)
return redirect(url_for('index'))
def wa_get_contacts():
# get WA contacts
# returns them formatted on screen
# for testing only
wapi,creds = wapi_init()
response = wapi.execute_request_raw(wa_uri_prefix_accounts +
creds['account'] +
"/contacts/?$async=false")
wa_accounts_contact_me = wapi.execute_request(
wa_uri_prefix_accounts + creds['account'] + "/contacts/" + str(current_user.id))
return('<pre>' + json.dumps(
response.read().decode(),
indent=4,sort_keys=True
) + '</pre>')
def wa_execute_request_raw(wapi,ep):
try:
response = wapi.execute_request_raw(ep)
except urllib.error.HTTPError as e:
return {"error":1,"error_message": ep + ':' + str(e) }
except WaApi.ApiException as e:
return {"error":1,"error_message": ep + ':' + str(e) }
decoded = json.loads(response.read().decode())
result = []
if isinstance(decoded, list):
for item in decoded:
result.append(item)
elif isinstance(decoded, dict):
result.append(decoded)
return result
################################################################################
class WAAPIProxy(FlaskRestResource):
"""
Proxy all API requests to WA
"""
def get(self, path):
return wa_api_proxy("GET", path)
def post(self, path):
return wa_api_proxy("POST", path)
def put(self, path):
return wa_api_proxy("PUT", path)
restapi.add_resource(WAAPIProxy,'/api/v1/wa_proxy/<path:path>')
def wa_api_allowed_paths(path):
"""
API Methods that non-admins are allowed to call
"""
# Closes security holes like injecting someone else's account id
allowed = [
'^accounts/\{accountId\}$',
'^accounts/\{accountId\}/contacts/\d+$',
'^accounts/\{accountId\}/events$',
'^accounts/\{accountId\}/events/\d+$',
'^accounts/\{accountId\}/eventregistrations',
'^accounts/\{accountId\}/EventRegistrationTypes/\d+$'
]
reg_list = map(re.compile, allowed)
return any(regex.match(path) for regex in reg_list)
def wa_api_allowed_for_user(path):
if wa_api_allowed_paths(path):
return True
wapi,creds = wapi_init()
wac = wapi.execute_request_raw(wa_uri_prefix_accounts + creds['account'] + "/contacts/" + str(current_user.id))
waco = json.loads(wac.read().decode('utf-8'))
if is_account_admin(waco) or has_wautils_signoff(waco):
return True
return False
def wa_api_munge_path(account_id):
path = request.full_path
path = path.replace('/api/v1/wa_proxy/', '')
path = path.replace('{accountId}', account_id)
path = wa_uri_prefix + path
return path
def wa_api_proxy(method, path_template):
wapi,creds = wapi_init()
account_id = creds['account']
path = wa_api_munge_path(account_id)
data = None
if method != "GET":
data = FlaskRestRequest.json
if wa_api_allowed_for_user(path_template):
resp = wapi.execute_request_raw(path, data=data, method=method)
return json.loads(resp.read().decode())
else:
return {
"error":1,
"path": path,
"error_message":"You are not a WA account admin nor do you have the wautils signoff"
}
################################################################################
# Execution starts here
if __name__ == '__main__':
# parse cmd line args and perform operations
try:
# parse cmd line args
ops,args = getopt.getopt(sys.argv[1:],"c:",["debug","cmd="])
except getopt.GetoptError as err:
sys.stderr.write(str(err) + '\n')
sys.stderr.write(usage_mesg)
sys.exit(ex_code_fail)
for o,a in ops:
if (o == '--debug'):
db.create_all()
#os.environ['OAUTH_REDIRECT_URL']='http://srv-a.nova-labs.org:8080/callback/wildapricot'
app.run(host='0.0.0.0',port=7000,debug=True)
# run production on local port that apache proxy's to
sys.stderr.write("Starting web server\n")
db.create_all()
app.run(port=7000)
# no options given. print usage and exit
sys.stderr.write(usage_mesg)
sys.exit(ex_code_fail)