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. The secret will have a length of 80. """ # Even a length of 20 has 112 bits of entropy, so uniqueness is a # good assumption. If we generate a duplicate then the DB insertion # will crash, which is desirable because it indicates an RNG issue. key_length = 20 key = create_token(key_length) secret_length = 80 secret = create_token(secret_length) return key, secret
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 allocate(url): """Allocate a token for url path in the librarian. :param url: A url bytestring. e.g. https://i123.restricted.launchpad-librarian.net/123/foo.txt Note that the token is generated for 123/foo.txt :return: A url fragment token ready to be attached to the url. e.g. 'a%20token' """ store = session_store() path = TimeLimitedToken.url_to_token_path(url) token = create_token(32).encode('ascii') store.add(TimeLimitedToken(path, token)) # The session isn't part of the main transaction model, and in fact it # has autocommit on. The commit here is belts and bracers: after # allocation the external librarian must be able to serve the file # immediately. store.commit() return token
def storeSimpleVote(self, person, option, when=None): """See IPoll.""" voter = self._assertEverythingOkAndGetVoter(person, when=when) assert self.type == PollAlgorithm.SIMPLE voteset = getUtility(IVoteSet) if option is None and not self.allowspoilt: raise ValueError("This poll doesn't allow spoilt votes.") elif option is not None: assert option.poll == self, ( "The option %r doesn't belong to this poll" % option) assert option.active, "Option %r is not active" % option token = create_token(20) # This is a simple-style poll, so you can vote only on a single option # and this option's preference must be 1 preference = 1 vote = voteset.new(self, option, preference, token, voter) getUtility(IVoteCastSet).new(self, person) return vote
def storeCondorcetVote(self, person, options, when=None): """See IPoll.""" voter = self._assertEverythingOkAndGetVoter(person, when=when) assert self.type == PollAlgorithm.CONDORCET voteset = getUtility(IVoteSet) token = create_token(20) votes = [] activeoptions = self.getActiveOptions() for option, preference in options.items(): assert option.poll == self, ( "The option %r doesn't belong to this poll" % option) assert option.active, "Option %r is not active" % option votes.append(voteset.new(self, option, preference, token, voter)) # Store a vote with preference = None for each active option of this # poll that wasn't in the options argument. for option in activeoptions: if option not in options: votes.append(voteset.new(self, option, None, token, voter)) getUtility(IVoteCastSet).new(self, person) return votes
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_token(20) return LoginToken(requester=requester, requesteremail=requesteremail, email=email, token=token, tokentype=tokentype, created=UTC_NOW, fingerprint=fingerprint, redirection_url=redirection_url)
def test_length(self): token = create_token(99) self.assertEqual(len(token), 99)
def test_length(self): token = create_token(99) self.assertEquals(len(token), 99)