Search in sources :

Example 1 with JwtAuthCdiExtension

use of fish.payara.microprofile.jwtauth.cdi.JwtAuthCdiExtension in project Payara by payara.

the class JwtAuthDeployer method load.

@Override
@SuppressWarnings("unchecked")
public JwtAuthApplicationContainer load(JwtAuthContainer container, DeploymentContext deploymentContext) {
    // Register the JWTAuth Servlet
    WebBundleDescriptorImpl descriptor = deploymentContext.getModuleMetaData(WebBundleDescriptorImpl.class);
    if (descriptor != null) {
        descriptor.addAppListenerDescriptor(new AppListenerDescriptorImpl(RolesDeclarationInitializer.class.getName()));
    } else {
        LOGGER.warning("Failed to find WebBundleDescriptorImpl. JWT Auth roles will not be declared");
    }
    // Register the CDI extension
    Collection<Supplier<Extension>> snifferExtensions = deploymentContext.getTransientAppMetaData(WeldDeployer.SNIFFER_EXTENSIONS, Collection.class);
    if (snifferExtensions != null) {
        snifferExtensions.add(JwtAuthCdiExtension::new);
    }
    return new JwtAuthApplicationContainer(deploymentContext);
}
Also used : JwtAuthCdiExtension(fish.payara.microprofile.jwtauth.cdi.JwtAuthCdiExtension) WebBundleDescriptorImpl(org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl) AppListenerDescriptorImpl(org.glassfish.web.deployment.descriptor.AppListenerDescriptorImpl) Supplier(java.util.function.Supplier)

Example 2 with JwtAuthCdiExtension

use of fish.payara.microprofile.jwtauth.cdi.JwtAuthCdiExtension in project Payara by payara.

the class RolesDeclarationInitializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ServletContext ctx = sce.getServletContext();
    Set<String> roles = null;
    boolean addJWTAuthenticationMechanism = false;
    try {
        CDI<Object> cdi = CDI.current();
        if (cdi != null) {
            Instance<JwtAuthCdiExtension> extensionInstance = cdi.select(JwtAuthCdiExtension.class);
            if (extensionInstance != null && !extensionInstance.isUnsatisfied() && !extensionInstance.isAmbiguous()) {
                JwtAuthCdiExtension cdiExtension = extensionInstance.get();
                if (cdiExtension != null) {
                    roles = cdiExtension.getRoles();
                    addJWTAuthenticationMechanism = cdiExtension.isAddJWTAuthenticationMechanism();
                }
            }
        }
    } catch (Exception e) {
        logger.log(FINEST, "Exception trying to use CDI:", e);
    }
    if (roles == null) {
        logger.log(FINEST, "CDI not available for context " + ctx.getContextPath());
        return;
    }
    if (!addJWTAuthenticationMechanism) {
        // JWT authentication mechanism not installed, don't process the roles for Payara 4
        return;
    }
    if (logger.isLoggable(INFO)) {
        String version = getClass().getPackage().getImplementationVersion();
        logger.log(INFO, "Initializing MP-JWT {0} for context ''{1}''", new Object[] { version, ctx.getContextPath() });
    }
    ctx.declareRoles(roles.toArray(new String[0]));
}
Also used : JwtAuthCdiExtension(fish.payara.microprofile.jwtauth.cdi.JwtAuthCdiExtension) ServletContext(javax.servlet.ServletContext)

Aggregations

JwtAuthCdiExtension (fish.payara.microprofile.jwtauth.cdi.JwtAuthCdiExtension)2 Supplier (java.util.function.Supplier)1 ServletContext (javax.servlet.ServletContext)1 AppListenerDescriptorImpl (org.glassfish.web.deployment.descriptor.AppListenerDescriptorImpl)1 WebBundleDescriptorImpl (org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl)1