Skip to content

Commit

Permalink
Merge pull request #520 from auth0/fix-cdn-url
Browse files Browse the repository at this point in the history
Update Configuration URL (CDN URL) [SDK-2710]
  • Loading branch information
lbalmaceda authored Oct 1, 2021
2 parents 9e1c8cf + 59ee6b3 commit 9bbee00
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
27 changes: 2 additions & 25 deletions auth0/src/main/java/com/auth0/android/Auth0.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.auth0.android

import android.content.Context
import androidx.annotation.VisibleForTesting
import com.auth0.android.request.DefaultClient
import com.auth0.android.request.NetworkingClient
import com.auth0.android.util.Auth0UserAgent
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import java.util.*

Expand Down Expand Up @@ -93,39 +91,18 @@ public open class Auth0 @JvmOverloads constructor(
.build()
.toString()


private fun resolveConfiguration(configurationDomain: String?, domainUrl: HttpUrl): HttpUrl {
var url = ensureValidUrl(configurationDomain)
if (url == null) {
val host = domainUrl.host
url = if (host.endsWith(DOT_AUTH0_DOT_COM)) {
val parts = host.split(".").toTypedArray()
if (parts.size > 3) {
("https://cdn." + parts[parts.size - 3] + DOT_AUTH0_DOT_COM).toHttpUrl()
} else {
AUTH0_US_CDN_URL.toHttpUrl()
}
} else {
domainUrl
}
}
return url
}

private fun ensureValidUrl(url: String?): HttpUrl? {
if (url == null) {
return null
}
val normalizedUrl = url.toLowerCase(Locale.ROOT)
val normalizedUrl = url.lowercase(Locale.ROOT)
require(!normalizedUrl.startsWith("http://")) { "Invalid domain url: '$url'. Only HTTPS domain URLs are supported. If no scheme is passed, HTTPS will be used." }
val safeUrl =
if (normalizedUrl.startsWith("https://")) normalizedUrl else "https://$normalizedUrl"
return safeUrl.toHttpUrlOrNull()
}

private companion object {
private const val AUTH0_US_CDN_URL = "https://cdn.auth0.com"
private const val DOT_AUTH0_DOT_COM = ".auth0.com"
private fun getResourceFromContext(context: Context, resName: String): String {
val stringRes = context.resources.getIdentifier(resName, "string", context.packageName)
require(stringRes != 0) {
Expand All @@ -141,7 +118,7 @@ public open class Auth0 @JvmOverloads constructor(
init {
domainUrl = ensureValidUrl(domain)
requireNotNull(domainUrl) { String.format("Invalid domain url: '%s'", domain) }
configurationUrl = resolveConfiguration(configurationDomain, domainUrl)
configurationUrl = ensureValidUrl(configurationDomain) ?: domainUrl
auth0UserAgent = Auth0UserAgent()
}
}
10 changes: 5 additions & 5 deletions auth0/src/test/java/com/auth0/android/Auth0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void shouldBuildFromResources() {
assertThat(auth0, notNullValue());
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(auth0.getDomainUrl(), equalTo("https://samples.auth0.com/"));
assertThat(auth0.getConfigurationUrl(), equalTo("https://cdn.auth0.com/"));
assertThat(auth0.getConfigurationUrl(), equalTo("https://samples.auth0.com/"));
}

@Test
Expand Down Expand Up @@ -95,7 +95,7 @@ public void shouldBuildWithClientIdAndDomain() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(HttpUrl.parse(auth0.getDomainUrl()), equalTo(HttpUrl.parse("https://samples.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://cdn.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://samples.auth0.com")));
}

@Test
Expand All @@ -111,23 +111,23 @@ public void shouldHandleEUInstance() {
Auth0 auth0 = new Auth0(CLIENT_ID, EU_DOMAIN);
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(HttpUrl.parse(auth0.getDomainUrl()), equalTo(HttpUrl.parse("https://samples.eu.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://cdn.eu.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://samples.eu.auth0.com")));
}

@Test
public void shouldHandleAUInstance() {
Auth0 auth0 = new Auth0(CLIENT_ID, AU_DOMAIN);
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(HttpUrl.parse(auth0.getDomainUrl()), equalTo(HttpUrl.parse("https://samples.au.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://cdn.au.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://samples.au.auth0.com")));
}

@Test
public void shouldHandleOtherInstance() {
Auth0 auth0 = new Auth0(CLIENT_ID, OTHER_DOMAIN);
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(HttpUrl.parse(auth0.getDomainUrl()), equalTo(HttpUrl.parse("https://samples-test.other-subdomain.other.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://cdn.other.auth0.com")));
assertThat(HttpUrl.parse(auth0.getConfigurationUrl()), equalTo(HttpUrl.parse("https://samples-test.other-subdomain.other.auth0.com")));
}

@Test
Expand Down

0 comments on commit 9bbee00

Please sign in to comment.