001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.security.credentialstore;
022    
023    import java.util.Map;
024    import java.util.HashMap;
025    import java.lang.reflect.Constructor;
026    import java.lang.reflect.InvocationTargetException;
027    import java.security.Principal;
028    
029    import javax.security.auth.Subject;
030    import javax.security.auth.login.LoginException;
031    
032    import org.apache.geronimo.common.DeploymentException;
033    import org.apache.geronimo.gbean.GBeanInfo;
034    import org.apache.geronimo.gbean.GBeanInfoBuilder;
035    
036    /**
037     * Hopefully this will only be used for tests where you need to set up a simple credential store
038     * but don't want to set up a login configuration
039     *
040     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
041     */
042    public class DirectConfigurationCredentialStoreImpl implements CredentialStore {
043    
044        private final Map<String, Map<String, Subject>> subjectStore = new HashMap<String, Map<String, Subject>>();
045    
046        public DirectConfigurationCredentialStoreImpl(Map<String, Map<String, Map<String, String>>> subjectInfo, ClassLoader cl) throws DeploymentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
047            if (cl == null) {
048                cl = getClass().getClassLoader();
049            }
050            for (Map.Entry<String, Map<String, Map<String, String>>> realmEntry: subjectInfo.entrySet()) {
051                Map<String, Subject> realm = new HashMap<String, Subject>();
052                for (Map.Entry<String, Map<String, String>> subjectEntry: realmEntry.getValue().entrySet()) {
053                    String id = subjectEntry.getKey();
054                    Map<String, String> principals = subjectEntry.getValue();
055                    Subject subject = new Subject();
056                    for (Map.Entry<String, String> principalInfo: principals.entrySet()) {
057                        String className = principalInfo.getKey();
058                        String principalName = principalInfo.getValue();
059                        Class<? extends Principal> clazz = (Class<? extends Principal>) cl.loadClass(className);
060                        Constructor<? extends Principal> c = clazz.getConstructor(new Class[] {String.class});
061                        Principal p = c.newInstance(new Object[] {principalName});
062                        subject.getPrincipals().add(p);
063                    }
064                    realm.put(id, subject);
065                }
066                subjectStore.put(realmEntry.getKey(), realm);
067            }
068        }
069    
070        public Subject getSubject(String realm, String id) throws LoginException {
071            Map<String, Subject> realmMap = subjectStore.get(realm);
072            if (realmMap == null) {
073                throw new LoginException("Unknown realm : " + realm);
074            }
075            Subject subject = realmMap.get(id);
076            if (subject == null) {
077                throw new LoginException("Unknown id: " + id + " in realm: " + realm);
078            }
079            return subject;
080        }
081    
082        public static final GBeanInfo GBEAN_INFO;
083    
084        static {
085            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DirectConfigurationCredentialStoreImpl.class);
086    
087            infoBuilder.addAttribute("credentialStore", Map.class, true);
088            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
089    
090            infoBuilder.setConstructor(new String[]{"credentialStore", "classLoader"});
091    
092            GBEAN_INFO = infoBuilder.getBeanInfo();
093        }
094    
095        public static GBeanInfo getGBeanInfo() {
096            return GBEAN_INFO;
097        }
098    
099    }