forked from greenmail-mail-test/greenmail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements greenmail-mail-test#449 Make authentication details availa…
…ble in MovingMessage * Implements greenmail-mail-test#449: Stores the authentication details in the moving message so that it is available in e.g. MessageDeliveryHandler * Since different authentication methods have different details, there's a AuthenticationState base interface with PlainAuthenticationState (with an additional authorizationId) and LoginAuthenticationState POJOs for the currently supported AUTH mechanisms LOGIN and AUTH. Other AUTH mechanisms may or may not have a username and/or password. * What I don't fully understand is why the SMTPState needs to be cleared so often. It's also cleared by the MailCommand, which removes the authentication details from the MovingMessage. I modfied the MailCommand so that it clears the SMTPState, but transfers the authentication details from the old moving message to the new one. We could also store the authentication details in the SMTPState as well, but that feels a bit redundant. * When I run the tests locally, the DummySSLServerSocketFactoryTest#testLoadKeyStoreViaSystemProperty fails, but that's because I don't have a certificate with an `amazonrootca1` in my system root key store (there is, e.g. a `debian:amazon_root_ca_1.pem` certificate and when I change the test to use that, it passes)
- Loading branch information
1 parent
22e8461
commit 306165a
Showing
11 changed files
with
344 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
greenmail-core/src/main/java/com/icegreen/greenmail/smtp/auth/AuthenticationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.icegreen.greenmail.smtp.auth; | ||
|
||
/** | ||
* Base interface for the state used by various authentication methods. The data contained | ||
* in the state depends on the authentication method that was used. The state can be | ||
* mutable, such as when the authentication method requires multiple exchanges between the | ||
* client and the server. | ||
*/ | ||
public interface AuthenticationState { | ||
/** | ||
* @return The type of the used authentication mechanism, e.g. {@code PLAIN} or {@code LOGIN}. | ||
*/ | ||
String getType(); | ||
} |
45 changes: 45 additions & 0 deletions
45
greenmail-core/src/main/java/com/icegreen/greenmail/smtp/auth/LoginAuthenticationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.icegreen.greenmail.smtp.auth; | ||
|
||
import com.icegreen.greenmail.smtp.commands.AuthCommand.AuthMechanism; | ||
|
||
/** | ||
* Details from the {@link AuthMechanism#LOGIN} authorization mechanism, when | ||
* that mechanism was used for authentication. | ||
*/ | ||
public class LoginAuthenticationState implements AuthenticationState, UsernameAuthentication { | ||
private final String username; | ||
|
||
private final String password; | ||
|
||
/** | ||
* @param username The username from the AUTH command. | ||
* @param password The password from the AUTH command. | ||
*/ | ||
public LoginAuthenticationState(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return AuthMechanism.LOGIN.name(); | ||
} | ||
|
||
/** | ||
* Retrieves the username that was used for {@code PLAIN} or {@code LOGIN} authentication. | ||
* Note that this will return {@code null} when no authentication was performed or needed. | ||
* @return The username from the AUTH command. | ||
*/ | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
/** | ||
* Retrieves the password that was used for {@code PLAIN} or {@code LOGIN} authentication. | ||
* Note that this will return {@code null} when no authentication was performed or needed. | ||
* @return The password from the AUTH command. | ||
*/ | ||
public String getPassword() { | ||
return password; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
greenmail-core/src/main/java/com/icegreen/greenmail/smtp/auth/PlainAuthenticationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.icegreen.greenmail.smtp.auth; | ||
|
||
import com.icegreen.greenmail.smtp.commands.AuthCommand.AuthMechanism; | ||
import com.icegreen.greenmail.util.SaslMessage; | ||
|
||
/** | ||
* Details from the {@link AuthMechanism#PLAIN} authorization mechanism, when | ||
* that mechanism was used for authentication. | ||
*/ | ||
public class PlainAuthenticationState implements AuthenticationState, UsernameAuthentication { | ||
private final String authorizationId; | ||
private final String authenticationId; | ||
private final String password; | ||
|
||
/** | ||
* @param saslMessage The parsed message sent by the client with the {@code AUTH} command. | ||
*/ | ||
public PlainAuthenticationState(SaslMessage saslMessage) { | ||
this(saslMessage.getAuthzid(), saslMessage.getAuthcid(), saslMessage.getPasswd()); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return AuthMechanism.PLAIN.name(); | ||
} | ||
|
||
/** | ||
* @param authorizationId The authorization ID sent by the client with the {@code AUTH} command. | ||
* @param authenticationId The authentication ID sent by the client with the {@code AUTH} command. | ||
* @param password The password sent by the client with the {@code AUTH} command. | ||
*/ | ||
public PlainAuthenticationState(String authorizationId, String authenticationId, String password) { | ||
this.authorizationId = authorizationId; | ||
this.authenticationId = authenticationId; | ||
this.password = password; | ||
} | ||
|
||
/** | ||
* @return The authorization ID sent by the client with the {@code AUTH} command. | ||
*/ | ||
public String getAuthorizationId() { | ||
return authorizationId; | ||
} | ||
|
||
/** | ||
* @return The authentication ID sent by the client with the {@code AUTH} command. | ||
*/ | ||
public String getAuthenticationId() { | ||
return authenticationId; | ||
} | ||
|
||
/** | ||
* @return password The password sent by the client with the {@code AUTH} command. | ||
*/ | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return authenticationId; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
greenmail-core/src/main/java/com/icegreen/greenmail/smtp/auth/UsernameAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.icegreen.greenmail.smtp.auth; | ||
|
||
/** | ||
* Base interface for authentication methods that provide or make use of | ||
* a plain text username to identify a user, such as the {@code PLAIN} or | ||
* {@code LOGIN} authentication mechanisms. | ||
*/ | ||
public interface UsernameAuthentication { | ||
String getUsername(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.