diff --git a/android/app/src/main/java/com/ts3client/android/ui/MyTeamSpeakScreen.kt b/android/app/src/main/java/com/ts3client/android/ui/MyTeamSpeakScreen.kt index 579919c..75dbe31 100644 --- a/android/app/src/main/java/com/ts3client/android/ui/MyTeamSpeakScreen.kt +++ b/android/app/src/main/java/com/ts3client/android/ui/MyTeamSpeakScreen.kt @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material3.Button @@ -41,11 +42,15 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import com.ts3client.android.SessionController +import com.ts3client.myts.AccountInfo import com.ts3client.myts.MyTeamSpeak import com.ts3client.myts.MyTsException import com.ts3client.teamspeak.SyncItem import kotlinx.coroutines.launch import java.io.IOException +import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle private data class ItemRow(val item: SyncItem, val imported: Boolean, val selected: Boolean) @@ -64,11 +69,13 @@ fun MyTeamSpeakScreen(vm: SessionController, onBack: () -> Unit) { var error by remember { mutableStateOf(false) } var email by remember { mutableStateOf("") } var password by remember { mutableStateOf("") } + var info by remember { mutableStateOf(null) } var all by remember { mutableStateOf(emptyList()) } var rows by remember { mutableStateOf(emptyList()) } BackHandler(onBack = onBack) fun show(account: MyTeamSpeak.Account) { + info = account.info() all = account.items() rows = all.filter { it !is SyncItem.Folder }.map { val imported = vm.isImported(it) @@ -157,10 +164,20 @@ fun MyTeamSpeakScreen(vm: SessionController, onBack: () -> Unit) { signedIn = false rows = emptyList() all = emptyList() + info = null message = null }) { Text("Sign out") } } } + info?.let { details -> + item { + Column(Modifier.padding(horizontal = 16.dp)) { + if (details.userId().isNotEmpty()) Detail("ID", details.userId()) + if (details.uuid().isNotEmpty()) Detail("UUID", details.uuid()) + details.created()?.let { Detail("Created", dateFormat.format(it)) } + } + } + } if (rows.isNotEmpty()) { item { Text("Synchronised by the account", Modifier.padding(start = 16.dp, top = 16.dp), @@ -204,6 +221,17 @@ fun MyTeamSpeakScreen(vm: SessionController, onBack: () -> Unit) { } } +private val dateFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withZone(ZoneId.systemDefault()) + +/** A labelled value that can be selected and copied. */ +@Composable +private fun Detail(label: String, value: String) { + Column(Modifier.padding(top = 8.dp)) { + Text(label, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + SelectionContainer { Text(value, style = MaterialTheme.typography.bodyMedium) } + } +} + private fun name(item: SyncItem) = when (item) { is SyncItem.Bookmark -> item.name().ifBlank { item.address() } is SyncItem.Identity -> "Identity: ${item.name()}" diff --git a/ts3-client/core/src/main/java/com/ts3client/myts/AccountInfo.java b/ts3-client/core/src/main/java/com/ts3client/myts/AccountInfo.java new file mode 100644 index 0000000..b7865cf --- /dev/null +++ b/ts3-client/core/src/main/java/com/ts3client/myts/AccountInfo.java @@ -0,0 +1,38 @@ +package com.ts3client.myts; + +import java.time.Instant; + +/** + * Who a myTeamSpeak account is, as its sign-in reply says. The {@code userId} is the + * account's myTeamSpeak ID: also the localpart of its TeamSpeak chat ID and the user the + * file API addresses. + * + * @param userId empty when the reply carried none + * @param created null when the reply carried none + */ +public record AccountInfo(String username, String userId, String uuid, Instant created) { + + private static final String BASE32 = "abcdefghijklmnopqrstuvwxyz234567"; + + static AccountInfo of(String username, String uuid, byte[] userId, long createdSeconds) { + return new AccountInfo(username, userId == null ? "" : base32(userId), uuid, + createdSeconds > 0 ? Instant.ofEpochSecond(createdSeconds) : null); + } + + /** RFC 4648 base32, lowercase and unpadded. */ + static String base32(byte[] data) { + StringBuilder out = new StringBuilder((data.length * 8 + 4) / 5); + int buffer = 0, bits = 0; + for (byte b : data) { + buffer = (buffer << 8) | (b & 0xff); + bits += 8; + while (bits >= 5) { + out.append(BASE32.charAt((buffer >> (bits - 5)) & 31)); + bits -= 5; + } + buffer &= (1 << bits) - 1; + } + if (bits > 0) out.append(BASE32.charAt((buffer << (5 - bits)) & 31)); + return out.toString(); + } +} diff --git a/ts3-client/core/src/main/java/com/ts3client/myts/MyTeamSpeak.java b/ts3-client/core/src/main/java/com/ts3client/myts/MyTeamSpeak.java index 5b4cf7a..a897dce 100644 --- a/ts3-client/core/src/main/java/com/ts3client/myts/MyTeamSpeak.java +++ b/ts3-client/core/src/main/java/com/ts3client/myts/MyTeamSpeak.java @@ -24,11 +24,15 @@ public final class MyTeamSpeak { public static final URI API = URI.create("https://clientapi.myteamspeak.com/"); /** What the account holds, in the same form as a local TeamSpeak 3 client's store. */ - public record Account(String username, List items) { + public record Account(AccountInfo info, List items) { public Account { items = List.copyOf(items); } + public String username() { + return info.username(); + } + public long identities() { return items.stream().filter(SyncItem.Identity.class::isInstance).count(); } @@ -94,14 +98,27 @@ public final class MyTeamSpeak { new ProtobufWriter().string(1, email).string(2, loginToken).toByteArray())); byte[] keyPackage = null; - String session = "", username = ""; + String session = "", username = "", uuid = ""; + byte[] userId = null; + long createdAt = 0; int error = 0; while (reply.next()) { switch (reply.fieldNumber()) { case 1 -> keyPackage = reply.readBytes(); case 2 -> session = reply.readString(); case 5 -> error = (int) reply.readVarint(); + case 4 -> uuid = reply.readString(); case 8 -> username = reply.readString(); + case 9 -> { + ProtobufReader identity = reply.readMessage(); + while (identity.next()) { + switch (identity.fieldNumber()) { + case 3 -> createdAt = identity.readVarint(); + case 4 -> userId = identity.readBytes(); + default -> identity.skip(); + } + } + } default -> reply.skip(); } } @@ -114,7 +131,7 @@ public final class MyTeamSpeak { } catch (GeneralSecurityException e) { throw new MyTsException("Could not unlock the account's encrypted data", e); } - return new Account(username, pull(session, itemKey)); + return new Account(AccountInfo.of(username, uuid, userId, createdAt), pull(session, itemKey)); } finally { signOut(session); } diff --git a/ts3-client/core/src/test/java/com/ts3client/myts/AccountInfoTest.java b/ts3-client/core/src/test/java/com/ts3client/myts/AccountInfoTest.java new file mode 100644 index 0000000..6f27728 --- /dev/null +++ b/ts3-client/core/src/test/java/com/ts3client/myts/AccountInfoTest.java @@ -0,0 +1,33 @@ +package com.ts3client.myts; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class AccountInfoTest { + + private static String base32(String text) { + return AccountInfo.base32(text.getBytes(StandardCharsets.US_ASCII)); + } + + @Test + void encodesRfc4648VectorsLowercaseAndUnpadded() { + assertEquals("", base32("")); + assertEquals("my", base32("f")); + assertEquals("mzxq", base32("fo")); + assertEquals("mzxw6", base32("foo")); + assertEquals("mzxw6yq", base32("foob")); + assertEquals("mzxw6ytb", base32("fooba")); + assertEquals("mzxw6ytboi", base32("foobar")); + } + + @Test + void toleratesAReplyWithoutIdentityData() { + AccountInfo info = AccountInfo.of("tester", "u", null, 0); + assertEquals("", info.userId()); + assertNull(info.created()); + } +} diff --git a/ts3-client/core/src/test/java/com/ts3client/myts/MyTeamSpeakTest.java b/ts3-client/core/src/test/java/com/ts3client/myts/MyTeamSpeakTest.java index ebe0eee..901c29e 100644 --- a/ts3-client/core/src/test/java/com/ts3client/myts/MyTeamSpeakTest.java +++ b/ts3-client/core/src/test/java/com/ts3client/myts/MyTeamSpeakTest.java @@ -48,7 +48,11 @@ class MyTeamSpeakTest { .bytes(1, MyTsCryptoTest.KEY_PACKAGE) .string(2, SESSION) .varint(5, 200) + .string(4, "828613f7-3cde-45e6-993e-c57ee979a39f") .string(8, "tester") + .message(9, new ProtobufWriter().varint(3, 1_770_000_000L) + .bytes(4, java.util.HexFormat.of().parseHex( + "0120b05c4b1a2d3e4f5061728394a5b6c7d8e9f00112233445566778899aabbccd"))) .toByteArray(); } @@ -82,6 +86,9 @@ class MyTeamSpeakTest { MyTeamSpeak.Account account = new MyTeamSpeak(server).download(credentials(MyTsCryptoTest.PASSWORD)); assertEquals("tester", account.username()); + assertEquals("828613f7-3cde-45e6-993e-c57ee979a39f", account.info().uuid()); + assertEquals(java.time.Instant.ofEpochSecond(1_770_000_000L), account.info().created()); + assertEquals("aeqlaxcldiwt4t2qmfzihfffw3d5r2pqaejcgncfkztxrcm2vo6m2", account.info().userId()); assertEquals(1, account.items().size()); SyncItem.Bookmark bookmark = assertInstanceOf(SyncItem.Bookmark.class, account.items().get(0)); assertEquals("server.lixko.eu", bookmark.address()); diff --git a/ts3-client/swing/src/main/java/com/ts3client/ui/MyTeamSpeakPanel.java b/ts3-client/swing/src/main/java/com/ts3client/ui/MyTeamSpeakPanel.java index 8fa3ab1..c9e5c02 100644 --- a/ts3-client/swing/src/main/java/com/ts3client/ui/MyTeamSpeakPanel.java +++ b/ts3-client/swing/src/main/java/com/ts3client/ui/MyTeamSpeakPanel.java @@ -2,6 +2,7 @@ package com.ts3client.ui; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.UIScale; +import com.ts3client.myts.AccountInfo; import com.ts3client.myts.MyTeamSpeak; import com.ts3client.myts.MyTeamSpeakLogin; import com.ts3client.myts.MyTsException; @@ -25,6 +26,9 @@ import java.awt.CardLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.io.IOException; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.ArrayList; import java.util.List; @@ -50,6 +54,7 @@ final class MyTeamSpeakPanel extends JPanel { private final JLabel signInStatus = new JLabel(" "); private final JLabel account = new JLabel(); + private final JPanel details = new JPanel(new GridBagLayout()); private final ItemTable items = new ItemTable(); private final JButton refresh = new JButton("Refresh"); private final JButton importButton = new JButton("Import selected"); @@ -108,6 +113,9 @@ final class MyTeamSpeakPanel extends JPanel { signOut.addActionListener(e -> signOut()); header.add(account, BorderLayout.CENTER); header.add(signOut, BorderLayout.EAST); + JPanel top = new JPanel(new BorderLayout(0, UIScale.scale(8))); + top.add(header, BorderLayout.NORTH); + top.add(details, BorderLayout.CENTER); JPanel buttons = new JPanel(); buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); @@ -126,7 +134,7 @@ final class MyTeamSpeakPanel extends JPanel { JPanel panel = new JPanel(new BorderLayout(0, UIScale.scale(12))); panel.setBorder(new ScaledEmptyBorder(12, 12, 12, 12)); - panel.add(header, BorderLayout.NORTH); + panel.add(top, BorderLayout.NORTH); panel.add(data, BorderLayout.CENTER); return panel; } @@ -167,6 +175,7 @@ final class MyTeamSpeakPanel extends JPanel { private void signOut() { login.signOut(); accountItems = List.of(); + details.removeAll(); items.set(List.of()); fetched = false; email.setText(""); @@ -193,8 +202,32 @@ final class MyTeamSpeakPanel extends JPanel { }); } + private void showDetails(AccountInfo info) { + details.removeAll(); + GridBagConstraints c = FormPanel.gbc(); + int row = 0; + if (!info.userId().isEmpty()) FormPanel.addRow(details, c, row++, new JLabel("ID:"), selectable(info.userId())); + if (!info.uuid().isEmpty()) FormPanel.addRow(details, c, row++, new JLabel("UUID:"), selectable(info.uuid())); + if (info.created() != null) { + String date = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) + .format(info.created().atZone(ZoneId.systemDefault())); + FormPanel.addRow(details, c, row, new JLabel("Created:"), selectable(date)); + } + details.revalidate(); + details.repaint(); + } + + /** A value that can be selected and copied but not edited. */ + private static JTextField selectable(String value) { + JTextField field = new JTextField(value); + field.setEditable(false); + field.setCaretPosition(0); + return field; + } + private void showItems(MyTeamSpeak.Account result) { fetched = true; + showDetails(result.info()); accountItems = result.items(); List rows = new ArrayList<>(); for (SyncItem item : accountItems) {