Skip to content

Commit 7264901

Browse files
committed
feat: add no-arg RevokeToken and GetAccessToken
Adds a no-arg RevokeToken() overload that revokes the currently authenticated session without requiring the caller to manually pass the access token. Also exposes GetAccessToken() on both InfisicalSdk and ApiClient so callers can retrieve the token if needed. Previously, all auth login methods (LdapAuthLogin, UniversalAuthLogin, etc.) stored the token internally with no way to retrieve it, making it impossible to call RevokeToken(token) after authentication.
1 parent c000d25 commit 7264901

5 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/main/java/com/infisical/sdk/InfisicalSdk.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ private void onAuthenticate(String accessToken) {
3232
this.authClient = new AuthClient(apiClient, this::onAuthenticate);
3333
}
3434

35+
public String GetAccessToken() {
36+
return this.apiClient.GetAccessToken();
37+
}
38+
3539
public AuthClient Auth() {
3640
return this.authClient;
3741
}

src/main/java/com/infisical/sdk/api/ApiClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public String GetBaseUrl() {
4848
return this.baseUrl;
4949
}
5050

51+
public String GetAccessToken() {
52+
return this.accessToken;
53+
}
54+
5155
@SuppressWarnings(value = "lombok")
5256
public OkHttpClient getClient() {
5357
return this.client;

src/main/java/com/infisical/sdk/resources/AuthClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public void SetAccessToken(String accessToken) {
6060
this.onAuthenticate.accept(accessToken);
6161
}
6262

63+
public void RevokeToken() throws InfisicalException {
64+
RevokeToken(this.apiClient.GetAccessToken());
65+
}
66+
6367
public void RevokeToken(String accessToken) throws InfisicalException {
6468
RevokeTokenInput input = RevokeTokenInput.builder().accessToken(accessToken).build();
6569

src/test/java/com/infisical/sdk/InfisicalSdkTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.infisical.sdk;
22

33
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.List;
67

@@ -16,6 +17,23 @@
1617
public class InfisicalSdkTest {
1718
private static final Logger logger = LoggerFactory.getLogger(InfisicalSdkTest.class);
1819

20+
@Test
21+
public void TestRevokeToken() {
22+
EnvironmentVariables envVars = new EnvironmentVariables();
23+
24+
InfisicalSdk sdk = new InfisicalSdk(new SdkConfig.Builder().withSiteUrl(envVars.getSiteUrl()).build());
25+
26+
assertDoesNotThrow(() -> {
27+
sdk.Auth().UniversalAuthLogin(envVars.getMachineIdentityClientId(), envVars.getMachineIdentityClientSecret());
28+
});
29+
30+
String token = sdk.GetAccessToken();
31+
assertDoesNotThrow(() -> sdk.Auth().RevokeToken());
32+
33+
// Verify the token is actually revoked — revoking it again should fail
34+
assertThrows(InfisicalException.class, () -> sdk.Auth().RevokeToken(token));
35+
}
36+
1937
@Test
2038
public void TestListSecrets() {
2139
EnvironmentVariables envVars = new EnvironmentVariables();

src/test/java/com/infisical/sdk/resources/AuthClientTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ public class AuthClientTest {
2121
@Mock
2222
private ApiClient apiClient;
2323

24+
@Test
25+
public void RevokeToken_noArg_throwsWhenNoTokenIsSet() {
26+
when(apiClient.GetAccessToken()).thenReturn(null);
27+
AuthClient authClient = new AuthClient(apiClient, token -> {});
28+
29+
InfisicalException ex = assertThrows(InfisicalException.class, () -> authClient.RevokeToken());
30+
assertEquals("Access token is required", ex.getMessage());
31+
}
32+
33+
@Test
34+
public void RevokeToken_noArg_callsPostWithStoredToken() throws InfisicalException {
35+
when(apiClient.GetBaseUrl()).thenReturn("http://localhost");
36+
when(apiClient.GetAccessToken()).thenReturn("stored-token-456");
37+
AuthClient authClient = new AuthClient(apiClient, token -> {});
38+
39+
authClient.RevokeToken();
40+
41+
verify(apiClient).post(
42+
eq("http://localhost/api/v1/auth/token/revoke"),
43+
any(RevokeTokenInput.class),
44+
eq(Void.class));
45+
}
46+
2447
@Test
2548
public void RevokeToken_throwsWhenAccessTokenIsNull() {
2649
AuthClient authClient = new AuthClient(apiClient, token -> {});

0 commit comments

Comments
 (0)