Exemplo n.º 1
0
    async def _get_device_invitation(self, conn,
                                     organization_id: OrganizationID,
                                     device_id: DeviceID):
        if await self._device_exists(conn, organization_id, device_id):
            raise UserAlreadyExistsError(
                f"Device `{device_id}` already exists")

        result = await conn.fetchrow(
            """
SELECT device_invitations.device_id, devices.device_id, device_invitations.created_on
FROM device_invitations
LEFT JOIN devices ON device_invitations.creator = devices._id
WHERE
    device_invitations.organization = (
        SELECT _id from organizations WHERE organization_id = $1
    )
    AND device_invitations.device_id = $2
""",
            organization_id,
            device_id,
        )
        if not result:
            raise UserNotFoundError(device_id)

        return DeviceInvitation(device_id=DeviceID(result[0]),
                                creator=DeviceID(result[1]),
                                created_on=result[2])
Exemplo n.º 2
0
async def test_device_invite(monkeypatch, backend, alice_backend_sock, alice, alice_nd_id):
    dummy_device_id = DeviceID(f"{alice.user_id}@pc1")
    await backend.user.create_device_invitation(
        alice.organization_id, DeviceInvitation(dummy_device_id, alice.device_id)
    )

    device_invitation_created = trio.Event()

    vanilla_create_device_invitation = backend.user.create_device_invitation

    async def _mocked_create_device_invitation(*args, **kwargs):
        ret = await vanilla_create_device_invitation(*args, **kwargs)
        device_invitation_created.set()
        return ret

    monkeypatch.setattr(backend.user, "create_device_invitation", _mocked_create_device_invitation)

    with trio.fail_after(1):
        async with device_invite(
            alice_backend_sock, invited_device_name=alice_nd_id.device_name
        ) as prep:

            # Wait for invitation to be created before fetching it !
            await device_invitation_created.wait()

            # No the user we are waiting for
            await backend.user.claim_device_invitation(
                alice.organization_id, dummy_device_id, b"<alice@dummy encrypted_claim>"
            )

            await backend.user.claim_device_invitation(
                alice.organization_id, alice_nd_id, b"<alice@new_device encrypted_claim>"
            )

        assert prep[0] == {"status": "ok", "encrypted_claim": b"<alice@new_device encrypted_claim>"}
Exemplo n.º 3
0
async def _get_device_invitation(conn, organization_id: OrganizationID,
                                 device_id: DeviceID) -> DeviceInvitation:
    if await _device_exists(conn, organization_id, device_id):
        raise UserAlreadyExistsError(f"Device `{device_id}` already exists")

    result = await conn.fetchrow(_q_get_invitation, organization_id, device_id)
    if not result:
        raise UserNotFoundError(device_id)

    return DeviceInvitation(device_id=DeviceID(result[0]),
                            creator=DeviceID(result[1]),
                            created_on=result[2])
Exemplo n.º 4
0
async def test_device_get_invitation_creator_with_trustchain_ok(
    backend_data_binder_factory, local_device_factory, backend, alice, anonymous_backend_sock
):
    binder = backend_data_binder_factory(backend)
    certificates_store = binder.certificates_store

    roger1 = local_device_factory("roger@dev1")
    mike1 = local_device_factory("mike@dev1")

    await binder.bind_device(roger1, certifier=alice)
    await binder.bind_device(mike1, certifier=roger1)
    await binder.bind_revocation(roger1.user_id, certifier=mike1)

    invitation = DeviceInvitation(
        device_id=DeviceID(f"{alice.user_id}@new"), creator=mike1.device_id
    )
    await backend.user.create_device_invitation(alice.organization_id, invitation)

    rep = await device_get_invitation_creator(
        anonymous_backend_sock, invited_device_id=invitation.device_id
    )
    cooked_rep = {
        **rep,
        "device_certificate": certificates_store.translate_certif(rep["device_certificate"]),
        "user_certificate": certificates_store.translate_certif(rep["user_certificate"]),
        "trustchain": {
            "devices": sorted(certificates_store.translate_certifs(rep["trustchain"]["devices"])),
            "users": sorted(certificates_store.translate_certifs(rep["trustchain"]["users"])),
            "revoked_users": sorted(
                certificates_store.translate_certifs(rep["trustchain"]["revoked_users"])
            ),
        },
    }

    assert cooked_rep == {
        "status": "ok",
        "device_certificate": "<mike@dev1 device certif>",
        "user_certificate": "<mike user certif>",
        "trustchain": {
            "devices": sorted(
                [
                    "<alice@dev1 device certif>",
                    "<roger@dev1 device certif>",
                    "<mike@dev1 device certif>",
                ]
            ),
            "users": sorted(["<alice user certif>", "<roger user certif>", "<mike user certif>"]),
            "revoked_users": ["<roger revoked user certif>"],
        },
    }
async def alice_nd_invitation(backend, alice):
    invitation = DeviceInvitation(DeviceID(f"{alice.user_id}@new_device"),
                                  alice.device_id, Pendulum(2000, 1, 2))
    await backend.user.create_device_invitation(alice.organization_id,
                                                invitation)
    return invitation
async def alice_nd_invitation(backend, alice):
    invitation = DeviceInvitation(alice.user_id.to_device_id("new_device"),
                                  alice.device_id, datetime(2000, 1, 2))
    await backend.user.create_device_invitation(alice.organization_id,
                                                invitation)
    return invitation