From e838750ee2217160e50076ded13cefb0412184b4 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Sat, 2 Dec 2017 21:21:23 +0100 Subject: [PATCH] Add proxy password interceptor. It passes request chain either to hex password interceptor or to md5+salt interceptor depending on current protocol version. --- dependencies.gradle | 3 ++ subsonic-api/build.gradle | 2 ++ .../interceptors/ProxyPasswordInterceptor.kt | 23 ++++++++++++ .../ProxyPasswordInterceptorTest.kt | 36 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptor.kt create mode 100644 subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptorTest.kt diff --git a/dependencies.gradle b/dependencies.gradle index 6121ccd2..89a1869c 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -21,6 +21,7 @@ ext.versions = [ okhttp : "3.9.0", junit : "4.12", + mockito : "2.12.0", mockitoKotlin : "1.5.0", kluent : "1.26", apacheCodecs : "1.10", @@ -53,6 +54,8 @@ ext.testing = [ junit : "junit:junit:$versions.junit", kotlinJunit : "org.jetbrains.kotlin:kotlin-test-junit:$versions.kotlin", mockitoKotlin : "com.nhaarman:mockito-kotlin:$versions.mockitoKotlin", + mockito : "org.mockito:mockito-core:$versions.mockito", + mockitoInline : "org.mockito:mockito-inline:$versions.mockito", kluent : "org.amshove.kluent:kluent:$versions.kluent", mockWebServer : "com.squareup.okhttp3:mockwebserver:$versions.okhttp", apacheCodecs : "commons-codec:commons-codec:$versions.apacheCodecs", diff --git a/subsonic-api/build.gradle b/subsonic-api/build.gradle index 7fbf319c..004778b0 100644 --- a/subsonic-api/build.gradle +++ b/subsonic-api/build.gradle @@ -21,6 +21,8 @@ dependencies { testImplementation testing.junit testImplementation testing.kotlinJunit + testImplementation testing.mockito + testImplementation testing.mockitoInline testImplementation testing.mockitoKotlin testImplementation testing.kluent testImplementation testing.mockWebServer diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptor.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptor.kt new file mode 100644 index 00000000..0273f984 --- /dev/null +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptor.kt @@ -0,0 +1,23 @@ +package org.moire.ultrasonic.api.subsonic.interceptors + +import okhttp3.Interceptor +import okhttp3.Interceptor.Chain +import okhttp3.Response +import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions + +/** + * Proxy [Interceptor] that uses one of [hexInterceptor] or [mD5Interceptor] depends on [apiVersion]. + */ +internal class ProxyPasswordInterceptor( + initialAPIVersions: SubsonicAPIVersions, + private val hexInterceptor: PasswordHexInterceptor, + private val mD5Interceptor: PasswordMD5Interceptor) : Interceptor { + var apiVersion: SubsonicAPIVersions = initialAPIVersions + + override fun intercept(chain: Chain): Response = + if (apiVersion < SubsonicAPIVersions.V1_13_0) { + hexInterceptor.intercept(chain) + } else { + mD5Interceptor.intercept(chain) + } +} diff --git a/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptorTest.kt b/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptorTest.kt new file mode 100644 index 00000000..2b7d9bcd --- /dev/null +++ b/subsonic-api/src/test/kotlin/org/moire/ultrasonic/api/subsonic/interceptors/ProxyPasswordInterceptorTest.kt @@ -0,0 +1,36 @@ +package org.moire.ultrasonic.api.subsonic.interceptors + +import com.nhaarman.mockito_kotlin.mock +import com.nhaarman.mockito_kotlin.verify +import okhttp3.Interceptor.Chain +import org.junit.Test +import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_12_0 +import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_13_0 + +/** + * Unit test for [ProxyPasswordInterceptor]. + */ +class ProxyPasswordInterceptorTest { + private val mockPasswordHexInterceptor = mock() + private val mockPasswordMd5Interceptor = mock() + private val mockChain = mock() + + private val proxyInterceptor = ProxyPasswordInterceptor(V1_12_0, + mockPasswordHexInterceptor, mockPasswordMd5Interceptor) + + @Test + fun `Should use hex password on versions less then 1 13 0`() { + proxyInterceptor.intercept(mockChain) + + verify(mockPasswordHexInterceptor).intercept(mockChain) + } + + @Test + fun `Should use md5 password on version 1 13 0`() { + proxyInterceptor.apiVersion = V1_13_0 + + proxyInterceptor.intercept(mockChain) + + verify(mockPasswordMd5Interceptor).intercept(mockChain) + } +}