|
|
@ -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|