diff --git a/.gitignore b/.gitignore index 2e6c6a2..07226dd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ static/ myenv/ +tournaments/config_local.py + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/requirements.txt b/requirements.txt index f2dc5ad..30a0bf5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ djangorestframework==3.14.0 psycopg2-binary==2.9.9 dj-rest-auth==5.1.0 django-qr-code==4.0.1 +pycryptodome==3.20.0 diff --git a/tournaments/admin.py b/tournaments/admin.py index 845d1c0..bba13c7 100644 --- a/tournaments/admin.py +++ b/tournaments/admin.py @@ -39,7 +39,7 @@ class TeamRegistrationAdmin(admin.ModelAdmin): list_display = ['player_names', 'group_stage_position', 'name', 'tournament'] class TournamentAdmin(admin.ModelAdmin): - list_display = ['display_name', 'event', 'is_private', 'start_date'] + list_display = ['display_name', 'event', 'is_private', 'start_date', 'payment', 'is_canceled'] class TeamScoreAdmin(admin.ModelAdmin): list_display = ['team_registration', 'match', 'score', 'walk_out'] diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 59590ab..d3e4439 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -7,7 +7,8 @@ from . import Event, TournamentPayment, FederalMatchCategory, FederalCategory, F import uuid from django.utils import timezone, formats from datetime import datetime, timedelta - +from .. import config_local +from ..utils.crypto import decrypt_aes_gcm class TeamSortingType(models.IntegerChoices): RANK = 1, 'Rank' @@ -61,6 +62,25 @@ class Tournament(models.Model): else: return self.display_name() + def is_canceled(self): + if self.local_id and config_local.CRYPTO_KEY: + decrypted = decrypt_aes_gcm(self.local_id, config_local.CRYPTO_KEY) + value = int(decrypted[18]) + if 0 <= value <= 4: + return True + else: + return False + else: + return False + + def payment(self): + if self.global_id and config_local.CRYPTO_KEY: + decrypted = decrypt_aes_gcm(self.global_id, config_local.CRYPTO_KEY) + value = int(decrypted[18]) + return TournamentPayment(value) + else: + return None + def display_name(self): if self.name: return self.base_name() + " " + self.name diff --git a/tournaments/utils/crypto.py b/tournaments/utils/crypto.py new file mode 100644 index 0000000..1a69d30 --- /dev/null +++ b/tournaments/utils/crypto.py @@ -0,0 +1,22 @@ +from Crypto.Cipher import AES +import base64 + + + +def decrypt_aes_gcm(encrypted_base64, key_base64): + # Decode the base64 encoded data and key + encrypted_data = base64.b64decode(encrypted_base64) + key = base64.b64decode(key_base64) + + # Extract the nonce, tag, and ciphertext from the combined encrypted data + nonce = encrypted_data[:12] # AES GCM nonce is 12 bytes + tag = encrypted_data[-16:] # AES GCM tag is 16 bytes + ciphertext = encrypted_data[12:-16] # Ciphertext is everything in between + + # Create the cipher object and decrypt the data + cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) + decrypted_data = cipher.decrypt_and_verify(ciphertext, tag) + + # Convert decrypted bytes to string (assuming UTF-8 encoding) + decrypted_text = decrypted_data.decode('utf-8') + return decrypted_text