Skip to content

Commit 40e78ed

Browse files
authored
Add support for provider detection (#100)
# Description This PR adds support for the Nylas provider detection endpoint that allows users to pass in an email address and get the provider that matches it. This is especially helpful for users that implement native authentication. This change also enhances the `ProviderSettings` class by introducing a new static method that is able to match a provider setting by the provider string (`ProviderSettings.getProviderSettingsByProvider`). There's also an enum with all the providers supported for the native authentication flow (`NativeAuthentication.Provider`). # Usage ```java // Set up your client NylasClient client = new NylasClient(); NativeAuthentication nativeAuthentication = client.application("CLIENT_ID", "CLIENT_SECRET").nativeAuthentication(); // Detect the provider for an email address NativeAuthentication.DetectedProvider detectedProvider = nativeAuthentication.detectProvider("test@gmail.com"); // The following fields are made available detectedProvider.isDetected(); detectedProvider.getProviderName(); detectedProvider.getAuthName(); detectedProvider.getEmailAddress(); detectedProvider.isImap(); // You can use the result from above to get the correct provider settings ProviderSettings detectedSettings = ProviderSettings.getProviderSettingsByProvider(provider.getAuthName()); // The enum is provided for consistency and equality if(detectedProvider.getAuthName() == NativeAuthentication.Provider.GMAIL.getName()) { // do something here for a Gmail case } ``` # License <!-- Your PR comment must contain the following line for us to merge the PR. --> I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.
1 parent fb262e6 commit 40e78ed

7 files changed

Lines changed: 156 additions & 9 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This section contains changes that have been committed but not yet released.
66

77
### Added
88

9+
* Added support for provider detection
10+
* Added an enum for all known providers for native authentication
911
* Improved webhook notification support with the addition of more notification attributes and event metadata fields
1012

1113
### Changed

src/main/java/com/nylas/GoogleProviderSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class GoogleProviderSettings extends ProviderSettings {
2020

2121
protected GoogleProviderSettings() {
22-
super("gmail");
22+
super(NativeAuthentication.Provider.GMAIL.getName());
2323
}
2424

2525
public GoogleProviderSettings googleClientId(String googleClientId) {

src/main/java/com/nylas/ImapProviderSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class ImapProviderSettings extends ProviderSettings {
1111

1212
protected ImapProviderSettings() {
13-
super("imap");
13+
super(NativeAuthentication.Provider.IMAP.getName());
1414
}
1515

1616
public ImapProviderSettings imapHost(String imapHost) {

src/main/java/com/nylas/MicrosoftExchangeProviderSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class MicrosoftExchangeProviderSettings extends ProviderSettings {
1212

1313
protected MicrosoftExchangeProviderSettings() {
14-
super("exchange");
14+
super(NativeAuthentication.Provider.EXCHANGE.getName());
1515
}
1616

1717
public MicrosoftExchangeProviderSettings username(String username) {

src/main/java/com/nylas/MicrosoftOffice365ProviderSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class MicrosoftOffice365ProviderSettings extends ProviderSettings {
1212

1313
protected MicrosoftOffice365ProviderSettings() {
14-
super("office365");
14+
super(NativeAuthentication.Provider.OFFICE_365.getName());
1515
}
1616

1717
public MicrosoftOffice365ProviderSettings microsoftClientId(String microsoftClientId) {

src/main/java/com/nylas/NativeAuthentication.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ public class NativeAuthentication {
1717

1818
private final NylasApplication application;
1919

20+
/** Supported providers for Native Authentication */
21+
public enum Provider {
22+
GMAIL("gmail"),
23+
IMAP("imap"),
24+
OFFICE_365("office365"),
25+
EXCHANGE("exchange"),
26+
YAHOO("yahoo"),
27+
AOL("aol"),
28+
HOTMAIL("hotmail"),
29+
OUTLOOK("outlook"),
30+
ICLOUD("icloud"),
31+
32+
;
33+
34+
private final String name;
35+
36+
Provider(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public static Provider getProviderByName(String name) {
45+
for(Provider provider : Provider.values()) {
46+
if(provider.name.equals(name)) {
47+
return provider;
48+
}
49+
}
50+
51+
return null;
52+
}
53+
}
54+
2055
NativeAuthentication(NylasApplication application) {
2156
this.application = application;
2257
}
@@ -115,6 +150,11 @@ public String execute() throws IOException, RequestFailedException {
115150
}
116151
}
117152

153+
/**
154+
* Exchange the code from sending the native authentication for an access token
155+
* @param authorizationCode The code received from the native authentication
156+
* @return The access token details
157+
*/
118158
public AccessToken fetchToken(String authorizationCode) throws IOException, RequestFailedException {
119159
Map<String, Object> params = new HashMap<>();
120160
params.put("client_id", application.getClientId());
@@ -125,4 +165,72 @@ public AccessToken fetchToken(String authorizationCode) throws IOException, Requ
125165
return application.getClient().executePost(null, tokenUrl, params, AccessToken.class);
126166
}
127167

168+
/**
169+
* Detect which provider an email uses
170+
* @param emailAddress The email address to determine the provider of
171+
* @return The details of the provider detection
172+
*/
173+
public DetectedProvider detectProvider(String emailAddress) throws RequestFailedException, IOException {
174+
Map<String, Object> params = new HashMap<>();
175+
params.put("client_id", application.getClientId());
176+
params.put("client_secret", application.getClientSecret());
177+
params.put("email_address", emailAddress);
178+
179+
HttpUrl.Builder detectUrl = application.getClient().newUrlBuilder().addPathSegments("connect/detect-provider");
180+
return application.getClient().executePost(null, detectUrl, params, DetectedProvider.class);
181+
}
182+
183+
/**
184+
* Get settings of a provider detected from an email
185+
* @param emailAddress The email address to determine the provider of
186+
* @return The settings for the detected provider
187+
*/
188+
public ProviderSettings getDetectedProviderSettings(String emailAddress) throws RequestFailedException, IOException {
189+
DetectedProvider detectedProvider = detectProvider(emailAddress);
190+
return ProviderSettings.getProviderSettingsByProvider(detectedProvider.getAuthName());
191+
}
192+
193+
public static class DetectedProvider {
194+
private String auth_name;
195+
private String email_address;
196+
private String provider_name;
197+
private Boolean detected;
198+
private Boolean is_imap;
199+
200+
/**
201+
* The provider's auth name to be used in Native Authentication
202+
*/
203+
public String getAuthName() {
204+
return auth_name;
205+
}
206+
207+
/**
208+
* The email address being checked
209+
*/
210+
public String getEmailAddress() {
211+
return email_address;
212+
}
213+
214+
/**
215+
* The name of the detected provider
216+
*/
217+
public String getProviderName() {
218+
return provider_name;
219+
}
220+
221+
/**
222+
* If this account's mail server settings and provider were auto-detected
223+
*/
224+
public Boolean isDetected() {
225+
return detected;
226+
}
227+
228+
/**
229+
* If the account should be authenticated with IMAP.
230+
* If {@link #isDetected()} returns false, ignore this value.
231+
*/
232+
public Boolean isImap() {
233+
return is_imap;
234+
}
235+
}
128236
}

src/main/java/com/nylas/ProviderSettings.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nylas;
22

33
import static com.nylas.Validations.assertState;
4+
import com.nylas.NativeAuthentication.Provider;
45

56
import java.util.HashMap;
67
import java.util.Map;
@@ -19,6 +20,42 @@ public class ProviderSettings {
1920

2021
private final String providerName;
2122
private final Map<String, Object> settings = new HashMap<>();
23+
24+
/**
25+
* Returns the provider settings for a provider. If the provider is not found, it will return a
26+
* {@link KnownImapProviderSettings} with the provider set to the string passed in.
27+
* @param providerName The provider for the native authentication
28+
* @return The settings for the provider
29+
*/
30+
public static ProviderSettings getProviderSettingsByProvider(String providerName) {
31+
Provider provider = Provider.getProviderByName(providerName);
32+
if(provider == null) {
33+
return knownImap(providerName);
34+
}
35+
36+
switch (provider) {
37+
case GMAIL:
38+
return ProviderSettings.google();
39+
case IMAP:
40+
return ProviderSettings.imap();
41+
case OFFICE_365:
42+
return ProviderSettings.office365();
43+
case EXCHANGE:
44+
return ProviderSettings.exchange();
45+
case YAHOO:
46+
return ProviderSettings.yahoo();
47+
case AOL:
48+
return ProviderSettings.aol();
49+
case HOTMAIL:
50+
return ProviderSettings.hotmail();
51+
case OUTLOOK:
52+
return ProviderSettings.outlook();
53+
case ICLOUD:
54+
return ProviderSettings.icloud();
55+
default:
56+
return knownImap(provider.getName());
57+
}
58+
}
2259

2360
public static GoogleProviderSettings google() {
2461
return new GoogleProviderSettings();
@@ -46,7 +83,7 @@ public static KnownImapProviderSettings knownImap(String provider) {
4683
* NOTE - Many Yahoo accounts currently require the user to generate an App Password for this to work.
4784
*/
4885
public static KnownImapProviderSettings yahoo() {
49-
return new KnownImapProviderSettings("yahoo");
86+
return new KnownImapProviderSettings(Provider.YAHOO.getName());
5087
}
5188

5289
/**
@@ -55,19 +92,19 @@ public static KnownImapProviderSettings yahoo() {
5592
* NOTE - Many AOL accounts currently require the user to generate an App Password for this to work.
5693
*/
5794
public static KnownImapProviderSettings aol() {
58-
return new KnownImapProviderSettings("aol");
95+
return new KnownImapProviderSettings(Provider.AOL.getName());
5996
}
6097

6198
public static KnownImapProviderSettings hotmail() {
62-
return new KnownImapProviderSettings("hotmail");
99+
return new KnownImapProviderSettings(Provider.HOTMAIL.getName());
63100
}
64101

65102
public static KnownImapProviderSettings outlook() {
66-
return new KnownImapProviderSettings("outlook");
103+
return new KnownImapProviderSettings(Provider.OUTLOOK.getName());
67104
}
68105

69106
public static KnownImapProviderSettings icloud() {
70-
return new KnownImapProviderSettings("icloud");
107+
return new KnownImapProviderSettings(Provider.ICLOUD.getName());
71108
}
72109

73110
public ProviderSettings(String providerName) {

0 commit comments

Comments
 (0)