Skip to content

Commit

Permalink
Changes for #268 from master
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewcomb authored and marcelmay committed Apr 13, 2020
1 parent 41a350c commit 4ece734
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Pop3State {
public Pop3State(UserManager manager) {
this.manager = manager;
this.imapHostManager = manager.getImapHostManager();

}

public GreenMailUser getUser() {
Expand Down Expand Up @@ -59,4 +58,15 @@ public void authenticate(String pass)
public MailFolder getFolder() {
return inbox;
}

public GreenMailUser findOrCreateUser(String username) throws UserException {
if (manager.hasUser(username)) {
return manager.getUser(username);
}
if (!manager.isAuthRequired()) {
return manager.createUser(username, username, username);
}
throw new UserException("Unable to find or create user '" + username +"'");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import com.icegreen.greenmail.pop3.Pop3Connection;
import com.icegreen.greenmail.pop3.Pop3State;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.user.UserException;


public class UserCommand
extends Pop3Command {
@Override
Expand All @@ -26,12 +26,16 @@ public void execute(Pop3Connection conn, Pop3State state,
String[] args = cmd.split(" ");
if (args.length < 2) {
conn.println("-ERR Required syntax: USER <username>");

return;
}

String username = args[1];
state.setUser(state.getUser(username));
GreenMailUser user = state.findOrCreateUser(username);
if (null == user) {
conn.println("-ERR User '" + username + "' not found");
return;
}
state.setUser(user);
conn.println("+OK");
} catch (UserException nsue) {
conn.println("-ERR " + nsue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
*/
package com.icegreen.greenmail.user;

import com.icegreen.greenmail.imap.ImapHostManager;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.icegreen.greenmail.imap.ImapHostManager;

public class UserManager {
private static final Logger log = LoggerFactory.getLogger(UserManager.class);
Expand Down Expand Up @@ -65,7 +69,7 @@ public boolean test(String userId, String password) {
GreenMailUser u = getUser(userId);

if (!authRequired) {
if(null == u) { // Auto create user
if (null == u) { // Auto create user
try {
createUser(userId, userId, password);
} catch (UserException e) {
Expand All @@ -88,6 +92,10 @@ public void setAuthRequired(boolean auth) {
authRequired = auth;
}

public boolean isAuthRequired() {
return authRequired;
}

public ImapHostManager getImapHostManager() {
return imapHostManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.icegreen.greenmail.test.commands;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.mail.MessagingException;

import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.pop3.commands.AuthCommand;
import com.icegreen.greenmail.user.UserException;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.pop3.commands.AuthCommand;
import com.icegreen.greenmail.user.UserException;
import com.icegreen.greenmail.util.ServerSetupTest;

public class POP3CommandTest {
private static final String CRLF = "\r\n";
Expand Down Expand Up @@ -75,4 +78,34 @@ public void authPlainWithContinuation() throws IOException, UserException {
}
}

@Test
public void authDisabled() throws IOException, UserException {
try (Socket socket = new Socket(hostAddress, port)) {
assertThat(socket.isConnected(), is(equalTo(true)));
PrintStream printStream = new PrintStream(socket.getOutputStream());
final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

greenMail.getManagers().getUserManager().setAuthRequired(false);

assertThat(reader.readLine(), is(startsWith("+OK POP3 GreenMail Server v")));
printStream.print("USER [email protected]" + CRLF);
assertThat(reader.readLine(), is(equalTo("+OK")));
}
}

@Test
public void authEnabled() throws IOException, UserException {
try (Socket socket = new Socket(hostAddress, port)) {
assertThat(socket.isConnected(), is(equalTo(true)));
PrintStream printStream = new PrintStream(socket.getOutputStream());
final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

greenMail.getManagers().getUserManager().setAuthRequired(true);

assertThat(reader.readLine(), is(startsWith("+OK POP3 GreenMail Server v")));
printStream.print("USER [email protected]" + CRLF);
assertThat(reader.readLine(), is(not(equalTo("+OK"))));
}
}

}

0 comments on commit 4ece734

Please sign in to comment.