View Javadoc
1   package com.nilhcem.fakesmtp.server;
2   
3   import org.subethamail.smtp.AuthenticationHandler;
4   
5   /**
6    * Simulates an authentication handler to allow capturing emails that are set up with login authentication.
7    *
8    * @author jasonpenny
9    * @since 1.2
10   */
11  /*package*/ final class SMTPAuthHandler implements AuthenticationHandler {
12  	private static final String USER_IDENTITY = "User";
13  	private static final String PROMPT_USERNAME = "334 VXNlcm5hbWU6"; // VXNlcm5hbWU6 is base64 for "Username:"
14  	private static final String PROMPT_PASSWORD = "334 UGFzc3dvcmQ6"; // UGFzc3dvcmQ6 is base64 for "Password:"
15  
16  	private int pass = 0;
17  
18  	/**
19  	 * Simulates an authentication process.
20  	 * <p>
21  	 * <ul>
22  	 *   <li>first prompts for username;</li>
23  	 *   <li>then, prompts for password;</li>
24  	 *   <li>finally, returns {@code null} to finish the authentication process;</li>
25  	 * </ul>
26  	 * </p>
27  	 *
28  	 * @return <code>null</code> if the authentication process is finished, otherwise a string to hand back to the client.
29  	 * @param clientInput The client's input, eg "AUTH PLAIN dGVzdAB0ZXN0ADEyMzQ="
30  	 */
31  	@Override
32  	public String auth(String clientInput) {
33  		String prompt;
34  
35  		if (++pass == 1) {
36  			prompt = SMTPAuthHandler.PROMPT_USERNAME;
37  		} else if (pass == 2) {
38  			prompt = SMTPAuthHandler.PROMPT_PASSWORD;
39  		} else {
40  			pass = 0;
41  			prompt = null;
42  		}
43  		return prompt;
44  	}
45  
46  	/**
47  	 * If the authentication process was successful, this returns the identity
48  	 * of the user. The type defining the identity can vary depending on the
49  	 * authentication mechanism used, but typically this returns a String username.
50  	 * If authentication was not successful, the return value is undefined.
51  	 */
52  	@Override
53  	public Object getIdentity() {
54  		return SMTPAuthHandler.USER_IDENTITY;
55  	}
56  }