Skip to content

Commit

Permalink
Also parse provider within domain
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Aug 13, 2024
1 parent bf21e67 commit 4a6984c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import myconext.exceptions.DisposableEmailProviderException;
import myconext.model.IdentityProvider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,6 +15,7 @@
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -55,14 +57,28 @@ public void resolveIDisposableEmailProviders() {
objectMapper.readValue(new ClassPathResource(localLocation).getInputStream(), mapTypeReference) :
objectMapper.readValue(new URL(remoteLocation), mapTypeReference);

disposableEmailProviders = ((Map<String, Object>) emailProviders.get("domains")).keySet();
Map<String, Object> domains = (Map<String, Object>) emailProviders.get("domains");
disposableEmailProviders = parseDisposableEmailProviders(domains);
LOG.info(String.format("Resolved %s disposable email providers %s in %s ms",
disposableEmailProviders.size(), this.testEnvironment ? localLocation : remoteLocation, System.currentTimeMillis() - start));
} catch (Exception e) {
LOG.error("Error in resolveIDisposableEmailProviders", e);
}
}

@SuppressWarnings("unchecked")
private HashSet<String> parseDisposableEmailProviders(Map<String, Object> domains) {
Set<Map.Entry<String, Object>> entries = domains.entrySet();
HashSet<String> newDisposableEmailProviders = new HashSet<>();
entries.forEach(entry -> {
String domain = entry.getKey();
Map<String, Object> provider = (Map<String, Object>) entry.getValue();
newDisposableEmailProviders.add(domain.toLowerCase());
newDisposableEmailProviders.add(((String) provider.get("provider")).toLowerCase());
});
return newDisposableEmailProviders;
}

public void verifyDisposableEmailProviders(String email) {
if (!denyDisposableEmailProviders) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void isDisposableEmailProviders() {
Environment environment = new MockEnvironment();
DisposableEmailProviders subject = new DisposableEmailProviders(objectMapper, environment, true);
assertThrows(DisposableEmailProviderException.class, () -> subject.verifyDisposableEmailProviders("[email protected]"));
assertThrows(DisposableEmailProviderException.class, () -> subject.verifyDisposableEmailProviders("[email protected]"));
subject.verifyDisposableEmailProviders("[email protected]");

DisposableEmailProviders subjectFeatureOff = new DisposableEmailProviders(objectMapper, environment, false);
Expand All @@ -29,5 +30,6 @@ void isDisposableEmailProvidersLocalEnvironment() {
environment.addActiveProfile("test");
DisposableEmailProviders subject = new DisposableEmailProviders(objectMapper, environment, true);
assertThrows(DisposableEmailProviderException.class, () -> subject.verifyDisposableEmailProviders("[email protected]"));
assertThrows(DisposableEmailProviderException.class, () -> subject.verifyDisposableEmailProviders("[email protected]"));
}
}

0 comments on commit 4a6984c

Please sign in to comment.