Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use RequestExecutor the Bearer AuthenticationManager #126

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ package com.expediagroup.sdk.core.authentication.bearer

import com.expediagroup.sdk.core.authentication.common.AuthenticationManager
import com.expediagroup.sdk.core.authentication.common.Credentials
import com.expediagroup.sdk.core.client.Transport
import com.expediagroup.sdk.core.http.Method
import com.expediagroup.sdk.core.client.RequestExecutor
import com.expediagroup.sdk.core.http.CommonMediaTypes
import com.expediagroup.sdk.core.http.Method
import com.expediagroup.sdk.core.http.Request
import com.expediagroup.sdk.core.http.RequestBody
import com.expediagroup.sdk.core.http.Response
import com.expediagroup.sdk.core.logging.common.LoggerDecorator
import com.expediagroup.sdk.core.logging.common.RequestLogger
import com.expediagroup.sdk.core.logging.common.ResponseLogger
import com.expediagroup.sdk.core.model.exception.client.ExpediaGroupResponseParsingException
import com.expediagroup.sdk.core.model.exception.service.ExpediaGroupAuthException
import com.expediagroup.sdk.core.model.exception.service.ExpediaGroupNetworkException
import org.slf4j.LoggerFactory

/**
* Manages bearer token authentication for HTTP requests.
Expand All @@ -39,14 +35,14 @@ import org.slf4j.LoggerFactory
* and validation. It interacts with an authentication server to fetch tokens using client credentials,
* ensures tokens are refreshed when necessary, and provides them in the required format for authorization headers.
*
* @param transport The [Transport] used to execute authentication requests.
* @param requestExecutor The [RequestExecutor] used to execute authentication requests.
* @param authUrl The URL of the authentication server's endpoint to obtain bearer tokens.
* @param credentials The [Credentials] containing the client key and secret used for authentication.
*/
class BearerAuthenticationManager(
val authUrl: String,
private val transport: Transport,
private val credentials: Credentials
private val requestExecutor: RequestExecutor,
private val credentials: Credentials,
) : AuthenticationManager {

@Volatile
Expand All @@ -65,13 +61,9 @@ class BearerAuthenticationManager(
override fun authenticate() {
clearAuthentication()
.let {
buildAuthenticationRequest().also {
RequestLogger.log(logger, it, "Authentication")
}
buildAuthenticationRequest()
}.let {
executeAuthenticationRequest(it).also {
ResponseLogger.log(logger, it, "Authentication")
}
executeAuthenticationRequest(it)
}.let {
TokenResponse.parse(it)
}.also {
Expand Down Expand Up @@ -130,7 +122,7 @@ class BearerAuthenticationManager(
*/
@Throws(ExpediaGroupAuthException::class, ExpediaGroupNetworkException::class)
private fun executeAuthenticationRequest(request: Request): Response = run {
transport.execute(request).apply {
requestExecutor.execute(request).apply {
if (!this.isSuccessful) {
throw ExpediaGroupAuthException(this.status, "Authentication failed")
}
Expand All @@ -148,8 +140,4 @@ class BearerAuthenticationManager(
expiresIn = tokenResponse.expiresIn
)
}

private companion object {
private val logger = LoggerDecorator(LoggerFactory.getLogger(this::class.java.enclosingClass))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import com.expediagroup.sdk.core.logging.common.LoggerDecorator
import com.expediagroup.sdk.core.logging.common.RequestLogger
import com.expediagroup.sdk.core.logging.common.ResponseLogger
import java.io.IOException
import org.slf4j.LoggerFactory

/**
* An interceptor that logs HTTP requests and responses.
*
* @param maxBodyLogSize The maximum size of the request/response body to log. Defaults to 1MB.
*/
class LoggingInterceptor(
private val logger: LoggerDecorator,
private val maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE
) : Interceptor {

Expand All @@ -30,4 +30,8 @@ class LoggingInterceptor(

return response
}

companion object {
private val logger = LoggerDecorator(LoggerFactory.getLogger(this::class.java.enclosingClass))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import com.expediagroup.sdk.core.http.Response
import com.expediagroup.sdk.core.interceptor.Interceptor
import com.expediagroup.sdk.core.interceptor.InterceptorsChainExecutor
import com.expediagroup.sdk.core.logging.LoggingInterceptor
import com.expediagroup.sdk.core.logging.common.LoggerDecorator
import com.expediagroup.sdk.core.okhttp.BaseOkHttpClient
import com.expediagroup.sdk.core.okhttp.OkHttpTransport
import com.expediagroup.sdk.lodgingconnectivity.configuration.ApiEndpoint
import com.expediagroup.sdk.lodgingconnectivity.configuration.ClientConfiguration
import com.expediagroup.sdk.lodgingconnectivity.configuration.CustomClientConfiguration
import com.expediagroup.sdk.lodgingconnectivity.configuration.DefaultClientConfiguration
import org.slf4j.LoggerFactory

internal fun getHttpTransport(configuration: ClientConfiguration): Transport = when (configuration) {
is CustomClientConfiguration -> configuration.transport
Expand All @@ -30,12 +28,12 @@ class DefaultRequestExecutor(
) : RequestExecutor(getHttpTransport(configuration)) {

override val interceptors: List<Interceptor> = listOf(
LoggingInterceptor(logger),
LoggingInterceptor(),
BearerAuthenticationInterceptor(
BearerAuthenticationManager(
transport = this.transport,
requestExecutor = this,
authUrl = apiEndpoint.authEndpoint,
credentials = Credentials(configuration.key, configuration.secret)
credentials = Credentials(configuration.key, configuration.secret),
)
)
)
Expand All @@ -49,9 +47,5 @@ class DefaultRequestExecutor(

return chainExecutor.proceed(request)
}

companion object {
private val logger = LoggerDecorator(LoggerFactory.getLogger(this::class.java.enclosingClass))
}
}