Skip to content

Commit d664a5f

Browse files
committed
Public, comments
1 parent 3c37daa commit d664a5f

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Sources/LiveKit/Auth/TokenSource.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ public enum Token {
2626
/// Request parameters for generating connection credentials.
2727
public struct Request: Encodable, Sendable, Equatable {
2828
/// The name of the room being requested when generating credentials.
29-
let roomName: String?
29+
public let roomName: String?
3030
/// The name of the participant being requested for this client when generating credentials.
31-
let participantName: String?
31+
public let participantName: String?
3232
/// The identity of the participant being requested for this client when generating credentials.
33-
let participantIdentity: String?
33+
public let participantIdentity: String?
3434
/// Any participant metadata being included along with the credentials generation operation.
35-
let participantMetadata: String?
35+
public let participantMetadata: String?
3636
/// Any participant attributes being included along with the credentials generation operation.
37-
let participantAttributes: [String: String]?
38-
/// A `RoomConfiguration` object can be passed to request extra parameters should be included when generating connection credentials - dispatching agents, etc.
37+
public let participantAttributes: [String: String]?
38+
/// A `RoomConfiguration` object can be passed to request extra parameters when generating connection credentials.
39+
/// Used for advanced room configuration like dispatching agents, setting room limits, etc.
3940
/// - SeeAlso: [Room Configuration Documentation](https://docs.livekit.io/home/get-started/authentication/#room-configuration) for more info.
40-
let roomConfiguration: RoomConfiguration?
41+
public let roomConfiguration: RoomConfiguration?
4142

4243
// enum CodingKeys: String, CodingKey {
4344
// case roomName = "room_name"
@@ -68,9 +69,9 @@ public enum Token {
6869
/// Response containing the credentials needed to connect to a room.
6970
public struct Response: Decodable, Sendable {
7071
/// The WebSocket URL for the LiveKit server.
71-
let serverURL: URL
72+
public let serverURL: URL
7273
/// The JWT token containing participant permissions and metadata.
73-
let participantToken: String
74+
public let participantToken: String
7475

7576
enum CodingKeys: String, CodingKey {
7677
case serverURL = "serverUrl"
@@ -92,6 +93,10 @@ public enum Token {
9293
/// Protocol for types that can provide connection credentials.
9394
/// Implement this protocol to create custom credential providers (e.g., fetching from your backend API).
9495
public protocol TokenSource: Sendable {
96+
/// Fetch connection credentials for the given request.
97+
/// - Parameter request: The token request containing room and participant information
98+
/// - Returns: A token response containing the server URL and participant token
99+
/// - Throws: An error if the token generation fails
95100
func fetch(_ request: Token.Request) async throws -> Token.Response
96101
}
97102

@@ -150,6 +155,10 @@ public actor CachingTokenSource: TokenSource, Loggable {
150155
/// A tuple containing the request and response that were cached.
151156
public typealias Cached = (Token.Request, Token.Response)
152157
/// A closure that validates whether cached credentials are still valid.
158+
/// - Parameters:
159+
/// - request: The original token request
160+
/// - response: The cached token response
161+
/// - Returns: `true` if the cached credentials are still valid, `false` otherwise
153162
public typealias TokenValidator = (Token.Request, Token.Response) -> Bool
154163

155164
private let source: TokenSource
@@ -236,6 +245,9 @@ public actor InMemoryTokenStore: TokenStore {
236245
// MARK: - Validation
237246

238247
public extension Token.Response {
248+
/// Validates whether the JWT token is still valid.
249+
/// - Parameter tolerance: Time tolerance in seconds for token expiration check (default: 60 seconds)
250+
/// - Returns: `true` if the token is valid and not expired, `false` otherwise
239251
func hasValidToken(withTolerance tolerance: TimeInterval = 60) -> Bool {
240252
let parts = participantToken.components(separatedBy: ".")
241253
guard parts.count == 3 else {

0 commit comments

Comments
 (0)