Giter Site home page Giter Site logo

sesame tokens seem to be missing a bunch of entropy on my Django installation (first characters are all 'AAAAAA' about django-sesame HOT 3 CLOSED

joshburnett avatar joshburnett commented on June 2, 2024
sesame tokens seem to be missing a bunch of entropy on my Django installation (first characters are all 'AAAAAA'

from django-sesame.

Comments (3)

adamchainz avatar adamchainz commented on June 2, 2024

This is expected behaviour - sesame uses the struct library to encode your user primary key: https://github.com/aaugustin/django-sesame/blob/main/src/sesame/packers.py . It then base64 encodes this:

def create_token(user, scope=""):
"""
Create a v2 signed token for a user.
"""
primary_key = packers.packer.pack_pk(user.pk)
timestamp = pack_timestamp()
revocation_key = get_revocation_key(user)
signature = sign(primary_key + timestamp + revocation_key + scope.encode())
# If the revocation key changes, the signature becomes invalid, so we
# don't need to include a hash of the revocation key in the token.
data = primary_key + timestamp + signature
token = base64.urlsafe_b64encode(data).rstrip(b"=")
return token.decode()
.

For BigAutoField, it will encodes 8 bytes, for example for user ID 1 you'll see AAAAAAAAAAE (the trailing = is not required):

>>> import struct
>>> struct.pack('!q', 1)
b'\x00\x00\x00\x00\x00\x00\x00\x01'
>>> import base64
>>> base64.b64encode(struct.pack('!q', 1))
b'AAAAAAAAAAE='

Perhaps the docs examples could be updated to show this. Would you like to make a PR?

from django-sesame.

joshburnett avatar joshburnett commented on June 2, 2024

Ahh, I see. The docs do briefly mention this in the 'Tokens security' section but don't give an example, so I just rolled right over it. I didn't think about it until I noticed that the tokens that I'm generating look very different than the example ones shown.

I'll think about pulling together a PR to put just a little more detail there. I'll probably also investigate how hard it is to write a custom packer, though I'm not sure it's really worth it for my particular low-stakes application.

Thanks again for creating this library, and for clearly already putting a bunch of work into the docs. Overall they are really quite good!

from django-sesame.

aaugustin avatar aaugustin commented on June 2, 2024

This deserves at least an entry in the Infrequently asked questions section of the README.

from django-sesame.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.