Skip to content

Commit 318c218

Browse files
authored
Merge pull request #121 from intercom/ed/support-PAT
Support PAT
2 parents f50c869 + 75e3d7c commit 318c218

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,22 @@ Resources this API supports:
9595

9696
## Authorization
9797

98-
You can set the app's id and api key via the `Intercom` object -
98+
99+
If you're using a Personal Access Token (PAT) you can just use the following helper method, which will automatically set up the authentication scheme for you -
100+
101+
```java
102+
Intercom.setToken("da39a3ee5e6b4b0d3255bfef95601890afd80709");
103+
```
104+
105+
If you're still using API Keys (now deprecated), you can set up your App ID and API Key using the following methods -
99106

100107
```java
101108
Intercom.setAppID("pi3243fa");
102109
Intercom.setApiKey("da39a3ee5e6b4b0d3255bfef95601890afd80709");
103110
```
104111

105112

113+
106114
## Usage
107115

108116
### Users

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

Lines changed: 11 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,21 @@ 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.getAuthKeyType()) {
231+
case API_KEY:
232+
headers.put("Authorization", "Basic " + generateAuthString(Intercom.getAppID(),Intercom.getApiKey()));
233+
break;
234+
case TOKEN:
235+
headers.put("Authorization", "Basic " + generateAuthString(Intercom.getToken(),""));
236+
break;
237237
}
238238
return headers;
239239
}
240240

241+
private String generateAuthString(String username, String password) {
242+
return Base64.encodeBase64String((username + ":" + password).getBytes());
243+
}
244+
241245
private Map<String, String> createHeaders() {
242246
headers.put("User-Agent", USER_AGENT);
243247
headers.put("X-Client-Platform-Details", CLIENT_AGENT_DETAILS);

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ 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 AuthKeyType authKeyType = AuthKeyType.API_KEY;
1212

13-
private static final String AUTH_SCHEME = AUTH_BASIC;
14-
15-
static final String AUTH_BEARER = "Bearer";
13+
enum AuthKeyType {
14+
API_KEY,
15+
TOKEN
16+
}
1617

1718
private static final String VERSION = "2.1.0";
1819

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

2122
private static volatile String apiKey;
2223

24+
private static volatile String token;
25+
2326
private static volatile String appID;
2427

2528
private static volatile int connectionTimeout = 3 * 1000;
@@ -76,11 +79,17 @@ public static void setAppID(String appID) {
7679
Intercom.appID = appID;
7780
}
7881

82+
public static void setToken(String token) {
83+
authKeyType = AuthKeyType.TOKEN;
84+
Intercom.token = token;
85+
}
86+
7987
public static String getApiKey() {
8088
return Intercom.apiKey;
8189
}
8290

8391
public static void setApiKey(String apiKey) {
92+
authKeyType = AuthKeyType.API_KEY;
8493
Intercom.apiKey = apiKey;
8594
}
8695

@@ -92,8 +101,13 @@ public static void setApiBaseURI(URI apiBaseURI) {
92101
Intercom.apiBaseURI = apiBaseURI;
93102
}
94103

95-
static String getAuthScheme() {
96-
return AUTH_SCHEME;
104+
static AuthKeyType getAuthKeyType() {
105+
return authKeyType;
106+
}
107+
108+
public static String getToken() {
109+
return token;
97110
}
98111

112+
99113
}

0 commit comments

Comments
 (0)