1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.wallace.messages;
20
21 import javax.mail.NoSuchProviderException;
22 import javax.mail.Session;
23 import javax.mail.Store;
24 import javax.mail.URLName;
25
26 import net.sf.acegisecurity.Authentication;
27 import net.sf.acegisecurity.AuthenticationException;
28 import net.sf.acegisecurity.AuthenticationManager;
29 import net.sf.acegisecurity.BadCredentialsException;
30 import net.sf.acegisecurity.DisabledException;
31 import net.sf.acegisecurity.LockedException;
32 import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
33 import net.sf.wallace.Command;
34 import net.sf.wallace.ServerResponse;
35 import net.sf.wallace.WallaceServerConfig;
36 import net.sf.wallace.WallaceSession;
37 import net.sf.wallace.WallaceSessionState;
38 import net.sf.wallace.WallaceUser;
39 import net.sf.wallace.commands.NonAuthCommand;
40
41 /***
42 * The LOGIN command.
43 *
44 * @author rnewson
45 */
46 public final class LoginMessage extends AbstractMessage {
47
48 private String username;
49
50 private String password;
51
52 public void setUsername(final String newUsername) {
53 username = newUsername;
54 }
55
56 public String getUsername() {
57 return username;
58 }
59
60 public void setPassword(final String newPassword) {
61 password = newPassword;
62 }
63
64 public String getPassword() {
65 return password;
66 }
67
68 public Command getCommand() {
69 return new LoginCommand();
70 }
71
72 private final class LoginCommand extends NonAuthCommand {
73 protected void onExecute(final WallaceSession wallaceSession) throws NoSuchProviderException {
74
75 final AuthenticationManager authenticationManager = wallaceSession.getServerConfig().getAuthenticationManager();
76
77 final Authentication authentication = new UsernamePasswordAuthenticationToken(LoginMessage.this.getUsername(),
78 LoginMessage.this.getPassword());
79
80 final WallaceServerConfig config = wallaceSession.getServerConfig();
81
82 final Session session = config.getSession();
83
84 try {
85 authenticationManager.authenticate(authentication);
86 final WallaceUser user = new WallaceUser(LoginMessage.this.getUsername());
87
88 final StringBuffer buffer = new StringBuffer(50);
89 buffer.append("maildir://");
90 buffer.append(config.getMailboxRootDirectory().getAbsolutePath());
91 buffer.append("/");
92 buffer.append(user);
93 final URLName urlName = new URLName(buffer.toString());
94
95 final Store store = session.getStore(urlName);
96
97 wallaceSession.setStore(store);
98 wallaceSession.setUser(user);
99 wallaceSession.setState(WallaceSessionState.AUTHENTICATED);
100 wallaceSession.respond(new ServerResponse(LoginMessage.this, "OK LOGIN"));
101 } catch (DisabledException e) {
102 wallaceSession.respond(new ServerResponse(LoginMessage.this, "NO " + LoginMessage.this.getUsername()
103 + " account disabled."));
104 } catch (LockedException e) {
105 wallaceSession.respond(new ServerResponse(LoginMessage.this, "NO " + LoginMessage.this.getUsername()
106 + " account locked."));
107 } catch (BadCredentialsException e) {
108 wallaceSession.respond(new ServerResponse(LoginMessage.this, "NO unknown user or incorrect password."));
109 } catch (AuthenticationException e) {
110 wallaceSession.respond(new ServerResponse(LoginMessage.this, "NO unknown authentication failure."));
111 }
112 }
113 }
114
115 }