-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtoken_source.cpp
More file actions
274 lines (234 loc) · 11.6 KB
/
Copy pathtoken_source.cpp
File metadata and controls
274 lines (234 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations.
*/
#include "livekit/token_source.h"
#include <algorithm>
#include <cctype>
#include <exception>
#include <mutex>
#include <utility>
#include "token_source_internal.h"
namespace livekit {
namespace {
using TokenSourceResult = Result<TokenSourceResponse, TokenSourceError>;
using TokenSourceFuture = std::future<TokenSourceResult>;
constexpr const char* kDefaultSandboxBaseUrl = "https://cloud-api.livekit.io";
bool tokenRequestOptionsEqual(const TokenRequestOptions& a, const TokenRequestOptions& b) {
return a.room_name == b.room_name && a.participant_name == b.participant_name &&
a.participant_identity == b.participant_identity && a.participant_metadata == b.participant_metadata &&
a.participant_attributes == b.participant_attributes && a.agent_name == b.agent_name &&
a.agent_metadata == b.agent_metadata && a.agent_deployment == b.agent_deployment;
}
TokenSourceFuture makeFailedFuture(std::string message) {
std::promise<TokenSourceResult> promise;
promise.set_value(TokenSourceResult::failure(TokenSourceError{std::move(message)}));
return promise.get_future();
}
template <typename WorkFn>
TokenSourceFuture runAsyncTokenSource(std::string context, WorkFn&& work_fn) {
try {
return std::async(std::launch::async,
[context = std::move(context), work_fn = std::forward<WorkFn>(work_fn)]() mutable {
try {
return work_fn();
} catch (const std::exception& e) {
return TokenSourceResult::failure(TokenSourceError{context + ": " + std::string(e.what())});
} catch (...) {
return TokenSourceResult::failure(TokenSourceError{context + ": unknown exception"});
}
});
} catch (const std::exception& e) {
return makeFailedFuture(context + ": failed to start async work: " + std::string(e.what()));
} catch (...) {
return makeFailedFuture(context + ": failed to start async work: unknown exception");
}
}
std::string trimSandboxId(const std::string& sandbox_id) {
const auto is_space = [](unsigned char ch) { return std::isspace(ch) != 0; };
const auto begin = std::find_if_not(sandbox_id.begin(), sandbox_id.end(), is_space);
const auto end = std::find_if_not(sandbox_id.rbegin(), sandbox_id.rend(), is_space).base();
if (begin >= end) {
return {};
}
return std::string(begin, end);
}
std::string joinUrlPath(const std::string& base_url, const std::string& path) {
if (base_url.empty()) {
return path;
}
if (base_url.back() == '/') {
if (path.empty()) {
return base_url;
}
if (path.front() == '/') {
return base_url + path.substr(1);
}
return base_url + path;
}
if (path.empty()) {
return base_url;
}
if (path.front() == '/') {
return base_url + path;
}
return base_url + "/" + path;
}
struct ResolvedSandboxEndpoint {
std::string url;
TokenEndpointOptions options;
};
// Apply the sandbox header and resolve the connection-details URL shared by the
// production and test-only sandbox factories.
ResolvedSandboxEndpoint resolveSandboxEndpoint(const std::string& sandbox_id, TokenEndpointOptions options,
const std::string& base_url) {
options.headers["X-Sandbox-ID"] = trimSandboxId(sandbox_id);
const std::string resolved_base_url = base_url.empty() ? kDefaultSandboxBaseUrl : base_url;
return {joinUrlPath(resolved_base_url, "/api/v2/sandbox/connection-details"), std::move(options)};
}
} // namespace
std::unique_ptr<LiteralTokenSource> LiteralTokenSource::create(std::string server_url, std::string participant_token) {
TokenSourceResponse details;
details.server_url = std::move(server_url);
details.participant_token = std::move(participant_token);
return std::unique_ptr<LiteralTokenSource>(new LiteralTokenSource(std::move(details)));
}
std::unique_ptr<LiteralTokenSource> LiteralTokenSource::create(
std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>()> provider) {
return std::unique_ptr<LiteralTokenSource>(new LiteralTokenSource(std::move(provider)));
}
LiteralTokenSource::LiteralTokenSource(TokenSourceResponse details) : details_(std::move(details)) {}
LiteralTokenSource::LiteralTokenSource(
std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>()> provider)
: provider_(std::move(provider)) {}
std::future<Result<TokenSourceResponse, TokenSourceError>> LiteralTokenSource::fetch() {
if (provider_) {
return provider_();
}
return std::async(std::launch::deferred, [details = details_]() {
if (details.server_url.empty() || details.participant_token.empty()) {
return Result<TokenSourceResponse, TokenSourceError>::failure(
TokenSourceError{"literal token source returned empty server_url or participant_token"});
}
return Result<TokenSourceResponse, TokenSourceError>::success(details);
});
}
std::unique_ptr<CustomTokenSource> CustomTokenSource::create(
std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>(const TokenRequestOptions&)> provider) {
return std::unique_ptr<CustomTokenSource>(new CustomTokenSource(std::move(provider)));
}
CustomTokenSource::CustomTokenSource(
std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>(const TokenRequestOptions&)> provider)
: provider_(std::move(provider)) {}
std::future<Result<TokenSourceResponse, TokenSourceError>> CustomTokenSource::fetch(
const TokenRequestOptions& options) {
return provider_(options);
}
std::unique_ptr<EndpointTokenSource> EndpointTokenSource::create(std::string endpoint_url,
TokenEndpointOptions options) {
return std::unique_ptr<EndpointTokenSource>(
new EndpointTokenSource(std::move(endpoint_url), std::move(options), &tokenSourceHttpRequest));
}
EndpointTokenSource::EndpointTokenSource(std::string endpoint_url, TokenEndpointOptions options,
HttpTransport transport)
: endpoint_url_(std::move(endpoint_url)), options_(std::move(options)), transport_(std::move(transport)) {}
std::unique_ptr<EndpointTokenSource> EndpointTokenSourceTestAccess::create(std::string endpoint_url,
TokenEndpointOptions options,
TokenSourceHttpTransport transport) {
return std::unique_ptr<EndpointTokenSource>(
new EndpointTokenSource(std::move(endpoint_url), std::move(options), std::move(transport)));
}
std::future<Result<TokenSourceResponse, TokenSourceError>> EndpointTokenSource::fetch(
const TokenRequestOptions& options) {
std::shared_ptr<TokenRequestOptions> options_snapshot;
try {
options_snapshot = std::make_shared<TokenRequestOptions>(options);
} catch (const std::exception& e) {
return makeFailedFuture("token source endpoint fetch failed: failed to copy request options: " +
std::string(e.what()));
} catch (...) {
return makeFailedFuture("token source endpoint fetch failed: failed to copy request options: unknown exception");
}
return runAsyncTokenSource("token source endpoint fetch failed",
[this, options_snapshot]() { return fetchSync(*options_snapshot); });
}
Result<TokenSourceResponse, TokenSourceError> EndpointTokenSource::fetchSync(const TokenRequestOptions& options) const {
const std::string request_json = buildTokenSourceRequestJson(options);
auto headers = options_.headers;
auto http_result = transport_(options_.method, endpoint_url_, headers, request_json, options_.timeout);
if (!http_result) {
return Result<TokenSourceResponse, TokenSourceError>::failure(
TokenSourceError{"token server request failed: " + http_result.error()});
}
return parseTokenSourceResponseJson(http_result.value());
}
std::unique_ptr<SandboxTokenSource> SandboxTokenSource::create(const std::string& sandbox_id,
const SandboxTokenServerOptions& options) {
auto resolved = resolveSandboxEndpoint(sandbox_id, {}, options.base_url);
auto endpoint = EndpointTokenSource::create(std::move(resolved.url), std::move(resolved.options));
return std::unique_ptr<SandboxTokenSource>(new SandboxTokenSource(std::move(endpoint)));
}
SandboxTokenSource::SandboxTokenSource(std::unique_ptr<TokenSourceConfigurable> endpoint)
: endpoint_(std::move(endpoint)) {}
std::unique_ptr<SandboxTokenSource> SandboxTokenSourceTestAccess::create(const std::string& sandbox_id,
const SandboxTokenServerOptions& options,
TokenSourceHttpTransport transport) {
auto resolved = resolveSandboxEndpoint(sandbox_id, {}, options.base_url);
auto endpoint =
EndpointTokenSourceTestAccess::create(std::move(resolved.url), std::move(resolved.options), std::move(transport));
return std::unique_ptr<SandboxTokenSource>(new SandboxTokenSource(std::move(endpoint)));
}
std::future<Result<TokenSourceResponse, TokenSourceError>> SandboxTokenSource::fetch(
const TokenRequestOptions& options) {
return endpoint_->fetch(options);
}
std::unique_ptr<CachingTokenSource> CachingTokenSource::create(std::unique_ptr<TokenSourceConfigurable> inner) {
return std::unique_ptr<CachingTokenSource>(new CachingTokenSource(std::move(inner)));
}
CachingTokenSource::CachingTokenSource(std::unique_ptr<TokenSourceConfigurable> inner) : inner_(std::move(inner)) {}
std::future<Result<TokenSourceResponse, TokenSourceError>> CachingTokenSource::fetch(
const TokenRequestOptions& options) {
std::shared_ptr<TokenRequestOptions> options_snapshot;
try {
options_snapshot = std::make_shared<TokenRequestOptions>(options);
} catch (const std::exception& e) {
return makeFailedFuture("token source cache fetch failed: failed to copy request options: " +
std::string(e.what()));
} catch (...) {
return makeFailedFuture("token source cache fetch failed: failed to copy request options: unknown exception");
}
return runAsyncTokenSource("token source cache fetch failed", [this, options_snapshot]() {
const std::scoped_lock<std::mutex> lock(mutex_);
if (cached_details_.has_value() && cached_options_.has_value() &&
tokenRequestOptionsEqual(*cached_options_, *options_snapshot) &&
isParticipantTokenValid(cached_details_->participant_token)) {
return TokenSourceResult::success(*cached_details_);
}
auto result = inner_->fetch(*options_snapshot).get();
if (result) {
cached_options_ = *options_snapshot;
cached_details_ = result.value();
}
return result;
});
}
void CachingTokenSource::invalidate() {
const std::scoped_lock<std::mutex> lock(mutex_);
cached_options_.reset();
cached_details_.reset();
}
std::optional<TokenSourceResponse> CachingTokenSource::cachedResponse() const {
const std::scoped_lock<std::mutex> lock(mutex_);
return cached_details_;
}
} // namespace livekit