Retry other auth methods on SASL failure

pull/172/head
M66B 5 years ago
parent 54f1948886
commit fed48455b0

@ -58,7 +58,7 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator {
final String p) throws ProtocolException { final String p) throws ProtocolException {
if (!pr.hasCapability("AUTH=CRAM-MD5")) if (!pr.hasCapability("AUTH=CRAM-MD5"))
throw new UnsupportedOperationException(); throw new UnsupportedOperationException("SASL not supported");
List<Response> v = new ArrayList<>(); List<Response> v = new ArrayList<>();
String tag = null; String tag = null;
@ -66,11 +66,11 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator {
boolean done = false; boolean done = false;
try { try {
Log.i("IMAP SASL command=AUTHENTICATE"); Log.i("SASL IMAP command=AUTHENTICATE");
Argument args = new Argument(); Argument args = new Argument();
args.writeAtom("CRAM-MD5"); args.writeAtom("CRAM-MD5");
tag = pr.writeCommand("AUTHENTICATE", args); tag = pr.writeCommand("AUTHENTICATE", args);
Log.i("IMAP SASL tag=" + tag); Log.i("SASL IMAP tag=" + tag);
} catch (Exception ex) { } catch (Exception ex) {
r = Response.byeResponse(ex); r = Response.byeResponse(ex);
done = true; done = true;
@ -79,13 +79,13 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator {
while (!done) { while (!done) {
try { try {
r = pr.readResponse(); r = pr.readResponse();
Log.i("IMAP SASL response=" + r); Log.i("SASL IMAP response=" + r);
if (r.isContinuation()) { if (r.isContinuation()) {
byte[] nonce = Base64.decode(r.getRest(), Base64.NO_WRAP); byte[] nonce = Base64.decode(r.getRest(), Base64.NO_WRAP);
Log.i("IMAP SASL nonce=" + new String(nonce)); Log.i("SASL IMAP nonce=" + new String(nonce));
String hmac = Helper.HMAC("MD5", 64, p.getBytes(), nonce); String hmac = Helper.HMAC("MD5", 64, p.getBytes(), nonce);
String hash = Base64.encodeToString((u + " " + hmac).getBytes(), Base64.NO_WRAP) + "\r\n"; String hash = Base64.encodeToString((u + " " + hmac).getBytes(), Base64.NO_WRAP) + "\r\n";
Log.i("IMAP SASL hash=" + hash); Log.i("SASL IMAP hash=" + hash);
pr.getIMAPOutputStream().write(hash.getBytes()); pr.getIMAPOutputStream().write(hash.getBytes());
pr.getIMAPOutputStream().flush(); pr.getIMAPOutputStream().flush();
} else if (r.isTagged() && r.getTag().equals(tag)) } else if (r.isTagged() && r.getTag().equals(tag))
@ -99,11 +99,18 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator {
v.add(r); v.add(r);
} }
Response[] responses = v.toArray(new Response[0]); try {
pr.handleCapabilityResponse(responses); Response[] responses = v.toArray(new Response[0]);
pr.notifyResponseHandlers(responses); pr.handleCapabilityResponse(responses);
pr.handleLoginResult(r); pr.notifyResponseHandlers(responses);
pr.setCapabilities(r); pr.handleLoginResult(r);
pr.setCapabilities(r);
} catch (ProtocolException ex) {
Log.w(ex);
throw new UnsupportedOperationException("SASL not authenticated");
}
Log.i("SASL IMAP authenticated");
return true; return true;
} }
} }

@ -56,7 +56,7 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator {
final String p) throws MessagingException { final String p) throws MessagingException {
if (!pr.supportsAuthentication("CRAM-MD5")) if (!pr.supportsAuthentication("CRAM-MD5"))
throw new UnsupportedOperationException(); throw new UnsupportedOperationException("SASL not supported");
// https://tools.ietf.org/html/rfc4954 // https://tools.ietf.org/html/rfc4954
int resp = simpleCommand(pr, "AUTH CRAM-MD5"); int resp = simpleCommand(pr, "AUTH CRAM-MD5");
@ -65,11 +65,15 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator {
resp = simpleCommand(pr, "AUTH CRAM-MD5"); resp = simpleCommand(pr, "AUTH CRAM-MD5");
} }
if (resp == 235) // Authentication Succeeded if (resp == 235) { // Authentication Succeeded
Log.i("SASL SMTP already authenticated");
return true; return true;
}
if (resp != 334) // server challenge if (resp != 334) { // server challenge
return false; Log.i("SASL SMTP response=" + resp);
throw new UnsupportedOperationException("SASL not supported");
}
try { try {
String t = responseText(pr); String t = responseText(pr);
@ -81,13 +85,19 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator {
throw new MessagingException("CRAM-MD5", ex); throw new MessagingException("CRAM-MD5", ex);
} }
return (resp == 235); if (resp != 235) {
Log.i("SASL SMTP not authenticated response=" + resp);
throw new UnsupportedOperationException("SASL not authenticated");
}
Log.i("SASL SMTP authenticated");
return true;
} }
private static int simpleCommand(SMTPTransport pr, String command) throws MessagingException { private static int simpleCommand(SMTPTransport pr, String command) throws MessagingException {
Log.i("SMTP SASL command=" + command); Log.i("SASL SMTP command=" + command);
int resp = pr.simpleCommand(command); int resp = pr.simpleCommand(command);
Log.i("SMTP SASL response=" + pr.getLastServerResponse()); Log.i("SASL SMTP response=" + pr.getLastServerResponse());
return resp; return resp;
} }

Loading…
Cancel
Save