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.listeners;
015
016import java.nio.charset.StandardCharsets;
017
018import lombok.RequiredArgsConstructor;
019import org.apache.shiro.ee.filters.FormAuthenticationFilter;
020import org.apache.shiro.ee.filters.LogoutFilter;
021import org.apache.shiro.ee.filters.SslFilter;
022
023import java.util.Map;
024import java.util.function.Function;
025import java.util.function.Supplier;
026import javax.servlet.Filter;
027
028import lombok.extern.slf4j.Slf4j;
029import org.apache.shiro.cdi.annotations.CipherKeySupplier;
030import org.apache.shiro.config.Ini;
031import org.apache.shiro.crypto.cipher.AesCipherService;
032import org.apache.shiro.mgt.SecurityManager;
033import org.apache.shiro.web.config.WebIniSecurityManagerFactory;
034import org.apache.shiro.web.env.IniWebEnvironment;
035import org.apache.shiro.web.filter.mgt.DefaultFilter;
036import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
037import org.omnifaces.util.Beans;
038import org.omnifaces.util.Lazy;
039
040import static org.omnifaces.util.Utils.isBlank;
041
042/**
043 * Ability to merge two configuration files, exactly two
044 */
045@Slf4j
046public class IniEnvironment extends IniWebEnvironment {
047    private String otherConfigLocation;
048
049    @SuppressWarnings("deprecation")
050    @RequiredArgsConstructor
051    private static final class SecurityManagerFactory extends WebIniSecurityManagerFactory {
052        private final Function<Supplier<byte[]>, SecurityManager> securityManagerSupplier;
053        private final Lazy<AesCipherService> cipherService = new Lazy<>(AesCipherService::new);
054
055        @Override
056        protected Map<String, ?> createDefaults(Ini ini, Ini.Section mainSection) {
057            @SuppressWarnings("unchecked")
058            Map<String, Filter> defaults = (Map<String, Filter>) super.createDefaults(ini, mainSection);
059            try {
060                // SSL filter must be first because it will trigger an exception if OmniFaces is not available
061                var sslFilter = new SslFilter();
062                defaults.replace(DefaultFilter.authc.name(), new FormAuthenticationFilter());
063                defaults.replace(DefaultFilter.ssl.name(), sslFilter);
064                defaults.replace(DefaultFilter.logout.name(), new LogoutFilter());
065            } catch (Throwable e) {
066                log.warn("unable to initialize filters", e);
067            }
068            return defaults;
069        }
070
071        @Override
072        protected SecurityManager createDefaultInstance() {
073            return securityManagerSupplier.apply(this::generateCipherKey);
074        }
075
076        private byte[] generateCipherKey() {
077            var cipherKeySupplier = Beans.getManager() == null ? null
078                    : Beans.getReference(CipherKeySupplier.class);
079            if (cipherKeySupplier == null || isBlank(cipherKeySupplier.get())) {
080                return cipherService.get().generateNewKey().getEncoded();
081            } else {
082                return cipherKeySupplier.get().getBytes(StandardCharsets.UTF_8);
083            }
084        }
085    }
086
087    public IniEnvironment() {
088        this(DefaultWebSecurityManager::new);
089    }
090
091    public IniEnvironment(Function<Supplier<byte[]>, SecurityManager> securityManagerSupplier) {
092        var securityManagerFactory = new SecurityManagerFactory(securityManagerSupplier);
093        securityManagerFactory.getReflectionBuilder().setAlternateObjectSupplier(Beans::getInstance);
094        setSecurityManagerFactory(securityManagerFactory);
095    }
096
097    @Override
098    public void setConfigLocations(String[] configLocations) {
099        if (configLocations.length == 2) {
100            otherConfigLocation = configLocations[1];
101            super.setConfigLocations(configLocations[0]);
102        } else {
103            super.setConfigLocations(configLocations);
104        }
105    }
106
107    @Override
108    protected Ini getFrameworkIni() {
109        if (otherConfigLocation != null) {
110            return createIni(otherConfigLocation, true);
111        } else {
112            return super.getFrameworkIni();
113        }
114    }
115}