Skip to content

Commit 413c71e

Browse files
committed
Fix lint errors
1 parent 19f92c5 commit 413c71e

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

kafka-connect-fitbit-source/src/main/java/org/radarbase/connect/rest/fitbit/user/ServiceUserRepository.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ServiceUserRepository : UserRepository {
7575
private val credentialCacheConfig =
7676
CacheConfig(refreshDuration = 1.days, retryDuration = 1.minutes)
7777
private val mapper = ObjectMapper().registerKotlinModule().registerModule(JavaTimeModule())
78-
78+
7979
// User repository service token cache for OAuth2 client credentials authentication
8080
private var userRepositoryTokenCache: CachedValue<String>? = null
8181
private val userRepositoryCacheConfig = CacheConfig(
@@ -203,7 +203,9 @@ class ServiceUserRepository : UserRepository {
203203
val response = authClient.request {
204204
url(tokenUrl)
205205
method = HttpMethod.Post
206-
setBody("grant_type=client_credentials&scope=${scope ?: ""}&audience=${audience ?: ""}")
206+
setBody(
207+
"grant_type=client_credentials&scope=${scope ?: ""}&audience=${audience ?: ""}",
208+
)
207209
contentType(ContentType.Application.FormUrlEncoded)
208210
}
209211

@@ -227,7 +229,8 @@ class ServiceUserRepository : UserRepository {
227229

228230
private suspend fun getAuthorizationHeader(): String {
229231
return try {
230-
userRepositoryTokenCache?.get() ?: throw IllegalStateException("User repository token cache not initialized")
232+
userRepositoryTokenCache?.get()
233+
?: throw IllegalStateException("User repository token cache not initialized")
231234
} catch (e: Exception) {
232235
logger.error("Failed to get authorization header", e)
233236
throw e

kafka-connect-oura-source/src/main/java/org/radarbase/connect/rest/oura/user/OuraServiceUserRepository.kt

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import com.fasterxml.jackson.module.kotlin.registerKotlinModule
2323
import io.ktor.client.HttpClient
2424
import io.ktor.client.engine.cio.CIO
2525
import io.ktor.client.plugins.HttpTimeout
26-
import io.ktor.client.plugins.auth.Auth
27-
import io.ktor.client.plugins.auth.providers.BasicAuthCredentials
28-
import io.ktor.client.plugins.auth.providers.basic
2926
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
3027
import io.ktor.client.plugins.defaultRequest
3128
import io.ktor.client.request.HttpRequestBuilder
@@ -54,8 +51,6 @@ import org.radarbase.connect.rest.oura.OuraRestSourceConnectorConfig
5451
import org.radarbase.kotlin.coroutines.CacheConfig
5552
import org.radarbase.kotlin.coroutines.CachedSet
5653
import org.radarbase.kotlin.coroutines.CachedValue
57-
import org.radarbase.ktor.auth.ClientCredentialsConfig
58-
import org.radarbase.ktor.auth.clientCredentials
5954
import org.radarbase.oura.user.OuraUser
6055
import org.radarbase.oura.user.User
6156
import org.slf4j.LoggerFactory
@@ -75,13 +70,19 @@ class OuraServiceUserRepository : OuraUserRepository() {
7570
private var basicAuthCredentials: String? = null
7671
private val credentialCaches = ConcurrentHashMap<String, CachedValue<OAuth2UserCredentials>>()
7772
private val credentialCacheConfig =
78-
CacheConfig(refreshDuration = 1.days, retryDuration = 1.minutes)
73+
CacheConfig(
74+
refreshDuration = 1.days,
75+
retryDuration = 1.minutes,
76+
)
7977
private val userRepositoryCacheConfig =
80-
CacheConfig(refreshDuration = 50.minutes, retryDuration = 1.minutes) // Refresh before 1-hour expiry
78+
CacheConfig(
79+
refreshDuration = 50.minutes,
80+
retryDuration = 1.minutes,
81+
) // Refresh before 1-hour expiry
8182
private val mapper = ObjectMapper().registerKotlinModule().registerModule(JavaTimeModule())
8283

8384
private var tokenUrl: String? = null
84-
private var clientId: String? = null
85+
private var clientId: String? = null
8586
private var clientSecret: String? = null
8687

8788
@Throws(IOException::class)
@@ -97,16 +98,19 @@ class OuraServiceUserRepository : OuraUserRepository() {
9798
tokenUrl = config.ouraUserRepositoryTokenUrl?.toString()
9899
clientId = config.ouraUserRepositoryClientId
99100
clientSecret = config.ouraUserRepositoryClientSecret
100-
101+
101102
if (tokenUrl != null && clientId != null && clientSecret != null) {
102-
logger.info("Using OAuth2 client credentials authentication with token URL: $tokenUrl")
103-
103+
logger.info(
104+
"Using OAuth2 client credentials authentication with token URL: $tokenUrl",
105+
)
106+
104107
userRepositoryTokenCache = CachedValue(userRepositoryCacheConfig) {
105108
requestUserRepositoryToken()
106109
}
107110
} else if (!clientId.isNullOrBlank() && !clientSecret.isNullOrBlank()) {
108111
logger.info("Using basic authentication")
109-
basicAuthCredentials = "Basic " + java.util.Base64.getEncoder().encodeToString("$clientId:$clientSecret".toByteArray())
112+
basicAuthCredentials = "Basic " + java.util.Base64.getEncoder()
113+
.encodeToString("$clientId:$clientSecret".toByteArray())
110114
} else {
111115
logger.warn("No authentication configured - this may cause issues")
112116
}
@@ -144,31 +148,40 @@ class OuraServiceUserRepository : OuraUserRepository() {
144148
requestTimeoutMillis = 90.seconds.inWholeMilliseconds
145149
}
146150
}
147-
151+
148152
private suspend fun requestUserRepositoryToken(): String {
149153
val tmpClient = HttpClient(CIO) {
150154
install(ContentNegotiation) {
151155
json(Json { ignoreUnknownKeys = true })
152156
}
153157
}
154-
158+
155159
try {
156160
val response = tmpClient.request {
157161
method = HttpMethod.Post
158162
url(tokenUrl!!)
159163
contentType(ContentType.Application.FormUrlEncoded)
160-
setBody("grant_type=client_credentials&scope=SUBJECT.READ MEASUREMENT.CREATE&audience=res_restAuthorizer")
161-
headers.append("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString("$clientId:$clientSecret".toByteArray()))
164+
setBody(
165+
"grant_type=client_credentials&scope=SUBJECT.READ MEASUREMENT.CREATE" +
166+
"&audience=res_restAuthorizer",
167+
)
168+
headers.append(
169+
"Authorization",
170+
"Basic " + java.util.Base64.getEncoder()
171+
.encodeToString("$clientId:$clientSecret".toByteArray()),
172+
)
162173
}
163-
174+
164175
if (response.status.isSuccess()) {
165176
val tokenData = Json.parseToJsonElement(response.bodyAsText()).jsonObject
166177
val accessToken = tokenData["access_token"]?.toString()?.trim('"')
167178
?: throw IOException("No access token in response")
168179
logger.debug("Successfully obtained user repository access token")
169180
return "Bearer $accessToken"
170181
} else {
171-
throw IOException("Failed to get user repository token: ${response.status} - ${response.bodyAsText()}")
182+
throw IOException(
183+
"Failed to get repository token: ${response.status} - ${response.bodyAsText()}",
184+
)
172185
}
173186
} finally {
174187
tmpClient.close()
@@ -237,7 +250,9 @@ class OuraServiceUserRepository : OuraUserRepository() {
237250
when (ex.statusCode) {
238251
401 -> {
239252
credentialCaches -= user.id
240-
throw UserNotAuthorizedException("Token refresh failed - user needs re-authorization: ${ex.message}")
253+
throw UserNotAuthorizedException(
254+
"Token refresh failed - user needs re-authorization: ${ex.message}",
255+
)
241256
}
242257
407 -> {
243258
credentialCaches -= user.id
@@ -266,17 +281,17 @@ class OuraServiceUserRepository : OuraUserRepository() {
266281
): T =
267282
withContext(Dispatchers.IO) {
268283
val authorization = getAuthorizationHeader()
269-
284+
270285
val response = client.request {
271286
builder()
272287
if (authorization != null) {
273288
headers.append("Authorization", authorization)
274289
}
275290
}
276-
291+
277292
val contentLength = response.contentLength()
278293
val hasBody = contentLength != null && contentLength > 0
279-
294+
280295
if (response.status == HttpStatusCode.NotFound) {
281296
throw NoSuchElementException("URL " + response.request.url + " does not exist")
282297
} else if (!response.status.isSuccess() || !hasBody) {

0 commit comments

Comments
 (0)