Add flag to force using hex password authentication.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-01-21 18:06:41 +01:00
parent cd54632339
commit 7830ed3dbf
3 changed files with 26 additions and 5 deletions

View File

@ -41,13 +41,17 @@ class SubsonicAPIClient(baseUrl: String,
minimalProtocolVersion: SubsonicAPIVersions, minimalProtocolVersion: SubsonicAPIVersions,
clientID: String, clientID: String,
allowSelfSignedCertificate: Boolean = false, allowSelfSignedCertificate: Boolean = false,
enableLdapUserSupport: Boolean = false,
debug: Boolean = false) { debug: Boolean = false) {
private val versionInterceptor = VersionInterceptor(minimalProtocolVersion) { private val versionInterceptor = VersionInterceptor(minimalProtocolVersion) {
protocolVersion = it protocolVersion = it
} }
private val proxyPasswordInterceptor = ProxyPasswordInterceptor(minimalProtocolVersion, private val proxyPasswordInterceptor = ProxyPasswordInterceptor(
PasswordHexInterceptor(password), PasswordMD5Interceptor(password)) minimalProtocolVersion,
PasswordHexInterceptor(password),
PasswordMD5Interceptor(password),
enableLdapUserSupport)
/** /**
* Get currently used protocol version. * Get currently used protocol version.

View File

@ -7,15 +7,21 @@ import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
/** /**
* Proxy [Interceptor] that uses one of [hexInterceptor] or [mD5Interceptor] depends on [apiVersion]. * Proxy [Interceptor] that uses one of [hexInterceptor] or [mD5Interceptor] depends on [apiVersion].
*
* To force [hexInterceptor] set [forceHexPassword] to `true`. Usually it should be done only for
* ldap users.
*/ */
internal class ProxyPasswordInterceptor( internal class ProxyPasswordInterceptor(
initialAPIVersions: SubsonicAPIVersions, initialAPIVersions: SubsonicAPIVersions,
private val hexInterceptor: PasswordHexInterceptor, private val hexInterceptor: PasswordHexInterceptor,
private val mD5Interceptor: PasswordMD5Interceptor) : Interceptor { private val mD5Interceptor: PasswordMD5Interceptor,
private val forceHexPassword: Boolean = false
) : Interceptor {
var apiVersion: SubsonicAPIVersions = initialAPIVersions var apiVersion: SubsonicAPIVersions = initialAPIVersions
override fun intercept(chain: Chain): Response = override fun intercept(chain: Chain): Response =
if (apiVersion < SubsonicAPIVersions.V1_13_0) { if (apiVersion < SubsonicAPIVersions.V1_13_0 ||
forceHexPassword) {
hexInterceptor.intercept(chain) hexInterceptor.intercept(chain)
} else { } else {
mD5Interceptor.intercept(chain) mD5Interceptor.intercept(chain)

View File

@ -6,6 +6,7 @@ import okhttp3.Interceptor.Chain
import org.junit.Test import org.junit.Test
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_12_0 import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_12_0
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_13_0 import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_13_0
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_16_0
/** /**
* Unit test for [ProxyPasswordInterceptor]. * Unit test for [ProxyPasswordInterceptor].
@ -16,7 +17,7 @@ class ProxyPasswordInterceptorTest {
private val mockChain = mock<Chain>() private val mockChain = mock<Chain>()
private val proxyInterceptor = ProxyPasswordInterceptor(V1_12_0, private val proxyInterceptor = ProxyPasswordInterceptor(V1_12_0,
mockPasswordHexInterceptor, mockPasswordMd5Interceptor) mockPasswordHexInterceptor, mockPasswordMd5Interceptor, false)
@Test @Test
fun `Should use hex password on versions less then 1 13 0`() { fun `Should use hex password on versions less then 1 13 0`() {
@ -33,4 +34,14 @@ class ProxyPasswordInterceptorTest {
verify(mockPasswordMd5Interceptor).intercept(mockChain) verify(mockPasswordMd5Interceptor).intercept(mockChain)
} }
@Test
fun `Should use hex password if forceHex is true`() {
val interceptor = ProxyPasswordInterceptor(V1_16_0, mockPasswordHexInterceptor,
mockPasswordMd5Interceptor, true)
interceptor.intercept(mockChain)
verify(mockPasswordHexInterceptor).intercept(mockChain)
}
} }