Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't remove query parameters when applying akamai token #270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,22 @@ class AkamaiTokenProviderTest {
Assert.assertNotNull(acl)
Assert.assertEquals(expectedAcl, acl)
}

@Test
fun testAclWithQueryParameters() {
val uri = Uri.parse("https://srgssrch.akamaized.net/hls/live/2022027/srgssr-hls-stream15-ch-dvr/master.m3u8?start=1697860830&end=1697867100")
val expectedAcl = "/hls/live/2022027/srgssr-hls-stream15-ch-dvr/*"
val acl = AkamaiTokenProvider.getAcl(uri)
Assert.assertNotNull(acl)
Assert.assertEquals(expectedAcl, acl)
}

@Test
fun testAppendTokenToUri() {
val uri = Uri.parse("https://srgssrch.akamaized.net/hls/live/2022027/srgssr-hls-stream15-ch-dvr/master.m3u8?start=1697860830&end=1697867100")
val fakeToken = AkamaiTokenProvider.Token(authParams = "Token")
val actual = AkamaiTokenProvider.appendTokenToUri(uri, fakeToken)
Assert.assertNotEquals(uri, actual)
Assert.assertTrue("Contains base url", actual.toString().contains(uri.toString()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ch.srgssr.pillarbox.core.business.akamai

import android.net.Uri
import android.net.UrlQuerySanitizer
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultHttpClient
import io.ktor.client.HttpClient
import io.ktor.client.call.body
Expand Down Expand Up @@ -32,7 +33,7 @@ class AkamaiTokenProvider(private val httpClient: HttpClient = DefaultHttpClient
val tokenResult = getToken(it)
tokenResult.getOrDefault(null)
}
return token?.let { uri.buildUpon().encodedQuery(it.authParams).build() } ?: uri
return token?.let { appendTokenToUri(uri, it) } ?: uri
}

private suspend fun getToken(acl: String): Result<Token> {
Expand Down Expand Up @@ -81,5 +82,15 @@ class AkamaiTokenProvider(private val httpClient: HttpClient = DefaultHttpClient
}
return path.substring(0, path.lastIndexOf("/") + 1) + "*"
}

internal fun appendTokenToUri(uri: Uri, token: Token): Uri {
val sanitizer = UrlQuerySanitizer("u.rl/p?${token.authParams}")
sanitizer.parseQuery(token.authParams)
val builder = uri.buildUpon()
for (key in sanitizer.parameterSet) {
builder.appendQueryParameter(key, sanitizer.getValue(key))
}
return builder.build()
}
}
}