Skip to content

Commit 51915ab

Browse files
committed
Revert "Mutable variant with setRequest"
This reverts commit 6b47bb2.
1 parent 6b47bb2 commit 51915ab

File tree

3 files changed

+25
-50
lines changed

3 files changed

+25
-50
lines changed

Sources/LiveKit/Auth/Sandbox.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public struct Sandbox: TokenEndpoint {
2525
["X-Sandbox-ID": id]
2626
}
2727

28-
public var request: Token.Request?
29-
public mutating func setRequest(_ request: Token.Request) {
30-
self.request = request
31-
}
32-
33-
public mutating func clearRequest() {
34-
request = nil
35-
}
36-
3728
/// The sandbox ID provided by LiveKit Cloud.
3829
public let id: String
3930

Sources/LiveKit/Auth/TokenSource.swift

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public enum Token {
8282
}
8383
}
8484

85+
public typealias Options = Request
8586
public typealias Literal = Response
8687
}
8788

@@ -90,22 +91,18 @@ public enum Token {
9091
/// Protocol for types that can provide connection credentials.
9192
/// Implement this protocol to create custom credential providers (e.g., fetching from your backend API).
9293
public protocol TokenSource: Sendable {
93-
var request: Token.Request? { get async }
94-
mutating func setRequest(_ request: Token.Request) async
95-
mutating func clearRequest() async
96-
/// Get connection credentials for the given request.
94+
/// Fetch connection credentials for the given request.
95+
/// - Parameter request: The token request containing room and participant information
9796
/// - Returns: A token response containing the server URL and participant token
9897
/// - Throws: An error if the token generation fails
99-
func generate() async throws -> Token.Response
98+
func fetch(_ request: Token.Request) async throws -> Token.Response
10099
}
101100

102101
/// `Token.Literal` contains a single set of credentials, hard-coded or acquired from a static source.
103102
extension Token.Literal: TokenSource {
104-
public var request: Token.Request? { nil }
105-
public func setRequest(_: Token.Request) {}
106-
public func clearRequest() {}
107-
108-
public func generate() async throws -> Token.Response { self }
103+
public func fetch(_: Token.Request) async throws -> Token.Response {
104+
self
105+
}
109106
}
110107

111108
// MARK: - Endpoint
@@ -126,19 +123,17 @@ public extension TokenEndpoint {
126123
var method: String { "POST" }
127124
var headers: [String: String] { [:] }
128125

129-
func generate() async throws -> Token.Response {
126+
func fetch(_ request: Token.Request) async throws -> Token.Response {
130127
var urlRequest = URLRequest(url: url)
131128

132129
urlRequest.httpMethod = method
133130
for (key, value) in headers {
134131
urlRequest.addValue(value, forHTTPHeaderField: key)
135132
}
136-
urlRequest.httpBody = try await JSONEncoder().encode(request)
133+
urlRequest.httpBody = try JSONEncoder().encode(request)
137134

138135
let (data, response) = try await URLSession.shared.data(for: urlRequest)
139136

140-
try Task.checkCancellation()
141-
142137
guard let httpResponse = response as? HTTPURLResponse else {
143138
throw LiveKitError(.network, message: "Error generating token from the token server, no response")
144139
}
@@ -164,20 +159,7 @@ public actor CachingTokenSource: TokenSource, Loggable {
164159
/// - Returns: `true` if the cached credentials are still valid, `false` otherwise
165160
public typealias TokenValidator = (Token.Request, Token.Response) -> Bool
166161

167-
public var request: Token.Request? {
168-
get async { await source.request }
169-
}
170-
171-
public func setRequest(_ request: Token.Request) async {
172-
await source.setRequest(request)
173-
}
174-
175-
public func clearRequest() async {
176-
await source.clearRequest()
177-
await store.clear()
178-
}
179-
180-
private var source: TokenSource
162+
private let source: TokenSource
181163
private let store: TokenStore
182164
private let validator: TokenValidator
183165

@@ -196,29 +178,30 @@ public actor CachingTokenSource: TokenSource, Loggable {
196178
self.validator = validator
197179
}
198180

199-
public func generate() async throws -> Token.Response {
200-
let request = await request ?? .init()
201-
202-
if let (cachedRequest, cachedResponse) = await store.retrieve(), cachedRequest == request,
181+
public func fetch(_ request: Token.Request) async throws -> Token.Response {
182+
if let (cachedRequest, cachedResponse) = await store.retrieve(),
183+
cachedRequest == request,
203184
validator(cachedRequest, cachedResponse)
204185
{
205186
log("Using cached credentials", .debug)
206187
return cachedResponse
207188
}
208189

209190
log("Requesting new credentials", .debug)
210-
let response = try await source.generate()
211-
212-
guard validator(request, response) else {
213-
throw LiveKitError(.network, message: "Invalid credentials")
214-
}
215-
191+
let response = try await source.fetch(request)
216192
await store.store((request, response))
217193
return response
218194
}
219195

220-
var cachedResponse: Token.Response? {
221-
get async { await store.retrieve()?.1 }
196+
/// Invalidate the cached credentials, forcing a fresh fetch on the next request.
197+
public func invalidate() async {
198+
await store.clear()
199+
}
200+
201+
/// Get the cached credentials
202+
/// - Returns: The cached token if found, nil otherwise
203+
public func cachedToken() async -> Token.Response? {
204+
await store.retrieve()?.1
222205
}
223206
}
224207

Sources/LiveKit/Core/Room.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,13 @@ public class Room: NSObject, @unchecked Sendable, ObservableObject, Loggable {
412412
}
413413

414414
public func connect(tokenSource: TokenSource,
415+
tokenOptions: Token.Options = .init(),
415416
connectOptions: ConnectOptions? = nil,
416417
roomOptions: RoomOptions? = nil) async throws
417418
{
418419
self.tokenSource = tokenSource
419420

420-
let token = try await tokenSource.generate()
421+
let token = try await tokenSource.fetch(tokenOptions)
421422
try await connect(url: token.serverURL.absoluteString, token: token.participantToken, connectOptions: connectOptions, roomOptions: roomOptions)
422423
}
423424

0 commit comments

Comments
 (0)