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

Issue #20 added url constructor to JwkProviderBuilder #22

Merged
merged 5 commits into from Apr 26, 2018

Conversation

darthvalinor
Copy link
Contributor

@darthvalinor darthvalinor commented Apr 6, 2018

  • URL constructor added to JwkProviderBuilder, which allows to customize the url to get JWKS. Before, it could only be <domain>/.well-known/jwks.json, now it can be <domain>/sub/path/.well-known/jwks.json
  • In case url is null IllegalStateException is thrown, so the behavior is similar to the String constructor
  • Unfortunately, I didn't find a good way to move JwkProviderBuilder.normalizeDomain to UrlJwkProvider keeping the same behavior
  • Slight fixes for JavaDocs
  • Please, let me know if more tests required

@lbalmaceda
Copy link
Contributor

lbalmaceda commented Apr 13, 2018

Thanks for the contribution!
My only strong opinion is that I see no use on having a separate class for handling the construction of the URL from the string domain (that factory class). I'd keep that logic in the original class private method.

Unfortunately, I didn't find a good way to move JwkProviderBuilder.normalizeDomain to UrlJwkProvider keeping the same behavior

Regarding this, the JwkProviderBuilder shouldn't have that normalizeDomain method. That should be part of the URLJwKProvider because it already handles "that type of data". If you can make those 2 changes, I'll then review javadoc or error messages and merge it. 👍

@darthvalinor
Copy link
Contributor Author

darthvalinor commented Apr 17, 2018

@lbalmaceda thanks for the review!
I updated the pull-request with your points.

  • The added class removed. The URL creation returned to URLJwKProvider.urlForDomain as package-private and reused in JwkProviderBuilder(String).
  • Domain normalization had to be moved to URLJwKProvider which caused a change in behavior: JwkProviderBuilder(String) no longer fails on non-normilized domains. The according test removed. Hopefully, it's acceptable.
  • Tests and JavaDocs adjusted accordingly.
  • Also need your opinion on IllegalStateException in JwkProviderBuilder vs IllegalArgumentException in URLJwKProvider.

@lbalmaceda lbalmaceda self-assigned this Apr 20, 2018
@mhlulani
Copy link

When can we have this on Maven Central?

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly javadoc changes.

* @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}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • use {DOMAIN} instead of "some.domain". You can also say something like "for example, when -domain- is -acme.com- the jwks url that will be used is -https://acme.com/.well-known/jwks.json"
  • don't mention the constant. Use "/.well-known/jwks.json" instead, since the constant is package private anyway and users won't see it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the JavaDoc fixed and alligned with JwkProviderBuilder(String)

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.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to import the required ones only

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, safer, fixed

@@ -21,35 +21,24 @@
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() throws Exception {
public void setUp() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole "before" method is no longer being used generically. It's only used in shouldReturnSingleJwkById. Feel free to remove this and the provider field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some cleanup

/**
* 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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment above

//visible for testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used @VisibleForTesting from Guava instead

}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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."

  • being "protocol" part of the URL (first param) and
  • being "spec" the well-known constant, which is never null.

So protocol could be any of http, https, ftp, file, and jar. We are adding https if the domain doesn't begin with http, so those 2 protocols are covered. And because we're enforcing any of those 2, this should be a valid value too. I don't see how to trick our logic in a test to make it fail in this line. If you happen to find something, feel free to add it. But don't worry if you can't that's OK anyway and we should catch it here. (which again, should never be called).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

covered with a test with "httptest://..."

@@ -35,6 +38,26 @@ public JwkProviderBuilder(String domain) {
this.bucket = new BucketImpl(10, 1, TimeUnit.MINUTES);
}

/**
* Creates a new Builder with a domain where to look for the jwks.
* It can be a url link 'https://samples.auth0.com' or just a domain 'samples.auth0.com'.
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -122,4 +121,10 @@ public void shouldUseOnlyDomain() {
String actualJwksUrl = new UrlJwkProvider(domainWithSubPath).url.toString();
assertThat(actualJwksUrl, equalTo("https://" + domain + WELL_KNOWN_JWKS_PATH));
}

@Test(expected = IllegalArgumentException.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the other syntax so we have everything the same.

expectedException.expect(IllegalArgumentException.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done

@lbalmaceda
Copy link
Contributor

@cocojoe I really like this to be merged. Can I have your blessing?

@cocojoe
Copy link
Member

cocojoe commented Apr 23, 2018

@lbalmaceda If you're happy, I'm happy.

@darthvalinor
Copy link
Contributor Author

I accidently made a comment fix and then reverted it. In order to keep the history cleaner, I can rebase. What do you think?

@lbalmaceda
Copy link
Contributor

As you want, I can also "squash and merge" and keep 1 single commit for the entire PR. No worries. I'll merge this one and try to release by tomorrow.

@darthvalinor
Copy link
Contributor Author

Removed those two

@lbalmaceda lbalmaceda merged commit f6a209b into auth0:master Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants