Skip to content

Commit 9c06901

Browse files
committed
Fix token = null racing case issue
1 parent a10d6b6 commit 9c06901

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/main/java/com/ringcentral/RestClient.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ private String basicKey() {
7474
return new String(Base64.getEncoder().encode(MessageFormat.format("{0}:{1}", clientId, clientSecret).getBytes()));
7575
}
7676

77-
private String authorizationHeader() {
78-
if (token != null) {
79-
return MessageFormat.format("Bearer {0}", token.access_token);
77+
private String authorizationHeader(String endpoint) {
78+
if(endpoint.equals("/restapi/oauth/token") || endpoint.equals("/restapi/oauth/revoke")) {
79+
return MessageFormat.format("Basic {0}", basicKey());
8080
}
81-
return MessageFormat.format("Basic {0}", basicKey());
81+
return MessageFormat.format("Bearer {0}", token.access_token);
8282
}
8383

8484
public void revoke() throws IOException, RestException {
8585
if (token == null) {
8686
return;
8787
}
8888
RevokeTokenRequest revokeTokenRequest = new RevokeTokenRequest().token(token.access_token);
89-
token = null;
9089
this.restapi(null).oauth().revoke().post(revokeTokenRequest);
90+
token = null;
9191
}
9292

9393
public TokenInfo authorize(String username, String extension, String password) throws IOException, RestException {
@@ -108,7 +108,6 @@ public TokenInfo authorize(String authCode, String redirectUri) throws IOExcepti
108108
}
109109

110110
public TokenInfo authorize(GetTokenRequest getTokenRequest) throws IOException, RestException {
111-
token = null;
112111
token = this.restapi(null).oauth().token().post(getTokenRequest);
113112
return token;
114113
}
@@ -299,7 +298,7 @@ public Response requestRaw(HttpMethod httpMethod, String endpoint, Object queryP
299298
}
300299

301300
String userAgentHeader = String.format("RC-JAVA-SDK Java %s %s", System.getProperty("java.version"), System.getProperty("os.name"));
302-
Request request = builder.addHeader("Authorization", authorizationHeader())
301+
Request request = builder.addHeader("Authorization", authorizationHeader(endpoint))
303302
.addHeader("X-User-Agent", userAgentHeader)
304303
.build();
305304

0 commit comments

Comments
 (0)