Skip to content

Commit

Permalink
Implement personal access tokens for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
mmodzelewski committed Oct 12, 2024
1 parent 836f5ed commit 6288c4e
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/rs/iggy/clients/blocking/IggyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface IggyClient {

MessagesClient messages();

PersonalAccessTokensClient personalAccessTokens();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package rs.iggy.clients.blocking;

import rs.iggy.personalaccesstoken.PersonalAccessTokenInfo;
import rs.iggy.personalaccesstoken.RawPersonalAccessToken;
import rs.iggy.user.IdentityInfo;
import java.math.BigInteger;
import java.util.List;

public interface PersonalAccessTokensClient {

RawPersonalAccessToken createPersonalAccessToken(String name, BigInteger expiry);

List<PersonalAccessTokenInfo> getPersonalAccessTokens();

void deletePersonalAccessToken(String name);

IdentityInfo loginWithPersonalAccessToken(String token);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class IggyHttpClient implements IggyClient {
private final ConsumerGroupsHttpClient consumerGroupsClient;
private final ConsumerOffsetsHttpClient consumerOffsetsClient;
private final MessagesHttpClient messagesClient;
private final PersonalAccessTokensHttpClient personalAccessTokensHttpClient;

public IggyHttpClient(String url) {
HttpClient httpClient = new HttpClient(url);
Expand All @@ -23,6 +24,7 @@ public IggyHttpClient(String url) {
consumerGroupsClient = new ConsumerGroupsHttpClient(httpClient);
consumerOffsetsClient = new ConsumerOffsetsHttpClient(httpClient);
messagesClient = new MessagesHttpClient(httpClient);
personalAccessTokensHttpClient = new PersonalAccessTokensHttpClient(httpClient);
}

@Override
Expand Down Expand Up @@ -65,4 +67,9 @@ public MessagesClient messages() {
return messagesClient;
}

@Override
public PersonalAccessTokensClient personalAccessTokens() {
return personalAccessTokensHttpClient;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package rs.iggy.clients.blocking.http;

import com.fasterxml.jackson.core.type.TypeReference;
import rs.iggy.clients.blocking.PersonalAccessTokensClient;
import rs.iggy.personalaccesstoken.PersonalAccessTokenInfo;
import rs.iggy.personalaccesstoken.RawPersonalAccessToken;
import rs.iggy.user.IdentityInfo;
import rs.iggy.user.TokenInfo;
import java.math.BigInteger;
import java.util.List;

class PersonalAccessTokensHttpClient implements PersonalAccessTokensClient {

private static final String PERSONAL_ACCESS_TOKENS = "/personal-access-tokens";
private final HttpClient httpClient;

public PersonalAccessTokensHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}

@Override
public RawPersonalAccessToken createPersonalAccessToken(String name, BigInteger expiry) {
var request = httpClient.preparePostRequest(PERSONAL_ACCESS_TOKENS,
new CreatePersonalAccessToken(name, expiry));
return httpClient.execute(request, new TypeReference<>() {
});
}

@Override
public List<PersonalAccessTokenInfo> getPersonalAccessTokens() {
var request = httpClient.prepareGetRequest(PERSONAL_ACCESS_TOKENS);
return httpClient.execute(request, new TypeReference<>() {
});
}

@Override
public void deletePersonalAccessToken(String name) {
var request = httpClient.prepareDeleteRequest(PERSONAL_ACCESS_TOKENS + "/" + name);
httpClient.execute(request);
}

@Override
public IdentityInfo loginWithPersonalAccessToken(String token) {
var request = httpClient.preparePostRequest(PERSONAL_ACCESS_TOKENS + "/login",
new LoginWithPersonalAccessToken(token));
var response = httpClient.execute(request, IdentityInfo.class);
httpClient.setToken(response.accessToken().map(TokenInfo::token));
return response;
}

record CreatePersonalAccessToken(String name, BigInteger expiry) {
}

record LoginWithPersonalAccessToken(String token) {
}

}
6 changes: 6 additions & 0 deletions src/main/java/rs/iggy/clients/blocking/tcp/IggyTcpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ public ConsumerOffsetsClient consumerOffsets() {
public MessagesClient messages() {
return messagesClient;
}

@Override
public PersonalAccessTokensClient personalAccessTokens() {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rs.iggy.personalaccesstoken;

import java.math.BigInteger;
import java.util.Optional;

public record PersonalAccessTokenInfo(String name, Optional<BigInteger> expiryAt) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package rs.iggy.personalaccesstoken;

public record RawPersonalAccessToken(String token) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package rs.iggy.clients.blocking;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import rs.iggy.user.IdentityInfo;
import rs.iggy.user.UserInfoDetails;
import java.math.BigInteger;
import static org.assertj.core.api.Assertions.assertThat;

public abstract class PersonalAccessTokensBaseTest extends IntegrationTest {

protected PersonalAccessTokensClient personalAccessTokensClient;

@BeforeEach
void beforeEachBase() {
personalAccessTokensClient = client.personalAccessTokens();

login();
}

@Test
void shouldManagePersonalAccessTokens() {
// when
var createdToken = personalAccessTokensClient.createPersonalAccessToken("new-token",
BigInteger.valueOf(50_000));

// then
assertThat(createdToken).isNotNull();

// when
var tokens = personalAccessTokensClient.getPersonalAccessTokens();

// then
assertThat(tokens).isNotNull();
assertThat(tokens).hasSize(1);

// when
personalAccessTokensClient.deletePersonalAccessToken("new-token");
tokens = personalAccessTokensClient.getPersonalAccessTokens();

//
assertThat(tokens).hasSize(0);
}

@Test
void shouldCreateAndLogInWithPersonalAccessToken() {
// given
var createdToken = personalAccessTokensClient.createPersonalAccessToken("new-token",
BigInteger.valueOf(50_000));
client.users().logout();

// when
IdentityInfo identityInfo = personalAccessTokensClient.loginWithPersonalAccessToken(createdToken.token());

// then
assertThat(identityInfo).isNotNull();

// when
UserInfoDetails user = client.users().getUser(1L);

// then
assertThat(user).isNotNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package rs.iggy.clients.blocking.http;

import rs.iggy.clients.blocking.IggyClient;
import rs.iggy.clients.blocking.PersonalAccessTokensBaseTest;

class PersonalAccessTokensHttpClientTest extends PersonalAccessTokensBaseTest {

@Override
protected IggyClient getClient() {
return HttpClientFactory.create(iggyServer);
}

}

0 comments on commit 6288c4e

Please sign in to comment.