Skip to content

Commit

Permalink
feat: Supporting default auth url (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamzanael authored Sep 1, 2022
1 parent 97db542 commit c2ac700
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<artifactId>openworld-java-sdk-core</artifactId>
<name>EG Open World SDK :: Core</name>
<description>Core Modules of EG Travel SDK</description>
<version>0.0.1-alpha.4</version>
<version>0.0.1-alpha.5</version>

<build>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/expediagroup/sdk/core/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Client private constructor(
init {
httpClient = HttpClient(httpClientEngine) {
val authenticationConfigs =
AuthenticationConfigs.from(this, configuration.clientConfiguration, environmentConfigs.baseUrl)
AuthenticationConfigs.from(this, configuration.clientConfiguration, environmentConfigs.identityUrl)

plugins {
use(LoggingPlugin).with(LoggingConfigs.from(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
package com.expediagroup.sdk.core.config

import com.expediagroup.sdk.core.config.provider.FileConfigurationProvider
import com.expediagroup.sdk.core.constants.ClientConstants.AUTH_URL
import com.expediagroup.sdk.core.constants.ClientConstants.BASE_URL
import com.expediagroup.sdk.core.constants.ClientConstants.CLIENT_CONFIGS_FILE_PATH
import com.expediagroup.sdk.core.constants.ClientConstants.CLIENT_KEY
import com.expediagroup.sdk.core.constants.ClientConstants.CLIENT_SECRET
import com.expediagroup.sdk.core.constants.ClientConstants.CREDENTIALS_FILE_PATH
import com.expediagroup.sdk.core.constants.ClientConstants.DEFAULT_AUTH_URL
import com.expediagroup.sdk.core.constants.ClientConstants.DEFAULT_BASE_URL
import com.expediagroup.sdk.core.constants.ClientConstants.EMPTY_STRING
import com.expediagroup.sdk.core.plugin.authentication.IdentityUrl

object EnvironmentConfigurationProvider {
val configuration: Configuration = prepareConfiguration()
Expand All @@ -40,7 +41,7 @@ object EnvironmentConfigurationProvider {
private fun prepareClientEnvironmentConfigs(): EnvironmentConfigs {
val clientEnvironmentConfigsData = FileConfigurationProvider()[CLIENT_CONFIGS_FILE_PATH, true].data()
val baseUrl = clientEnvironmentConfigsData[BASE_URL] ?: DEFAULT_BASE_URL
val identityUrl = IdentityUrl.from(baseUrl)
val identityUrl = clientEnvironmentConfigsData[AUTH_URL] ?: DEFAULT_AUTH_URL

return EnvironmentConfigs(baseUrl, identityUrl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ object ClientConstants {

const val DEFAULT_BASE_URL = "https://api.expediagroup.com"

const val DEFAULT_AUTH_URL = "$DEFAULT_BASE_URL/identity/oauth2/v2/token"

const val AUTHORIZATION_REQUEST_LOCK_DELAY = 20L // TODO: Update me.

const val AUTHORIZATION_HEADER = "Authorization"
Expand All @@ -40,6 +42,8 @@ object ClientConstants {

const val BASE_URL = "base_url"

const val AUTH_URL = "auth_url"

private val OPEN_WORLD_HOME_PATH = "${System.getProperty("user.home")}${File.separator}.openworld${File.separator}"

val CREDENTIALS_FILE_PATH = "${OPEN_WORLD_HOME_PATH}credentials"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import io.ktor.client.engine.HttpClientEngineConfig
data class AuthenticationConfigs(
override val httpClientConfig: HttpClientConfig<out HttpClientEngineConfig>,
val clientConfiguration: ClientConfiguration,
val baseUrl: String
val authUrl: String
) : KtorPluginConfigs(httpClientConfig) {
companion object {
fun from(
httpClientConfig: HttpClientConfig<out HttpClientEngineConfig>,
clientConfiguration: ClientConfiguration,
baseUrl: String
): AuthenticationConfigs = AuthenticationConfigs(httpClientConfig, clientConfiguration, IdentityUrl.from(baseUrl))
authUrl: String
): AuthenticationConfigs = AuthenticationConfigs(httpClientConfig, clientConfiguration, authUrl)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ object AuthenticationPlugin : Plugin<AuthenticationConfigs> {
request: HttpRequestBuilder,
configs: AuthenticationConfigs
): Boolean =
request.url.buildString() != configs.baseUrl
request.url.buildString() != configs.authUrl

suspend fun refreshToken(client: HttpClient, configs: AuthenticationConfigs) {
clearTokens(client)
val refreshTokenResponse = client.request {
method = HttpMethod.Post
url(configs.baseUrl)
url(configs.authUrl)
buildTokenRequest()
basicAuth(configs.clientConfiguration)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.expediagroup.sdk.core.config.ClientCredentials
import com.expediagroup.sdk.core.config.Configuration
import com.expediagroup.sdk.core.config.EnvironmentConfigs
import com.expediagroup.sdk.core.config.EnvironmentConfigurationProvider
import com.expediagroup.sdk.core.plugin.authentication.IdentityUrl
import com.expediagroup.sdk.core.constants.ClientConstants.DEFAULT_AUTH_URL
import io.ktor.client.HttpClient
import io.mockk.every
import io.mockk.mockkConstructor
Expand All @@ -45,7 +45,7 @@ object ClientFactory {
)
every { EnvironmentConfigurationProvider.clientEnvironmentConfigs } returns EnvironmentConfigs(
TEST_URL,
IdentityUrl.from(TEST_URL)
DEFAULT_AUTH_URL
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import com.expediagroup.sdk.core.commons.ClientFactory
import com.expediagroup.sdk.core.commons.TestConstants.ACCESS_TOKEN
import com.expediagroup.sdk.core.commons.TestConstants.CLIENT_KEY_TEST_CREDENTIAL
import com.expediagroup.sdk.core.commons.TestConstants.CLIENT_SECRET_TEST_CREDENTIAL
import com.expediagroup.sdk.core.commons.TestConstants.TEST_URL
import com.expediagroup.sdk.core.config.ClientConfiguration
import com.expediagroup.sdk.core.config.ClientCredentials
import com.expediagroup.sdk.core.constants.ClientConstants.DEFAULT_AUTH_URL
import com.expediagroup.sdk.core.exceptions.ClientException
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
Expand Down Expand Up @@ -66,7 +66,7 @@ class AuthenticationPluginTest {
CLIENT_SECRET_TEST_CREDENTIAL + "invalid"
)
),
TEST_URL
DEFAULT_AUTH_URL
)
)
}
Expand Down

0 comments on commit c2ac700

Please sign in to comment.