@@ -23,9 +23,6 @@ import com.fasterxml.jackson.module.kotlin.registerKotlinModule
2323import io.ktor.client.HttpClient
2424import io.ktor.client.engine.cio.CIO
2525import 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
2926import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
3027import io.ktor.client.plugins.defaultRequest
3128import io.ktor.client.request.HttpRequestBuilder
@@ -54,8 +51,6 @@ import org.radarbase.connect.rest.oura.OuraRestSourceConnectorConfig
5451import org.radarbase.kotlin.coroutines.CacheConfig
5552import org.radarbase.kotlin.coroutines.CachedSet
5653import org.radarbase.kotlin.coroutines.CachedValue
57- import org.radarbase.ktor.auth.ClientCredentialsConfig
58- import org.radarbase.ktor.auth.clientCredentials
5954import org.radarbase.oura.user.OuraUser
6055import org.radarbase.oura.user.User
6156import 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