diff --git a/authorizer-app-backend/authorizer.yml b/authorizer-app-backend/authorizer.yml index 08c4917a..d844dd30 100644 --- a/authorizer-app-backend/authorizer.yml +++ b/authorizer-app-backend/authorizer.yml @@ -26,7 +26,6 @@ restSourceClients: clientId: clientSecret: scope: activity heartrate sleep profile - - sourceType: Huawei authorizationEndpoint: https://oauth-login.cloud.huawei.com/oauth2/v2/authorize tokenEndpoint: https://oauth-login.cloud.huawei.com/oauth2/v3/token diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/DelegatedRestSourceAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/DelegatedRestSourceAuthorizationService.kt index 6a3aad54..dd3b2a35 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/DelegatedRestSourceAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/DelegatedRestSourceAuthorizationService.kt @@ -33,6 +33,9 @@ class DelegatedRestSourceAuthorizationService( return provider.get() } + override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String): RestOauth2AccessToken = + delegate(sourceType).requestAccessToken(payload, sourceType) + override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String?): RestOauth2AccessToken = delegate(sourceType).requestAccessToken(payload, sourceType, token) diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOAuth2AuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOAuth2AuthorizationService.kt index 199ae649..e8fb7fd7 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOAuth2AuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOAuth2AuthorizationService.kt @@ -46,6 +46,7 @@ import org.radarbase.jersey.exception.HttpBadGatewayException import org.radarbase.jersey.exception.HttpInternalServerException import org.radarbase.jersey.service.AsyncCoroutineService import org.radarbase.kotlin.coroutines.forkJoin +import org.slf4j.LoggerFactory import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit @@ -225,6 +226,7 @@ class GarminOAuth2AuthorizationService( asyncService.runBlocking { userRepository .queryAllWithElapsedEndDate(GARMIN_AUTH) + .filter { it.authorized } .forkJoin { revokeToken(it) } } } @@ -244,7 +246,7 @@ class GarminOAuth2AuthorizationService( HttpStatusCode.OK -> response.body().userId HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden -> throw HttpBadGatewayException( - "Service was unable to fetch the external ID", + "Unable to fetch external ID (HTTP status ${response.status}): ${response.bodyAsText()}", ) else -> throw HttpBadGatewayException("Cannot connect to ${response.request.url}: HTTP status ${response.status}") @@ -252,6 +254,8 @@ class GarminOAuth2AuthorizationService( } companion object { + private val logger = LoggerFactory.getLogger(GarminOAuth2AuthorizationService::class.java) + private const val PKCE_CODE_CHALLENGE_METHOD = "S256" private const val GARMIN_USER_ID_ENDPOINT = "https://apis.garmin.com/wellness-api/rest/user/id" private const val DEREGISTER_CHECK_PERIOD = 3600000L diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOauth1AuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOauth1AuthorizationService.kt index d630d4e3..0cb0604e 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOauth1AuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/GarminOauth1AuthorizationService.kt @@ -80,6 +80,7 @@ class GarminOauth1AuthorizationService( asyncService.runBlocking { userRepository .queryAllWithElapsedEndDate(GARMIN_AUTH) + .filter { it.authorized } .forkJoin { revokeToken(it) } } } diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/HuaweiAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/HuaweiAuthorizationService.kt index c32a0526..6bcf1cc7 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/HuaweiAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/HuaweiAuthorizationService.kt @@ -21,6 +21,7 @@ import org.radarbase.authorizer.api.RestOauth2AccessToken import org.radarbase.authorizer.config.AuthorizerConfig import org.radarbase.authorizer.doa.entity.RestSourceUser import org.radarbase.jersey.exception.HttpBadGatewayException +import org.slf4j.LoggerFactory import java.util.Base64 /** @@ -162,6 +163,10 @@ class HuaweiAuthorizationService( } } + companion object { + private val logger = LoggerFactory.getLogger(HuaweiAuthorizationService::class.java) + } + @Serializable private data class HuaweiTokenResponse( @SerialName("access_token") val accessToken: String, diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth1RestSourceAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth1RestSourceAuthorizationService.kt index 834686ef..b40d10f5 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth1RestSourceAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth1RestSourceAuthorizationService.kt @@ -58,7 +58,7 @@ abstract class OAuth1RestSourceAuthorizationService( ) : RestSourceAuthorizationService { private val httpClient = RestSourceAuthorizationService.httpClient() - override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String?): RestOauth2AccessToken { + override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String): RestOauth2AccessToken { val authConfig = clientService.forSourceType(sourceType) logger.info("Requesting access token..") diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth2RestSourceAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth2RestSourceAuthorizationService.kt index e379e809..90cd7d3b 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth2RestSourceAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OAuth2RestSourceAuthorizationService.kt @@ -48,7 +48,7 @@ open class OAuth2RestSourceAuthorizationService( ) : RestSourceAuthorizationService { protected val httpClient = RestSourceAuthorizationService.httpClient() - override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String?): RestOauth2AccessToken = withContext(Dispatchers.IO) { + override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String): RestOauth2AccessToken = withContext(Dispatchers.IO) { logger.info("Requesting access token with authorization code") val response = submitForm(sourceType) { authorizationConfig -> payload.code?.let { append("code", it) } @@ -141,6 +141,6 @@ open class OAuth2RestSourceAuthorizationService( } companion object { - val logger: Logger = LoggerFactory.getLogger(OAuth2RestSourceAuthorizationService::class.java) + private val logger: Logger = LoggerFactory.getLogger(OAuth2RestSourceAuthorizationService::class.java) } } diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OuraAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OuraAuthorizationService.kt index b0c8ddf1..516a4a6e 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OuraAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/OuraAuthorizationService.kt @@ -19,14 +19,15 @@ import org.radarbase.authorizer.api.RestOauth2AccessToken import org.radarbase.authorizer.config.AuthorizerConfig import org.radarbase.authorizer.doa.entity.RestSourceUser import org.radarbase.jersey.exception.HttpBadGatewayException +import org.slf4j.LoggerFactory import java.io.IOException class OuraAuthorizationService( @Context private val clients: RestSourceClientService, @Context private val config: AuthorizerConfig, ) : OAuth2RestSourceAuthorizationService(clients, config) { - override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String?): RestOauth2AccessToken { - val accessToken: RestOauth2AccessToken = super.requestAccessToken(payload, sourceType, token) + override suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String): RestOauth2AccessToken { + val accessToken: RestOauth2AccessToken = super.requestAccessToken(payload, sourceType) return accessToken.copy(externalUserId = getExternalId(accessToken.accessToken)) } @@ -106,6 +107,7 @@ class OuraAuthorizationService( } companion object { + private val logger = LoggerFactory.getLogger(OuraAuthorizationService::class.java) private const val OURA_USER_ID_ENDPOINT = "https://api.ouraring.com/v2/usercollection/personal_info" } } diff --git a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/RestSourceAuthorizationService.kt b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/RestSourceAuthorizationService.kt index 0a455d04..842814af 100644 --- a/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/RestSourceAuthorizationService.kt +++ b/authorizer-app-backend/src/main/java/org/radarbase/authorizer/service/RestSourceAuthorizationService.kt @@ -31,7 +31,10 @@ import org.radarbase.authorizer.doa.entity.RestSourceUser import kotlin.time.Duration.Companion.seconds interface RestSourceAuthorizationService { - suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String? = null): RestOauth2AccessToken + suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String): RestOauth2AccessToken + + suspend fun requestAccessToken(payload: RequestTokenPayload, sourceType: String, token: String?): RestOauth2AccessToken = + requestAccessToken(payload, sourceType) suspend fun refreshToken(user: RestSourceUser): RestOauth2AccessToken?