-
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 TCP
- Loading branch information
1 parent
6288c4e
commit 500b481
Showing
4 changed files
with
101 additions
and
2 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
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
62 changes: 62 additions & 0 deletions
62
src/main/java/rs/iggy/clients/blocking/tcp/PersonalAccessTokensTcpClient.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,62 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import rs.iggy.clients.blocking.PersonalAccessTokensClient; | ||
import rs.iggy.personalaccesstoken.PersonalAccessTokenInfo; | ||
import rs.iggy.personalaccesstoken.RawPersonalAccessToken; | ||
import rs.iggy.user.IdentityInfo; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import static rs.iggy.clients.blocking.tcp.BytesDeserializer.readPersonalAccessTokenInfo; | ||
import static rs.iggy.clients.blocking.tcp.BytesDeserializer.readRawPersonalAccessToken; | ||
import static rs.iggy.clients.blocking.tcp.BytesSerializer.nameToBytes; | ||
import static rs.iggy.clients.blocking.tcp.BytesSerializer.toBytesAsU64; | ||
|
||
class PersonalAccessTokensTcpClient implements PersonalAccessTokensClient { | ||
|
||
private static final int GET_PERSONAL_ACCESS_TOKENS_CODE = 41; | ||
private static final int CREATE_PERSONAL_ACCESS_TOKEN_CODE = 42; | ||
private static final int DELETE_PERSONAL_ACCESS_TOKEN_CODE = 43; | ||
private static final int LOGIN_WITH_PERSONAL_ACCESS_TOKEN_CODE = 44; | ||
|
||
private final TcpConnectionHandler connection; | ||
|
||
public PersonalAccessTokensTcpClient(TcpConnectionHandler connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public RawPersonalAccessToken createPersonalAccessToken(String name, BigInteger expiry) { | ||
var payload = Unpooled.buffer(); | ||
payload.writeBytes(nameToBytes(name)); | ||
payload.writeBytes(toBytesAsU64(expiry)); | ||
var response = connection.send(CREATE_PERSONAL_ACCESS_TOKEN_CODE, payload); | ||
return readRawPersonalAccessToken(response); | ||
} | ||
|
||
@Override | ||
public List<PersonalAccessTokenInfo> getPersonalAccessTokens() { | ||
var response = connection.send(GET_PERSONAL_ACCESS_TOKENS_CODE); | ||
var tokens = new ArrayList<PersonalAccessTokenInfo>(); | ||
while (response.isReadable()) { | ||
tokens.add(readPersonalAccessTokenInfo(response)); | ||
} | ||
return tokens; | ||
} | ||
|
||
@Override | ||
public void deletePersonalAccessToken(String name) { | ||
var payload = nameToBytes(name); | ||
connection.send(DELETE_PERSONAL_ACCESS_TOKEN_CODE, payload); | ||
} | ||
|
||
@Override | ||
public IdentityInfo loginWithPersonalAccessToken(String token) { | ||
var payload = nameToBytes(token); | ||
var response = connection.send(LOGIN_WITH_PERSONAL_ACCESS_TOKEN_CODE, payload); | ||
var userId = response.readUnsignedIntLE(); | ||
return new IdentityInfo(userId, Optional.empty()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/rs/iggy/clients/blocking/tcp/PersonalAccessTokensTcpClientTest.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.tcp; | ||
|
||
import rs.iggy.clients.blocking.IggyClient; | ||
import rs.iggy.clients.blocking.PersonalAccessTokensBaseTest; | ||
|
||
class PersonalAccessTokensTcpClientTest extends PersonalAccessTokensBaseTest { | ||
|
||
@Override | ||
protected IggyClient getClient() { | ||
return TcpClientFactory.create(iggyServer); | ||
} | ||
|
||
} |