1#include "mtx/user_interactive.hpp"
2
3#include <nlohmann/json.hpp>
4
5namespace mtx::user_interactive {
6void
7from_json(const nlohmann::json &obj, OAuth2Params &params)
8{
9 params.uri = obj.value(key: "uri", default_value: "");
10}
11
12void
13from_json(const nlohmann::json &obj, PolicyDescription &d)
14{
15 d.name = obj.value(key: "name", default_value: "");
16 d.url = obj.value(key: "url", default_value: "");
17}
18void
19from_json(const nlohmann::json &obj, Policy &policy)
20{
21 policy.version = obj.at(key: "version").get<std::string>();
22
23 for (const auto &e : obj.items())
24 if (e.key() != "version")
25 policy.langToPolicy.emplace(args: e.key(), args: e.value().get<PolicyDescription>());
26}
27
28void
29from_json(const nlohmann::json &obj, TermsParams &terms)
30{
31 terms.policies = obj["policies"].get<std::unordered_map<std::string, Policy>>();
32}
33
34void
35from_json(const nlohmann::json &obj, Flow &flow)
36{
37 flow.stages = obj["stages"].get<Stages>();
38}
39void
40from_json(const nlohmann::json &obj, Unauthorized &u)
41{
42 if (obj.contains(key: "completed"))
43 u.completed = obj.at(key: "completed").get<Stages>();
44
45 u.session = obj.value(key: "session", default_value: "");
46 u.flows = obj.at(key: "flows").get<std::vector<Flow>>();
47
48 if (obj.contains(key: "params")) {
49 for (const auto &e : obj["params"].items()) {
50 if (e.key() == auth_types::terms)
51 u.params.emplace(args: e.key(), args: e.value().get<TermsParams>());
52 else if (e.key() == auth_types::oauth2)
53 u.params.emplace(args: e.key(), args: e.value().get<OAuth2Params>());
54 else
55 u.params.emplace(args: e.key(), args: e.value().dump());
56 }
57 }
58}
59
60namespace {
61template<class... Ts>
62struct overloaded : Ts...
63{
64 using Ts::operator()...;
65};
66template<class... Ts>
67overloaded(Ts...) -> overloaded<Ts...>;
68}
69
70namespace auth {
71static void
72to_json(nlohmann::json &obj, const ThreePIDCred &cred)
73{
74 obj["sid"] = cred.sid;
75 obj["client_secret"] = cred.client_secret;
76
77 if (!cred.id_server.empty()) {
78 obj["id_server"] = cred.id_server;
79 obj["id_access_token"] = cred.id_access_token;
80 }
81}
82}
83
84void
85to_json(nlohmann::json &obj, const Auth &auth)
86{
87 obj["session"] = auth.session;
88
89 std::visit(visitor: overloaded{
90 [&obj](const auth::Password &password) {
91 obj["type"] = auth_types::password;
92 obj["password"] = password.password;
93
94 if (password.identifier_type == auth::Password::IdType::UserId) {
95 obj["identifier"]["type"] = "m.id.user";
96 obj["identifier"]["user"] = password.identifier_user;
97 } else {
98 obj["identifier"]["type"] = "m.id.thirdparty";
99 obj["identifier"]["medium"] = password.identifier_medium;
100 obj["identifier"]["address"] = password.identifier_address;
101 }
102 },
103 [&obj](const auth::ReCaptcha &captcha) {
104 obj["type"] = auth_types::recaptcha;
105 obj["response"] = captcha.response;
106 },
107 [&obj](const auth::Token &token) {
108 obj["type"] = auth_types::token;
109 obj["token"] = token.token;
110 obj["txn_id"] = token.txn_id;
111 },
112 [&obj](const auth::EmailIdentity &id) {
113 obj["type"] = auth_types::email_identity;
114 obj["threepid_creds"] = id.threepidCred;
115 },
116 [&obj](const auth::MSISDN &id) {
117 obj["type"] = auth_types::msisdn;
118 obj["threepid_creds"] = id.threepidCred;
119 },
120 [&obj](const auth::RegistrationToken &registration_token) {
121 obj["type"] = auth_types::registration_token;
122 obj["token"] = registration_token.token;
123 },
124 [&obj](const auth::OAuth2 &) { obj["type"] = auth_types::oauth2; },
125 [&obj](const auth::SSO &) { obj["type"] = auth_types::sso; },
126 [&obj](const auth::Terms &) { obj["type"] = auth_types::terms; },
127 [&obj](const auth::Dummy &) { obj["type"] = auth_types::dummy; },
128 [](const auth::Fallback &) {},
129 },
130 variants: auth.content);
131}
132}
133