View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.maildir.security;
14  
15  import java.io.IOException;
16  import javax.security.auth.callback.Callback;
17  import javax.security.auth.callback.CallbackHandler;
18  import javax.security.auth.callback.NameCallback;
19  import javax.security.auth.callback.PasswordCallback;
20  import javax.security.auth.callback.UnsupportedCallbackException;
21  import javax.security.auth.login.LoginContext;
22  import javax.security.auth.login.LoginException;
23  
24  /**
25   * Authentication provider that uses Java security model. It tried to log in given login context.
26   *
27   * @author Daniel Sendula
28   */
29  public class JavaSecurityAuthenticationProvider implements AuthenticationProvider {
30  
31      /** Login context name */
32      protected String loginContext;
33  
34      /**
35       * Constructor
36       */
37      public JavaSecurityAuthenticationProvider() {
38      }
39  
40      /**
41       * Stores login context for further use
42       * @param loginContext login context name
43       */
44      public void init(String loginContext) {
45          this.loginContext = loginContext;
46      }
47  
48      /**
49       * Authenticates user by logging to given login context
50       * @param host not used
51       * @param port not used
52       * @param user user name to be used to log in login context
53       * @param pass password to be used to log in login context
54       * @return <code>true</code> if logging in login context was successful
55       */
56      public boolean authenticate(String host, int port, String user, char[] pass) {
57          try {
58              LoginContext lc = new LoginContext(loginContext, new Handler(user, pass));
59              lc.login();
60              lc.logout();
61              return true;
62          } catch (LoginException ignore) {
63          }
64          return false;
65      }
66  
67      /**
68       * Inner class that handles call backs
69       */
70      protected static class Handler implements CallbackHandler {
71          protected String user;
72          protected char[] pass;
73  
74          protected Handler(String user, char[] pass) {
75              this.user = user;
76              this.pass = pass;
77          }
78  
79          public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
80              for (int i = 0; i < callbacks.length; i++) {
81                  if (callbacks[i] instanceof NameCallback) {
82                      NameCallback nc = (NameCallback)callbacks[i];
83                      nc.setName(user);
84                  } else if (callbacks[i] instanceof PasswordCallback) {
85                      PasswordCallback pc = (PasswordCallback)callbacks[i];
86                      pc.setPassword(pass);
87                  }
88               }
89          }
90      }
91  }