Skip to content

Commit 2dbdbbc

Browse files
committed
fix(model-client): expired access token wasn't refreshed
1 parent c4c6787 commit 2dbdbbc

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

model-client/src/jvmMain/kotlin/org/modelix/model/oauth/ModelixAuthClient.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.google.api.client.auth.oauth2.AuthorizationCodeFlow
44
import com.google.api.client.auth.oauth2.BearerToken
55
import com.google.api.client.auth.oauth2.ClientParametersAuthentication
66
import com.google.api.client.auth.oauth2.Credential
7-
import com.google.api.client.auth.oauth2.StoredCredential
87
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
98
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
109
import com.google.api.client.http.GenericUrl
@@ -30,9 +29,20 @@ actual object ModelixAuthClient {
3029
private var DATA_STORE_FACTORY: DataStoreFactory = MemoryDataStoreFactory()
3130
private val HTTP_TRANSPORT: HttpTransport = NetHttpTransport()
3231
private val JSON_FACTORY: JsonFactory = GsonFactory()
32+
private val userId = "modelix-user"
33+
private var lastCredentials: Credential? = null
3334

34-
fun getTokens(): StoredCredential? {
35-
return StoredCredential.getDefaultDataStore(DATA_STORE_FACTORY).get("user")
35+
fun getTokens(): Credential? {
36+
return lastCredentials?.refreshIfExpired()?.takeIf { !it.isExpired() }
37+
}
38+
39+
private fun Credential.isExpired() = (expiresInSeconds ?: 0) < 60
40+
41+
private fun Credential.refreshIfExpired(): Credential {
42+
if (isExpired()) {
43+
refreshToken()
44+
}
45+
return this
3646
}
3747

3848
suspend fun authorize(modelixServerUrl: String): Credential {
@@ -67,6 +77,10 @@ actual object ModelixAuthClient {
6777
.enablePKCE()
6878
.setDataStoreFactory(DATA_STORE_FACTORY)
6979
.build()
80+
81+
val existingTokens = flow.loadCredential(userId)?.refreshIfExpired()
82+
if (existingTokens?.isExpired() == false) return@withContext existingTokens
83+
7084
val receiver: LocalServerReceiver = LocalServerReceiver.Builder().setHost("127.0.0.1").build()
7185
val browser = authRequestBrowser?.let {
7286
object : AuthorizationCodeInstalledApp.Browser {
@@ -75,7 +89,11 @@ actual object ModelixAuthClient {
7589
}
7690
}
7791
} ?: AuthorizationCodeInstalledApp.DefaultBrowser()
78-
AuthorizationCodeInstalledApp(flow, receiver, browser).authorize("user")
92+
val tokens = AuthorizationCodeInstalledApp(flow, receiver, browser).authorize(userId)
93+
if ((tokens.expiresInSeconds ?: 0) < 60) {
94+
tokens.refreshToken()
95+
}
96+
tokens
7997
}
8098
}
8199

@@ -136,8 +154,6 @@ actual object ModelixAuthClient {
136154
authorize(url)
137155
}
138156

139-
println("Access token: ${tokens.accessToken}")
140-
println("Refresh token: ${tokens.refreshToken}")
141157
BearerTokens(tokens.accessToken, tokens.refreshToken)
142158
}
143159
}

0 commit comments

Comments
 (0)