Skip to content

Commit

Permalink
Allow copying the OAuth2 access token from user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Nov 25, 2024
1 parent df91054 commit f7d2d7b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/faforever/client/api/TokenRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public class TokenRetriever implements InitializingBean {

private final Mono<String> refreshedTokenMono = Mono.defer(this::refreshAccess)
.cacheInvalidateWhen(this::getExpirationMono)

.map(OAuth2AccessToken::getTokenValue);
.map(OAuth2AccessToken::getTokenValue);

@Override
public void afterPropertiesSet() throws Exception {
Expand Down Expand Up @@ -130,4 +129,8 @@ public void invalidateToken() {
public Flux<Long> invalidationFlux() {
return invalidateFlux;
}

public String getAccessToken() {
return refreshedTokenMono.block();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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;
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.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
Expand All @@ -23,6 +26,7 @@ public class UserButtonController extends NodeController<Node> {
private final PlayerService playerService;
private final UiService uiService;
private final LoginService loginService;
private final TokenRetriever tokenRetriever;

public MenuButton userMenuButtonRoot;

Expand Down Expand Up @@ -57,6 +61,10 @@ public void onReport() {
reportDialogController.show();
}

public void onCopyAccessToken() {
Platform.runLater(() -> ClipboardUtil.copyToClipboard(tokenRetriever.getAccessToken()));
}

public void onLogOut() {
loginService.logOut();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/theme/headerbar/user_button.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<items>
<MenuItem mnemonicParsing="false" onAction="#onShowProfile" text="%userMenu.showProfile"/>
<MenuItem mnemonicParsing="false" onAction="#onReport" text="%userMenu.moderationReport"/>
<MenuItem mnemonicParsing="false" onAction="#onCopyAccessToken" text="%userMenu.copyAccessToken"/>
<MenuItem mnemonicParsing="false" onAction="#onLogOut" text="%userMenu.logOut"/>
</items>
</MenuButton>
12 changes: 12 additions & 0 deletions src/test/java/com/faforever/client/api/TokenRetrieverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> tokenProperties = Map.of(EXPIRES_IN, "3600", REFRESH_TOKEN, "refresh", ACCESS_TOKEN, "test",
TOKEN_TYPE, "bearer");
prepareTokenResponse(tokenProperties);

String result = instance.getAccessToken();

assertEquals(result, "test");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -30,6 +32,8 @@ public class UserButtonControllerTest extends PlatformTest {
private ReportDialogController reportDialogController;
@Mock
private PlayerInfoWindowController playerInfoWindowController;
@Mock
private TokenRetriever tokenRetriever;


@InjectMocks
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f7d2d7b

Please sign in to comment.