KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > auth > JBossAuthenticator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.auth;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.juddi.error.RegistryException;
21 import org.apache.juddi.error.UnknownUserException;
22 import org.apache.juddi.util.Config;
23 import org.jboss.security.AuthenticationManager;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import java.security.Principal JavaDoc;
29
30 /**
31  * This is a implementation of jUDDI's Authenticator interface,
32  * that uses the JBoss authentication manager.
33  *
34  * Usage:
35  *
36  * To use this class you must add the following properties
37  * to the juddi.properties file:
38  *
39  * # The JBoss Authenticator
40  * juddi.auth=org.apache.juddi.auth.JBossAuthenticator
41  *
42  * # The security-domain, defined in $JBOSS/default/conf/login-config.xml
43  * juddi.securityDomain=java:/jaas/other
44  *
45  * @author Antoni Reus (areus@ibit.org)
46  */

47 public class JBossAuthenticator implements Authenticator
48 {
49   // private reference to the jUDDI logger
50
private static Log log = LogFactory.getLog(JBossAuthenticator.class);
51   
52   // static default security-domain to use.
53
private static final String JavaDoc SECURITY_DOMAIN_KEY = "juddi.securityDomain";
54   private static final String JavaDoc DEFAULT_SECURITY_DOMAIN = "java:/jaas/other";
55   
56   // JBoss authentication manager
57
AuthenticationManager authManager;
58
59   /**
60    *
61    */

62   public JBossAuthenticator()
63   {
64     init();
65   }
66   
67   /**
68    *
69    */

70   public String JavaDoc authenticate(final String JavaDoc userID, final String JavaDoc credential)
71     throws RegistryException
72   {
73     if (userID == null) {
74       throw new UnknownUserException("Invalid user ID = "+userID);
75     }
76   
77     // Create a principal for the userID
78
Principal JavaDoc principal = new Principal JavaDoc()
79     {
80       public String JavaDoc getName() {
81         return userID;
82       }
83     };
84     
85     if (!authManager.isValid(principal, credential)) {
86       throw new UnknownUserException("Invalid credentials");
87     }
88     
89     return userID;
90   }
91
92
93   private void init()
94   {
95     String JavaDoc securityDomain = Config.getStringProperty(SECURITY_DOMAIN_KEY,DEFAULT_SECURITY_DOMAIN);
96
97     try {
98       // lookup for the authentication manager.
99
Context JavaDoc ctx = new InitialContext JavaDoc();
100       authManager = (AuthenticationManager) ctx.lookup(securityDomain);
101       ctx.close();
102     } catch (NamingException JavaDoc e) {
103       log.error("JNDI Exception looking for autentication manager: " +
104         securityDomain, e);
105     }
106   }
107 }
Popular Tags