KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > security > jaas > CredentialHandler


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.framework.security.jaas;
19
20 import java.io.*;
21 import java.util.logging.Logger JavaDoc;
22 import java.util.logging.Level JavaDoc;
23
24 import javax.security.auth.callback.*;
25
26 import sync4j.framework.core.Cred;
27 import sync4j.framework.logging.Sync4jLogger;
28 import sync4j.framework.tools.Base64;
29 import org.apache.commons.lang.StringUtils;
30
31 /**
32  * This handler implements the JAAS <i>CallbackHandler</i> interface. It stores
33  * a <i>Credential</i> object for later use as principal and credentials provider.
34  * This simple implementation supports basic authentication stored bas64 encoded
35  * in the form login:password.
36  *
37  * TO DO: supports MD5 authentication
38  *
39  * @author Stefano Fornari @ Funambol.com
40  * @version $Id: CredentialHandler.java,v 1.10 2005/03/02 20:57:38 harrie Exp $
41  */

42 public class CredentialHandler implements CallbackHandler {
43     
44     // --------------------------------------------------------------- Constants
45

46     public static final String JavaDoc SUPPORTED_TYPES = Cred.AUTH_TYPE_BASIC;
47     
48     // ------------------------------------------------------------ Private data
49

50     private String JavaDoc login = null;
51     private char[] password = null;
52     
53     // ---------------------------------------------------------- Protected data
54

55     protected final Logger JavaDoc log = Sync4jLogger.getLogger();
56     
57     // ------------------------------------------------------------ Constructors
58

59     /**
60      * Creates a new instance of CredentialHandler
61      *
62      * @param credential
63      *
64      * @throws IllegalArgumentException
65      */

66     public CredentialHandler(Cred credential) throws IllegalArgumentException JavaDoc {
67         
68         String JavaDoc type = credential.getType();
69         
70         if (log.isLoggable(Level.FINEST)) {
71             log.finest("credential: " + credential);
72         }
73         
74         if (SUPPORTED_TYPES.indexOf(type) < 0) {
75             throw new IllegalArgumentException JavaDoc( "Authorization type '"
76                                               + type
77                                               + "' not supported"
78                                               );
79         }
80         
81         if (Cred.AUTH_TYPE_BASIC.equals(type)) {
82             String JavaDoc s = new String JavaDoc(Base64.decode(credential.getData()));
83             
84             int p = s.indexOf(':');
85             
86             if (p == -1) {
87                 login = s;
88                 password = null;
89             } else {
90                 login = (p>0) ? s.substring(0, p) : "";
91                 password = toChars((p == (s.length()-1)) ? "" : s.substring(p+1));
92             }
93         }
94     }
95     
96     // ---------------------------------------------------------- Public methods
97

98     /**
99      * Returns the login of this credential.
100      *
101      * @return the login value
102      */

103     public String JavaDoc getLogin() {
104         return login;
105     }
106     
107     // ------------------------------------------------------------------ handle
108

109     public void handle(Callback[] callbacks)
110     throws IOException, UnsupportedCallbackException {
111         for (int i = 0; i < callbacks.length; i++) {
112             if (log.isLoggable(Level.FINEST)) {
113                 log.finest("Handling " + callbacks[i]);
114             }
115             
116             if (callbacks[i] instanceof TextOutputCallback) {
117
118                 // display the message according to the specified type
119
TextOutputCallback toc = (TextOutputCallback)callbacks[i];
120                 switch (toc.getMessageType()) {
121                    case TextOutputCallback.INFORMATION:
122                       log.info(toc.getMessage());
123                       break;
124                    case TextOutputCallback.ERROR:
125                       log.severe(toc.getMessage());
126                       break;
127                    case TextOutputCallback.WARNING:
128                       log.warning(toc.getMessage());
129                      break;
130                    default:
131                       throw new IOException( "Unsupported message type: "
132                                            + toc.getMessageType()
133                                            );
134                 }
135
136             } else if (callbacks[i] instanceof NameCallback) {
137                 NameCallback nc = (NameCallback)callbacks[i];
138                 nc.setName(login);
139             } else if (callbacks[i] instanceof PasswordCallback) {
140                 PasswordCallback nc = (PasswordCallback)callbacks[i];
141                 nc.setPassword(password);
142             } else {
143                 throw new UnsupportedCallbackException
144                  (callbacks[i], "Unrecognized Callback");
145             }
146           }
147     }
148     
149     // --------------------------------------------------------- Private methods
150

151     private char[] toChars(String JavaDoc str) {
152         if (StringUtils.isEmpty(str)) {
153             return new char[0];
154         }
155         
156         int l = str.length();
157         char[] ret = new char[l];
158         
159         str.getChars(0, l, ret, 0);
160         
161         return ret;
162     }
163 }
Popular Tags