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

Concatenate JWKS path with existing path on domain if present #67

Merged
merged 3 commits into from
Sep 10, 2019
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
15 changes: 9 additions & 6 deletions src/main/java/com/auth0/jwk/UrlJwkProvider.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.auth0.jwk;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.common.annotations.VisibleForTesting;
Expand All @@ -11,6 +8,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
Expand Down Expand Up @@ -66,8 +65,12 @@ public UrlJwkProvider(URL url, Integer connectTimeout, Integer readTimeout) {
* <br><br> It can be a url link 'https://samples.auth0.com' or just a domain 'samples.auth0.com'.
* If the protocol (http or https) is not provided then https is used by default.
* The default jwks path "/.well-known/jwks.json" is appended to the given string domain.
* If the domain url contains a path, e.g. 'https://auth.example.com/some-resource', the path is preserved and the
* default jwks path is appended.
* <br><br> For example, when the domain is "samples.auth0.com"
* the jwks url that will be used is "https://samples.auth0.com/.well-known/jwks.json"
* If the domain string is "https://auth.example.com/some-resource", the jwks url that will be used is
* "https://auth.example.com/some-resource/.well-known/jwks.json"
* <br><br> Use {@link #UrlJwkProvider(URL)} if you need to pass a full URL.
*
* @param domain where jwks is published
Expand All @@ -84,9 +87,9 @@ static URL urlForDomain(String domain) {
}

try {
final URL url = new URL(domain);
return new URL(url, WELL_KNOWN_JWKS_PATH);
} catch (MalformedURLException e) {
final URI uri = new URI(domain + WELL_KNOWN_JWKS_PATH).normalize();
return uri.toURL();
} catch (MalformedURLException | URISyntaxException e) {
throw new IllegalArgumentException("Invalid jwks uri", e);
}
}
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/com/auth0/jwk/UrlJwkProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

import static com.auth0.jwk.UrlJwkProvider.WELL_KNOWN_JWKS_PATH;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -147,11 +151,35 @@ public void shouldBuildCorrectHttpUrlOnDomainWithHttpAndSlash() {
}

@Test
public void shouldUseOnlyDomain() {
public void shouldUseDomainAndPathWithSlashIfPresent() {
String domain = "samples.auth0.com";
String domainWithSubPath = domain + "/sub/path/";
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + WELL_KNOWN_JWKS_PATH));
assertThat(actualJwksUrl, equalTo("https://" + domain + "/sub/path" + WELL_KNOWN_JWKS_PATH));
}

@Test
public void shouldUseDomainAndPathWithoutSlashIfPresent() {
String domain = "samples.auth0.com";
String domainWithSubPath = domain + "/sub/path";
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + "/sub/path" + WELL_KNOWN_JWKS_PATH));
}

@Test
public void shouldUseDomainAndSinglePathWithSlashIfPresent() {
String domain = "samples.auth0.com";
String domainWithSubPath = domain + "/path/";
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + "/path" + WELL_KNOWN_JWKS_PATH));
}

@Test
public void shouldUseDomainAndSinglePathWithoutSlashIfPresent() {
String domain = "samples.auth0.com";
String domainWithSubPath = domain + "/path";
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + "/path" + WELL_KNOWN_JWKS_PATH));
}

@Test
Expand Down