Skip to content

Commit 709949f

Browse files
committed
Expose cached, naming
1 parent f67c892 commit 709949f

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

Sources/LiveKit/Auth/ConnectionCredentials.swift

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import Foundation
1818

19+
#warning("Fix camel case after deploying backend")
20+
1921
/// `ConnectionCredentials` represent the credentials needed for connecting to a new Room.
2022
/// - SeeAlso: [LiveKit's Authentication Documentation](https://docs.livekit.io/home/get-started/authentication/) for more information.
2123
public enum ConnectionCredentials {
@@ -132,21 +134,21 @@ public extension TokenServer {
132134
public struct SandboxTokenServer: TokenServer {
133135
public let url = URL(string: "https://cloud-api.livekit.io/api/sandbox/connection-details")!
134136
public var headers: [String: String] {
135-
["X-Sandbox-ID": id.trimmingCharacters(in: CharacterSet(charactersIn: "\""))]
137+
["X-Sandbox-ID": id]
136138
}
137139

138140
/// The sandbox ID provided by LiveKit Cloud.
139141
public let id: String
140142

141143
/// Initialize with a sandbox ID from LiveKit Cloud.
142144
public init(id: String) {
143-
self.id = id
145+
self.id = id.trimmingCharacters(in: CharacterSet(charactersIn: "\""))
144146
}
145147
}
146148

147149
// MARK: - Cache
148150

149-
/// `CachingCredentialsProvider` handles caching of credentials from any other `CredentialsProvider` using configurable storage.
151+
/// `CachingCredentialsProvider` handles caching of credentials from any other `CredentialsProvider` using configurable store.
150152
public actor CachingCredentialsProvider: CredentialsProvider, Loggable {
151153
/// A tuple containing the request and response that were cached.
152154
public typealias Cached = (ConnectionCredentials.Request, ConnectionCredentials.Response)
@@ -155,25 +157,25 @@ public actor CachingCredentialsProvider: CredentialsProvider, Loggable {
155157

156158
private let provider: CredentialsProvider
157159
private let validator: Validator
158-
private let storage: CredentialsStorage
160+
private let store: CredentialsStore
159161

160162
/// Initialize a caching wrapper around any credentials provider.
161163
/// - Parameters:
162164
/// - provider: The underlying credentials provider to wrap
163-
/// - storage: The storage implementation to use for caching (defaults to in-memory storage)
165+
/// - store: The store implementation to use for caching (defaults to in-memory store)
164166
/// - validator: A closure to determine if cached credentials are still valid (defaults to JWT expiration check)
165167
public init(
166168
_ provider: CredentialsProvider,
167-
storage: CredentialsStorage = InMemoryCredentialsStorage(),
169+
store: CredentialsStore = InMemoryCredentialsStore(),
168170
validator: @escaping Validator = { _, res in res.hasValidToken() }
169171
) {
170172
self.provider = provider
171-
self.storage = storage
173+
self.store = store
172174
self.validator = validator
173175
}
174176

175177
public func fetch(_ request: ConnectionCredentials.Request) async throws -> ConnectionCredentials.Response {
176-
if let (cachedRequest, cachedResponse) = await storage.retrieve(),
178+
if let (cachedRequest, cachedResponse) = await store.retrieve(),
177179
cachedRequest == request,
178180
validator(cachedRequest, cachedResponse)
179181
{
@@ -182,23 +184,29 @@ public actor CachingCredentialsProvider: CredentialsProvider, Loggable {
182184
}
183185

184186
let response = try await provider.fetch(request)
185-
try await storage.store((request, response))
187+
await store.store((request, response))
186188
return response
187189
}
188190

189191
/// Invalidate the cached credentials, forcing a fresh fetch on the next request.
190192
public func invalidate() async {
191-
await storage.clear()
193+
await store.clear()
194+
}
195+
196+
/// Get the cached credentials
197+
/// - Returns: The cached credentials if found, nil otherwise
198+
public func getCachedCredentials() async -> CachingCredentialsProvider.Cached? {
199+
await store.retrieve()
192200
}
193201
}
194202

195-
// MARK: - Storage
203+
// MARK: - Store
196204

197-
/// Protocol for abstract storage that can persist and retrieve a single cached credential pair.
198-
/// Implement this protocol to create custom storage implementations e.g. for Keychain.
199-
public protocol CredentialsStorage: Sendable {
200-
/// Store credentials in the storage (replaces any existing credentials)
201-
func store(_ credentials: CachingCredentialsProvider.Cached) async throws
205+
/// Protocol for abstract store that can persist and retrieve a single cached credential pair.
206+
/// Implement this protocol to create custom store implementations e.g. for Keychain.
207+
public protocol CredentialsStore: Sendable {
208+
/// Store credentials in the store (replaces any existing credentials)
209+
func store(_ credentials: CachingCredentialsProvider.Cached) async
202210

203211
/// Retrieve the cached credentials
204212
/// - Returns: The cached credentials if found, nil otherwise
@@ -208,13 +216,13 @@ public protocol CredentialsStorage: Sendable {
208216
func clear() async
209217
}
210218

211-
/// Simple in-memory storage implementation
212-
public actor InMemoryCredentialsStorage: CredentialsStorage {
219+
/// Simple in-memory store implementation
220+
public actor InMemoryCredentialsStore: CredentialsStore {
213221
private var cached: CachingCredentialsProvider.Cached?
214222

215223
public init() {}
216224

217-
public func store(_ credentials: CachingCredentialsProvider.Cached) async throws {
225+
public func store(_ credentials: CachingCredentialsProvider.Cached) async {
218226
cached = credentials
219227
}
220228

0 commit comments

Comments
 (0)