View Javadoc

1   /*
2    * Copyright (c) 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.common;
14  
15  import java.io.IOException;
16  
17  import javax.mail.Folder;
18  import javax.mail.MessagingException;
19  import javax.security.auth.callback.Callback;
20  import javax.security.auth.callback.CallbackHandler;
21  import javax.security.auth.callback.NameCallback;
22  import javax.security.auth.callback.PasswordCallback;
23  import javax.security.auth.callback.UnsupportedCallbackException;
24  import javax.security.auth.login.LoginContext;
25  import javax.security.auth.login.LoginException;
26  
27  import org.abstracthorizon.mercury.common.exception.UserRejectedException;
28  
29  /**
30   * Simple JAAS storage manager. This storage manager uses login context
31   * to authenticate user
32   *
33   * @author Daniel Sendula
34   */
35  public class SimpleJAASStorageManager extends SimpleStorageManager {
36  
37      /** Login context name */
38      protected String loginContext;
39  
40      /**
41       * Constructor
42       */
43      public SimpleJAASStorageManager() {
44      }
45  
46      /**
47       * Sets login context string
48       * @param loginContext login context string
49       */
50      public void setLoginContext(String loginContext) {
51          this.loginContext = loginContext;
52      }
53  
54      /**
55       * Returns login context string
56       * @return login context string
57       */
58      public String getLoginContext() {
59          return loginContext;
60      }
61  
62      /**
63       * This method calls super find inbox method and then authenticates user against given password.
64       * Mailbox is used for user's name.
65       * @param mailbox mailbox
66       * @param domain domain
67       * @param password password
68       * @throws UserRejectedException
69       * @throws {@link MessagingException}
70       */
71      @Override
72      public Folder findInbox(String mailbox, String domain, char[] password) throws UserRejectedException, MessagingException {
73          Folder folder = super.findInbox(mailbox, domain, password);
74          String loginContext = getLoginContext();
75          if ((loginContext != null) && (loginContext.length() > 0)) {
76              LoginContext lc = null;
77              try {
78                  lc = new LoginContext(loginContext, new Handler(mailbox, password));
79                  lc.login();
80              } catch (LoginException e) {
81                  throw new UserRejectedException("Access to mailbox " + mailbox + " is rejected");
82              } finally {
83                  if (lc != null) {
84                      try {
85                          lc.logout();
86                      } catch (LoginException ignore) {
87                      }
88                  }
89              }
90          }
91  
92          return folder;
93      }
94  
95      /**
96       * Callback handler
97       */
98      protected static class Handler implements CallbackHandler {
99          /** Username */
100         protected String user;
101         /** Password */
102         protected char[] pass;
103 
104         /**
105          * Constructor
106          * @param user username
107          * @param pass password
108          */
109         protected Handler(String user, char[] pass) {
110             this.user = user;
111             this.pass = pass;
112         }
113 
114         /**
115          * Handles callback
116          * @param callbacks callbacks
117          * @throws IOException
118          * @throws UnsupportedCallbackException
119          */
120         public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
121             for (int i = 0; i < callbacks.length; i++) {
122                 if (callbacks[i] instanceof NameCallback) {
123                     NameCallback nc = (NameCallback)callbacks[i];
124                     nc.setName(user);
125                 } else if (callbacks[i] instanceof PasswordCallback) {
126                     PasswordCallback pc = (PasswordCallback)callbacks[i];
127                     pc.setPassword(pass);
128                 }
129              }
130         }
131     }
132 
133 
134 }