def test_set_and_get_keyring_expired(self):
        cl = httpclient.HTTPClient(username=USERNAME,
                                   password=PASSWORD,
                                   tenant_id=TENANT_ID,
                                   auth_url=AUTH_URL,
                                   use_keyring=True)

        # set an expired token into the keyring
        auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        expired = timeutils.utcnow() - datetime.timedelta(minutes=30)
        auth_ref['token']['expires'] = timeutils.isotime(expired)
        self.memory_keyring.password = pickle.dumps(auth_ref)

        # stub and check that a new token is received, so not using expired
        with mock.patch.object(cl, 'get_raw_token_from_identity_service') \
                as meth:
            meth.return_value = (True, PROJECT_SCOPED_TOKEN)

            self.assertTrue(cl.authenticate())

            meth.assert_called_once()

        # check that a value was returned from the keyring
        self.assertTrue(self.memory_keyring.fetched)

        # check that the new token has been loaded into the keyring
        new_auth_ref = pickle.loads(self.memory_keyring.password)
        self.assertEqual(new_auth_ref['token']['expires'],
                         PROJECT_SCOPED_TOKEN['access']['token']['expires'])
    def test_set_and_get_keyring_expired(self):
        cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
                                   tenant_id=TENANT_ID, auth_url=AUTH_URL,
                                   use_keyring=True)

        # set an expired token into the keyring
        auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        expired = timeutils.utcnow() - datetime.timedelta(minutes=30)
        auth_ref['token']['expires'] = timeutils.isotime(expired)
        self.memory_keyring.password = pickle.dumps(auth_ref)

        # stub and check that a new token is received, so not using expired
        with mock.patch.object(cl, 'get_raw_token_from_identity_service') \
                as meth:
            meth.return_value = (True, PROJECT_SCOPED_TOKEN)

            self.assertTrue(cl.authenticate())

            self.assertEqual(1, meth.call_count)

        # check that a value was returned from the keyring
        self.assertTrue(self.memory_keyring.fetched)

        # check that the new token has been loaded into the keyring
        new_auth_ref = pickle.loads(self.memory_keyring.password)
        self.assertEqual(new_auth_ref['token']['expires'],
                         PROJECT_SCOPED_TOKEN['access']['token']['expires'])
Exemple #3
0
    def create(self, trustee_user, trustor_user, role_names=None,
               project=None, impersonation=False, expires_at=None):
        """Create a Trust.
        :param string trustee_user: user who's authorization is being delegated
        :param string trustor_user: user who is capable of consuming the trust
        :param string role_names: subset of trustor's roles to be granted
        :param string project: project which the trustor is delegating
        :param boolean impersonation: enable explicit impersonation
        :param datetime.datetime expires_at: expiry time
        """
        # Convert role_names list into list-of-dict API format
        if role_names:
            roles = [{'name': n} for n in role_names]
        else:
            roles = None

        # Convert datetime.datetime expires_at to iso format string
        if expires_at:
            expires_str = timeutils.isotime(at=expires_at, subsecond=True)
        else:
            expires_str = None

        return super(TrustManager, self).create(
            expires_at=expires_str,
            impersonation=impersonation,
            project_id=base.getid(project),
            roles=roles,
            trustee_user_id=base.getid(trustee_user),
            trustor_user_id=base.getid(trustor_user))
Exemple #4
0
 def to_dict(self):
     keys = ['user_id', 'role_id', 'domain_id', 'project_id']
     event = dict((key, self.__dict__[key]) for key in keys
                  if self.__dict__[key] is not None)
     if self.trust_id is not None:
         event['OS-TRUST:trust_id'] = self.trust_id
     if self.consumer_id is not None:
         event['OS-OAUTH1:consumer_id'] = self.consumer_id
     if self.consumer_id is not None:
         event['OS-OAUTH1:access_token_id'] = self.access_token_id
     if self.expires_at is not None:
         event['expires_at'] = timeutils.isotime(self.expires_at,
                                                 subsecond=True)
     if self.issued_before is not None:
         event['issued_before'] = timeutils.isotime(self.issued_before,
                                                    subsecond=True)
     return event
 def to_dict(self):
     keys = ['user_id',
             'role_id',
             'domain_id',
             'project_id']
     event = dict((key, self.__dict__[key]) for key in keys
                  if self.__dict__[key] is not None)
     if self.trust_id is not None:
         event['OS-TRUST:trust_id'] = self.trust_id
     if self.consumer_id is not None:
         event['OS-OAUTH1:consumer_id'] = self.consumer_id
     if self.consumer_id is not None:
         event['OS-OAUTH1:access_token_id'] = self.access_token_id
     if self.expires_at is not None:
         event['expires_at'] = timeutils.isotime(self.expires_at,
                                                 subsecond=True)
     if self.issued_before is not None:
         event['issued_before'] = timeutils.isotime(self.issued_before,
                                                    subsecond=True)
     return event
