Generating unique and opaque user IDs in Google App Engine

后端 未结 3 1045
终归单人心
终归单人心 2021-02-04 13:42

I\'m working on an application that lets registered users create or upload content, and allows anonymous users to view that content and browse registered users\' pages to find t

相关标签:
3条回答
  • 2021-02-04 14:17

    Your timing is impeccable: Just yesterday, a new release of the SDK came out, with support for unique, permanent user IDs. They meet all the criteria you specified.

    0 讨论(0)
  • 2021-02-04 14:22

    I think you should distinguish between two types of users:

    1) users that have logged in via Google Accounts or that have already registered on your site with a non-google e-mail address

    2) users that opened your site for the first time and are not logged in in any way

    For the second case, I can see no other way than to generate some random string (e.g. via uuid.uuid4() or from this user's session cookie key), as an anonymous user does not carry any unique information with himself.

    For users that are logged in, however, you already have a unique identifier -- their e-mail address. I agree with your privacy concerns -- you shouldn't use it as an identifier. Instead, how about generating a string that seems random, but is in fact generated from the e-mail address? Hashing functions are perfect for this purpose. Example:

    >>> import hashlib
    
    >>> email = 'user@host.com'
    >>> salt = 'SomeLongStringThatWillBeAppendedToEachEmail'
    
    >>> key = hashlib.sha1('%s$%s' % (email, salt)).hexdigest()
    >>> print key
    f6cd3459f9a39c97635c652884b3e328f05be0f7
    

    As hashlib.sha1 is not a random function, but for given data returns always the same result, but it is proven to be practically irreversible, you can safely present the hashed key on the website without compromising user's e-mail address. Also, you can safely assume that no two hashes of distinct e-mails will be the same (they can be, but probability of it happening is very, very small). For more information on hashing functions, consult the Wikipedia entry.

    0 讨论(0)
  • 2021-02-04 14:43

    Do you mean session cookies?

    Try http://code.google.com/p/gaeutilities/


    What DzinX said. The only way to create an opaque key that can be authenticated without a database roundtrip is using encryption or a cryptographic hash.

    Give the user a random number and hash it or encrypt it with a private key. You still run the (tiny) risk of collisions, but you can avoid this by touching the database on key creation, changing the random number in case of a collision. Make sure the random number is cryptographic, and add a long server-side random number to prevent chosen plaintext attacks.

    You'll end up with a token like the Google Docs key, basically a signature proving the user is authenticated, which can be verified without touching the database.

    However, given the pricing of GAE and the speed of bigtable, you're probably better off using a session ID if you really can't use Google's own authentication.

    0 讨论(0)
提交回复
热议问题