def test_token_uniqueness(self): orig_state = random.getstate() self.addCleanup(lambda: random.setstate(orig_state)) # Calling create_unique_token_for_table() twice with the same # random.seed() will generate two identical tokens, as the token was # never inserted in the table. random.seed(0) token1 = create_unique_token_for_table(99, LoginToken.token) random.seed(0) token2 = create_unique_token_for_table(99, LoginToken.token) self.assertEquals(token1, token2) # Now insert the token in the table so that the next time we call # create_unique_token_for_table() we get a different token. LoginToken( requester=None, token=token2, email='*****@*****.**', tokentype=LoginTokenType.ACCOUNTMERGE, created=UTC_NOW) random.seed(0) token3 = create_unique_token_for_table(99, LoginToken.token) self.assertNotEquals(token1, token3)
def create_token_key_and_secret(table): """Create a key and secret for an OAuth token. :table: The table in which the key/secret are going to be used. Must be one of OAuthAccessToken or OAuthRequestToken. The key will have a length of 20 and we'll make sure it's not yet in the given table. The secret will have a length of 80. """ key_length = 20 key = create_unique_token_for_table(key_length, getattr(table, "key")) secret_length = 80 secret = create_token(secret_length) return key, secret
def new(self, requester, requesteremail, email, tokentype, fingerprint=None, redirection_url=None): """See ILoginTokenSet.""" assert valid_email(email) if tokentype not in LoginTokenType.items: # XXX: Guilherme Salgado, 2005-12-09: # Aha! According to our policy, we shouldn't raise ValueError. raise ValueError( "tokentype is not an item of LoginTokenType: %s" % tokentype) token = create_unique_token_for_table(20, LoginToken.token) return LoginToken(requester=requester, requesteremail=requesteremail, email=email, token=token, tokentype=tokentype, created=UTC_NOW, fingerprint=fingerprint, redirection_url=redirection_url)