Skip to content

Commit 547cdac

Browse files
authored
Merge pull request #19 from Infisical/fix/revoke-token-self
fix: add no-arg RevokeToken() and expose GetAccessToken()
2 parents 284c040 + 61051cf commit 547cdac

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private void onAuthenticate(String accessToken) {
2929
this.secretsClient = new SecretsClient(apiClient);
3030
this.foldersClient = new FoldersClient(apiClient);
3131
this.projectsClient = new ProjectsClient(apiClient);
32-
this.authClient = new AuthClient(apiClient, this::onAuthenticate);
32+
this.authClient = new AuthClient(apiClient, this::onAuthenticate, accessToken);
3333
}
3434

3535
public AuthClient Auth() {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@
1313
public class AuthClient {
1414
private final ApiClient apiClient;
1515
private final Consumer<String> onAuthenticate;
16+
private String currentAccessToken;
1617

1718
public AuthClient(ApiClient apiClient, Consumer<String> onAuthenticate) {
1819
this.apiClient = apiClient;
1920
this.onAuthenticate = onAuthenticate;
2021
}
2122

23+
public AuthClient(ApiClient apiClient, Consumer<String> onAuthenticate, String initialToken) {
24+
this.apiClient = apiClient;
25+
this.onAuthenticate = onAuthenticate;
26+
this.currentAccessToken = initialToken;
27+
}
28+
2229
public void UniversalAuthLogin(String clientId, String clientSecret) throws InfisicalException {
2330
UniversalAuthLoginInput params = UniversalAuthLoginInput.builder().clientId(clientId).clientSecret(clientSecret)
2431
.build();
2532

2633
String url = String.format("%s%s", this.apiClient.GetBaseUrl(), "/api/v1/auth/universal-auth/login");
2734
MachineIdentityCredential credential = this.apiClient.post(url, params, MachineIdentityCredential.class);
28-
this.onAuthenticate.accept(credential.getAccessToken());
35+
this.currentAccessToken = credential.getAccessToken();
36+
this.onAuthenticate.accept(this.currentAccessToken);
2937
}
3038

3139
public void LdapAuthLogin(LdapAuthLoginInput input) throws InfisicalException {
@@ -37,7 +45,8 @@ public void LdapAuthLogin(LdapAuthLoginInput input) throws InfisicalException {
3745

3846
String url = String.format("%s%s", this.apiClient.GetBaseUrl(), "/api/v1/auth/ldap-auth/login");
3947
MachineIdentityCredential credential = this.apiClient.post(url, input, MachineIdentityCredential.class);
40-
this.onAuthenticate.accept(credential.getAccessToken());
48+
this.currentAccessToken = credential.getAccessToken();
49+
this.onAuthenticate.accept(this.currentAccessToken);
4150
}
4251

4352
public void AwsAuthLogin(String identityId) throws InfisicalException {
@@ -53,13 +62,19 @@ public void AwsAuthLogin(AwsAuthLoginInput input) throws InfisicalException {
5362

5463
String url = String.format("%s%s", this.apiClient.GetBaseUrl(), "/api/v1/auth/aws-auth/login");
5564
MachineIdentityCredential credential = this.apiClient.post(url, input, MachineIdentityCredential.class);
56-
this.onAuthenticate.accept(credential.getAccessToken());
65+
this.currentAccessToken = credential.getAccessToken();
66+
this.onAuthenticate.accept(this.currentAccessToken);
5767
}
5868

5969
public void SetAccessToken(String accessToken) {
70+
this.currentAccessToken = accessToken;
6071
this.onAuthenticate.accept(accessToken);
6172
}
6273

74+
public void RevokeToken() throws InfisicalException {
75+
RevokeToken(this.currentAccessToken);
76+
}
77+
6378
public void RevokeToken(String accessToken) throws InfisicalException {
6479
RevokeTokenInput input = RevokeTokenInput.builder().accessToken(accessToken).build();
6580

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

Lines changed: 17 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,22 @@
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+
assertDoesNotThrow(() -> sdk.Auth().RevokeToken());
31+
32+
// Verify the token is actually revoked — revoking it again should fail
33+
assertThrows(InfisicalException.class, () -> sdk.Auth().RevokeToken());
34+
}
35+
1936
@Test
2037
public void TestListSecrets() {
2138
EnvironmentVariables envVars = new EnvironmentVariables();

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

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

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

0 commit comments

Comments
 (0)