-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
2169 lines (1461 loc) · 72.6 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import sys
from time import strftime
from dotenv import load_dotenv
from datetime import datetime
from datetime import date
from loguru import logger
# Flask-related imports.
from flask import Flask, request, send_file
from flask_restful import Api
from werkzeug.utils import secure_filename
# Local imports.
from database import Database
from database_utils import DatabaseUtils
from email_manager import EmailManager
from api_resource import APIResource
from email_utils import EmailUtils
from file_manager import FileManager
from structures.application_session import ApplicationSession
from structures.friend import Friend
from structures.friend_request import FriendRequest
from structures.iap import IAP
from structures.iap_record import IAPRecord
from structures.application_key import ApplicationKey
from structures.application_version import ApplicationVersion
from structures.sale import Sale
from structures.session import Session
from structures.transaction import Transaction
from structures.user import User
from utils import Utils
# Constants.
BASE_DIRECTORY: str = 'data'
PHOTOS_DIRECTORY: str = 'photos'
APPLICATIONS_DIRECTORY: str = 'applications'
ALLOWED_IMAGE_TYPES: list = ['png', 'jpg', 'jpeg']
# Variables.
email_manager: EmailManager | None = None
database: Database | None = None
database_utils: DatabaseUtils | None = None
file_manager: FileManager | None = None
app = None
api = None
# Api classes.
class Ping(APIResource):
@staticmethod
def get():
return {'ping': 'pong'}
class RequestEmailVerification(APIResource):
required_parameters = ['email_address']
def post(self):
# Ensure that there are no missing parameters.
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
# Get the email address from the form data.
email_address: str = request.form.get('email_address')
# Request a verification code from the database.
database.request_email_verification_code(email_address)
return {}, 200
class CheckEmailVerification(APIResource):
required_parameters = ['email_address', 'verification_code']
def post(self):
# Ensure that there are no missing parameters.
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
# Get the parameters.
email_address: str = request.form.get('email_address')
verification_code: int = Utils.safe_int_cast(request.form.get('verification_code'))
# Check the provided code against the database.
email_verified: bool = database.check_email_verification(email_address, verification_code)
return {'email_verified': email_verified}, 200
class Register(APIResource):
required_parameters = ['username', 'name', 'email_address', 'password', 'email_verification_code']
def post(self):
# Ensure that there are no missing parameters.
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
# Get the parameters.
username: str = request.form.get('username')
name: str = request.form.get('name')
email_address: str = request.form.get('email_address')
password: str = request.form.get('password')
email_verification_code: int = Utils.safe_int_cast(request.form.get('email_verification_code'))
# Attempt to register the user.
success, response = database.create_user(username, name, email_address, password, email_verification_code)
if success:
return response, 201
else:
return response, 400
class Login(APIResource):
required_parameters = ['username', 'password', 'hostname', 'mac_address',
'platform'] # Session-related parameters are also required.
def post(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
# Get the parameters.
username: str = request.form.get('username')
password: str = request.form.get('password') # The provided password, not hashed.
hostname: str = request.form.get('hostname')
mac_address: str = request.form.get('mac_address')
platform: str = request.form.get('platform')
# Attempt to fetch the user based on the provided username.
user: User | None = database.get_user(username, 'username')
if not user:
return {'details': 'A user with the specified username does not exist.'}, 400
# The user exists; check the password.
# Get the password.
user_password: str = user.password # The user's hashed password, retrieved from the database.
# Check the provided password against the one from the database.
if not Utils.password_matches(password, user_password):
return {'details': 'Password does not match.'}, 400
# The password matched, attempt to create the session.
success, response = (
database.create_session(user.id, hostname, mac_address, platform, date.today(), date.today()))
# Generate the login email.
subject, text, html = EmailUtils.generate_email(
'Frogworks Login Notification',
'New Session Created',
f'Someone recently logged into your Frogworks account, we just thought you\'d like to know.<br><br>Device Details:<br>Hostname: {hostname}<br>MAC Address: {mac_address}<br>Platform: {platform}<br><br>If this wasn\'t you, please take these steps:<br><ul><li>Change your password to one that is more secure</li><li>End the new session (or all your sessions)</li><ul><li>You can do this from the launcher, in the sessions UI</li></ul></ul>',
'Ensure that your account is secure.'
)
# Send the verification email.
email_manager.send_email(
subject,
text,
html,
[user.email_address]
)
if not success:
return {'details': 'Failed to create session.', 'additional_data': response}, 400
# The session was successfully created.
# Get the session id.
session_id: str = response['session_id']
return {'session_id': session_id}, 200
class GetUser(APIResource):
required_parameters = ['identifier']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
identifier = request.form.get('identifier')
# Get the optional parameters.
identifier_type = request.form.get('identifier_type') if 'identifier_type' in request.form else 'id'
# Get the requested user.
target_user = database.get_user(identifier, identifier_type)
if not target_user:
return {'details': 'The specified user does not exist.'}, 400
return target_user.into_dict(user.is_or_admin(target_user.id)), 200
class AuthenticateSession(APIResource):
required_parameters = []
def get(self):
# Ensure that the client is authenticated with the session id they would like to check.
authenticated, session_id = self.get_authentication()
if not authenticated:
return {}, 401
# The user is authenticated. Now, validate their session id.
session = database.get_session(session_id)
if not session:
return {'authenticated': False}
# The user's session is valid. Update the session's last activity date to prevent it from being deleted.
database.update_session_last_activity(session.id, date.today())
return {'authenticated': True, 'user_id': session.user_id}, 200
class DeleteSession(APIResource):
def delete(self):
# Ensure that the client is authenticated with the session id they would like to check.
authenticated, session_id = self.get_authentication()
if not authenticated:
return {}, 401
# The user is authenticated. Now, get their session id.
session = database.get_session(session_id)
if not session:
return {'details': 'The specified session does not exist.'}, 400
database.delete_session(session.id)
return {}
class CreateApplication(APIResource):
required_parameters = ['name', 'package_name', 'type', 'description', 'release_date', 'early_access',
'supported_platforms', 'genres', 'tags', 'base_price']
def post(self):
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Ensure that the user has developer permissions.
if not user:
return {'details': 'The session\'s user does not exist.'}, 400
if not user.has_developer_permissions():
return {'details': 'You are not a developer!'}, 403
# Get the parameters.
name: str = request.form.get('name')
package_name: str = request.form.get('package_name')
type_: str = request.form.get('type')
description: str = request.form.get('description')
release_date: date = datetime.strptime(request.form.get('release_date'), '%Y-%m-%d').date()
early_access: bool = Utils.safe_bool_cast(request.form.get('early_access'))
latest_version: str = '' # Empty, signifying that there is no version available (at this time).
supported_platforms: list = request.form.get('supported_platforms').split(',')
genres: list = request.form.get('genres').split(',')
tags: list = request.form.get('tags').split(',')
base_price: float = Utils.safe_float_cast(request.form.get('base_price'))
owners: list = [str(user.id)]
# Attempt to create the application.
success, response = database.create_application(
name,
package_name,
type_,
description,
release_date,
early_access,
latest_version,
supported_platforms,
genres,
tags,
base_price,
owners
)
# Create the application's data folder (for storing versions).
file_manager.create_application_folder(package_name)
if not success:
return response, 400
return {'details': 'Successfully created application.', 'application_id': response['application_id']}, 201
class GetApplication(APIResource):
required_parameters = ['application_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
# Get the application.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
return application.into_dict(user.administrator or user.id in application.owners), 200
class UpdateApplicationVersion(APIResource):
required_parameters = ['application_id', 'version']
def put(self):
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Ensure that the user has developer permissions.
if not user.has_developer_permissions():
return {'details': 'You are not a developer!'}, 403
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
version: str = request.form.get('version')
# Ensure that the application exists.
# Get the application.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
# Ensure that the user is an owner of this application.
if not (user.id in application.owners or user.administrator):
return {'details': 'This is not your application; you cannot update its version.'}, 403
database.update_application_property(application_id, 'latest_version', version)
return {}
class GetApplicationVersions(APIResource):
required_parameters = ['application_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
# Verify that the user owns the specified application.
if not database_utils.user_owns(user.id, application_id):
return {'details': 'You do not own this application.'}, 403
versions: list[ApplicationVersion]
if 'platform' in request.form:
versions = database.get_application_versions_for_platform(application_id, str(request.form.get('platform')))
else:
versions = database.get_application_versions(application_id)
return {'versions': Utils.serialize(versions)}, 200
class DownloadApplicationVersion(APIResource):
required_parameters = ['version_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
version_id: int = Utils.safe_int_cast(request.form.get('version_id'))
# Get the version.
version = database.get_application_version_by_id(version_id)
if not version:
return {'details': 'The specified version does not exist.'}, 400
# Verify that the user owns the specified application.
if not database_utils.user_owns(user.id, version.application_id):
return {'details': 'You do not own this application.'}, 403
return send_file(file_manager.get_version_filepath(version_id))
class CreateVersion(APIResource):
required_parameters = ['application_id', 'name', 'platform', 'release_date', 'filename', 'executable']
required_files = ['file']
def post(self):
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
if not user.has_developer_permissions():
return {'details': 'You are not a developer!'}, 403
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
name: str = request.form.get('name')
platform: str = request.form.get('platform')
release_date: date = datetime.strptime(request.form.get('release_date'), '%Y-%m-%d').date()
filename: str = secure_filename(Utils.generate_uuid4() + '_' + request.form.get('filename'))
executable = request.form.get('executable')
# Ensure that the application exists.
# Get the application.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
# Ensure that the user is an owner of this application.
if not (user.id in application.owners or user.administrator):
return {'details': 'This is not your application; you cannot push versions to it.'}, 403
# Everything is good; create the version.
# Ensure that a file was provided.
missing_files, missing_files_list = self.missing_files()
if missing_files:
return {'details': 'Please provide a version file.'}, 400
# Create the version entry in the database first to make sure there is not an issue.
success, response = database.create_application_version(
application_id,
name,
platform,
release_date,
filename,
executable
)
# Handle errors.
if not success:
return response, 400
# No issues creating the version; upload the file.
# Get the version file.
version_file = request.files['file']
# Save the version file.
version_file.save(file_manager.generate_version_filepath(application.package_name, filename))
return response, 200
class GetVersion(APIResource):
required_parameters = ['application_id', 'version_name', 'platform']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
version_name: str = request.form.get('version_name')
platform: str = request.form.get('platform')
# Verify that the user owns the specified application.
if not database_utils.user_owns(user.id, application_id):
return {'details': 'You do not own this application.'}, 403
# Get the version.
version = database.get_application_version(application_id, version_name, platform)
if not version:
return {'details': 'The specified version does not exist.'}, 400
# Get the application.
application = database.get_application(application_id)
return version.into_dict(user.administrator or user.id in application.owners), 200
class CreateSale(APIResource):
required_parameters = ['application_id', 'title', 'description', 'price', 'start_date', 'end_date']
def post(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
if not user.has_developer_permissions():
return {'details': 'You are not a developer!'}, 403
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
title: str = request.form.get('title')
description: str = request.form.get('description')
price: float = Utils.safe_float_cast(request.form.get('price'))
start_date: date = datetime.strptime(request.form.get('start_date'), '%Y-%m-%d').date()
end_date: date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d').date()
# Ensure that the application exists.
# Get the application.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
# Ensure that the user is an owner of this application.
if not (user.id in application.owners or user.administrator):
return {'details': 'This is not your application; you cannot create sales for it.'}, 403
success, response = database.create_sale(application_id, title, description, price, start_date, end_date)
if success:
return response, 200
return response, 400
class GetActiveSale(APIResource):
required_parameters = ['application_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
# Ensure that the application exists.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
# Attempt to find the active sale of a specific application.
sale = database.get_active_sale(application_id, date.today())
if not sale:
return {'details': 'The specified application is not currently on sale.'}, 400
return sale.into_dict(), 200
class GetAllActiveSales(APIResource):
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
sales: list[Sale] = []
# Loop through all applications.
for application in database.get_all_applications():
# Attempt to grab the currently active sale for the application (if any).
active_sale = database.get_active_sale(application.id, date.today())
# If there is an active sale, add it to the list.
if active_sale:
sales.append(active_sale)
return {'sales': Utils.serialize(sales)}, 200
class DeleteSale(APIResource):
required_parameters = ['sale_id']
def delete(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
if not user.has_developer_permissions():
return {'details': 'You are not a developer!'}, 403
# Get the parameters.
sale_id: int = Utils.safe_int_cast(request.form.get('sale_id'))
# Ensure that the sale exists.
# Get the sale.
sale = database.get_sale(sale_id)
if not sale:
return {'details': 'The specified sale does not exist.'}, 400
# Get the application.
application = database.get_application(sale.application_id)
# Ensure that the user is an owner of this application.
if not (user.id in application.owners or user.administrator):
return {'details': 'This is not your application; you cannot create sales for it.'}, 403
# Delete the sale.
success, response = database.delete_sale(sale_id)
if success:
return response, 200
return response, 400
class GetTransactions(APIResource):
required_parameters = ['user_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
user_id: int = Utils.safe_int_cast(request.form.get('user_id'))
# Ensure that the user exists.
target_user = database.get_user(user_id)
if not target_user:
return {'details': 'The specified user does not exist.'}, 400
# Ensure that the user requesting to fetch the transactions has the proper authority to do so.
if not user.is_or_admin(user_id):
return {'details': 'You do not have the authority to access this user\'s transaction records.'}, 403
transactions: list[Transaction] = []
# Get the user's transactions.
if 'application_id' in request.form:
transactions = database.get_user_transactions_in(user_id,
Utils.safe_int_cast(request.form.get('application_id')))
else:
transactions = database.get_user_transactions(user_id)
return {'transactions': Utils.serialize(transactions, True)}, 200
class GetTransaction(APIResource):
required_parameters = ['transaction_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
transaction_id: int = Utils.safe_int_cast(request.form.get('transaction_id'))
# Ensure that the transaction exists.
transaction = database.get_transaction(transaction_id)
if not transaction:
return {'details': 'The specified transaction does not exist.'}, 400
print(f'{user.id} ?= {transaction.user_id}')
# Ensure that the user requesting the transaction has the proper authority to view it.
if not user.is_or_admin(transaction.user_id):
return {'details': 'You do not have the authority to access this user\'s transaction(s).'}, 403
return Utils.serialize(transaction, True), 200
class GetPurchase(APIResource):
required_parameters = ['purchase_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters
purchase_id: int = Utils.safe_int_cast(request.form.get('purchase_id'))
# Ensure that the purchase exists.
purchase = database.get_purchase(purchase_id)
if not purchase:
return {'details': 'The specified purchase does not exist.'}, 400
# Ensure that the user has the proper authority to access this purchase.
if not user.is_or_admin(database_utils.get_purchase_source(purchase_id)):
return {'details': 'You do not have the authority to access this user\'s purchase(s).'}, 403
return Utils.serialize(purchase, True), 200
class GetDeposit(APIResource):
required_parameters = ['deposit_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
deposit_id: int = Utils.safe_int_cast(request.form.get('deposit_id'))
# Ensure that the deposit exists.
deposit = database.get_deposit(deposit_id)
if not deposit:
return {'details': 'The specified deposit does not exist.'}, 400
# Ensure that the user has the authority to view the requested deposit.
if not user.is_or_admin(deposit.user_id):
return {'details': 'You do not have the authority to access this user\'s deposit(s).'}, 403
return Utils.serialize(deposit, True), 200
class GetApplicationKey(APIResource):
required_parameters = ['key']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
key = request.form.get('key')
# Attempt to get the application key.
application_key = database.get_application_key(key)
if not application_key:
return {'details': 'The specified application key does not exist.'}, 400
# Ensure the user has the authority to access this application key.
if not user.is_or_admin(application_key.user_id):
return {'details': 'You do not have the authority to access this user\'s application key(s).'}, 403
return Utils.serialize(application_key, True), 200
class GetApplicationKeys(APIResource):
required_parameters = ['user_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
user_id: int = Utils.safe_int_cast(request.form.get('user_id'))
# Ensure that the user exists.
target_user = database.get_user(user_id)
if not target_user:
return {'details': 'The specified user does not exist.'}, 400
# Ensure that the user has authority to access the authentication keys.
if not user.is_or_admin(user_id):
return {'details': 'You do not have the authority to access this user\'s application key(s).'}, 403
# Get the authentication keys.
application_keys: list[ApplicationKey] = database.get_user_application_keys(user_id)
return {'application_keys': Utils.serialize(application_keys, True)}, 200
class PurchaseApplication(APIResource):
required_parameters = ['application_id']
def post(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
# Ensure that the application exists.
application = database.get_application(application_id)
if not application:
return {'details': 'The specified application does not exist.'}, 400
# Get the optional parameters.
for_user_id: int = Utils.safe_int_cast(request.form.get('for_user_id')) \
if 'for_user_id' in request.form \
else user.id
# Attempt to purchase the application.
success, response = database_utils.purchase(user.id, application_id, for_user_id)
if not success:
return response, 400
return {'details': 'Successfully purchased application.', 'transaction_id': response['transaction_id']}, 200
class PurchaseIAP(APIResource):
required_parameters = ['iap_id']
def post(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
iap_id: int = Utils.safe_int_cast(request.form.get('iap_id'))
# Ensure that the iap exists.
iap = database.get_iap(iap_id)
if not iap:
return {'details': 'The specified iap does not exist.'}, 400
# Get the optional parameters.
for_user_id: int = Utils.safe_int_cast(request.form.get('for_user_id')) \
if 'for_user_id' in request.form \
else user.id
success, response = database_utils.purchase(user.id, iap_id, for_user_id, iap_id)
if not success:
return response, 400
return {'details': 'Successfully purchased iap.', 'transaction_id': response['transaction_id']}, 200
class GetIAPRecords(APIResource):
required_parameters = ['user_id', 'application_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
user_id: int = Utils.safe_int_cast(request.form.get('user_id'))
application_id: int = Utils.safe_int_cast(request.form.get('application_id'))
# Get the optional parameters.
only_unacknowledged: bool = Utils.safe_bool_cast(request.form.get('only_unacknowledged')) \
if 'only_unacknowledged' in request.form \
else False
# Ensure that the user exists.
target_user = database.get_user(user_id)
if not target_user:
return {'details': 'The specified user does not exist.'}, 400
# Ensure that the user has the authority to view this user's iap records.
if not user.is_or_admin(target_user.id):
return {'details': 'You do not have the authority to access this user\'s iap records(s).'}, 403
# Get the iap records.
records: list[IAPRecord] = database.get_iap_records(user_id, application_id, only_unacknowledged)
return {'iap_records': Utils.serialize(records, True)}, 200
class GetSession(APIResource):
required_parameters = ['session_id']
def get(self):
missing, parameters = self.missing_parameters()
if missing:
return {'missing_parameters': parameters}, 400
success, response, response_code, session, user = self.verify_session(database)
if not success:
return response, response_code
# Get the parameters.
session_id: str = request.form.get('session_id')
# Ensure that the session exists.
session = database.get_session(session_id)