def test_supports_legacy_is_admin_field(alice):
    # Manually craft a local user in legacy format
    raw_legacy_local_user = {
        "organization_addr": alice.organization_addr.to_url(),
        "device_id": str(alice.device_id),
        "signing_key": alice.signing_key.encode(),
        "private_key": alice.private_key.encode(),
        "is_admin": True,
        "user_manifest_id": UUID(alice.user_manifest_id.hex),
        "user_manifest_key": bytes(alice.user_manifest_key.secret),
        "local_symkey": bytes(alice.local_symkey.secret),
    }
    dumped_legacy_local_user = packb(raw_legacy_local_user)

    # Make sure the legacy format can be loaded
    legacy_local_user = LocalDevice.load(dumped_legacy_local_user)
    assert legacy_local_user == alice

    # Manually decode new format to check it is compatible with legacy
    dumped_local_user = alice.dump()
    raw_local_user = unpackb(dumped_local_user)
    assert raw_local_user == {
        **raw_legacy_local_user,
        "profile": alice.profile.value,
        "human_handle": None,
        "device_label": None,
    }
Exemple #2
0
 def _on_notification(self, connection, pid, channel, payload):
     data = unpackb(b64decode(payload.encode("ascii")))
     signal = data.pop("__signal__")
     logger.debug("notif received",
                  pid=pid,
                  channel=channel,
                  payload=payload)
     self.event_bus.send(signal, **data)
Exemple #3
0
 def _on_notification(self, connection, pid, channel, payload):
     data = unpackb(b64decode(payload.encode("ascii")))
     data.pop("__id__")  # Simply discard the notification id
     signal = data.pop("__signal__")
     logger.debug("notif received", pid=pid, channel=channel, payload=payload)
     # Kind of a hack, but fine enough for the moment
     if signal == "realm.roles_updated":
         data["role"] = STR_TO_REALM_ROLE.get(data.pop("role_str"))
     self.event_bus.send(signal, **data)
Exemple #4
0
    async def _http_api_anonymous(self, req: HTTPRequest,
                                  **kwargs: str) -> HTTPResponse:
        # Check whether the organization exists
        try:
            organization_id = OrganizationID(kwargs["organization_id"])
            await self._organization_component.get(organization_id)
        except OrganizationNotFoundError:
            organization_exists = False
        except ValueError:
            return HTTPResponse.build_msgpack(404, {})
        else:
            organization_exists = True

        # Reply to GET
        if req.method == "GET":
            status = 200 if organization_exists else 404
            return HTTPResponse.build_msgpack(status, {})

        # Reply early to POST when the organization doesn't exists
        if not organization_exists and not self._config.organization_spontaneous_bootstrap:
            return HTTPResponse.build_msgpack(404, {})

        # Get and unpack the body
        body = await req.get_body()
        try:
            msg = unpackb(body)
        except SerdePackingError:
            return HTTPResponse.build_msgpack(200,
                                              {"status": "invalid_msg_format"})

        # Lazy creation of the organization if necessary
        cmd = msg.get("cmd")
        if cmd == "organization_bootstrap" and not organization_exists:
            assert self._config.organization_spontaneous_bootstrap
            try:
                await self._organization_component.create(id=organization_id,
                                                          bootstrap_token="")
            except OrganizationAlreadyExistsError:
                pass

        # Retreive command
        client_ctx = AnonymousClientContext(organization_id)
        try:
            if not isinstance(cmd, str):
                raise KeyError()
            cmd_func = self.anonymous_api[cmd]
        except KeyError:
            return HTTPResponse.build_msgpack(200,
                                              {"status": "unknown_command"})

        # Run command
        rep = await cmd_func(client_ctx, msg)
        return HTTPResponse.build_msgpack(200, rep)
Exemple #5
0
 def _on_notification(self, connection, pid, channel, payload):
     data = unpackb(b64decode(payload.encode("ascii")))
     data.pop("__id__")  # Simply discard the notification id
     signal = data.pop("__signal__")
     logger.debug("notif received",
                  pid=pid,
                  channel=channel,
                  payload=payload)
     # Convert strings to enums
     signal = STR_TO_BACKEND_EVENTS[signal]
     # Kind of a hack, but fine enough for the moment
     if signal == BackendEvent.REALM_ROLES_UPDATED:
         data["role"] = STR_TO_REALM_ROLE.get(data.pop("role_str"))
     elif signal == BackendEvent.INVITE_STATUS_CHANGED:
         data["status"] = STR_TO_INVITATION_STATUS.get(
             data.pop("status_str"))
     self.event_bus.send(signal, **data)
Exemple #6
0
def test_user_certificate_supports_legacy_is_admin_field(alice, bob):
    now = pendulum_now()
    certif = UserCertificateContent(
        author=bob.device_id,
        timestamp=now,
        user_id=alice.user_id,
        human_handle=None,
        public_key=alice.public_key,
        profile=alice.profile,
    )

    # Manually craft a certificate in legacy format
    raw_legacy_certif = {
        "type": "user_certificate",
        "author": bob.device_id,
        "timestamp": now,
        "user_id": alice.user_id,
        "public_key": alice.public_key.encode(),
        "is_admin": True,
    }
    dumped_legacy_certif = bob.signing_key.sign(
        zlib.compress(packb(raw_legacy_certif)))

    # Make sure the legacy format can be loaded
    legacy_certif = UserCertificateContent.verify_and_load(
        dumped_legacy_certif,
        author_verify_key=bob.verify_key,
        expected_author=bob.device_id,
        expected_user=alice.user_id,
        expected_human_handle=None,
    )
    assert legacy_certif == certif

    # Manually decode new format to check it is compatible with legacy
    dumped_certif = certif.dump_and_sign(bob.signing_key)
    raw_certif = unpackb(zlib.decompress(bob.verify_key.verify(dumped_certif)))
    assert raw_certif == {
        **raw_legacy_certif, "profile": alice.profile.value,
        "human_handle": None
    }
Exemple #7
0
def test_pack_uuid():
    data = {"uuid": uuid.uuid4()}
    packed = packb(data)
    unpacked = unpackb(packed)
    assert unpacked == data
    assert isinstance(unpacked["uuid"], uuid.UUID)
Exemple #8
0
def test_pack_datetime():
    data = {"date": pendulum.now()}
    packed = packb(data)
    unpacked = unpackb(packed)
    assert unpacked == data
    assert isinstance(unpacked["date"], pendulum.DateTime)