Example #1
0
 async def _db_refresh(self) -> None:
     """Fetch the latest values from the database"""
     async with self._application.postgres_connector(
             on_error=self.on_postgres_error) as conn:
         result = await conn.execute(self.SQL_REFRESH,
                                     {'username': self.username},
                                     'user-refresh')
         self._assign_values(result.row)
     self.groups = await self._db_groups()
     self.last_refreshed_at = max(timestamp.utcnow(), self.last_seen_at
                                  or timestamp.utcnow())
Example #2
0
    async def _ldap_refresh(self) -> None:
        """Update the attributes from LDAP"""
        LOGGER.debug('Refreshing attributes from LDAP server')
        attrs = await self._ldap.attributes(self._conn, self.external_id)
        LOGGER.debug('Attributes: %r', attrs)
        self.display_name = attrs.get('displayName', attrs['cn'])
        self.email_address = attrs.get('mail')

        async with self._application.postgres_connector(
                on_error=self.on_postgres_error) as conn:
            result = await conn.execute(
                self.SQL_UPDATE_USER_FROM_LDAP, {
                    'username': self.username,
                    'user_type': 'ldap',
                    'external_id': self.external_id,
                    'display_name': self.display_name,
                    'email_address': self.email_address
                }, 'user-update')
            self.last_seen_at = result.row['last_seen_at']

        # Update the groups in the database and get the group names
        ldap_groups = await self._ldap.groups(self._conn, self.external_id)
        async with self._application.postgres_connector(
                on_error=self.on_postgres_error) as conn:
            await conn.callproc(
                'v1.maintain_group_membership_from_ldap_groups',
                (self.username, ldap_groups), 'user-maintain-groups')

        # Update the groups attribute
        self.groups = await self._db_groups()
        self.permissions = sorted(
            set(chain.from_iterable([g.permissions for g in self.groups])))
        self.last_refreshed_at = max(timestamp.utcnow(), self.last_seen_at)
Example #3
0
 async def test_should_refresh_true(self):
     user_value = await self.setup_user()
     obj = user.User(self.app, user_value['username'],
                     user_value['password'])
     self.assertTrue(await obj.authenticate())
     self.assertFalse(obj.should_refresh)
     obj.last_refreshed_at = timestamp.utcnow() - (obj.REFRESH_AFTER * 2)
     self.assertTrue(obj.should_refresh)
Example #4
0
 async def setup_user(self, values=None):
     if not values:
         values = {}
     values.setdefault('username', str(uuid.uuid4()))
     values.setdefault('display_name', str(uuid.uuid4()))
     values.setdefault('email_address', '{}@{}'.format(
         str(uuid.uuid4()), str(uuid.uuid4())))
     values.setdefault('last_seen_at', timestamp.utcnow())
     password = values.get('password', str(uuid.uuid4()))
     values['password'] = self._app.encrypt_value('password', password)
     await self.postgres_execute(self.SQL_INSERT_USER, values)
     values['password'] = password
     return values
Example #5
0
 async def test_should_refresh_true(self):
     obj = user.User(self.app, LDAP_USERNAME, LDAP_PASSWORD)
     self.assertTrue(await obj.authenticate())
     self.assertFalse(obj.should_refresh)
     obj.last_refreshed_at = timestamp.utcnow() - (obj.REFRESH_AFTER * 2)
     self.assertTrue(obj.should_refresh)
Example #6
0
 def test_utcnow(self):
     self.assertEqual(
         timestamp.isoformat(timestamp.utcnow()),
         timestamp.isoformat(
             datetime.datetime.now(tz=tz.tzoffset(None, 0))))