Skip to content

Commit ba3326d

Browse files
author
Ed Fricker
committed
Ed: some better refactoring for future proofing of other auth schemes
1 parent 7587c40 commit ba3326d

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

intercom-java/src/main/java/io/intercom/api/HttpClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ private static String clientAgentDetails() {
5959

6060
private final Map<String, String> headers;
6161

62-
private final String apiKey = Intercom.getApiKey();
63-
6462
private final HttpConnectorSupplier connection = Intercom.getHttpConnectorSupplier();
6563

6664
public HttpClient(URI uri) {
@@ -229,15 +227,24 @@ private HttpURLConnection prepareConnection(HttpURLConnection conn) {
229227
}
230228

231229
private Map<String, String> createAuthorizationHeaders() {
232-
if (Intercom.getAuthScheme().equals(Intercom.AUTH_BEARER)) {
233-
headers.put("Authorization", "Bearer " + apiKey);
234-
} else if (Intercom.getAuthScheme().equals(Intercom.AUTH_BASIC)) {
235-
final String authString = Intercom.getAppID() + ":" + Intercom.getApiKey();
236-
headers.put("Authorization", "Basic " + Base64.encodeBase64String(authString.getBytes()));
230+
switch (Intercom.getAuthScheme()) {
231+
case BEARER:
232+
headers.put("Authorization", "Bearer " + Intercom.getApiKey());
233+
break;
234+
case API_KEY:
235+
headers.put("Authorization", "Basic " + Base64.encodeBase64String(generateAuthString(Intercom.getAppID(),Intercom.getApiKey()).getBytes()));
236+
break;
237+
case PERSONAL_ACCESS_TOKEN:
238+
headers.put("Authorization", "Basic " + Base64.encodeBase64String(generateAuthString(Intercom.getPersonalAccessToken(),"").getBytes()));
239+
break;
237240
}
238241
return headers;
239242
}
240243

244+
private String generateAuthString(String username, String password) {
245+
return username + ":" + password;
246+
}
247+
241248
private Map<String, String> createHeaders() {
242249
headers.put("User-Agent", USER_AGENT);
243250
headers.put("X-Client-Platform-Details", CLIENT_AGENT_DETAILS);

intercom-java/src/main/java/io/intercom/api/Intercom.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ public class Intercom {
88

99
private static volatile URI apiBaseURI = API_BASE_URI;
1010

11-
static final String AUTH_BASIC = "Basic";
11+
private static volatile AuthenticationSchemes authenticationScheme = AuthenticationSchemes.API_KEY;
1212

13-
private static final String AUTH_SCHEME = AUTH_BASIC;
14-
15-
static final String AUTH_BEARER = "Bearer";
13+
enum AuthenticationSchemes {
14+
API_KEY,
15+
BEARER,
16+
PERSONAL_ACCESS_TOKEN
17+
}
1618

1719
private static final String VERSION = "2.1.0";
1820

1921
public static final String USER_AGENT = "intercom-java/" + Intercom.VERSION;
2022

2123
private static volatile String apiKey;
2224

25+
private static volatile String personalAccessToken;
26+
2327
private static volatile String appID;
2428

2529
private static volatile int connectionTimeout = 3 * 1000;
@@ -77,15 +81,16 @@ public static void setAppID(String appID) {
7781
}
7882

7983
public static void setPersonalAccessToken(String personalAccessToken) {
80-
Intercom.appID = personalAccessToken;
81-
Intercom.apiKey = "";
84+
authenticationScheme = AuthenticationSchemes.PERSONAL_ACCESS_TOKEN;
85+
Intercom.personalAccessToken = personalAccessToken;
8286
}
8387

8488
public static String getApiKey() {
8589
return Intercom.apiKey;
8690
}
8791

8892
public static void setApiKey(String apiKey) {
93+
authenticationScheme = AuthenticationSchemes.API_KEY;
8994
Intercom.apiKey = apiKey;
9095
}
9196

@@ -97,8 +102,13 @@ public static void setApiBaseURI(URI apiBaseURI) {
97102
Intercom.apiBaseURI = apiBaseURI;
98103
}
99104

100-
static String getAuthScheme() {
101-
return AUTH_SCHEME;
105+
static AuthenticationSchemes getAuthScheme() {
106+
return authenticationScheme;
102107
}
103108

109+
public static String getPersonalAccessToken() {
110+
return personalAccessToken;
111+
}
112+
113+
104114
}

0 commit comments

Comments
 (0)