Adds crypto decoding for payment and canceled status

clubs
Laurent 1 year ago
parent 92171532f2
commit a8ca8b1faf
  1. 2
      .gitignore
  2. 1
      requirements.txt
  3. 2
      tournaments/admin.py
  4. 22
      tournaments/models/tournament.py
  5. 22
      tournaments/utils/crypto.py

2
.gitignore vendored

@ -6,6 +6,8 @@ static/
myenv/ myenv/
tournaments/config_local.py
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

@ -3,3 +3,4 @@ djangorestframework==3.14.0
psycopg2-binary==2.9.9 psycopg2-binary==2.9.9
dj-rest-auth==5.1.0 dj-rest-auth==5.1.0
django-qr-code==4.0.1 django-qr-code==4.0.1
pycryptodome==3.20.0

@ -39,7 +39,7 @@ class TeamRegistrationAdmin(admin.ModelAdmin):
list_display = ['player_names', 'group_stage_position', 'name', 'tournament'] list_display = ['player_names', 'group_stage_position', 'name', 'tournament']
class TournamentAdmin(admin.ModelAdmin): 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): class TeamScoreAdmin(admin.ModelAdmin):
list_display = ['team_registration', 'match', 'score', 'walk_out'] list_display = ['team_registration', 'match', 'score', 'walk_out']

@ -7,7 +7,8 @@ from . import Event, TournamentPayment, FederalMatchCategory, FederalCategory, F
import uuid import uuid
from django.utils import timezone, formats from django.utils import timezone, formats
from datetime import datetime, timedelta from datetime import datetime, timedelta
from .. import config_local
from ..utils.crypto import decrypt_aes_gcm
class TeamSortingType(models.IntegerChoices): class TeamSortingType(models.IntegerChoices):
RANK = 1, 'Rank' RANK = 1, 'Rank'
@ -61,6 +62,25 @@ class Tournament(models.Model):
else: else:
return self.display_name() 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): def display_name(self):
if self.name: if self.name:
return self.base_name() + " " + self.name return self.base_name() + " " + self.name

@ -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
Loading…
Cancel
Save