View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security.auth.callback;
35  
36  import info.magnolia.cms.security.User;
37  
38  import java.io.IOException;
39  
40  import javax.security.auth.callback.Callback;
41  import javax.security.auth.callback.CallbackHandler;
42  import javax.security.auth.callback.NameCallback;
43  import javax.security.auth.callback.PasswordCallback;
44  import javax.security.auth.callback.TextOutputCallback;
45  import javax.security.auth.callback.UnsupportedCallbackException;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * Plain text callback handler
53   * @author Sameer Charles
54   * @version $Id: CredentialsCallbackHandler.java 32667 2010-03-13 00:37:06Z gjoseph $
55   */
56  public class CredentialsCallbackHandler implements CallbackHandler {
57  
58      /**
59       * Logger
60       */
61      protected static Logger log = LoggerFactory.getLogger(CredentialsCallbackHandler.class);
62  
63      /**
64       * user id
65       */
66      protected String name;
67  
68      /**
69       * password
70       */
71      protected char[] pswd;
72  
73      /**
74       * The realm to which we login
75       */
76      protected String realm;
77  
78      protected User user;
79  
80      /**
81       * default constructor required by java security framework
82       */
83      public CredentialsCallbackHandler() {
84          // do not instanciate with this constructor
85      }
86  
87      public CredentialsCallbackHandler(String name, char[] pswd) {
88          this.name = name;
89          this.pswd = pswd;
90      }
91  
92      public CredentialsCallbackHandler(String name, char[] pswd, String realm) {
93          this(name, pswd);
94          this.realm = realm;
95      }
96  
97      /**
98       * handle name and password callback which must be set while constructing this handler
99       */
100     public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
101         for (int i = 0; i < callbacks.length; i++) {
102             if (callbacks[i] instanceof NameCallback) {
103                 ((NameCallback) callbacks[i]).setName(this.name);
104             }
105             else if (callbacks[i] instanceof PasswordCallback) {
106                 ((PasswordCallback) callbacks[i]).setPassword(this.pswd);
107             }
108             else if (callbacks[i] instanceof RealmCallback) {
109                 ((RealmCallback) callbacks[i]).setRealm(this.realm);
110             }
111             else if (callbacks[i] instanceof UserCallback) {
112                 user = ((UserCallback) callbacks[i]).getUser();
113             }
114             else if (callbacks[i] instanceof TextOutputCallback) {
115                 TextOutputCallback outputCallback = (TextOutputCallback) callbacks[i];
116                 switch (outputCallback.getMessageType()) {
117                     case TextOutputCallback.INFORMATION:
118                         log.info(outputCallback.getMessage());
119                         break;
120                     case TextOutputCallback.ERROR:
121                         log.error(outputCallback.getMessage());
122                         break;
123                     case TextOutputCallback.WARNING:
124                         log.warn(outputCallback.getMessage());
125                         break;
126                     default:
127                         if (log.isDebugEnabled()) {
128                             log.debug("Unsupported message type : {}", Integer
129                                 .toString(outputCallback.getMessageType()));
130                             log.debug("Message : {}", outputCallback.getMessage());
131                         }
132                 }
133             }
134         }
135     }
136 
137     
138     public User getUser() {
139         return this.user;
140     }
141 }