Skip to content

Commit d9d3d66

Browse files
authored
Merge pull request #2735 from wmathurin/dev
Revoke refresh token with a HTTP POST instead of a HTTP GET call
2 parents 45a7fd8 + f3bf740 commit d9d3d66

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

  • libs
    • SalesforceSDK/src/com/salesforce/androidsdk/auth
    • test/SalesforceSDKTest/src/com/salesforce/androidsdk/auth

libs/SalesforceSDK/src/com/salesforce/androidsdk/auth/OAuth2.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public class OAuth2 {
143143
private static final String ASSERTION = "assertion";
144144
private static final String JWT_BEARER = "urn:ietf:params:oauth:grant-type:jwt-bearer";
145145
protected static final String OAUTH_AUTH_PATH = "/services/oauth2/authorize";
146+
private static final String REVOKE_REASON = "revoke_reason";
146147

147148
/** Endpoint path for Salesforce Identity API initialize headless, password-less login flow */
148149
protected static String OAUTH_ENDPOINT_HEADLESS_INIT_PASSWORDLESS_LOGIN = "/services/auth/headless/init/passwordless/login";
@@ -155,7 +156,7 @@ public class OAuth2 {
155156

156157
private static final String OAUTH_DISPLAY_PARAM = "?display=";
157158
protected static final String OAUTH_TOKEN_PATH = "/services/oauth2/token";
158-
private static final String OAUTH_REVOKE_PATH = "/services/oauth2/revoke?token=%s&revoke_reason=%s";
159+
private static final String OAUTH_REVOKE_PATH = "/services/oauth2/revoke";
159160
private static final String LIGHTNING_DOMAIN = "lightning_domain";
160161
private static final String LIGHTNING_SID = "lightning_sid";
161162
private static final String VF_DOMAIN = "visualforce_domain";
@@ -470,15 +471,23 @@ public static TokenEndpointResponse refreshAuthToken(HttpAccess httpAccessor, UR
470471
* @param reason The reason the refresh token is being revoked.
471472
*/
472473
public static void revokeRefreshToken(HttpAccess httpAccessor, URI loginServer, String refreshToken, LogoutReason reason) {
473-
final String requestPath = String.format(OAUTH_REVOKE_PATH, refreshToken, reason.toString());
474-
final Request request = new Request.Builder().url(loginServer.toString() + requestPath).get().build();
474+
final Request request = buildRevokeRefreshTokenRequest(loginServer, refreshToken, reason);
475475
try {
476476
httpAccessor.getOkHttpClient().newCall(request).execute();
477477
} catch (IOException e) {
478478
SalesforceSDKLogger.w(TAG, "Exception thrown while revoking refresh token", e);
479479
}
480480
}
481481

482+
protected static Request buildRevokeRefreshTokenRequest(URI loginServer, String refreshToken, LogoutReason reason) {
483+
final String requestUrl = loginServer.toString() + OAUTH_REVOKE_PATH;
484+
final FormBody body = new FormBody.Builder()
485+
.add(TOKEN, refreshToken)
486+
.add(REVOKE_REASON, reason.toString())
487+
.build();
488+
return new Request.Builder().url(requestUrl).post(body).build();
489+
}
490+
482491
/**
483492
* Swaps a JWT for regular OAuth tokens. This is typically the first step after
484493
* receiving a JWT from a link. In addition, this will also call the identity

libs/test/SalesforceSDKTest/src/com/salesforce/androidsdk/auth/OAuth2Test.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import okhttp3.HttpUrl;
6262
import okhttp3.Request;
6363
import okhttp3.Response;
64+
import okio.Buffer;
6465

6566
/**
6667
* Tests for OAuth2.
@@ -482,4 +483,36 @@ public void testGetOpenIDToken() {
482483
TestCredentials.CLIENT_ID, TestCredentials.REFRESH_TOKEN);
483484
Assert.assertNotNull("OpenID token should not be null", openIdToken);
484485
}
486+
487+
/**
488+
* Testing buildRevokeRefreshTokenRequest.
489+
*/
490+
@Test
491+
public void testBuildRevokeRefreshTokenRequest() throws Exception {
492+
String refreshToken = "test_refresh_token_123";
493+
OAuth2.LogoutReason reason = OAuth2.LogoutReason.USER_LOGOUT;
494+
URI loginServer = new URI(TestCredentials.LOGIN_URL);
495+
496+
Request request = OAuth2.buildRevokeRefreshTokenRequest(loginServer, refreshToken, reason);
497+
498+
// Verify URL
499+
String expectedUrl = TestCredentials.LOGIN_URL + "/services/oauth2/revoke";
500+
Assert.assertEquals(expectedUrl, request.url().toString());
501+
502+
// Verify method
503+
Assert.assertEquals("POST", request.method());
504+
505+
// Verify body contains expected parameters
506+
String body = getRequestBodyAsString(request);
507+
Assert.assertTrue(body.contains("token=" + refreshToken));
508+
Assert.assertTrue(body.contains("revoke_reason=" + reason.toString()));
509+
}
510+
511+
private String getRequestBodyAsString(Request request) throws IOException {
512+
okio.Buffer buffer = new okio.Buffer();
513+
if (request.body() != null) {
514+
request.body().writeTo(buffer);
515+
}
516+
return buffer.readUtf8();
517+
}
485518
}

0 commit comments

Comments
 (0)