/** * Authenticate a user and return the {@link Response} * * @param session * @param user * @param pass * @return response */ protected final Response doAuth(POP3Session session, String user, String pass) { try { Mailbox mailbox = auth(session, user, pass); if (mailbox != null) { session.setUserMailbox(mailbox); stat(session); session.setHandlerState(POP3Session.TRANSACTION); StringBuilder responseBuffer = new StringBuilder(64).append("Welcome ").append(session.getUser()); return new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString()); } else { session.setHandlerState(POP3Session.AUTHENTICATION_READY); return AUTH_FAILED; } } catch (Exception e) { LOGGER.error("Unexpected error accessing mailbox for {}", session.getUser(), e); session.setHandlerState(POP3Session.AUTHENTICATION_READY); return UNEXPECTED_ERROR; } }
/** * Handler method called upon receipt of a PASS command. Reads in and * validates the password. */ @Override public Response onCommand(POP3Session session, Request request) { String parameters = request.getArgument(); if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) { return doAuth(session, session.getUser(), parameters); } else { session.setHandlerState(POP3Session.AUTHENTICATION_READY); return AUTH_FAILED; } }
@Override protected Mailbox auth(POP3Session session, String username, String password) throws Exception { MailboxSession mSession = null; try { mSession = manager.login(session.getUser(), password); manager.startProcessingRequest(mSession); MailboxPath inbox = MailboxPath.inbox(mSession); // check if the mailbox exists, if not create it if (!manager.mailboxExists(inbox, mSession)) { Optional<MailboxId> mailboxId = manager.createMailbox(inbox, mSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId); } MessageManager mailbox = manager.getMailbox(MailboxPath.inbox(mSession), mSession); return new MailboxAdapter(manager, mailbox, mSession); } catch (BadCredentialsException e) { return null; } catch (MailboxException e) { throw new IOException("Unable to access mailbox for user " + session.getUser(), e); } finally { if (mSession != null) { manager.endProcessingRequest(mSession); } } }