93{
94
96 try
97 {
98 j = json::parse(text);
99 }
100 catch (...)
101 {
102 return;
103 }
104
106 if (type.empty())
107 return;
108
110 {
112 if (!nm)
113 throw std::runtime_error("NetworkManager expired");
114
116 const std::string expected = nm->getNetworkPassword();
117
118 const bool ok = (expected.empty() || provided == expected);
119 const std::string username = j.value(std::string(
msg::key::Username),
"guest" + clientId);
121
122 if (ok)
123 {
125
126 std::vector<std::string> others;
129 if (id != clientId)
130 others.emplace_back(id);
131
132
133 const std::string gmUniqueId = nm->getMyUniqueId();
134
135
137 clientId, username,
138 others,
139 gmUniqueId,
140 clientUniqueId);
141 sendTo(clientId, resp.dump());
142 }
143 else
144 {
146 clientId, username);
147 sendTo(clientId, resp.dump());
148
150 it->second->close();
152 it2->second->close();
153 }
154 return;
155 }
156
157
159 {
161 clientId, "guest" + clientId);
162 sendTo(clientId, resp.dump());
163 return;
164 }
165
166
168
169
171 {
173 if (target.empty())
174 return;
175
176
177 const std::string dump = j.dump();
179 {
180 if (ws && !ws->isClosed())
181 ws->send(dump);
182 }
183
184
186 {
187 if (it->second)
188 it->second->close();
189 }
190 return;
191 }
192
193
195 {
196 const std::string dump = j.dump();
198 {
199 if (id == clientId)
200 continue;
201 if (ws && !ws->isClosed())
202 ws->send(dump);
203 }
204 return;
205 }
206
207
209 if (to.empty())
210 return;
212 if (it !=
authClients_.end() && it->second && !it->second->isClosed())
213 {
214 it->second->send(j.dump());
215 }
216}
bool isAuthenticated(const std::string &clientId) const
void moveToAuthenticated(const std::string &clientId)
void sendTo(const std::string &clientId, const std::string &message)
constexpr std::string_view AuthToken
constexpr std::string_view Type
constexpr std::string_view Target
constexpr std::string_view To
constexpr std::string_view UniqueId
constexpr std::string_view From
constexpr std::string_view Username
constexpr std::string_view Broadcast
constexpr std::string_view PeerDisconnect
constexpr std::string_view Auth
constexpr std::string False
constexpr std::string True
nlohmann::json makeAuthResponse(const std::string ok, const std::string &msg, const std::string &clientId, const std::string &username, const std::vector< std::string > &clients={}, const std::string &gmPeerId="", const std::string &uniqueId="")