Interface JwksSource

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface JwksSource
Where JwtAuthenticator gets an issuer's JSON Web Key Set (RFC 7517) from. The application configures the source; a token can never choose it.

Implement it to fetch the document some other way, or in tests to inject a key set without a network:


 JwksSource fixed = maxBytes -> jwksJson.getBytes(StandardCharsets.UTF_8);
 

url(URI) and file(Path) are the supplied sources. Both read at most the requested number of bytes and fail with an IOException otherwise. An implementation is called by one thread at a time per authenticator, may block, and must return within a bounded time.

  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    fetch(int maxBytes)
    Reads the key set document.
    static JwksSource
    file(Path path)
    Reads the key set from a file, for example one a deployment tool refreshes.
    static JwksSource
    url(URI uri)
    Fetches the key set over HTTPS with a 5 second timeout.
    static JwksSource
    url(URI uri, Duration timeout)
    Fetches the key set with java.net.http.
  • Method Details

    • fetch

      byte[] fetch(int maxBytes) throws IOException
      Reads the key set document.
      Parameters:
      maxBytes - the longest document the caller accepts; an implementation should stop reading beyond it and fail (a longer result is rejected by the caller anyway)
      Returns:
      the document, UTF-8 JSON
      Throws:
      IOException - if the document cannot be read within the bounds
    • url

      static JwksSource url(URI uri)
      Fetches the key set over HTTPS with a 5 second timeout.
      Parameters:
      uri - an https URI, or http to a loopback address for development
      Returns:
      the source
      Throws:
      IllegalArgumentException - for another scheme, user information, a fragment or no host
      See Also:
    • url

      static JwksSource url(URI uri, Duration timeout)
      Fetches the key set with java.net.http. Redirects are never followed, so a response cannot send the client to another host: a 3xx status is a failure. Only status 200 with a application/json or application/jwk-set+json content type is accepted. The body is read incrementally and the exchange is abandoned as soon as it exceeds the byte limit or the timeout, which covers connecting, the response head and the whole body. No credentials, cookies or proxy authentication are sent; the connection uses the JVM's default TLS trust and proxy settings.
      Parameters:
      uri - an https URI, or http to a loopback address for development
      timeout - 100 milliseconds to 60 seconds for the whole exchange
      Returns:
      the source, which owns one HTTP client for its lifetime
      Throws:
      IllegalArgumentException - for another scheme, user information, a fragment, no host or a timeout out of range
    • file

      static JwksSource file(Path path)
      Reads the key set from a file, for example one a deployment tool refreshes.
      Parameters:
      path - a regular file
      Returns:
      the source; the file is read afresh on every fetch