LiveKit C++ Client SDK v1.4.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
token_source.h
1/*
2 * Copyright 2026 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the License governing permissions and limitations.
14 */
15
16#pragma once
17
18#include <chrono>
19#include <functional>
20#include <future>
21#include <map>
22#include <memory>
23#include <mutex>
24#include <optional>
25#include <string>
26
27#include "livekit/result.h"
28#include "livekit/visibility.h"
29
30namespace livekit {
31
45 std::string server_url;
46
48 std::string participant_token;
49
51 std::optional<std::string> participant_name;
52
54 std::optional<std::string> room_name;
55};
56
75 std::optional<std::string> room_name;
76
81 std::optional<std::string> participant_name;
82
88 std::optional<std::string> participant_identity;
89
94 std::optional<std::string> participant_metadata;
95
100 std::map<std::string, std::string> participant_attributes;
101
111 std::optional<std::string> agent_name;
112
118 std::optional<std::string> agent_metadata;
119
124 std::optional<std::string> agent_deployment;
125};
126
130 std::string method = "POST";
131
133 std::map<std::string, std::string> headers;
134
139 std::chrono::milliseconds timeout = std::chrono::seconds(30);
140};
141
145 std::string base_url = "https://cloud-api.livekit.io";
146};
147
150 std::string message;
151};
152
154class LIVEKIT_API TokenSourceFixed {
155public:
156 virtual ~TokenSourceFixed() = default;
157
161 virtual std::future<Result<TokenSourceResponse, TokenSourceError>> fetch() = 0;
162};
163
165class LIVEKIT_API TokenSourceConfigurable {
166public:
167 virtual ~TokenSourceConfigurable() = default;
168
173 virtual std::future<Result<TokenSourceResponse, TokenSourceError>> fetch(const TokenRequestOptions& options = {}) = 0;
174};
175
182class LIVEKIT_API LiteralTokenSource final : public TokenSourceFixed {
183public:
191 static std::unique_ptr<LiteralTokenSource> create(std::string server_url, std::string participant_token);
192
205 static std::unique_ptr<LiteralTokenSource> create(
206 std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>()> provider);
207
208 std::future<Result<TokenSourceResponse, TokenSourceError>> fetch() override;
209
210private:
211 explicit LiteralTokenSource(TokenSourceResponse details);
212 explicit LiteralTokenSource(std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>()> provider);
213
214 TokenSourceResponse details_;
215 std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>()> provider_;
216};
217
223class LIVEKIT_API CustomTokenSource final : public TokenSourceConfigurable {
224public:
232 static std::unique_ptr<CustomTokenSource> create(
233 std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>(const TokenRequestOptions&)> provider);
234
237 std::future<Result<TokenSourceResponse, TokenSourceError>> fetch(const TokenRequestOptions& options = {}) override;
238
239private:
240 explicit CustomTokenSource(
241 std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>(const TokenRequestOptions&)> provider);
242
243 std::function<std::future<Result<TokenSourceResponse, TokenSourceError>>(const TokenRequestOptions&)> provider_;
244};
245
253class LIVEKIT_API EndpointTokenSource final : public TokenSourceConfigurable {
254public:
260 static std::unique_ptr<EndpointTokenSource> create(std::string endpoint_url, TokenEndpointOptions options = {});
261
264 std::future<Result<TokenSourceResponse, TokenSourceError>> fetch(const TokenRequestOptions& options = {}) override;
265
266private:
267 // Network transport seam. Mirrors the internal HTTP client signature
268 // (returns the raw response body or an error string) so tests can inject a
269 // stub and assert the serialized request / parse a canned response without a
270 // live server. Defaults to the real HTTP client in production.
271 using HttpTransport = std::function<Result<std::string, std::string>(
272 const std::string& method, const std::string& url, const std::map<std::string, std::string>& headers,
273 const std::string& json_body, std::chrono::milliseconds timeout)>;
274
275 EndpointTokenSource(std::string endpoint_url, TokenEndpointOptions options, HttpTransport transport);
276
278
279 std::string endpoint_url_;
280 TokenEndpointOptions options_;
281 HttpTransport transport_;
282
283 friend struct EndpointTokenSourceTestAccess;
284};
285
292class LIVEKIT_API SandboxTokenSource final : public TokenSourceConfigurable {
293public:
299 static std::unique_ptr<SandboxTokenSource> create(const std::string& sandbox_id,
300 const SandboxTokenServerOptions& options = {});
301
302 std::future<Result<TokenSourceResponse, TokenSourceError>> fetch(const TokenRequestOptions& options = {}) override;
303
304private:
305 explicit SandboxTokenSource(std::unique_ptr<TokenSourceConfigurable> endpoint);
306
307 std::unique_ptr<TokenSourceConfigurable> endpoint_;
308
309 friend struct SandboxTokenSourceTestAccess;
310};
311
318class LIVEKIT_API CachingTokenSource final : public TokenSourceConfigurable {
319public:
326 static std::unique_ptr<CachingTokenSource> create(std::unique_ptr<TokenSourceConfigurable> inner);
327
328 std::future<Result<TokenSourceResponse, TokenSourceError>> fetch(const TokenRequestOptions& options = {}) override;
329
332
336 std::optional<TokenSourceResponse> cachedResponse() const;
337
338private:
339 explicit CachingTokenSource(std::unique_ptr<TokenSourceConfigurable> inner);
340
341 std::unique_ptr<TokenSourceConfigurable> inner_;
342 mutable std::mutex mutex_;
343 std::optional<TokenRequestOptions> cached_options_;
344 std::optional<TokenSourceResponse> cached_details_;
345};
346
347} // namespace livekit
Decorator that adds JWT-aware caching to another configurable token source.
Definition token_source.h:318
std::future< Result< TokenSourceResponse, TokenSourceError > > fetch(const TokenRequestOptions &options={}) override
Fetch connection credentials.
static std::unique_ptr< CachingTokenSource > create(std::unique_ptr< TokenSourceConfigurable > inner)
Wrap inner with JWT-aware caching.
void invalidate()
Clear any cached credentials so the next fetch re-queries the inner source.
std::optional< TokenSourceResponse > cachedResponse() const
Return the currently cached credentials, if any.
Token source that delegates token generation to your callback.
Definition token_source.h:223
static std::unique_ptr< CustomTokenSource > create(std::function< std::future< Result< TokenSourceResponse, TokenSourceError > >(const TokenRequestOptions &)> provider)
Create a token source that delegates fetching to provider.
std::future< Result< TokenSourceResponse, TokenSourceError > > fetch(const TokenRequestOptions &options={}) override
Token source that calls your backend token endpoint over HTTP.
Definition token_source.h:253
std::future< Result< TokenSourceResponse, TokenSourceError > > fetch(const TokenRequestOptions &options={}) override
static std::unique_ptr< EndpointTokenSource > create(std::string endpoint_url, TokenEndpointOptions options={})
Create a token source that fetches credentials from endpoint_url.
Token source that returns credentials you already created yourself.
Definition token_source.h:182
static std::unique_ptr< LiteralTokenSource > create(std::string server_url, std::string participant_token)
Create a token source from a static server URL and participant token.
static std::unique_ptr< LiteralTokenSource > create(std::function< std::future< Result< TokenSourceResponse, TokenSourceError > >()> provider)
Create a token source from an async provider that returns full credentials.
std::future< Result< TokenSourceResponse, TokenSourceError > > fetch() override
Fetch connection credentials.
Lightweight success-or-error return type for non-exceptional API failures.
Definition result.h:40
Token source that uses LiveKit Cloud's sandbox token server (development only).
Definition token_source.h:292
std::future< Result< TokenSourceResponse, TokenSourceError > > fetch(const TokenRequestOptions &options={}) override
Fetch connection credentials.
static std::unique_ptr< SandboxTokenSource > create(const std::string &sandbox_id, const SandboxTokenServerOptions &options={})
Create a token source backed by the LiveKit Cloud sandbox token server.
Base interface for token sources that generate credentials from request options.
Definition token_source.h:165
virtual std::future< Result< TokenSourceResponse, TokenSourceError > > fetch(const TokenRequestOptions &options={})=0
Fetch connection credentials.
Base interface for token sources that provide full credentials directly.
Definition token_source.h:154
virtual std::future< Result< TokenSourceResponse, TokenSourceError > > fetch()=0
Fetch connection credentials.
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
Options for SandboxTokenSource.
Definition token_source.h:143
std::string base_url
LiveKit Cloud API base URL (default https://cloud-api.livekit.io).
Definition token_source.h:145
HTTP options for EndpointTokenSource.
Definition token_source.h:128
std::map< std::string, std::string > headers
Additional request headers.
Definition token_source.h:133
std::string method
HTTP method (default POST).
Definition token_source.h:130
std::chrono::milliseconds timeout
Request timeout (default 30 seconds).
Definition token_source.h:139
Per-call options sent to configurable token sources (endpoint, sandbox, custom).
Definition token_source.h:68
std::optional< std::string > agent_metadata
Opaque metadata passed to the dispatched agent job at startup.
Definition token_source.h:118
std::optional< std::string > participant_name
Participant display name shown in UIs and room rosters.
Definition token_source.h:81
std::optional< std::string > room_name
Target room name encoded into the token request.
Definition token_source.h:75
std::optional< std::string > participant_identity
Stable participant identity encoded into the JWT.
Definition token_source.h:88
std::optional< std::string > agent_deployment
LiveKit Cloud deployment to target for agent dispatch.
Definition token_source.h:124
std::optional< std::string > agent_name
Name of a registered LiveKit agent to dispatch into the room.
Definition token_source.h:111
std::map< std::string, std::string > participant_attributes
Key/value participant attributes encoded into the token request.
Definition token_source.h:100
std::optional< std::string > participant_metadata
Opaque participant metadata string stored on the participant record.
Definition token_source.h:94
Error returned when token fetching fails.
Definition token_source.h:149
Credentials produced by a token source and consumed by Room::connect.
Definition token_source.h:43
std::optional< std::string > room_name
Name of the room the participant will join. May be unset if not specified.
Definition token_source.h:54
std::optional< std::string > participant_name
Display name for the participant in the room. May be unset if not specified.
Definition token_source.h:51
std::string server_url
WebSocket URL of the LiveKit server. Use this to establish the connection.
Definition token_source.h:45
std::string participant_token
JWT token containing participant permissions and metadata. Required for authentication.
Definition token_source.h:48