Skip to content

Commit

Permalink
Decode params
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 12, 2023
1 parent 1302c73 commit da7b0b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/heckbot/cogs/picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
from datetime import timedelta
from pathlib import Path
from urllib.parse import quote
from urllib.parse import quote, quote_from_bytes

from discord.ext import commands
from discord.ext.commands import Bot
Expand Down Expand Up @@ -94,7 +94,9 @@ def get_pick_link(user_name: str) -> str:
datetime.utcnow() + timedelta(seconds=TTL)
).isoformat()
token, iv = encrypt(user_name, expiry)
return PICK_SERVER_URL + f'?token={quote(token.decode())}&iv={quote(iv.decode())}'
return (PICK_SERVER_URL +
f'/form?token={quote_from_bytes(token)}'
f'&iv={quote_from_bytes(iv)}')


class Picker(commands.Cog):
Expand Down
14 changes: 6 additions & 8 deletions src/heckbot/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

import os
from datetime import datetime
from datetime import timedelta
Expand All @@ -10,31 +8,31 @@
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers import modes

SECRET_KEY = os.getenv('HECKBOT_SECRET_KEY')
SECRET_KEY = os.getenv('HECKBOT_SECRET_KEY', '').encode()


def encrypt(username: str, expiry: str) -> tuple[bytes, bytes]:
message = f'{username}:{expiry}'.encode()
iv = os.urandom(16)
cipher = Cipher(
algorithms.AES(
SECRET_KEY.encode(),
SECRET_KEY,
), modes.CFB(iv), backend=default_backend(),
)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(message) + encryptor.finalize()
return ciphertext, iv


def decrypt(ciphertext: str, iv: str) -> str | None:
def decrypt(ciphertext: bytes, iv: bytes) -> Optional[str]:
cipher = Cipher(
algorithms.AES(
SECRET_KEY.encode(),
), modes.CFB(iv.encode()), backend=default_backend(),
SECRET_KEY
), modes.CFB(iv), backend=default_backend()
)
decryptor = cipher.decryptor()
decrypted_data = decryptor.update(
ciphertext.encode(),
ciphertext
) + decryptor.finalize()
decoded_data = decrypted_data.decode()
username, timestamp = decoded_data.split(':')
Expand Down

0 comments on commit da7b0b4

Please sign in to comment.