1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 21 package org.apache.geronimo.security.jaspi; 22 23 import java.io.IOException; 24 import java.io.StringReader; 25 26 import javax.security.auth.callback.CallbackHandler; 27 import javax.security.auth.message.AuthException; 28 import javax.security.auth.message.config.AuthConfigFactory; 29 import javax.security.auth.message.config.AuthConfigProvider; 30 import javax.security.auth.message.module.ClientAuthModule; 31 import javax.xml.bind.JAXBException; 32 import javax.xml.parsers.ParserConfigurationException; 33 import javax.xml.stream.XMLStreamException; 34 35 import org.apache.geronimo.components.jaspi.ClassLoaderLookup; 36 import org.apache.geronimo.components.jaspi.ConstantClassLoaderLookup; 37 import org.apache.geronimo.components.jaspi.model.AuthModuleType; 38 import org.apache.geronimo.components.jaspi.model.JaspiUtil; 39 import org.apache.geronimo.components.jaspi.model.JaspiXmlUtil; 40 import org.apache.geronimo.gbean.GBeanLifecycle; 41 import org.apache.geronimo.gbean.annotation.GBean; 42 import org.apache.geronimo.gbean.annotation.ParamAttribute; 43 import org.apache.geronimo.gbean.annotation.ParamSpecial; 44 import org.apache.geronimo.gbean.annotation.SpecialAttributeType; 45 import org.xml.sax.SAXException; 46 47 /** 48 * Holds a bit of xml configuring an AuthConfigProvider, [Client|Client][AuthConfig|AuthContext|AuthModule] 49 * and registers/unregisters it when start/stopped. 50 * 51 * @version $Rev: 720842 $ $Date: 2008-11-26 05:18:49 -0800 (Wed, 26 Nov 2008) $ 52 */ 53 54 @GBean 55 public class ClientAuthModuleGBean implements GBeanLifecycle { 56 57 private final String registrationID; 58 59 public ClientAuthModuleGBean( 60 @ParamAttribute(name = "messageLayer") String messageLayer, 61 @ParamAttribute(name = "appContext") String appContext, 62 @ParamAttribute(name = "authenticationID") String authenticationID, 63 @ParamAttribute(name = "config") String config, 64 @ParamSpecial(type = SpecialAttributeType.classLoader) ClassLoader classLoader) throws AuthException, JAXBException, IOException, ParserConfigurationException, SAXException, XMLStreamException { 65 ClassLoaderLookup classLoaderLookup = new ConstantClassLoaderLookup(classLoader); 66 67 AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory(); 68 AuthModuleType<ClientAuthModule> clientAuthModuleType = JaspiXmlUtil.loadClientAuthModule(new StringReader(config)); 69 AuthConfigProvider authConfigProvider = JaspiUtil.wrapClientAuthModule(messageLayer, appContext, authenticationID, clientAuthModuleType, true, classLoaderLookup); 70 registrationID = authConfigFactory.registerConfigProvider(authConfigProvider, messageLayer, appContext, null); 71 } 72 73 74 /** 75 * Starts the GBean. This informs the GBean that it is about to transition to the running state. 76 * 77 * @throws Exception if the target failed to start; this will cause a transition to the failed state 78 */ 79 public void doStart() throws Exception { 80 } 81 82 /** 83 * Stops the target. This informs the GBean that it is about to transition to the stopped state. 84 * 85 * @throws Exception if the target failed to stop; this will cause a transition to the failed state 86 */ 87 public void doStop() throws Exception { 88 AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory(); 89 authConfigFactory.removeRegistration(registrationID); 90 } 91 92 /** 93 * Fails the GBean. This informs the GBean that it is about to transition to the failed state. 94 */ 95 public void doFail() { 96 } 97 }