View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.security.realm.providers;
19  
20  import java.util.Map;
21  import javax.security.auth.Subject;
22  import javax.security.auth.callback.Callback;
23  import javax.security.auth.callback.CallbackHandler;
24  import javax.security.auth.callback.NameCallback;
25  import javax.security.auth.callback.PasswordCallback;
26  import javax.security.auth.callback.UnsupportedCallbackException;
27  import javax.security.auth.login.LoginException;
28  import javax.security.auth.spi.LoginModule;
29  
30  
31  /**
32   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
33   */
34  public class GeronimoPasswordCredentialLoginModule implements LoginModule {
35  
36      private Subject subject;
37      private CallbackHandler callbackHandler;
38  
39      private GeronimoPasswordCredential geronimoPasswordCredential;
40  
41      public void initialize(Subject subject, CallbackHandler callbackHandler,
42                             Map sharedState, Map options) {
43          this.subject = subject;
44          this.callbackHandler = callbackHandler;
45      }
46  
47      public boolean login() throws LoginException {
48          Callback[] callbacks = new Callback[2];
49          callbacks[0] = new NameCallback("");
50          callbacks[1] = new PasswordCallback("", false);
51          try {
52              callbackHandler.handle(callbacks);
53          } catch (java.io.IOException e) {
54          } catch (UnsupportedCallbackException e) {
55              throw (LoginException) new LoginException("Unlikely UnsupportedCallbackException").initCause(e);
56          }
57          geronimoPasswordCredential = new GeronimoPasswordCredential(((NameCallback) callbacks[0]).getName(),
58                                                                      ((PasswordCallback) callbacks[1]).getPassword());
59          return true;
60      }
61  
62      public boolean commit() throws LoginException {
63          subject.getPrivateCredentials().add(geronimoPasswordCredential);
64          return true;
65      }
66  
67      public boolean abort() throws LoginException {
68          geronimoPasswordCredential = null;
69          return true;
70      }
71  
72      public boolean logout() throws LoginException {
73          geronimoPasswordCredential = null;
74          return true;
75      }
76  }