001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *      http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.apache.shiro.ee.cdi;
015
016import static org.apache.shiro.ee.filters.FormResubmitSupport.getNativeSessionManager;
017
018import java.io.Serializable;
019import java.lang.annotation.Annotation;
020import javax.enterprise.context.spi.Context;
021import javax.enterprise.context.spi.Contextual;
022import javax.enterprise.context.spi.CreationalContext;
023import javax.enterprise.inject.spi.CDI;
024
025import org.apache.shiro.mgt.SecurityManager;
026import org.apache.shiro.SecurityUtils;
027import org.apache.shiro.UnavailableSecurityManagerException;
028import org.apache.shiro.session.Session;
029import org.apache.shiro.web.mgt.WebSecurityManager;
030import org.omnifaces.cdi.BeanStorage;
031import org.omnifaces.cdi.viewscope.ViewScopeManager;
032import org.omnifaces.util.Beans;
033
034/**
035 * If web environment, delegate to SessionScoped,
036 * otherwise use Shiro sessions to store session beans
037 */
038public class ShiroScopeContext implements Context, Serializable {
039    private static final String BEAN_STORAGE_KEY = "org.apache.shiro.ee.bean-storage";
040    private static final long serialVersionUID = 1L;
041    private final Class<? extends Annotation> scopeType;
042    private final Class<? extends Annotation> webScopeType;
043    private final boolean isViewScoped;
044
045    public ShiroScopeContext(Class<? extends Annotation> scopeType, Class<? extends Annotation> webScopeType) {
046        this.scopeType = scopeType;
047        this.webScopeType = webScopeType;
048        isViewScoped = webScopeType == javax.faces.view.ViewScoped.class
049                || webScopeType == org.omnifaces.cdi.ViewScoped.class;
050    }
051
052    @Override
053    public Class<? extends Annotation> getScope() {
054        return scopeType;
055    }
056
057    @Override
058    public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
059        if (isWebContainerSessions()) {
060            Context ctx = CDI.current().getBeanManager().getContext(webScopeType);
061            return ctx.get(contextual, creationalContext);
062        } else {
063            synchronized (contextual) {
064                if (isViewScoped) {
065                    return Beans.getReference(ViewScopeManager.class).createBean(contextual, creationalContext);
066                } else {
067                    return getBeanStorage(SecurityUtils.getSubject().getSession())
068                            .createBean(contextual, creationalContext);
069                }
070            }
071        }
072    }
073
074    @Override
075    public <T> T get(Contextual<T> contextual) {
076        if (isWebContainerSessions()) {
077            Context ctx = CDI.current().getBeanManager().getContext(webScopeType);
078            return ctx.get(contextual);
079        } else {
080            if (isViewScoped) {
081                return Beans.getReference(ViewScopeManager.class).getBean(contextual);
082            } else {
083                return getBeanStorage(SecurityUtils.getSubject().getSession()).getBean(contextual);
084            }
085        }
086    }
087
088    @Override
089    public boolean isActive() {
090        return true;
091    }
092
093    @SuppressWarnings("MagicNumber")
094    void onCreate(Session session) {
095        session.setAttribute(BEAN_STORAGE_KEY, new BeanStorage(20));
096    }
097
098    void onDestroy(Session session) {
099        getBeanStorage(session).destroyBeans();
100    }
101
102    public static boolean isWebContainerSessions(SecurityManager sm) {
103        if (sm instanceof WebSecurityManager) {
104            WebSecurityManager wsm = (WebSecurityManager) sm;
105            return wsm.isHttpSessionMode();
106        }
107        return false;
108    }
109
110    public static void addScopeSessionListeners(WebSecurityManager wsm) {
111        if (!isWebContainerSessions(wsm)) {
112            var dsm = getNativeSessionManager(wsm);
113            Beans.getReference(ShiroSessionScopeExtension.class)
114                    .addSessionListeners(dsm.getSessionListeners(), wsm);
115        }
116    }
117
118    static boolean isWebContainerSessions() {
119        try {
120            return isWebContainerSessions(SecurityUtils.getSecurityManager());
121        } catch (UnavailableSecurityManagerException unavailable) {
122            return true;
123        }
124    }
125
126    private BeanStorage getBeanStorage(Session session) {
127        return (BeanStorage) session.getAttribute(BEAN_STORAGE_KEY);
128    }
129}