Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/infisical/sdk/InfisicalSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void onAuthenticate(String accessToken) {
this.secretsClient = new SecretsClient(apiClient);
this.foldersClient = new FoldersClient(apiClient);
this.projectsClient = new ProjectsClient(apiClient);
this.authClient = new AuthClient(apiClient, this::onAuthenticate);
this.authClient = new AuthClient(apiClient, this::onAuthenticate, accessToken);
}

public AuthClient Auth() {
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/infisical/sdk/resources/AuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@
public class AuthClient {
private final ApiClient apiClient;
private final Consumer<String> onAuthenticate;
private String currentAccessToken;

public AuthClient(ApiClient apiClient, Consumer<String> onAuthenticate) {
this.apiClient = apiClient;
this.onAuthenticate = onAuthenticate;
}

public AuthClient(ApiClient apiClient, Consumer<String> onAuthenticate, String initialToken) {
this.apiClient = apiClient;
this.onAuthenticate = onAuthenticate;
this.currentAccessToken = initialToken;
}

public void UniversalAuthLogin(String clientId, String clientSecret) throws InfisicalException {
UniversalAuthLoginInput params = UniversalAuthLoginInput.builder().clientId(clientId).clientSecret(clientSecret)
.build();

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

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

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

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

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

public void SetAccessToken(String accessToken) {
this.currentAccessToken = accessToken;
this.onAuthenticate.accept(accessToken);
}

public void RevokeToken() throws InfisicalException {
RevokeToken(this.currentAccessToken);
}

public void RevokeToken(String accessToken) throws InfisicalException {
RevokeTokenInput input = RevokeTokenInput.builder().accessToken(accessToken).build();

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/infisical/sdk/InfisicalSdkTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.infisical.sdk;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.List;

Expand All @@ -16,6 +17,22 @@
public class InfisicalSdkTest {
private static final Logger logger = LoggerFactory.getLogger(InfisicalSdkTest.class);

@Test
public void TestRevokeToken() {
EnvironmentVariables envVars = new EnvironmentVariables();

InfisicalSdk sdk = new InfisicalSdk(new SdkConfig.Builder().withSiteUrl(envVars.getSiteUrl()).build());

assertDoesNotThrow(() -> {
sdk.Auth().UniversalAuthLogin(envVars.getMachineIdentityClientId(), envVars.getMachineIdentityClientSecret());
});

assertDoesNotThrow(() -> sdk.Auth().RevokeToken());

// Verify the token is actually revoked — revoking it again should fail
assertThrows(InfisicalException.class, () -> sdk.Auth().RevokeToken());
}

@Test
public void TestListSecrets() {
EnvironmentVariables envVars = new EnvironmentVariables();
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/infisical/sdk/resources/AuthClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ public class AuthClientTest {
@Mock
private ApiClient apiClient;

@Test
public void RevokeToken_noArg_throwsWhenNoTokenIsSet() {
AuthClient authClient = new AuthClient(apiClient, token -> {});

InfisicalException ex = assertThrows(InfisicalException.class, () -> authClient.RevokeToken());
assertEquals("Access token is required", ex.getMessage());
}

@Test
public void RevokeToken_noArg_callsPostWithStoredToken() throws InfisicalException {
when(apiClient.GetBaseUrl()).thenReturn("http://localhost");
AuthClient authClient = new AuthClient(apiClient, token -> {});
authClient.SetAccessToken("stored-token-456");

authClient.RevokeToken();

verify(apiClient).post(
eq("http://localhost/api/v1/auth/token/revoke"),
any(RevokeTokenInput.class),
eq(Void.class));
}

@Test
public void RevokeToken_throwsWhenAccessTokenIsNull() {
AuthClient authClient = new AuthClient(apiClient, token -> {});
Expand Down