-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement personal access tokens for HTTP
- Loading branch information
1 parent
836f5ed
commit 6288c4e
Showing
9 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,6 @@ public interface IggyClient { | |
|
||
MessagesClient messages(); | ||
|
||
PersonalAccessTokensClient personalAccessTokens(); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/rs/iggy/clients/blocking/PersonalAccessTokensClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/rs/iggy/clients/blocking/http/PersonalAccessTokensHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/rs/iggy/personalaccesstoken/PersonalAccessTokenInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/rs/iggy/personalaccesstoken/RawPersonalAccessToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package rs.iggy.personalaccesstoken; | ||
|
||
public record RawPersonalAccessToken(String token) { | ||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/rs/iggy/clients/blocking/PersonalAccessTokensBaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/rs/iggy/clients/blocking/http/PersonalAccessTokensHttpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |