Skip to content

Commit

Permalink
Don't remove query parameters when applying akamai token (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ authored Oct 24, 2023
1 parent 9cbab6d commit 17050e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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()
}
}
}

0 comments on commit 17050e9

Please sign in to comment.