Rostislav Ruban
Rostislav Ruban

Reputation: 127

Decoding base64 string return None

I tried to generate uid for a user confirmation email.

'uid':urlsafe_base64_encode(force_bytes(user.pk)),

so, it's works nice, it returns something like "Tm9uZQ"

Then, when I tried to decode it, using force_text(urlsafe_base64_decode(uidb64)) it return None.

The next string

urlsafe_base64_decode(uidb64)

also, return b'None'

I tried to google it, and see different implementations, but copy-paste code not works.

I write something like

    b64_string = uidb64
    b64_string += "=" * ((4 - len(b64_string) % 4) % 4)
    print(b64_string)
    print(force_text(base64.urlsafe_b64decode(b64_string)))

and the result still None:

Tm9uZQ==
None

I don't understand how the default decode doesn't work.

Upvotes: 2

Views: 1464

Answers (1)

mcnnowak
mcnnowak

Reputation: 285

"Tm9uZQ==" is the base64 encoding of the string "None",

>>> from base64 import b64encode, b64decode
>>> s = b'None'
>>> 
>>> b64encode(s)
b'Tm9uZQ=='
>>> b64decode(b64encode(s))
b'None'
>>> 

It could be possible that some of your data is missing. E.g. user.pk is not set. I think that force_bytes is turning a None user.pk into the bytestring b'None', from the Django source,

def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
    """
    Similar to smart_bytes, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    """
    # Handle the common case first for performance reasons.
    if isinstance(s, bytes):
        if encoding == 'utf-8':
            return s
        else:
            return s.decode('utf-8', errors).encode(encoding, errors)
    if strings_only and is_protected_type(s):
        return s
    if isinstance(s, memoryview):
        return bytes(s)
    return str(s).encode(encoding, errors)

You might be able to prevent None being turned into b'None' by setting strings_only=True when calling force_bytes.

Upvotes: 4

Related Questions