Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit fa80cd6

Browse files
author
Rustam Aliyev
committed
Added POP3 authentication.
1 parent 5fd9765 commit fa80cd6

File tree

12 files changed

+296
-31
lines changed

12 files changed

+296
-31
lines changed

modules/core/osgi.bnd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Export-Package: \
66
com.elasticinbox.core,\
77
com.elasticinbox.core.model,\
88
com.elasticinbox.core.utils,\
9-
com.elasticinbox.core.account.validator,\
9+
com.elasticinbox.core.account.*,\
1010
com.elasticinbox.core.message,\
1111
com.elasticinbox.core.message.id,\
1212
com.elasticinbox.core.blob,\
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
package com.elasticinbox.core.account.authenticator;
30+
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import com.elasticinbox.core.model.Mailbox;
35+
36+
/**
37+
* Authenticator which allows all users without checking password
38+
*
39+
* @author Rustam Aliyev
40+
*/
41+
public final class AllowAllAuthenticator implements IAuthenticator
42+
{
43+
private static final Logger logger = LoggerFactory
44+
.getLogger(AllowAllAuthenticator.class);
45+
46+
@Override
47+
public Mailbox authenticate(String username, String password)
48+
{
49+
Mailbox mailbox = new Mailbox(username);
50+
logger.debug("Authenticated " + mailbox.getId());
51+
return mailbox;
52+
}
53+
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
package com.elasticinbox.core.account.authenticator;
30+
31+
public class AuthenticationException extends Exception
32+
{
33+
private static final long serialVersionUID = 1L;
34+
35+
public AuthenticationException(String message)
36+
{
37+
super(message);
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
package com.elasticinbox.core.account.authenticator;
30+
31+
/**
32+
* Creates account authenticator
33+
*
34+
* @author Rustam Aliyev
35+
*/
36+
public class AuthenticatorFactory
37+
{
38+
private AuthenticatorFactory() {
39+
// ensure non-instantiability
40+
}
41+
42+
public static IAuthenticator getAuthenticator() {
43+
return new AllowAllAuthenticator();
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
package com.elasticinbox.core.account.authenticator;
30+
31+
import com.elasticinbox.core.model.Mailbox;
32+
33+
public interface IAuthenticator
34+
{
35+
public enum AccountStatus {
36+
ACTIVE, BLOCKED, NOT_FOUND
37+
}
38+
39+
/**
40+
* Authenticate user with provided username/password pair.
41+
*
42+
* @param username
43+
* @param password
44+
* @return
45+
*/
46+
public Mailbox authenticate(String username, String password) throws AuthenticationException;
47+
}

modules/core/src/main/java/com/elasticinbox/core/account/validator/DummyValidator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
*
3939
* @author Rustam Aliyev
4040
*/
41-
final class DummyValidator implements IValidator
41+
public final class DummyValidator implements IValidator
4242
{
4343
private static final Logger logger = LoggerFactory
4444
.getLogger(DummyValidator.class);
4545

4646
@Override
47-
public AccountStatus getAccountStatus(Mailbox mailbox)
47+
public AccountStatus getAccountStatus(String username)
4848
{
49+
Mailbox mailbox = new Mailbox(username);
4950
logger.debug("Validating " + mailbox.getId());
5051
return AccountStatus.ACTIVE;
5152
}

modules/core/src/main/java/com/elasticinbox/core/account/validator/IValidator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828

2929
package com.elasticinbox.core.account.validator;
3030

31-
import com.elasticinbox.core.model.Mailbox;
32-
3331
public interface IValidator
3432
{
3533
public enum AccountStatus {
3634
ACTIVE, BLOCKED, NOT_FOUND
3735
}
3836

39-
public AccountStatus getAccountStatus(Mailbox mailbox);
37+
public AccountStatus getAccountStatus(String username);
4038
}

modules/lmtp/src/main/java/com/elasticinbox/lmtp/server/api/handler/ValidRcptHandler.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
import com.elasticinbox.core.account.validator.IValidator;
88
import com.elasticinbox.core.account.validator.IValidator.AccountStatus;
99
import com.elasticinbox.core.account.validator.ValidatorFactory;
10-
import com.elasticinbox.core.model.Mailbox;
1110

1211
public class ValidRcptHandler extends AbstractValidRcptHandler
1312
{
14-
IValidator validator = ValidatorFactory.getValidator();
13+
private IValidator validator = ValidatorFactory.getValidator();
1514

1615
@Override
1716
protected boolean isValidRecipient(SMTPSession session, MailAddress recipient)
1817
{
19-
Mailbox mailbox = new Mailbox(recipient.toString());
20-
AccountStatus status = validator.getAccountStatus(mailbox);
21-
session.getLogger().debug("Validated account (" + mailbox.getId() +
22-
") status is " + status.toString());
18+
AccountStatus status = validator.getAccountStatus(recipient.toString());
19+
session.getLogger().debug("Validated account (" + recipient + ") status is " + status);
2320

2421
return status.equals(AccountStatus.ACTIVE) ? true : false;
2522
}

modules/pop3/src/main/java/com/elasticinbox/pop3/POP3ProxyServer.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ public void start() throws Exception
5959
{
6060
Logger logger = new POP3ProtocolLogger();
6161

62-
// POP3ProtocolHandlerChain chain = new POP3ProtocolHandlerChain();
63-
// chain.add(0, new PassCmdHandler(backend));
64-
// chain.wireExtensibleHandlers();
65-
6662
POP3ProtocolHandlerChain chain = new POP3ProtocolHandlerChain(new AuthHandler(backend));
6763

6864
server = new NettyServer(new POP3Protocol(chain, new POP3ServerConfig(), logger));
Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,94 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
129
package com.elasticinbox.pop3.server.handler;
230

331
import org.apache.james.protocols.pop3.POP3Session;
432
import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
5-
import org.apache.james.protocols.pop3.mailbox.Mailbox;
633

734
import org.slf4j.Logger;
835
import org.slf4j.LoggerFactory;
936

10-
public class AuthHandler extends AbstractPassCmdHandler
37+
import com.elasticinbox.core.account.authenticator.AuthenticationException;
38+
import com.elasticinbox.core.account.authenticator.AuthenticatorFactory;
39+
import com.elasticinbox.core.account.authenticator.IAuthenticator;
40+
import com.elasticinbox.core.account.validator.IValidator;
41+
import com.elasticinbox.core.account.validator.ValidatorFactory;
42+
import com.elasticinbox.core.account.validator.IValidator.AccountStatus;
43+
import com.elasticinbox.core.model.Mailbox;
44+
45+
/**
46+
* POP3 Authentication Handler (AUTH)
47+
*
48+
* @author Rustam Aliyev
49+
*/
50+
public final class AuthHandler extends AbstractPassCmdHandler
1151
{
1252
private static final Logger logger =
1353
LoggerFactory.getLogger(AuthHandler.class);
1454

1555
private MailboxHandlerFactory backend;
1656

57+
private IValidator validator = ValidatorFactory.getValidator();
58+
private IAuthenticator authenticator = AuthenticatorFactory.getAuthenticator();
59+
1760
public AuthHandler(MailboxHandlerFactory backend) {
1861
this.backend = backend;
1962
}
2063

2164
@Override
22-
protected Mailbox auth(POP3Session session, String username, String password) throws Exception
65+
protected org.apache.james.protocols.pop3.mailbox.Mailbox auth(POP3Session session, String username, String password) throws Exception
2366
{
67+
Mailbox mailbox;
68+
2469
logger.debug("POP3: Authenticating session {}, user {}, pass {}",
2570
new Object[] { session.getSessionID(), username, password });
2671

27-
// authenticate mailbox, if failed return null
28-
29-
// get mailbox info
30-
Mailbox mailbox = backend.getMailboxHander(username);
31-
return mailbox;
72+
try {
73+
// authenticate mailbox, if failed return null
74+
AccountStatus status = validator.getAccountStatus(username);
75+
session.getLogger().debug("Validated account (" + username + ") status is " + status.toString());
76+
77+
if (!status.equals(AccountStatus.ACTIVE)) {
78+
throw new AuthenticationException("User " + username + " does not exist or inactive");
79+
}
80+
81+
// authenticate user with password
82+
mailbox = authenticator.authenticate(username, password);
83+
84+
// return POP3 handler for mailbox
85+
return backend.getMailboxHander(mailbox);
86+
} catch (IllegalArgumentException iae) {
87+
logger.debug("POP3 Authentication failed. Invalid username [{}]: {}", username, iae.getMessage());
88+
return null;
89+
} catch (AuthenticationException ae) {
90+
logger.debug("POP3 Authentication failed. Invalid username [{}] or password [{}]", username, password);
91+
return null;
92+
}
3293
}
3394
}

0 commit comments

Comments
 (0)