in geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/transport/smtp/SMTPConnection.java [1048:1149]
protected boolean processAuthentication() throws MessagingException {
// no authentication defined?
if (!props.getBooleanProperty(MAIL_SMTP_AUTH, false)) {
return true;
}
// we need to authenticate, but we don't have userid/password
// information...fail this
// immediately.
if (username == null || password == null) {
return false;
}
// if unable to get an appropriate authenticator, just fail it.
ClientAuthenticator authenticator = getSaslAuthenticator();
if (authenticator == null) {
throw new MessagingException("Unable to obtain SASL authenticator");
}
if (debug) {
debugOut("Authenticating for user: " + username + " using " + authenticator.getMechanismName());
}
// if the authenticator has some initial data, we compose a command
// containing the initial data.
if (authenticator.hasInitialResponse()) {
StringBuffer command = new StringBuffer();
// the auth command initiates the handshaking.
command.append("AUTH ");
// and tell the server which mechanism we're using.
command.append(authenticator.getMechanismName());
command.append(" ");
// and append the response data
try {
command.append(new String(Base64.encode(authenticator.evaluateChallenge(null)), "US-ASCII"));
} catch (UnsupportedEncodingException e) {
}
// send the command now
sendLine(command.toString());
}
// we just send an auth command with the command type.
else {
StringBuffer command = new StringBuffer();
// the auth command initiates the handshaking.
command.append("AUTH ");
// and tell the server which mechanism we're using.
command.append(authenticator.getMechanismName());
// send the command now
sendLine(command.toString());
}
// now process the challenge sequence. We get a 235 response back when
// the server accepts the
// authentication, and a 334 indicates we have an additional challenge.
while (true) {
// get the next line, and if it is an error response, return now.
SMTPReply line;
try {
line = new SMTPReply(receiveLine());
} catch (MalformedSMTPReplyException e) {
throw new MessagingException(e.toString());
} catch (MessagingException e) {
throw e;
}
// if we get a completion return, we've passed muster, so give an
// authentication response.
if (line.getCode() == SMTPReply.AUTHENTICATION_COMPLETE) {
debugOut("Successful SMTP authentication");
return true;
}
// we have an additional challenge to process.
else if (line.getCode() == SMTPReply.AUTHENTICATION_CHALLENGE) {
// Does the authenticator think it is finished? We can't answer
// an additional challenge,
// so fail this.
if (authenticator.isComplete()) {
return false;
}
try {
// we're passed back a challenge value, Base64 encoded.
byte[] challenge = Base64.decode(line.getMessage().getBytes("ISO8859-1"));
// have the authenticator evaluate and send back the encoded
// response.
sendLine(new String(Base64.encode(authenticator.evaluateChallenge(challenge)), "US-ASCII"));
} catch (UnsupportedEncodingException e) {
}
}
// completion or challenge are the only responses we know how to
// handle. Anything else must
// be a failure.
else {
if (debug) {
debugOut("Authentication failure " + line);
}
return false;
}
}
}