- Gradle/Kotlin/Ktor project on JVM 21 with ShadowJar - SQLite WAL, Flyway migrations and config-driven users/folders - deterministic IDs, MP3 tag extraction, cover cache and delta scanner - OpenSubsonic JSON envelope with token/password authentication - system endpoints: ping, license, extensions, scan status
69 lines
2.4 KiB
Kotlin
69 lines
2.4 KiB
Kotlin
package win.taptap.musicmanager.subsonic
|
|
|
|
import io.ktor.http.Parameters
|
|
import kotlin.test.Test
|
|
import kotlin.test.assertEquals
|
|
import kotlin.test.assertNull
|
|
import win.taptap.musicmanager.config.UserAccount
|
|
import win.taptap.musicmanager.config.UsersConfig
|
|
|
|
class SubsonicAuthTest {
|
|
|
|
private val users = UsersConfig(
|
|
listOf(
|
|
UserAccount("alice", "secret", admin = true, folders = setOf(1)),
|
|
UserAccount("bob", "hunter2", admin = false, folders = setOf(1)),
|
|
),
|
|
)
|
|
private val authenticator = SubsonicAuthenticator(users)
|
|
|
|
@Test
|
|
fun `accepts token authentication`() {
|
|
val salt = "s4lt"
|
|
val token = SubsonicAuthenticator.md5Hex("secret$salt")
|
|
|
|
val user = authenticator.authenticate(params("u" to "alice", "t" to token, "s" to salt))
|
|
|
|
assertEquals("alice", user?.name)
|
|
}
|
|
|
|
@Test
|
|
fun `accepts plaintext password authentication`() {
|
|
assertEquals("bob", authenticator.authenticate(params("u" to "bob", "p" to "hunter2"))?.name)
|
|
}
|
|
|
|
@Test
|
|
fun `accepts enc-prefixed hex password authentication`() {
|
|
val encoded = "enc:" + "secret".toByteArray().joinToString("") { "%02x".format(it) }
|
|
|
|
assertEquals("alice", authenticator.authenticate(params("u" to "alice", "p" to encoded))?.name)
|
|
}
|
|
|
|
@Test
|
|
fun `rejects wrong credentials and malformed requests`() {
|
|
val salt = "s4lt"
|
|
assertNull(authenticator.authenticate(params("u" to "alice", "t" to "wrong", "s" to salt)))
|
|
assertNull(authenticator.authenticate(params("u" to "alice", "p" to "wrong")))
|
|
assertNull(authenticator.authenticate(params("u" to "nobody", "p" to "secret")))
|
|
assertNull(authenticator.authenticate(params("p" to "secret")))
|
|
assertNull(authenticator.authenticate(params("u" to "alice")))
|
|
assertNull(authenticator.authenticate(params("u" to "alice", "p" to "enc:zz")))
|
|
}
|
|
|
|
@Test
|
|
fun `token takes precedence over password`() {
|
|
val salt = "s4lt"
|
|
val token = SubsonicAuthenticator.md5Hex("secret$salt")
|
|
|
|
val user = authenticator.authenticate(
|
|
params("u" to "alice", "t" to token, "s" to salt, "p" to "wrong"),
|
|
)
|
|
|
|
assertEquals("alice", user?.name)
|
|
}
|
|
|
|
private fun params(vararg pairs: Pair<String, String>): Parameters = Parameters.build {
|
|
pairs.forEach { (key, value) -> append(key, value) }
|
|
}
|
|
}
|