From ee87c7910e3dd7ae413314b5ae0674710fce5299 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 | 14 ++++++++++++++ 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, 47 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..3987147d9d 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 Mono getAccessToken() { + return refreshedTokenMono; + } } diff --git a/src/main/java/com/faforever/client/headerbar/UserButtonController.java b/src/main/java/com/faforever/client/headerbar/UserButtonController.java index 050504e59f..3edb6869c7 100644 --- a/src/main/java/com/faforever/client/headerbar/UserButtonController.java +++ b/src/main/java/com/faforever/client/headerbar/UserButtonController.java @@ -1,28 +1,35 @@ package com.faforever.client.headerbar; +import com.faforever.client.api.TokenRetriever; import com.faforever.client.domain.server.PlayerInfo; +import com.faforever.client.fx.FxApplicationThreadExecutor; import com.faforever.client.fx.NodeController; import com.faforever.client.player.PlayerInfoWindowController; import com.faforever.client.player.PlayerService; 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.scene.Node; import javafx.scene.Scene; import javafx.scene.control.MenuButton; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +@Slf4j @RequiredArgsConstructor public class UserButtonController extends NodeController { + private final FxApplicationThreadExecutor fxApplicationThreadExecutor; private final PlayerService playerService; private final UiService uiService; private final LoginService loginService; + private final TokenRetriever tokenRetriever; public MenuButton userMenuButtonRoot; @@ -57,6 +64,13 @@ public void onReport() { reportDialogController.show(); } + public void onCopyAccessToken() { + tokenRetriever.getAccessToken().subscribe(accessToken -> { + log.info("Copied access token to clipboard"); + fxApplicationThreadExecutor.execute(() -> ClipboardUtil.copyToClipboard(accessToken)); + }); + } + 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..7768e47f25 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); + + StepVerifier.create(instance.getAccessToken()) + .assertNext(accessToken -> assertEquals(accessToken, "test")) + .verifyComplete(); + } } diff --git a/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java b/src/test/java/com/faforever/client/headerbar/UserButtonControllerTest.java index d459bc0338..0c57098d5c 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; @@ -13,9 +14,11 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; +import reactor.core.publisher.Mono; 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 +33,8 @@ public class UserButtonControllerTest extends PlatformTest { private ReportDialogController reportDialogController; @Mock private PlayerInfoWindowController playerInfoWindowController; + @Mock + private TokenRetriever tokenRetriever; @InjectMocks @@ -64,6 +69,15 @@ public void testReport() { verify(reportDialogController).show(); } + @Test + public void testCopyAccessToken() { + when(tokenRetriever.getAccessToken()).thenReturn(Mono.just("someToken")); + + instance.onCopyAccessToken(); + + verify(tokenRetriever).getAccessToken(); + } + @Test public void testLogOut() { instance.onLogOut();