-
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 4 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,7 @@ | |
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.annotations.VisibleForTesting; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.io.IOException; | ||
|
@@ -14,40 +14,53 @@ | |
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 { | ||
|
||
@VisibleForTesting | ||
static final String WELL_KNOWN_JWKS_PATH = "/.well-known/jwks.json"; | ||
|
||
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><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. | ||
* <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" | ||
* <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://..." |
||
} | ||
|
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.
please add a comment above
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.
Used
@VisibleForTesting
from Guava instead