Debian Codegraph @

mtxclient/lib/structs/user_interactive.cpp

raw symbols SCIP
1 #include "mtx/user_interactive.hpp"
2
3 #include <nlohmann/json.hpp>
4
5 namespace mtx::user_interactive {
6 void
7 from_json(const nlohmann::json &obj, OAuth2Params &params)
8 {
9 params.uri = obj.value("uri", "");
10 }
11
12 void
13 from_json(const nlohmann::json &obj, PolicyDescription &d)
14 {
15 d.name = obj.value("name", "");
16 d.url = obj.value("url", "");
17 }
18 void
19 from_json(const nlohmann::json &obj, Policy &policy)
20 {
21 policy.version = obj.at("version").get<std::string>();
22
23 for (const auto &e : obj.items())
24 if (e.key() != "version")
25 policy.langToPolicy.emplace(e.key(), e.value().get<PolicyDescription>());
26 }
27
28 void
29 from_json(const nlohmann::json &obj, TermsParams &terms)
30 {
31 terms.policies = obj["policies"].get<std::unordered_map<std::string, Policy>>();
32 }
33
34 void
35 from_json(const nlohmann::json &obj, Flow &flow)
36 {
37 flow.stages = obj["stages"].get<Stages>();
38 }
39 void
40 from_json(const nlohmann::json &obj, Unauthorized &u)
41 {
42 if (obj.contains("completed"))
43 u.completed = obj.at("completed").get<Stages>();
44
45 u.session = obj.value("session", "");
46 u.flows = obj.at("flows").get<std::vector<Flow>>();
47
48 if (obj.contains("params")) {
49 for (const auto &e : obj["params"].items()) {
50 if (e.key() == auth_types::terms)
51 u.params.emplace(e.key(), e.value().get<TermsParams>());
52 else if (e.key() == auth_types::oauth2)
53 u.params.emplace(e.key(), e.value().get<OAuth2Params>());
54 else
55 u.params.emplace(e.key(), e.value().dump());
56 }
57 }
58 }
59
60 namespace {
61 template<class... Ts>
62 struct overloaded : Ts...
63 {
64 using Ts::operator()...;
65 };
66 template<class... Ts>
67 overloaded(Ts...) -> overloaded<Ts...>;
68 }
69
70 namespace auth {
71 static void
72 to_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
84 void
85 to_json(nlohmann::json &obj, const Auth &auth)
86 {
87 obj["session"] = auth.session;
88
89 std::visit(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 auth.content);
131 }
132 }