Skip to content

Commit 3a5fae0

Browse files
committed
fix: Renew access_token each 15 minutes #WPB-17689
1 parent d310190 commit 3a5fae0

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ plugins {
3131
}
3232

3333
group = "com.wire"
34-
version = "0.0.6"
34+
version = "0.0.7"
3535
val artifactId = "wire-apps-jvm-sdk"
3636

3737
repositories {

lib/src/main/kotlin/com/wire/integrations/jvm/client/BackendClientDemo.kt

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,40 @@ internal class BackendClientDemo internal constructor(
132132
* because the new one is tied to client and has more permissions.
133133
* Not needed in the actual implementation, as the SDK is authenticated with the API_TOKEN
134134
*/
135-
private suspend fun loginUser(): String {
136-
if (cachedAccessToken != null) return cachedAccessToken as String
135+
private var tokenTimestamp: Long? = null
137136

138-
val loginResponse = httpClient.post("/$API_VERSION/login") {
139-
setBody(LoginRequest(DEMO_USER_EMAIL, DEMO_USER_PASSWORD))
140-
contentType(ContentType.Application.Json)
137+
private suspend fun loginUser(): String {
138+
val currentTime = System.currentTimeMillis()
139+
140+
// Check if token is valid (not null and not expired)
141+
if (cachedAccessToken != null && tokenTimestamp != null) {
142+
val timeSinceTokenIssued = currentTime - tokenTimestamp!!
143+
if (timeSinceTokenIssued < TOKEN_EXPIRATION_MS) {
144+
return cachedAccessToken as String
141145
}
142-
val zuidCookie = loginResponse.setCookie()["zuid"]
146+
// Token has expired, will get a new one
147+
logger.info("Access token expired, getting a new one")
148+
}
143149

144-
val accessResponse =
145-
httpClient.post("/$API_VERSION/access?client_id=$DEMO_USER_CLIENT") {
146-
headers {
147-
append(HttpHeaders.Cookie, "zuid=${zuidCookie!!.value}")
148-
}
149-
accept(ContentType.Application.Json)
150-
}.body<LoginResponse>()
151-
cachedAccessToken = accessResponse.accessToken
152-
return accessResponse.accessToken
150+
val loginResponse = httpClient.post("/$API_VERSION/login") {
151+
setBody(LoginRequest(DEMO_USER_EMAIL, DEMO_USER_PASSWORD))
152+
contentType(ContentType.Application.Json)
153153
}
154+
val zuidCookie = loginResponse.setCookie()["zuid"]
155+
156+
val accessResponse =
157+
httpClient.post("/$API_VERSION/access?client_id=$DEMO_USER_CLIENT") {
158+
headers {
159+
append(HttpHeaders.Cookie, "zuid=${zuidCookie!!.value}")
160+
}
161+
accept(ContentType.Application.Json)
162+
}.body<LoginResponse>()
163+
164+
cachedAccessToken = accessResponse.accessToken
165+
tokenTimestamp = currentTime
166+
167+
return accessResponse.accessToken
168+
}
154169

155170
override suspend fun updateClientWithMlsPublicKey(
156171
appClientId: AppClientId,
@@ -338,6 +353,7 @@ internal class BackendClientDemo internal constructor(
338353
const val PATH_PUBLIC_ASSETS_V3 = "assets/v3"
339354
const val PATH_PUBLIC_ASSETS_V4 = "assets/v4"
340355
const val HEADER_ASSET_TOKEN = "Asset-Token"
356+
const val TOKEN_EXPIRATION_MS = 14 * 60 * 1000 // 14 minutes in milliseconds
341357

342358
val DEMO_USER_EMAIL: String by lazy {
343359
DemoProperties.properties.getProperty(

0 commit comments

Comments
 (0)