Skip to content

Commit

Permalink
ircd::m::user::keys: Centralize key claiming for user device and algo…
Browse files Browse the repository at this point in the history
…rithm.
  • Loading branch information
jevolk committed Apr 28, 2023
1 parent 8af781c commit 82feeb5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 60 deletions.
1 change: 1 addition & 0 deletions include/ircd/m/user/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct ircd::m::user::keys
void cross_self(json::stack::object &) const;
void cross_user(json::stack::object &) const;

bool claim(json::stack::object &, const string_view &device_id, const string_view &algo) const;
void update(const m::signing_key_update &) const;

keys(const m::user &user)
Expand Down
72 changes: 72 additions & 0 deletions matrix/user_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,78 @@ const
};
}

bool
ircd::m::user::keys::claim(json::stack::object &object,
const string_view &device_id,
const string_view &algorithm)
const
{
const fmt::bsprintf<m::event::TYPE_MAX_SIZE> type
{
"ircd.device.one_time_key|%s",
algorithm
};

const m::room::type events
{
user_room, type, { -1UL, -1L }, true
};

return !events.for_each([this, &object, &device_id]
(const string_view &type, const auto &, const m::event::idx &event_idx)
{
if(m::redacted(event_idx))
return true;

const bool match
{
m::query(std::nothrow, event_idx, "state_key", [&device_id]
(const string_view &state_key) noexcept
{
return state_key == device_id;
})
};

if(!match)
return true;

const auto algorithm
{
split(type, '|').second
};

const bool fetched
{
m::get(std::nothrow, event_idx, "content", [&object, &algorithm]
(const json::object &content)
{
json::stack::member
{
object, algorithm, json::object
{
content[""] // ircd.device.* quirk
}
};
})
};

if(!fetched)
return true;

const auto event_id
{
m::event_id(event_idx)
};

const auto redact_id
{
m::redact(user_room, user_room.user, event_id, "claimed")
};

return false;
});
}

void
ircd::m::user::keys::device(json::stack::object &out,
const string_view &device_id)
Expand Down
65 changes: 5 additions & 60 deletions modules/federation/user_keys_claim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ post__user_keys_claim(client &client,
if(!m::exists(user_id))
continue;

const m::user::room user_room
const m::user::keys keys
{
user_id
};
Expand All @@ -77,74 +77,19 @@ post__user_keys_claim(client &client,
response_keys, user_id
};

for(const auto &[device_id_, algorithm_] : json::object(devices))
for(const auto &[device_id, algorithm_] : json::object(devices))
{
const auto &device_id{device_id_};
const json::string &algorithm{algorithm_};
const fmt::bsprintf<m::event::TYPE_MAX_SIZE> type
const json::string algorithm
{
"ircd.device.one_time_key|%s",
algorithm
};

const m::room::type events
{
user_room, type, { -1UL, -1L }, true
algorithm_
};

json::stack::object response_device
{
response_user, device_id
};

events.for_each([&user_room, &response_device, &device_id, &algorithm]
(const string_view &type, const auto &, const m::event::idx &event_idx)
{
if(m::redacted(event_idx))
return true;

const bool match
{
m::query(std::nothrow, event_idx, "state_key", [&device_id]
(const string_view &state_key) noexcept
{
return state_key == device_id;
})
};

if(!match)
return true;

const bool fetched
{
m::get(std::nothrow, event_idx, "content", [&response_device, &algorithm]
(const json::object &content)
{
json::stack::member
{
response_device, algorithm, json::object
{
content[""] // ircd.device.* quirk
}
};
})
};

if(!fetched)
return true;

const auto event_id
{
m::event_id(event_idx)
};

const auto redact_id
{
m::redact(user_room, user_room.user, event_id, "claimed")
};

return false;
});
keys.claim(response_device, device_id, algorithm);
}
}

Expand Down

0 comments on commit 82feeb5

Please sign in to comment.