Files
notifications-admin/notifications_utils/base64_uuid.py
Carlo Costino 57e9fc9ba1 Localize notification_utils to the Admin!
This changeset copies everything in notifications_utils to turn into a local project module.  It includes dependency changes to account for this adjustment and the notifications_utils tests.  It also fixes all of the import statements.

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2024-04-05 12:51:31 -04:00

23 lines
588 B
Python

from base64 import urlsafe_b64decode, urlsafe_b64encode
from uuid import UUID
def base64_to_bytes(key):
return urlsafe_b64decode(key + "==")
def bytes_to_base64(bytes):
# remove trailing = to save precious bytes
return urlsafe_b64encode(bytes).decode("ascii").rstrip("=")
def base64_to_uuid(value):
# uuids are 16 bytes, and will always have two ==s of padding
return UUID(bytes=urlsafe_b64decode(value.encode("ascii") + b"=="))
def uuid_to_base64(value):
if not isinstance(value, UUID):
value = UUID(value)
return bytes_to_base64(value.bytes)