Skip to content

Commit

Permalink
Prioritize the target token in okHttp
Browse files Browse the repository at this point in the history
Refactored the code

Signed-off-by: Saeed Rezaee <[email protected]>
  • Loading branch information
SaeedRe committed Feb 15, 2024
1 parent b9cac17 commit 571a41f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.eclipse.hara.ddi.security.Authentication
import org.eclipse.hara.ddi.security.HawkbitAuthenticationRequestInterceptor
import java.io.InputStream
import java.net.HttpURLConnection
import java.util.HashSet
import java.util.concurrent.Executors
import okhttp3.OkHttpClient
import org.eclipse.hara.ddiclient.api.HaraClientData
Expand Down Expand Up @@ -132,14 +131,14 @@ class DdiClientDefaultImpl private constructor(private val ddiRestApi: DdiRestAp
val LOG = LoggerFactory.getLogger(DdiClient::class.java)!!

fun of(haraClientData: HaraClientData, httpBuilder:OkHttpClient.Builder): DdiClientDefaultImpl {
val authentications = HashSet<Authentication>()
val authentications = mutableListOf<Authentication>()
with(haraClientData) {
if (gatewayToken != null) {
authentications.add(Authentication.newInstance(Authentication.AuthenticationType.GATEWAY_TOKEN_AUTHENTICATION, gatewayToken!!))
}
if (targetToken != null) {
if (!targetToken.isNullOrBlank()) {
authentications.add(Authentication.newInstance(Authentication.AuthenticationType.TARGET_TOKEN_AUTHENTICATION, targetToken!!))
}
if (!gatewayToken.isNullOrBlank()) {
authentications.add(Authentication.newInstance(Authentication.AuthenticationType.GATEWAY_TOKEN_AUTHENTICATION, gatewayToken!!))
}
httpBuilder.interceptors().add(0, HawkbitAuthenticationRequestInterceptor(authentications))
val ddiRestApi = Retrofit.Builder()
.baseUrl(serverUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
package org.eclipse.hara.ddi.security

import java.io.IOException
import java.util.ArrayList
import java.util.Objects
import okhttp3.Interceptor
import okhttp3.Response

/**
* @author Daniele Sergio
*/
class HawkbitAuthenticationRequestInterceptor(authentications: Set<Authentication>) : Interceptor {
class HawkbitAuthenticationRequestInterceptor(private val authentications: List<Authentication>) : Interceptor {

private val authentications: List<Authentication>
private var authenticationUse = 0

init {
Objects.requireNonNull(authentications)
this.authentications = ArrayList(authentications)
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data class HaraClientData constructor(
notEmpty(controllerId, "controllerId")
notEmpty(serverUrl, "serverUrl")
validUrl(serverUrl, "serverUrl")
if ((gatewayToken == null || gatewayToken.isBlank()) && (targetToken == null || targetToken.isBlank())) {
if (gatewayToken.isNullOrBlank() && targetToken.isNullOrBlank()) {
throw IllegalStateException("gatewayToken and targetToken cannot both be empty")
}
}
Expand Down

0 comments on commit 571a41f

Please sign in to comment.