Beispiel #1
0
    def load(status=None,
             short_uid=None,
             uid=None,
             scope=None,
             permissions=None):
        """Load and return a LoginSession specified from either a
           short_uid or a long uid. Note that if more than one
           session matches the short_uid then you will get a list
           of LoginSessions returned
        """
        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket

        if short_uid is None and uid is None:
            raise PermissionError(
                "You must supply one of the short_uid or uid to load "
                "a LoginSession!")

        if status is None:
            if uid is None:
                raise PermissionError(
                    "You must supply the full UID to get the status "
                    "of a specific login session")

            status = LoginSession.get_status(uid=uid)
        else:
            if status not in [
                    "approved", "pending", "denied", "suspicious", "logged_out"
            ]:
                raise ValueError("Cannot set an invalid status '%s'" % status)

        bucket = _get_service_account_bucket()

        if uid is not None:
            short_uid = LoginSession.to_short_uid(uid)

            key = "%s/%s/%s/%s" % (_sessions_key, status, short_uid, uid)
            try:
                data = _ObjectStore.get_object_from_json(bucket=bucket,
                                                         key=key)
                session = LoginSession.from_data(data)
            except:
                from Acquire.Identity import LoginSessionError
                raise LoginSessionError(
                    "There is no valid session with UID %s in "
                    "state %s" % (uid, status))

            session._localise(scope=scope, permissions=permissions)
            return session

        # the user may pass in the short_uid as YY.YY.YY.YY
        # so remove all dots
        short_uid = short_uid.replace(".", "")

        prefix = "%s/%s/%s/" % (_sessions_key, status, short_uid)

        try:
            keys = _ObjectStore.get_all_objects_from_json(bucket=bucket,
                                                          prefix=prefix)
        except:
            keys = {}

        sessions = []

        for data in keys.values():
            try:
                session = LoginSession.from_data(data)
                session._localise(scope=scope, permissions=permissions)
                sessions.append(session)
            except:
                pass

        if len(sessions) == 0:
            from Acquire.Identity import LoginSessionError
            raise LoginSessionError(
                "There is no valid session with short UID %s "
                "in state %s" % (short_uid, status))
        elif len(sessions) == 1:
            sessions = sessions[0]

        return sessions
    def get(par_uid, details_function, url_checksum=None):
        """Return the PAR that matches the passed PAR_UID.
           If 'url_checksum' is supplied then this verifies that
           the checksum of the secret URL is correct.

           This returns the PAR with a completed 'driver_details'.
           The 'driver_details' is created from the dictionary
           of data saved with the PAR. The signature should be;

           driver_details = details_function(data)
        """
        if par_uid is None or len(par_uid) == 0:
            return

        from Acquire.Service import is_running_service as _is_running_service

        if not _is_running_service():
            return

        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket

        from Acquire.ObjectStore import OSPar as _OSPar

        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.ObjectStore import string_to_datetime \
            as _string_to_datetime

        key = "%s/uid/%s" % (_registry_key, par_uid)

        bucket = _get_service_account_bucket()

        objs = _ObjectStore.get_all_objects_from_json(bucket=bucket,
                                                      prefix=key)

        data = None

        for obj in objs.values():
            if url_checksum is not None:
                if url_checksum == obj["url_checksum"]:
                    data = obj
                    break
            else:
                data = obj
                break

        if data is None:
            from Acquire.ObjectStore import ObjectStoreError
            raise ObjectStoreError(
                "There is matching PAR available to close...")

        par = _OSPar.from_data(data["par"])

        if "driver_details" in data:
            if details_function is not None:
                driver_details = details_function(data["driver_details"])
                par._driver_details = driver_details
            else:
                par._driver_details = driver_details

        return par