-
Notifications
You must be signed in to change notification settings - Fork 72
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
Issue #20 added url constructor to JwkProviderBuilder #22
Changes from 2 commits
15121b2
74de690
2e7e2fe
c4a5f97
d07af0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.io.IOException; | ||
|
@@ -14,40 +13,49 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
/** | ||
* Jwk provider that loads them from a {@link URL} | ||
*/ | ||
@SuppressWarnings("WeakerAccess") | ||
public class UrlJwkProvider implements JwkProvider { | ||
|
||
static final String WELL_KNOWN_JWKS_PATH = "/.well-known/jwks.json"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a comment above
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used |
||
|
||
final URL url; | ||
|
||
/** | ||
* Creates a provider that loads from a given URL | ||
* Creates a provider that loads from the given URL | ||
* @param url to load the jwks | ||
*/ | ||
public UrlJwkProvider(URL url) { | ||
if (url == null) { | ||
throw new IllegalArgumentException("A non-null url is required"); | ||
} | ||
checkArgument(url != null, "A non-null url is required"); | ||
this.url = url; | ||
} | ||
|
||
/** | ||
* Creates a provider that loads from the given domain's well-known directory | ||
* @param domain domain where to look for the jwks.json file | ||
* Creates a provider that loads from the given domain's well-known directory. | ||
* <br>If the protocol (http or https) is not provided then https is used by default | ||
* (some.domain -> "https://" + some.domain + {@value #WELL_KNOWN_JWKS_PATH}). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the JavaDoc fixed and alligned with |
||
* <br><br> Use {@link #UrlJwkProvider(URL)} if you need to pass a full URL. | ||
* @param domain where jwks is published | ||
*/ | ||
public UrlJwkProvider(String domain) { | ||
this(urlForDomain(domain)); | ||
} | ||
|
||
private static URL urlForDomain(String domain) { | ||
if (Strings.isNullOrEmpty(domain)) { | ||
throw new IllegalArgumentException("A domain is required"); | ||
static URL urlForDomain(String domain) { | ||
checkArgument(!isNullOrEmpty(domain), "A domain is required"); | ||
|
||
if (!domain.startsWith("http")) { | ||
domain = "https://" + domain; | ||
} | ||
|
||
try { | ||
final URL url = new URL(domain); | ||
return new URL(url, "/.well-known/jwks.json"); | ||
return new URL(url, WELL_KNOWN_JWKS_PATH); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException("Invalid jwks uri", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line hasn't been tested. Telling by the docs it will throw "if no protocol is specified, or an unknown protocol is found, or spec is null."
So protocol could be any of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covered with a test with "httptest://..." |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,37 +4,54 @@ | |
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.net.URL; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static com.auth0.jwk.UrlJwkProvider.WELL_KNOWN_JWKS_PATH; | ||
import static org.hamcrest.Matchers.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to import the required ones only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, safer, fixed |
||
import static org.junit.Assert.assertThat; | ||
|
||
public class JwkProviderBuilderTest { | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
private String domain = "samples.auth0.com"; | ||
private String normalizedDomain = "https://" + domain; | ||
|
||
@Test | ||
public void shouldCreateForUrl() throws Exception { | ||
URL urlToJwks = new URL(normalizedDomain + WELL_KNOWN_JWKS_PATH); | ||
assertThat(new JwkProviderBuilder(urlToJwks).build(), notNullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldCreateForDomain() throws Exception { | ||
assertThat(new JwkProviderBuilder("samples.auth0.com").build(), notNullValue()); | ||
assertThat(new JwkProviderBuilder(domain).build(), notNullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldCreateForHttpUrl() throws Exception { | ||
assertThat(new JwkProviderBuilder("https://samples.auth0.com").build(), notNullValue()); | ||
public void shouldCreateForNormalizedDomain() throws Exception { | ||
assertThat(new JwkProviderBuilder(normalizedDomain).build(), notNullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldFailWhenNoUrlIsProvided() throws Exception { | ||
expectedException.expect(IllegalStateException.class); | ||
expectedException.expectMessage("Cannot build provider without url to jwks"); | ||
new JwkProviderBuilder((URL) null).build(); | ||
} | ||
|
||
@Test | ||
public void shouldFailWhenNoDomainIsProvided() throws Exception { | ||
expectedException.expect(IllegalStateException.class); | ||
expectedException.expectMessage("Cannot build provider without domain"); | ||
new JwkProviderBuilder(null).build(); | ||
new JwkProviderBuilder((String) null).build(); | ||
} | ||
|
||
@Test | ||
public void shouldCreateCachedProvider() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.rateLimited(false) | ||
.cached(true) | ||
.build(); | ||
|
@@ -45,7 +62,7 @@ public void shouldCreateCachedProvider() throws Exception { | |
|
||
@Test | ||
public void shouldCreateCachedProviderWithCustomValues() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.rateLimited(false) | ||
.cached(10, 24, TimeUnit.HOURS) | ||
.build(); | ||
|
@@ -56,7 +73,7 @@ public void shouldCreateCachedProviderWithCustomValues() throws Exception { | |
|
||
@Test | ||
public void shouldCreateRateLimitedProvider() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.cached(false) | ||
.rateLimited(true) | ||
.build(); | ||
|
@@ -67,7 +84,7 @@ public void shouldCreateRateLimitedProvider() throws Exception { | |
|
||
@Test | ||
public void shouldCreateRateLimitedProviderWithCustomValues() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.cached(false) | ||
.rateLimited(10, 24, TimeUnit.HOURS) | ||
.build(); | ||
|
@@ -78,7 +95,7 @@ public void shouldCreateRateLimitedProviderWithCustomValues() throws Exception { | |
|
||
@Test | ||
public void shouldCreateCachedAndRateLimitedProvider() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.cached(true) | ||
.rateLimited(true) | ||
.build(); | ||
|
@@ -91,7 +108,7 @@ public void shouldCreateCachedAndRateLimitedProvider() throws Exception { | |
|
||
@Test | ||
public void shouldCreateCachedAndRateLimitedProviderWithCustomValues() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com") | ||
JwkProvider provider = new JwkProviderBuilder(domain) | ||
.cached(10, 24, TimeUnit.HOURS) | ||
.rateLimited(10, 24, TimeUnit.HOURS) | ||
.build(); | ||
|
@@ -104,11 +121,25 @@ public void shouldCreateCachedAndRateLimitedProviderWithCustomValues() throws Ex | |
|
||
@Test | ||
public void shouldCreateCachedAndRateLimitedProviderByDefault() throws Exception { | ||
JwkProvider provider = new JwkProviderBuilder("samples.auth0.com").build(); | ||
JwkProvider provider = new JwkProviderBuilder(domain).build(); | ||
assertThat(provider, notNullValue()); | ||
assertThat(provider, instanceOf(GuavaCachedJwkProvider.class)); | ||
JwkProvider baseProvider = ((GuavaCachedJwkProvider) provider).getBaseProvider(); | ||
assertThat(baseProvider, instanceOf(RateLimitedJwkProvider.class)); | ||
assertThat(((RateLimitedJwkProvider) baseProvider).getBaseProvider(), instanceOf(UrlJwkProvider.class)); | ||
} | ||
|
||
@Test | ||
public void shouldSupportUrlToJwksDomainWithSubPath() throws Exception { | ||
String urlToJwksWithSubPath = normalizedDomain + "/sub/path" + WELL_KNOWN_JWKS_PATH; | ||
URL url = new URL(urlToJwksWithSubPath); | ||
JwkProvider provider = new JwkProviderBuilder(url) | ||
.rateLimited(false) | ||
.cached(false) | ||
.build(); | ||
assertThat(provider, notNullValue()); | ||
assertThat(provider, instanceOf(UrlJwkProvider.class)); | ||
UrlJwkProvider urlJwkProvider = (UrlJwkProvider) provider; | ||
assertThat(urlJwkProvider.url.toString(), equalTo(urlToJwksWithSubPath)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should mention that this method appends the default jwks path to the given string domain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added