def confirm_token_not_expired(data):
    if not data:
        raise InvalidUserToken('Token authorization failed')
    if _token_is_v2(data):
        timestamp = data['access']['token']['expires']
    elif _token_is_v3(data):
        timestamp = data['token']['expires_at']
    else:
        raise InvalidUserToken('Token authorization failed')
    expires = timeutils.parse_isotime(timestamp)
    expires = timeutils.normalize_time(expires)
    utcnow = timeutils.utcnow()
    if utcnow >= expires:
        raise InvalidUserToken('Token authorization failed')
    return timeutils.isotime(at=expires, subsecond=True)
    def test_get_keyring(self):
        cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
                                   tenant_id=TENANT_ID, auth_url=AUTH_URL,
                                   use_keyring=True)

        # set an token into the keyring
        auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        future = timeutils.utcnow() + datetime.timedelta(minutes=30)
        auth_ref['token']['expires'] = timeutils.isotime(future)
        self.memory_keyring.password = pickle.dumps(auth_ref)

        # don't stub get_raw_token so will fail if authenticate happens

        self.assertTrue(cl.authenticate())
        self.assertTrue(self.memory_keyring.fetched)
def confirm_token_not_expired(data):
    if not data:
        raise InvalidUserToken('Token authorization failed')
    if _token_is_v2(data):
        timestamp = data['access']['token']['expires']
    elif _token_is_v3(data):
        timestamp = data['token']['expires_at']
    else:
        raise InvalidUserToken('Token authorization failed')
    expires = timeutils.parse_isotime(timestamp)
    expires = timeutils.normalize_time(expires)
    utcnow = timeutils.utcnow()
    if utcnow >= expires:
        raise InvalidUserToken('Token authorization failed')
    return timeutils.isotime(at=expires, subsecond=True)
    def test_get_keyring(self):
        cl = httpclient.HTTPClient(username=USERNAME,
                                   password=PASSWORD,
                                   tenant_id=TENANT_ID,
                                   auth_url=AUTH_URL,
                                   use_keyring=True)

        # set an token into the keyring
        auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        future = timeutils.utcnow() + datetime.timedelta(minutes=30)
        auth_ref['token']['expires'] = timeutils.isotime(future)
        self.memory_keyring.password = pickle.dumps(auth_ref)

        # don't stub get_raw_token so will fail if authenticate happens

        self.assertTrue(cl.authenticate())
        self.assertTrue(self.memory_keyring.fetched)
    def test_set_and_get_keyring_expired(self):
        cl = httpclient.HTTPClient(username=USERNAME, password=PASSWORD,
                                   tenant_id=TENANT_ID, auth_url=AUTH_URL,
                                   use_keyring=True)
        keyring_key = cl._build_keyring_key(auth_url=AUTH_URL,
                                            username=USERNAME,
                                            tenant_name=TENANT,
                                            tenant_id=TENANT_ID,
                                            token=TOKEN)

        cl.auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        expired = timeutils.utcnow() - datetime.timedelta(minutes=30)
        cl.auth_ref['token']['expires'] = timeutils.isotime(expired)
        cl.store_auth_ref_into_keyring(keyring_key)
        (keyring_key, auth_ref) = cl.get_auth_ref_from_keyring(
            auth_url=AUTH_URL,
            username=USERNAME,
            tenant_name=TENANT,
            tenant_id=TENANT_ID,
            token=TOKEN)
        self.assertIsNone(auth_ref)
    def test_set_and_get_keyring_expired(self):
        cl = client.HTTPClient(username=USERNAME,
                               password=PASSWORD,
                               tenant_id=TENANT_ID,
                               auth_url=AUTH_URL,
                               use_keyring=True)
        keyring_key = cl._build_keyring_key(auth_url=AUTH_URL,
                                            username=USERNAME,
                                            tenant_name=TENANT,
                                            tenant_id=TENANT_ID,
                                            token=TOKEN)

        cl.auth_ref = access.AccessInfo.factory(body=PROJECT_SCOPED_TOKEN)
        expired = timeutils.utcnow() - datetime.timedelta(minutes=30)
        cl.auth_ref['token']['expires'] = timeutils.isotime(expired)
        cl.store_auth_ref_into_keyring(keyring_key)
        (keyring_key,
         auth_ref) = cl.get_auth_ref_from_keyring(auth_url=AUTH_URL,
                                                  username=USERNAME,
                                                  tenant_name=TENANT,
                                                  tenant_id=TENANT_ID,
                                                  token=TOKEN)
        self.assertIsNone(auth_ref)
 def issued(self, value):
     self.issued_str = timeutils.isotime(value, subsecond=True)
Exemple #13
0
 def expires(self, value):
     self.expires_str = timeutils.isotime(value)
Exemple #14
0
 def issued(self, value):
     self.issued_str = timeutils.isotime(value, subsecond=True)
Exemple #15
0
 def expires(self, value):
     self.expires_str = timeutils.isotime(value, subsecond=True)
 def updated(self, value):
     self.updated_str = timeutils.isotime(value)
 def expires(self, value):
     self.expires_str = timeutils.isotime(value, subsecond=True)
Exemple #18
0
 def issued(self, value):
     self.issued_str = timeutils.isotime(value)
Exemple #19
0
 def expires(self, value):
     self.expires_str = timeutils.isotime(value)
 def updated(self, value):
     self.updated_str = timeutils.isotime(value)
Exemple #21
0
 def issued(self, value):
     self.issued_str = timeutils.isotime(value)