Skip to content

Commit ffd926f

Browse files
committed
refactor: detekt LongMethod
1 parent b92b9b8 commit ffd926f

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

platform/core/src/main/kotlin/spp/platform/core/interceptors/SkyWalkingGrpcInterceptor.kt

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,13 @@ class SkyWalkingGrpcInterceptor(
5454
): ServerCall.Listener<ReqT> {
5555
val authHeader = headers?.get(AUTH_HEAD_HEADER_NAME)
5656
if (authHeader != null && probeAuthCache.getIfPresent(authHeader) != null) {
57-
val authParts = authHeader.split(":")
58-
val clientId = authParts.getOrNull(0)?.takeIf { it.isNotBlank() && it != "null" }
59-
val clientSecret = authParts.getOrNull(1)?.takeIf { it.isNotBlank() && it != "null" }
60-
val tenantId = authParts.getOrNull(2)?.takeIf { it.isNotBlank() && it != "null" }
61-
val environment = authParts.getOrNull(3)?.takeIf { it.isNotBlank() && it != "null" }
62-
val commitId = authParts.getOrNull(4)?.takeIf { it.isNotBlank() && it != "null" }
63-
64-
val context = Context.current()
65-
.withValue(ContextUtil.CLIENT_ID, clientId)
66-
.withValue(ContextUtil.CLIENT_ACCESS, clientSecret)
67-
.withValue(ContextUtil.TENANT_ID, tenantId)
68-
.withValue(ContextUtil.ENVIRONMENT, environment)
69-
.withValue(ContextUtil.VERSION, commitId)
57+
val (clientId, clientSecret, tenantId, environment, version) = extractPartsFromAuth(authHeader)
58+
val context = getContextWithValues(clientId, clientSecret, tenantId, environment, version)
7059
return Contexts.interceptCall(context, call, headers, next)
7160
} else {
7261
val authEnabled = config.getJsonObject("client-access")?.getString("enabled")?.toBooleanStrictOrNull()
7362
if (authEnabled == true) {
74-
val authParts = authHeader?.split(":") ?: emptyList()
75-
val clientId = authParts.getOrNull(0)?.takeIf { it.isNotBlank() && it != "null" }
76-
val clientSecret = authParts.getOrNull(1)?.takeIf { it.isNotBlank() && it != "null" }
77-
val tenantId = authParts.getOrNull(2)?.takeIf { it.isNotBlank() && it != "null" }
78-
val environment = authParts.getOrNull(3)?.takeIf { it.isNotBlank() && it != "null" }
79-
val version = authParts.getOrNull(4)?.takeIf { it.isNotBlank() && it != "null" }
63+
val (clientId, clientSecret, tenantId, environment, version) = extractPartsFromAuth(authHeader)
8064
if (authHeader == null || clientId == null || clientSecret == null) {
8165
log.warn { "Invalid auth header: $authHeader" }
8266
call.close(Status.PERMISSION_DENIED, Metadata())
@@ -108,12 +92,7 @@ class SkyWalkingGrpcInterceptor(
10892
}
10993
probeAuthCache.put(authHeader, true)
11094

111-
val context = Context.current()
112-
.withValue(ContextUtil.CLIENT_ID, clientId)
113-
.withValue(ContextUtil.CLIENT_ACCESS, clientSecret)
114-
.withValue(ContextUtil.TENANT_ID, tenantId)
115-
.withValue(ContextUtil.ENVIRONMENT, environment)
116-
.withValue(ContextUtil.VERSION, version)
95+
val context = getContextWithValues(clientId, clientSecret, tenantId, environment, version)
11796
Contexts.interceptCall(context, call, headers, next)
11897
}
11998
}
@@ -122,4 +101,29 @@ class SkyWalkingGrpcInterceptor(
122101
}
123102
}
124103
}
104+
105+
private fun extractPartsFromAuth(authHeader: String?): List<String?> {
106+
val authParts = authHeader?.split(":") ?: emptyList()
107+
val clientId = authParts.getOrNull(0)?.takeIf { it.isNotBlank() && it != "null" }
108+
val clientSecret = authParts.getOrNull(1)?.takeIf { it.isNotBlank() && it != "null" }
109+
val tenantId = authParts.getOrNull(2)?.takeIf { it.isNotBlank() && it != "null" }
110+
val environment = authParts.getOrNull(3)?.takeIf { it.isNotBlank() && it != "null" }
111+
val version = authParts.getOrNull(4)?.takeIf { it.isNotBlank() && it != "null" }
112+
return listOf(clientId, clientSecret, tenantId, environment, version)
113+
}
114+
115+
private fun getContextWithValues(
116+
clientId: String?,
117+
clientSecret: String?,
118+
tenantId: String?,
119+
environment: String?,
120+
version: String?
121+
): Context {
122+
return Context.current()
123+
.withValue(ContextUtil.CLIENT_ID, clientId)
124+
.withValue(ContextUtil.CLIENT_ACCESS, clientSecret)
125+
.withValue(ContextUtil.TENANT_ID, tenantId)
126+
.withValue(ContextUtil.ENVIRONMENT, environment)
127+
.withValue(ContextUtil.VERSION, version)
128+
}
125129
}

0 commit comments

Comments
 (0)