Improved OAuth username check

pull/199/head
M66B 3 years ago
parent fce2ba29d1
commit 38a497280b

@ -475,6 +475,7 @@ public class FragmentOAuth extends FragmentBase {
String username = address; String username = address;
List<String> usernames = new ArrayList<>(); List<String> usernames = new ArrayList<>();
usernames.add(address);
if (token != null) { if (token != null) {
// https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens // https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens
@ -486,17 +487,17 @@ public class FragmentOAuth extends FragmentBase {
JSONObject jpayload = new JSONObject(payload); JSONObject jpayload = new JSONObject(payload);
if (jpayload.has("preferred_username")) { if (jpayload.has("preferred_username")) {
String u = jpayload.getString("preferred_username"); String u = jpayload.getString("preferred_username");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
if (jpayload.has("unique_name")) { if (jpayload.has("unique_name")) {
String u = jpayload.getString("unique_name"); String u = jpayload.getString("unique_name");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
if (jpayload.has("upn")) { if (jpayload.has("upn")) {
String u = jpayload.getString("upn"); String u = jpayload.getString("upn");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
@ -515,17 +516,17 @@ public class FragmentOAuth extends FragmentBase {
JSONObject jpayload = new JSONObject(payload); JSONObject jpayload = new JSONObject(payload);
if (jpayload.has("preferred_username")) { if (jpayload.has("preferred_username")) {
String u = jpayload.getString("preferred_username"); String u = jpayload.getString("preferred_username");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
if (jpayload.has("email")) { if (jpayload.has("email")) {
String u = jpayload.getString("email"); String u = jpayload.getString("email");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
if (jpayload.has("unique_name")) { if (jpayload.has("unique_name")) {
String u = jpayload.getString("unique_name"); String u = jpayload.getString("unique_name");
if (!usernames.contains(u)) if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u); usernames.add(u);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
@ -533,22 +534,21 @@ public class FragmentOAuth extends FragmentBase {
} }
} }
for (String alt : usernames) { if (usernames.size() > 1)
if (TextUtils.isEmpty(alt) || alt.equals(username)) for (String alt : usernames) {
continue; EntityLog.log(context, "Trying username=" + alt);
EntityLog.log(context, "Trying username=" + alt); try (EmailService iservice = new EmailService(
try (EmailService iservice = new EmailService( context, aprotocol, null, aencryption, false,
context, aprotocol, null, aencryption, false, EmailService.PURPOSE_CHECK, true)) {
EmailService.PURPOSE_CHECK, true)) { iservice.connect(
iservice.connect( provider.imap.host, provider.imap.port,
provider.imap.host, provider.imap.port, AUTH_TYPE_OAUTH, provider.id,
AUTH_TYPE_OAUTH, provider.id, alt, state,
alt, state, null, null);
null, null); EntityLog.log(context, "Using username=" + alt);
EntityLog.log(context, "Using username=" + alt); username = alt;
username = alt; }
} }
}
List<Pair<String, String>> identities = new ArrayList<>(); List<Pair<String, String>> identities = new ArrayList<>();

Loading…
Cancel
Save