Source:
public Mono<AccessToken> authenticateWithSharedTokenCache(TokenRequestContext request, String username) { return Mono.fromFuture(() -> getPublicClientApplication(true).getAccounts()) .onErrorResume(t -> Mono.error(new CredentialUnavailableException( "Cannot get accounts from token cache. Error: " + t.getMessage(), t))) .flatMap(set -> { IAccount requestedAccount; Map<String, IAccount> accounts = new HashMap<>(); for (IAccount cached : set) { if (username == null || username.equals(cached.username())) { if (!accounts.containsKey(cached.homeAccountId())) { accounts.put(cached.homeAccountId(), cached); } } } if (accounts.size() == 0) { if (username == null) { return Mono.error(new CredentialUnavailableException("No accounts were discovered in the " + "shared token cache. To fix, authenticate through tooling supporting azure " + "developer sign on.")); } else { return Mono.error(new CredentialUnavailableException(String.format("User account '%s' was " + "not found in the shared token cache. Discovered Accounts: [ '%s' ]", username, set.stream().map(IAccount::username).distinct() .collect(Collectors.joining(", "))))); } } else if (accounts.size() > 1) { if (username == null) { return Mono.error(new CredentialUnavailableException("Multiple accounts were discovered " + "in the shared token cache. To fix, set the AZURE_USERNAME and AZURE_TENANT_ID " + "environment variable to the preferred username, or specify it when " + "constructing SharedTokenCacheCredential.")); } else { return Mono.error(new CredentialUnavailableException("Multiple entries for the user " + "account " + username + " were found in the shared token cache. This is not " + "currently supported by the SharedTokenCacheCredential.")); } } else { requestedAccount = accounts.values().iterator().next(); } return authenticateWithMsalAccount(request, requestedAccount); }); }

Target:
public Mono<MsalToken> authenticateWithSharedTokenCache(TokenRequestContext request, String username) { return Mono.fromFuture(() -> getPublicClientApplication(true).getAccounts()) .onErrorMap(t -> new CredentialUnavailableException( "Cannot get accounts from token cache. Error: " + t.getMessage(), t)) .flatMap(set -> { IAccount requestedAccount; Map<String, IAccount> accounts = new HashMap<>(); if (set.isEmpty()) { return Mono.error(new CredentialUnavailableException("SharedTokenCacheCredential " + "authentication unavailable. No accounts were found in the cache.")); } for (IAccount cached : set) { if (username == null || username.equals(cached.username())) { if (!accounts.containsKey(cached.homeAccountId())) { accounts.put(cached.homeAccountId(), cached); } } } if (accounts.isEmpty()) { return Mono.error(new RuntimeException(String.format("SharedTokenCacheCredential " + "authentication unavailable. No account matching the specified username: %s was " + "found in the cache.", username))); } else if (accounts.size() > 1) { if (username == null) { return Mono.error(new RuntimeException("SharedTokenCacheCredential authentication " + "unavailable. Multiple accounts were found in the cache. Use username and " + "tenant id to disambiguate.")); } else { return Mono.error(new RuntimeException(String.format("SharedTokenCacheCredential " + "authentication unavailable. Multiple accounts matching the specified username: " + "%s were found in the cache.", username))); } } else { requestedAccount = accounts.values().iterator().next(); } return authenticateWithMsalAccount(request, requestedAccount); }); }