Skip to content

Commit

Permalink
Merge pull request #243 from cghislai/cdi-jndi-lookup
Browse files Browse the repository at this point in the history
Replace CDI.current() calls by CDIUtils#getBeanReference (JNDI loookup)
  • Loading branch information
arjantijms committed Dec 18, 2018
2 parents 230c070 + da65989 commit 0156152
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 30 deletions.
11 changes: 3 additions & 8 deletions impl/src/main/java/org/glassfish/soteria/cdi/CdiExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;
import javax.security.enterprise.authentication.mechanism.http.AutoApplySession;
Expand Down Expand Up @@ -160,9 +159,7 @@ public <T> void processBean(@Observes ProcessBean<T> eventIn, BeanManager beanMa
.types(Object.class, HttpAuthenticationMechanism.class)
.addToId(FormAuthenticationMechanismDefinition.class)
.create(e -> {
FormAuthenticationMechanism authMethod = CDI.current()
.select(FormAuthenticationMechanism.class)
.get();
FormAuthenticationMechanism authMethod = CdiUtils.getBeanReference(FormAuthenticationMechanism.class);

authMethod.setLoginToContinue(
LoginToContinueAnnotationLiteral.eval(formAuthenticationMechanismDefinition.loginToContinue()));
Expand All @@ -181,10 +178,8 @@ public <T> void processBean(@Observes ProcessBean<T> eventIn, BeanManager beanMa
.types(Object.class, HttpAuthenticationMechanism.class)
.addToId(CustomFormAuthenticationMechanismDefinition.class)
.create(e -> {
CustomFormAuthenticationMechanism authMethod = CDI.current()
.select(CustomFormAuthenticationMechanism.class)
.get();

CustomFormAuthenticationMechanism authMethod = CdiUtils.getBeanReference(CustomFormAuthenticationMechanism.class);

authMethod.setLoginToContinue(
LoginToContinueAnnotationLiteral.eval(customFormAuthenticationMechanismDefinition.loginToContinue()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import javax.enterprise.inject.Intercepted;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
Expand Down Expand Up @@ -93,7 +92,7 @@ public Object intercept(InvocationContext invocationContext) throws Exception {

private AuthenticationStatus validateRequest(InvocationContext invocationContext, HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws Exception {

RememberMeIdentityStore rememberMeIdentityStore = CDI.current().select(RememberMeIdentityStore.class).get();
RememberMeIdentityStore rememberMeIdentityStore = CdiUtils.getBeanReference(RememberMeIdentityStore.class);
RememberMe rememberMeAnnotation = getRememberMeFromIntercepted(getElProcessor(invocationContext, httpMessageContext), invocationContext);

Cookie rememberMeCookie = getCookie(request, rememberMeAnnotation.cookieName());
Expand Down Expand Up @@ -155,7 +154,7 @@ private AuthenticationStatus validateRequest(InvocationContext invocationContext

private void cleanSubject(InvocationContext invocationContext, HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws Exception {

RememberMeIdentityStore rememberMeIdentityStore = CDI.current().select(RememberMeIdentityStore.class).get(); // TODO ADD CHECKS
RememberMeIdentityStore rememberMeIdentityStore = CdiUtils.getBeanReference(RememberMeIdentityStore.class); // TODO ADD CHECKS
RememberMe rememberMeAnnotation = getRememberMeFromIntercepted(getElProcessor(invocationContext, httpMessageContext), invocationContext);

Cookie rememberMeCookie = getCookie(request, rememberMeAnnotation.cookieName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static javax.xml.bind.DatatypeConverter.parseBase64Binary;
import static org.glassfish.soteria.Utils.isEmpty;

import javax.enterprise.inject.spi.CDI;
import javax.security.enterprise.AuthenticationException;
import javax.security.enterprise.AuthenticationStatus;
import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition;
Expand All @@ -34,6 +33,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.glassfish.soteria.cdi.CdiUtils;


/**
* Authentication mechanism that authenticates using basic authentication
Expand Down Expand Up @@ -61,7 +62,7 @@ public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServ
String[] credentials = getCredentials(request);
if (!isEmpty(credentials)) {

IdentityStoreHandler identityStoreHandler = CDI.current().select(IdentityStoreHandler.class).get();
IdentityStoreHandler identityStoreHandler = CdiUtils.getBeanReference(IdentityStoreHandler.class);

CredentialValidationResult result = identityStoreHandler.validate(
new UsernamePasswordCredential(credentials[0], new Password(credentials[1])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.glassfish.soteria.mechanisms;

import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.CDI;
import javax.security.enterprise.AuthenticationException;
import javax.security.enterprise.AuthenticationStatus;
import javax.security.enterprise.authentication.mechanism.http.AutoApplySession;
Expand All @@ -28,6 +27,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.glassfish.soteria.cdi.CdiUtils;

/**
* Authentication mechanism that authenticates according to the Servlet spec defined FORM
Expand All @@ -48,7 +48,7 @@ public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServ

if (hasCredential(httpMessageContext)) {

IdentityStoreHandler identityStoreHandler = CDI.current().select(IdentityStoreHandler.class).get();
IdentityStoreHandler identityStoreHandler = CdiUtils.getBeanReference(IdentityStoreHandler.class);

return httpMessageContext.notifyContainerAboutLogin(
identityStoreHandler.validate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.glassfish.soteria.Utils.notNull;

import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.CDI;
import javax.security.enterprise.AuthenticationException;
import javax.security.enterprise.AuthenticationStatus;
import javax.security.enterprise.authentication.mechanism.http.AutoApplySession;
Expand All @@ -32,6 +31,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.glassfish.soteria.cdi.CdiUtils;

/**
* Authentication mechanism that authenticates according to the Servlet spec defined FORM
Expand All @@ -52,7 +52,7 @@ public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServ

if (isValidFormPostback(request)) {

IdentityStoreHandler identityStoreHandler = CDI.current().select(IdentityStoreHandler.class).get();
IdentityStoreHandler identityStoreHandler = CdiUtils.getBeanReference(IdentityStoreHandler.class);

return httpMessageContext.notifyContainerAboutLogin(
identityStoreHandler.validate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Map;

import javax.enterprise.inject.spi.CDI;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.message.AuthException;
Expand All @@ -39,6 +38,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.glassfish.soteria.cdi.CdiUtils;
import org.glassfish.soteria.cdi.spi.CDIPerRequestInitializer;
import org.glassfish.soteria.mechanisms.HttpMessageContextImpl;

Expand Down Expand Up @@ -85,8 +85,7 @@ public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject
setLastAuthenticationStatus(msgContext.getRequest(), status);

try {
status = CDI.current()
.select(HttpAuthenticationMechanism.class).get()
status = CdiUtils.getBeanReference(HttpAuthenticationMechanism.class)
.validateRequest(
msgContext.getRequest(),
msgContext.getResponse(),
Expand All @@ -109,8 +108,7 @@ public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject
HttpMessageContext msgContext = new HttpMessageContextImpl(handler, messageInfo, null);

try {
AuthenticationStatus status = CDI.current()
.select(HttpAuthenticationMechanism.class).get()
AuthenticationStatus status = CdiUtils.getBeanReference(HttpAuthenticationMechanism.class)
.secureResponse(
msgContext.getRequest(),
msgContext.getResponse(),
Expand Down Expand Up @@ -138,8 +136,7 @@ public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
HttpMessageContext msgContext = new HttpMessageContextImpl(handler, messageInfo, subject);

CDI.current()
.select(HttpAuthenticationMechanism.class).get()
CdiUtils.getBeanReference(HttpAuthenticationMechanism.class)
.cleanSubject(msgContext.getRequest(), msgContext.getResponse(), msgContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import java.util.Set;
import java.util.logging.Logger;

import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.BeanManager;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;

import org.glassfish.soteria.cdi.CdiExtension;
import org.glassfish.soteria.cdi.CdiUtils;
import org.glassfish.soteria.cdi.spi.CDIPerRequestInitializer;
import org.glassfish.soteria.cdi.spi.impl.LibertyCDIPerRequestInitializer;
import org.glassfish.soteria.mechanisms.jaspic.HttpBridgeServerAuthModule;
Expand All @@ -59,11 +60,11 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti

// Obtain a reference to the CdiExtension that was used to see if
// there's an enabled bean
CDI<Object> cdi;

BeanManager beanManager;
try {
cdi = CDI.current();
beanManager = CdiUtils.getBeanManager();

if (logger.isLoggable(INFO)) {
String version = getClass().getPackage().getImplementationVersion();
logger.log(INFO, "Initializing Soteria {0} for context ''{1}''", new Object[]{version, ctx.getContextPath()});
Expand All @@ -78,7 +79,7 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
return;
}

CdiExtension cdiExtension = cdi.select(CdiExtension.class).get();
CdiExtension cdiExtension = CdiUtils.getBeanReference(beanManager, CdiExtension.class);

if (cdiExtension.isHttpAuthenticationMechanismFound()) {

Expand Down

0 comments on commit 0156152

Please sign in to comment.