Make vtokens scheme configurable

This commit is contained in:
Mikołaj Pich 2024-05-14 21:43:27 +02:00
parent 50e06b2a06
commit 03066e18a2
No known key found for this signature in database
7 changed files with 60 additions and 9 deletions

View file

@ -83,6 +83,7 @@ public final class io/github/wulkanowy/sdk/scrapper/Scrapper {
public final fun getUserAgentTemplate ()Ljava/lang/String;
public final fun getUserSubjects (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getVTokenMapping ()Ljava/util/Map;
public final fun getVTokenSchemeMapping ()Ljava/util/Map;
public final fun isEduOne ()Z
public final fun isSymbolNotExist (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun restoreMessages (Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@ -112,6 +113,7 @@ public final class io/github/wulkanowy/sdk/scrapper/Scrapper {
public final fun setUnitId (I)V
public final fun setUserAgentTemplate (Ljava/lang/String;)V
public final fun setVTokenMapping (Ljava/util/Map;)V
public final fun setVTokenSchemeMapping (Ljava/util/Map;)V
public final fun unregisterDevice (ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

View file

@ -271,3 +271,12 @@ internal val ApiEndpointsVTokenMap = mapOf(
),
),
)
internal val ApiEndpointsVTokenSchemeMap = mapOf(
"24.04.0003.58698" to mapOf(
"uonetplus-wiadomosciplus" to "{UUID}-{name}-{appCustomerDb}-{appVersion}",
),
"24.04.0004.58722" to mapOf(
"uonetplus-wiadomosciplus" to "{UUID}-{appCustomerDb}-{appVersion}",
),
)

View file

@ -224,9 +224,16 @@ class Scrapper {
vTokenMap = value
}
var vTokenSchemeMapping: Map<String, Map<String, String>>
get() = vTokenSchemeMap
set(value) {
vTokenSchemeMap = value
}
internal companion object {
var endpointsMap: Map<String, Map<String, Map<String, String>>> = ApiEndpointsMap
var vTokenMap: Map<String, Map<String, Map<String, String>>> = ApiEndpointsVTokenMap
var vTokenSchemeMap: Map<String, Map<String, String>> = ApiEndpointsVTokenSchemeMap
}
private val appInterceptors: MutableList<Pair<Interceptor, Boolean>> = mutableListOf()

View file

@ -242,7 +242,10 @@ internal fun getPathIndexByModuleHost(moduleHost: String): Int = when (moduleHos
else -> -1
}
private val vParamsRegex = "([a-zA-Z]+)\\s*:\\s*'([^']*)'".toRegex()
internal fun getModuleHeadersFromDocument(htmlContent: String): ModuleHeaders {
val matches = vParamsRegex.findAll(htmlContent)
return ModuleHeaders(
token = getScriptParam("antiForgeryToken", htmlContent),
appGuid = getScriptParam("appGuid", htmlContent),
@ -251,6 +254,13 @@ internal fun getModuleHeadersFromDocument(htmlContent: String): ModuleHeaders {
},
email = getScriptParam("name", htmlContent),
symbol = getScriptParam("appCustomerDb", htmlContent),
vParams = matches.toList().associate { match ->
if (match.groupValues.size == 3) {
match.groupValues[1] to match.groupValues[2]
} else {
null to null
}
},
)
}
@ -268,17 +278,31 @@ internal fun HttpUrl.getMatchedVToken(moduleHost: String, headers: ModuleHeaders
?.get(pathKey)
?: return null
return getVToken(mappedUuid, headers)
return getVToken(mappedUuid, headers, moduleHost)
}
private fun getVToken(uuid: String, headers: ModuleHeaders?): String? {
private val vTokenSchemeKeysRegex = "\\{([^{}]+)\\}".toRegex()
private fun getVToken(uuid: String, headers: ModuleHeaders?, moduleHost: String): String? {
if (uuid.isBlank()) return null
return buildString {
append(uuid)
append("-")
append(headers?.symbol)
append("-")
append(headers?.appVersion)
}.md5()
val scheme = Scrapper.vTokenSchemeMap[headers?.appVersion]
?.get(moduleHost)
?: "{UUID}-{appCustomerDb}-{appVersion}"
val schemeToSubstitute = scheme.replace("{UUID}", uuid)
val vTokenEncoded = runCatching {
vTokenSchemeKeysRegex.replace(schemeToSubstitute) {
val key = it.groupValues[1]
headers?.vParams.orEmpty()[key] ?: key
}
}.onFailure {
logger.error("Error preparing vtoken!", it)
}.getOrDefault(
schemeToSubstitute
.replace("{appCustomerDb}", headers?.symbol.orEmpty())
.replace("{appVersion}", headers?.appVersion.orEmpty())
)
return vTokenEncoded.md5()
}

View file

@ -6,4 +6,5 @@ internal data class ModuleHeaders(
val appVersion: String,
val symbol: String? = null,
val email: String? = null,
val vParams: Map<String?, String?> = emptyMap(),
)

View file

@ -91,6 +91,7 @@ public final class io/github/wulkanowy/sdk/Sdk {
public final fun getUserSubjectsFromScrapper (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getUserSubjectsFromScrapper$default (Lio/github/wulkanowy/sdk/Sdk;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getVTokenMapping ()Ljava/util/Map;
public final fun getVTokenSchemeMapping ()Ljava/util/Map;
public final fun isEduOne ()Z
public final fun isSymbolNotExist (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun restoreMessages (Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@ -123,6 +124,7 @@ public final class io/github/wulkanowy/sdk/Sdk {
public final fun setUnitId (I)V
public final fun setUserAgentTemplate (Ljava/lang/String;)V
public final fun setVTokenMapping (Ljava/util/Map;)V
public final fun setVTokenSchemeMapping (Ljava/util/Map;)V
public final fun switchDiary (III)Lio/github/wulkanowy/sdk/Sdk;
public final fun switchDiary (IIII)Lio/github/wulkanowy/sdk/Sdk;
public static synthetic fun switchDiary$default (Lio/github/wulkanowy/sdk/Sdk;IIIIILjava/lang/Object;)Lio/github/wulkanowy/sdk/Sdk;

View file

@ -247,6 +247,12 @@ class Sdk {
scrapper.vTokenMapping = value
}
var vTokenSchemeMapping
get() = scrapper.vTokenSchemeMapping
set(value) {
scrapper.vTokenSchemeMapping = value
}
var emptyCookieJarInterceptor: Boolean = false
set(value) {
field = value