KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > authentication > PasswordAuthenticator


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marc Wick.
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.common.authentication;
24
25 import javax.management.remote.JMXAuthenticator JavaDoc;
26 import javax.security.auth.Subject JavaDoc;
27
28 import org.continuent.sequoia.common.log.Trace;
29
30 /**
31  * This class defines a PasswordAuthenticator
32  *
33  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
34  * @version 1.0
35  */

36 public class PasswordAuthenticator implements JMXAuthenticator JavaDoc
37
38 {
39
40   /**
41    * to enable subject delegation we use a dummy authentication even if none is
42    * configured
43    */

44   public static final PasswordAuthenticator NO_AUTHENICATION = new PasswordAuthenticator(
45                                                                  null, null);
46
47   static Trace logger = Trace
48                                                                  .getLogger("org.continuent.sequoia.common.authentication");
49
50   private String JavaDoc username;
51   private String JavaDoc password;
52
53   /**
54    * Creates a new <code>PasswordAuthenticator.java</code> object
55    *
56    * @param username username/loginname
57    * @param password password
58    */

59   public PasswordAuthenticator(String JavaDoc username, String JavaDoc password)
60   {
61     this.username = username;
62     this.password = password;
63   }
64
65   /**
66    * create a credentials object with the supplied username and password
67    *
68    * @param username username
69    * @param password password
70    * @return credentials Object to be used for authentication,
71    */

72   public static Object JavaDoc createCredentials(String JavaDoc username, String JavaDoc password)
73   {
74     return new String JavaDoc[]{username, password};
75   }
76
77   /**
78    * @see javax.management.remote.JMXAuthenticator#authenticate(java.lang.Object)
79    */

80   public Subject JavaDoc authenticate(Object JavaDoc credentials) throws SecurityException JavaDoc
81   {
82     try
83     {
84       if (username == null && password == null)
85       {
86         // no authentication is required we return
87
return new Subject JavaDoc();
88       }
89
90       if (credentials == null)
91       {
92         throw new SecurityException JavaDoc("credentials are required");
93       }
94
95       try
96       {
97         String JavaDoc[] credentialsArray = (String JavaDoc[]) credentials;
98         if (username.equals(credentialsArray[0])
99             && password.equals(credentialsArray[1]))
100         {
101           // username and password are ok
102
if (logger.isDebugEnabled())
103           {
104             logger.debug("successfully authenitcated ");
105           }
106           return new Subject JavaDoc();
107         }
108       }
109       catch (Exception JavaDoc e)
110       {
111         // the credentials object makes problems, is was probably not created
112
// with the createCredentials method
113
throw new SecurityException JavaDoc("problems with credentials object : "
114             + e.getMessage());
115       }
116
117       // username and password do not match
118
throw new SecurityException JavaDoc("invalid credentials");
119     }
120     catch (SecurityException JavaDoc e)
121     {
122       logger.error(e.getMessage());
123       try
124       {
125         String JavaDoc clientId = java.rmi.server.RemoteServer.getClientHost();
126         logger.warn("refused unauthorized access for client " + clientId);
127       }
128       catch (Exception JavaDoc ex)
129       {
130
131       }
132       throw e;
133     }
134   }
135 }
136
Popular Tags