You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@jena.apache.org by GitBox <gi...@apache.org> on 2022/05/30 08:18:49 UTC

[GitHub] [jena] rvesse commented on a diff in pull request #1341: Improved Bearer Auth verifier support (GH-1292)

rvesse commented on code in PR #1341:
URL: https://github.com/apache/jena/pull/1341#discussion_r884549378


##########
jena-fuseki2/jena-fuseki-main/src/main/java/org/apache/jena/fuseki/main/auth/AuthBearerFilter.java:
##########
@@ -34,36 +37,129 @@
 import org.slf4j.Logger;
 
 /**
- * Process an "Authorization: Bearer" header.
- * If present, extract as JWT
+ * Process an {@code Authorization: Bearer <token>} header. If present, extract {@code <token>} as JWT and try to verify
+ * with a configurable user verifier function.
+ * <h3>User Verifier Function</h3>
+ * <p>
+ * The user verifier function has the signature of {@code Function<String, String>} where the input is the raw base64
+ * encoded JWT as obtained from the {@code Authorization} header.  If your function considers the token valid then it
+ * <strong>MUST</strong> return the username for the user, if it considers the token invalid then it
+ * <strong>MUST</strong> return {@code null} to indicate this.  Your function <strong>MUST NOT</strong> throw any
+ * exceptions, however you may wish to log the reason for considering a token invalid to the server logs.
+ * </p>
+ * <h3>Programmatic Usage</h3>
+ * <p>
+ * For programmatic server building the verification function can be provided directly to the constructor.
+ * </p>
+ * <h3>Servlet Configuration Usage</h3>
+ * <p>
+ * For use in {@code web.xml} or other Java servlet based environments the filter can configure itself automatically. It
+ * first looks for a function from the servlet context attribute (defined by the static field
+ * {@link AuthBearerFilter#USER_VERIFIER_FUNCTION_ATTRIBUTE}) and if that is a valid function uses that.  This allows
+ * for deployments to use a {@link ServletContextListener} or similar servlet lifecycle mechanism to construct and
+ * inject their desired function programmatically at runtime and have the filter honour it.
+ * </p>
+ * <p>
+ * If the function to use can be statically configured then you can use the
+ * {@link AuthBearerFilter#USER_VERIFIER_FUNCTION_ATTRIBUTE} parameters as an {@code <init-param>}'s to specify the
+ * class and method of the function to use in the {@code <filter>} definition e.g.
+ * </p>
+ * <pre>
+ * <filter>
+ *     <filter-name>BearerAuthFilter</filter-name>
+ *     <filter-class>org.apache.jena.fuseki.main.auth.AuthBearerFilter</filter-class>
+ *     <init-param>
+ *         <param-name>org.apache.jena.fuseki.main.auth.AuthBearerFilter.userVerifier</param-name>
+ *         <param-value>your.package.SomeClass.SomeMethod</param-value>
+ *     </init-param>
+ * </filter>
+ * </pre>
+ * <p>
+ * In this scenario {@code your.package.SomeClass} must be a valid Java class and {@code SomeMethod} a static method on
+ * that class that conforms to the
+ * </p>
  */
 public class AuthBearerFilter implements Filter {
     private static Logger log = Fuseki.serverLog;
-    private final Function<String, String> verifiedUser;
+    private Function<String, String> userVerifier;
+
+    public static final String USER_VERIFIER_FUNCTION_ATTRIBUTE =
+            AuthBearerFilter.class.getCanonicalName() + ".userVerifier";
 
     @Override
-    public void init(FilterConfig filterConfig) {}
+    public void init(FilterConfig filterConfig) {
+        // For web.xml usage allow initializing in the following ways:

Review Comment:
   Yes but other $dayjob stuff does, and I would prefer to have a Jena provided filter that I can reuse in non-Fuseki contexts rather than being forced to implement another filter



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org