Skip to content

Commit dfb92d0

Browse files
committed
Add invalidate and cachedResponse functions to CachingTokenSource
1 parent 81a0fbd commit dfb92d0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

livekit-android-sdk/src/main/java/io/livekit/android/token/CachingTokenSource.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ abstract class BaseCachingTokenSource(
2525
private val store: TokenStore,
2626
private val validator: TokenValidator,
2727
) {
28-
suspend fun fetchImpl(options: TokenRequestOptions?): TokenSourceResponse {
28+
29+
/**
30+
* The entrypoint for the caching store; subclasses should call this from their fetch methods.
31+
*
32+
* If a new token is needed, [fetchFromSource] will be called.
33+
*/
34+
internal suspend fun fetchImpl(options: TokenRequestOptions?): TokenSourceResponse {
2935
val cached = store.retrieve()
3036

3137
if (cached != null && cached.options == options && validator.invoke(cached.options, cached.response)) {
@@ -37,7 +43,24 @@ abstract class BaseCachingTokenSource(
3743
return response
3844
}
3945

46+
/**
47+
* Implement this to fetch the [TokenSourceResponse] from the token source.
48+
*/
4049
abstract suspend fun fetchFromSource(options: TokenRequestOptions?): TokenSourceResponse
50+
51+
/**
52+
* Invalidate the cached credentials, forcing a fresh fetch on the next request.
53+
*/
54+
suspend fun invalidate() {
55+
store.clear()
56+
}
57+
58+
/**
59+
* Get the cached credentials if one exists.
60+
*/
61+
suspend fun cachedResponse(): TokenSourceResponse? {
62+
return store.retrieve()?.response
63+
}
4164
}
4265

4366
class CachingFixedTokenSource(

livekit-android-sdk/src/main/java/io/livekit/android/token/TokenSource.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,25 @@ interface TokenSource {
6666
companion object {
6767
/**
6868
* Creates a [FixedTokenSource] that immediately returns with the supplied [serverUrl] and [participantToken].
69+
*
70+
* @see cached
71+
* @see CachingFixedTokenSource
6972
*/
7073
fun fromLiteral(serverUrl: String, participantToken: String): FixedTokenSource = LiteralTokenSource(serverUrl, participantToken)
7174

7275
/**
7376
* Creates a custom [ConfigurableTokenSource] that executes [block] to fetch the credentials.
77+
*
78+
* @see cached
79+
* @see CachingConfigurableTokenSource
7480
*/
7581
fun fromCustom(block: suspend (options: TokenRequestOptions) -> TokenSourceResponse): ConfigurableTokenSource = CustomTokenSource(block)
7682

7783
/**
7884
* Creates a [ConfigurableTokenSource] that fetches from a given [url] using the standard token server format.
85+
*
86+
* @see cached
87+
* @see CachingConfigurableTokenSource
7988
*/
8089
fun fromEndpoint(url: URL, method: String = "POST", headers: Map<String, String> = emptyMap()): ConfigurableTokenSource = EndpointTokenSourceImpl(
8190
url = url,
@@ -88,6 +97,9 @@ interface TokenSource {
8897
* which supports quick prototyping/getting started types of use cases.
8998
*
9099
* Note: This token provider is **insecure** and should **not** be used in production.
100+
*
101+
* @see cached
102+
* @see CachingConfigurableTokenSource
91103
*/
92104
fun fromSandboxTokenServer(sandboxId: String, options: SandboxTokenServerOptions = SandboxTokenServerOptions()): ConfigurableTokenSource = SandboxTokenSource(
93105
sandboxId = sandboxId,

0 commit comments

Comments
 (0)