From f7d2d7b2b1c1bb9be81324e820a092807df9c083 Mon Sep 17 00:00:00 2001 From: Brutus5000 Date: Mon, 25 Nov 2024 20:28:56 +0100 Subject: [PATCH] Allow copying the OAuth2 access token from user menu --- .../com/faforever/client/api/TokenRetriever.java | 7 +++++-- .../client/headerbar/UserButtonController.java | 8 ++++++++ src/main/resources/i18n/messages.properties | 1 + .../resources/theme/headerbar/user_button.fxml | 1 + .../faforever/client/api/TokenRetrieverTest.java | 12 ++++++++++++ .../client/headerbar/UserButtonControllerTest.java | 14 ++++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/faforever/client/api/TokenRetriever.java b/src/main/java/com/faforever/client/api/TokenRetriever.java index 1199a521d1..d314057801 100644 --- a/src/main/java/com/faforever/client/api/TokenRetriever.java +++ b/src/main/java/com/faforever/client/api/TokenRetriever.java @@ -43,8 +43,7 @@ public class TokenRetriever implements InitializingBean { private final Mono refreshedTokenMono = Mono.defer(this::refreshAccess) .cacheInvalidateWhen(this::getExpirationMono) - - .map(OAuth2AccessToken::getTokenValue); + .map(OAuth2AccessToken::getTokenValue); @Override public void afterPropertiesSet() throws Exception { @@ -130,4 +129,8 @@ public void invalidateToken() { public Flux invalidationFlux() { return invalidateFlux; } + + public String getAccessToken() { + return refreshedTokenMono.block(); + } } diff --git a/src/main/java/com/faforever/client/headerbar/UserButtonController.java b/src/main/java/com/faforever/client/headerbar/UserButtonController.java index 050504e59f..d8f3ecae1d 100644 --- a/src/main/java/com/faforever/client/headerbar/UserButtonController.java +++ b/src/main/java/com/faforever/client/headerbar/UserButtonController.java @@ -1,5 +1,6 @@ package com.faforever.client.headerbar; +import com.faforever.client.api.TokenRetriever; import com.faforever.client.domain.server.PlayerInfo; import com.faforever.client.fx.NodeController; import com.faforever.client.player.PlayerInfoWindowController; @@ -7,6 +8,8 @@ import com.faforever.client.reporting.ReportDialogController; import com.faforever.client.theme.UiService; import com.faforever.client.user.LoginService; +import com.faforever.client.util.ClipboardUtil; +import javafx.application.Platform; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.MenuButton; @@ -23,6 +26,7 @@ public class UserButtonController extends NodeController { private final PlayerService playerService; private final UiService uiService; private final LoginService loginService; + private final TokenRetriever tokenRetriever; public MenuButton userMenuButtonRoot; @@ -57,6 +61,10 @@ public void onReport() { reportDialogController.show(); } + public void onCopyAccessToken() { + Platform.runLater(() -> ClipboardUtil.copyToClipboard(tokenRetriever.getAccessToken())); + } + public void onLogOut() { loginService.logOut(); } diff --git a/src/main/resources/i18n/messages.properties b/src/main/resources/i18n/messages.properties index c3d1e31c07..58470cc31b 100644 --- a/src/main/resources/i18n/messages.properties +++ b/src/main/resources/i18n/messages.properties @@ -21,6 +21,7 @@ view.tiles = Tiles view.table = Table userMenu.logOut = Log out userMenu.showProfile = Show profile +userMenu.copyAccessToken=Copy access token menu.feedback = Feedback menu.exit = Exit menu.settings = Settings diff --git a/src/main/resources/theme/headerbar/user_button.fxml b/src/main/resources/theme/headerbar/user_button.fxml index bc6df83e25..9c4ddfb319 100644 --- a/src/main/resources/theme/headerbar/user_button.fxml +++ b/src/main/resources/theme/headerbar/user_button.fxml @@ -9,6 +9,7 @@ + diff --git a/src/test/java/com/faforever/client/api/TokenRetrieverTest.java b/src/test/java/com/faforever/client/api/TokenRetrieverTest.java index 8d6b3ec666..5c9d37253a 100644 --- a/src/test/java/com/faforever/client/api/TokenRetrieverTest.java +++ b/src/test/java/com/faforever/client/api/TokenRetrieverTest.java @@ -183,4 +183,16 @@ public void testGetRefreshToken() throws Exception { assertEquals(tokenProperties.get(REFRESH_TOKEN), loginPrefs.getRefreshToken()); } + + @Test + public void testGetAccessToken() throws Exception { + loginPrefs.setRememberMe(true); + Map tokenProperties = Map.of(EXPIRES_IN, "3600", REFRESH_TOKEN, "refresh", ACCESS_TOKEN, "test", + TOKEN_TYPE, "bearer"); + prepareTokenResponse(tokenProperties); + + String result = instance.getAccessToken(); + + assertEquals(result, "test"); + } } diff --git a/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java b/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java index d459bc0338..242cde4d51 100644 --- a/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java +++ b/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java @@ -1,5 +1,6 @@ package com.faforever.client.headerbar; +import com.faforever.client.api.TokenRetriever; import com.faforever.client.builders.PlayerInfoBuilder; import com.faforever.client.domain.server.PlayerInfo; import com.faforever.client.player.PlayerInfoWindowController; @@ -16,6 +17,7 @@ import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class UserButtonControllerTest extends PlatformTest { private static final String TEST_USER_NAME = "junit"; @@ -30,6 +32,8 @@ public class UserButtonControllerTest extends PlatformTest { private ReportDialogController reportDialogController; @Mock private PlayerInfoWindowController playerInfoWindowController; + @Mock + private TokenRetriever tokenRetriever; @InjectMocks @@ -64,6 +68,16 @@ public void testReport() { verify(reportDialogController).show(); } + @Test + public void testCopyAccessToken() { + when(tokenRetriever.getAccessToken()).thenReturn("someToken"); + + instance.onCopyAccessToken(); + + waitFxEvents(); + verify(tokenRetriever).getAccessToken(); + } + @Test public void testLogOut() { instance.onLogOut();