Esempio n. 1
0
 def testGetOAuthConsumersWithoutMatches(self):
     """
     L{getOAuthConsumers} returns an empty C{ResultSet} if there are no
     matches for the specified L{User.id}s.
     """
     consumer = getOAuthConsumers(userIDs=[sys.maxint]).one()
     self.assertIdentical(None, consumer)
Esempio n. 2
0
    def decrypt(cls, consumerUser, encryptedToken):
        """Decrypt a token and convert it into a stateful object.

        @param cls: The class representing the token.
        @param consumerUser: The L{User} instance of the consumer that holds
            the token.
        @param: The encrypted token as a C{str}.
        @raise UnknownConsumerError: Raised if C{consumerUser} doesn't have a
            matching L{OAuthConsumer} in the system.
        @raise UnknownUserError: Raised if the L{User} the token provides
            access on behalf of doesn't exist.
        @return: An instance of C{cls}.
        """
        result = getOAuthConsumers(userIDs=[consumerUser.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer."
                                       % consumerUser.username)
        _, consumer = result
        salt = getConfig().get('oauth', cls.configName)
        secret = salt + consumer.secret
        data = tokenToData(secret, encryptedToken)
        username = data['username'].lower()
        user = getUser(username)
        if user is None:
            raise UnknownUserError([username])
        token = cls(consumerUser, user)
        try:
            token.creationTime = datetime.strptime(data['creationTime'],
                                                   '%Y%m%d-%H%M%S')
        except KeyError:
            token.creationTime = None
        return token
Esempio n. 3
0
 def testGetOAuthConsumersWithoutMatches(self):
     """
     L{getOAuthConsumers} returns an empty C{ResultSet} if there are no
     matches for the specified L{User.id}s.
     """
     consumer = getOAuthConsumers(userIDs=[sys.maxint]).one()
     self.assertIdentical(None, consumer)
Esempio n. 4
0
    def decrypt(cls, consumerUser, encryptedToken):
        """Decrypt a token and convert it into a stateful object.

        @param cls: The class representing the token.
        @param consumerUser: The L{User} instance of the consumer that holds
            the token.
        @param: The encrypted token as a C{str}.
        @raise UnknownConsumerError: Raised if C{consumerUser} doesn't have a
            matching L{OAuthConsumer} in the system.
        @raise UnknownUserError: Raised if the L{User} the token provides
            access on behalf of doesn't exist.
        @return: An instance of C{cls}.
        """
        result = getOAuthConsumers(userIDs=[consumerUser.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer." %
                                       consumerUser.username)
        _, consumer = result
        salt = getConfig().get('oauth', cls.configName)
        secret = salt + consumer.secret
        data = tokenToData(secret, encryptedToken)
        username = data['username'].lower()
        user = getUser(username)
        if user is None:
            raise UnknownUserError([username])
        token = cls(consumerUser, user)
        try:
            token.creationTime = datetime.strptime(data['creationTime'],
                                                   '%Y%m%d-%H%M%S')
        except KeyError:
            token.creationTime = None
        return token
Esempio n. 5
0
    def get(self, user):
        """Get the L{OAuthConsumer} associated with the specified L{User}.

        @param user: The L{User} (probably an application) that is a consumer.
        @return: The associated L{OAuthConsumer} instance or C{None} if one
            isn't available.
        """
        result = getOAuthConsumers(userIDs=[user.id]).one()
        if result is not None:
            return result[1]
        return result
Esempio n. 6
0
 def testGetOAuthConsumersFilteredByUserID(self):
     """
     L{getOAuthConsumers} returns the L{User} and L{OAuthConsumer}
     instances that match the specified L{User.id}.
     """
     user1 = createUser(u'user1', u'secret', u'User1', u'*****@*****.**')
     consumer1 = createOAuthConsumer(user1)
     user2 = createUser(u'user2', u'secret', u'User2', u'*****@*****.**')
     createOAuthConsumer(user2)
     self.assertEqual((user1, consumer1),
                      getOAuthConsumers(userIDs=[user1.id]).one())
Esempio n. 7
0
 def testGetOAuthConsumers(self):
     """
     L{getOAuthConsumers} returns all L{OAuthConsumer}s in the database
     when no filtering options are provided.
     """
     user1 = createUser(u'user1', u'secret', u'User1', u'*****@*****.**')
     consumer1 = createOAuthConsumer(user1)
     user2 = createUser(u'user2', u'secret', u'User2', u'*****@*****.**')
     consumer2 = createOAuthConsumer(user2)
     self.assertEqual([(user1, consumer1), (user2, consumer2)],
                      list(getOAuthConsumers().order_by(User.username)))
Esempio n. 8
0
 def testGetOAuthConsumersFilteredByUserID(self):
     """
     L{getOAuthConsumers} returns the L{User} and L{OAuthConsumer}
     instances that match the specified L{User.id}.
     """
     user1 = createUser(u'user1', u'secret', u'User1', u'*****@*****.**')
     consumer1 = createOAuthConsumer(user1)
     user2 = createUser(u'user2', u'secret', u'User2', u'*****@*****.**')
     createOAuthConsumer(user2)
     self.assertEqual((user1, consumer1),
                      getOAuthConsumers(userIDs=[user1.id]).one())
Esempio n. 9
0
 def testGetOAuthConsumers(self):
     """
     L{getOAuthConsumers} returns all L{OAuthConsumer}s in the database
     when no filtering options are provided.
     """
     user1 = createUser(u'user1', u'secret', u'User1', u'*****@*****.**')
     consumer1 = createOAuthConsumer(user1)
     user2 = createUser(u'user2', u'secret', u'User2', u'*****@*****.**')
     consumer2 = createOAuthConsumer(user2)
     self.assertEqual([(user1, consumer1), (user2, consumer2)],
                      list(getOAuthConsumers().order_by(User.username)))
Esempio n. 10
0
    def get(self, user):
        """Get the L{OAuthConsumer} associated with the specified L{User}.

        @param user: The L{User} (probably an application) that is a consumer.
        @return: The associated L{OAuthConsumer} instance or C{None} if one
            isn't available.
        """
        result = getOAuthConsumers(userIDs=[user.id]).one()
        if result is not None:
            return result[1]
        return result
Esempio n. 11
0
    def encrypt(self):
        """Convert this token into an encrypted blob.

        @return: A encrypted token as a C{str}.
        """
        result = getOAuthConsumers(userIDs=[self.consumer.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer."
                                       % self.consumer.username)
        _, consumer = result
        salt = getConfig().get('oauth', self.configName)
        secret = salt + consumer.secret
        creationTime = self.creationTime.strftime('%Y%m%d-%H%M%S')
        return dataToToken(secret, {'username': self.user.username,
                                    'creationTime': creationTime})
Esempio n. 12
0
    def encrypt(self):
        """Convert this token into an encrypted blob.

        @return: A encrypted token as a C{str}.
        """
        result = getOAuthConsumers(userIDs=[self.consumer.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer." %
                                       self.consumer.username)
        _, consumer = result
        salt = getConfig().get('oauth', self.configName)
        secret = salt + consumer.secret
        creationTime = self.creationTime.strftime('%Y%m%d-%H%M%S')
        return dataToToken(secret, {
            'username': self.user.username,
            'creationTime': creationTime
        })
Esempio n. 13
0
    def getRenewalToken(self, consumer, user, now=None):
        """
        Get a renewal token for an L{OAuthConsumer} to generate a new access
        token for a L{User}.

        @param consumer: The L{User} consumer to generate a renewal token for.
        @param user: The L{User} to act on behalf of when the renewal token is
            used.
        @param now: Optionally, a C{datetime.utcnow}-like function, used for
            testing purposes.
        @raise UnknownConsumerError: Raised if C{consumer} doesn't have a
            matching L{OAuthConsumer} in the system.
        @return: An L{OAuthRenewalToken} instance.
        """
        result = getOAuthConsumers(userIDs=[consumer.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer."
                                       % consumer.username)
        return OAuthRenewalToken(consumer, user, now=now)
Esempio n. 14
0
    def getRenewalToken(self, consumer, user, now=None):
        """
        Get a renewal token for an L{OAuthConsumer} to generate a new access
        token for a L{User}.

        @param consumer: The L{User} consumer to generate a renewal token for.
        @param user: The L{User} to act on behalf of when the renewal token is
            used.
        @param now: Optionally, a C{datetime.utcnow}-like function, used for
            testing purposes.
        @raise UnknownConsumerError: Raised if C{consumer} doesn't have a
            matching L{OAuthConsumer} in the system.
        @return: An L{OAuthRenewalToken} instance.
        """
        result = getOAuthConsumers(userIDs=[consumer.id]).one()
        if result is None:
            raise UnknownConsumerError("'%s' is not a consumer." %
                                       consumer.username)
        return OAuthRenewalToken(consumer, user, now=now)