示例#1
0
def get_permissions(actor_id):
    """ Return all permissions for an actor
    :param actor_id:
    :return:
    """
    try:
        return permissions_store[actor_id]
    except KeyError:
        raise errors.PermissionsException("Actor {} does not exist".format(actor_id))
示例#2
0
    def check_and_redeem_nonce(cls, actor_id, nonce_id, level):
        """
        Atomically, check for the existence of a nonce for a given actor_id and redeem it if it
        has not expired. Otherwise, raises PermissionsError. 
        """
        def _transaction(nonces):
            """
            This function can be passed to nonce_store.within_transaction() to atomically check 
            whether a nonce is expired and, if not, redeem a use. The parameter, nonces, should
            be the value under the key `actor_id` associated with the nonce.
            """
            # first pull the nonce from the nonces parameter
            try:
                nonce = nonces[nonce_id]
            except KeyError:
                raise errors.PermissionsException("Nonce does not exist.")
            # check if the nonce level is sufficient
            try:
                if PermissionLevel(nonce['level']) < level:
                    raise errors.PermissionsException(
                        "Nonce does not have sufficient permissions level.")
            except KeyError:
                raise errors.PermissionsException(
                    "Nonce did not have an associated level.")

            # check if there are remaining uses
            try:
                if nonce['remaining_uses'] == -1:
                    logger.debug("nonce has infinite uses. updating nonce.")
                    nonce['current_uses'] += 1
                    nonce['last_use_time'] = get_current_utc_time()
                    nonce_store.update(actor_id, nonce_id, nonce)
                elif nonce['remaining_uses'] > 0:
                    logger.debug(
                        "nonce still has uses remaining. updating nonce.")
                    nonce['current_uses'] += 1
                    nonce['remaining_uses'] -= 1
                    nonce_store.update(actor_id, nonce_id, nonce)
                else:
                    logger.debug(
                        "nonce did not have at least 1 use remaining.")
                    raise errors.PermissionsException(
                        "No remaining uses left for this nonce.")
            except KeyError:
                logger.debug("nonce did not have a remaining_uses attribute.")
                raise errors.PermissionsException(
                    "No remaining uses left for this nonce.")

        # first, make sure the nonce exists for the actor id:
        try:
            nonce_store[actor_id][nonce_id]
        except KeyError:
            raise errors.PermissionsException("Nonce does not exist.")
        # atomically, check if the nonce is still valid and add a use if so:
        nonce_store.within_transaction(_transaction, actor_id)