diff --git a/.jbang/JabSrvLauncher.java b/.jbang/JabSrvLauncher.java index d305f09e6f96..433dfb9f5d4f 100755 --- a/.jbang/JabSrvLauncher.java +++ b/.jbang/JabSrvLauncher.java @@ -55,6 +55,8 @@ //DEPS tools.jackson.core:jackson-core:3.2.4 //DEPS tools.jackson.core:jackson-databind:3.2.4 //DEPS tools.jackson.dataformat:jackson-dataformat-yaml:3.2.4 +//DEPS com.nimbusds:oauth2-oidc-sdk:11.30.1 +//DEPS net.minidev:json-smart:2.6.0 //SOURCES ../jabsrv/src/main/java/org/jabref/http/dto/BibEntryDTO.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/dto/cayw/SimpleJson.java @@ -92,11 +94,12 @@ //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/gui/SearchField.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/gui/SearchResultContainer.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/gui/SelectedItemsContainer.java -//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/command/Command.java -//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/command/CommandResource.java -//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/command/FocusCommand.java -//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/command/OpenLibrariesCommand.java -//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/command/SelectEntriesCommand.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/callback/CallbackResource.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/command/Command.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/command/FocusCommand.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/command/OpenLibrariesCommand.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/command/CommandResource.java +//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/command/SelectEntriesCommand.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/CitationsResource.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/LibrariesQueryResource.java //SOURCES ../jabsrv/src/main/java/org/jabref/http/server/resources/LibrariesResource.java @@ -114,6 +117,7 @@ // new jablib sources not yet in the 6.0-SNAPSHOT artifact //SOURCES ../jablib/src/main/java/org/jabref/logic/importer/plaincitation/PlainCitationParserFactory.java //SOURCES ../jablib/src/main/java/org/jabref/logic/UiCommand.java +//SOURCES ../jablib/src/main/java/org/jabref/logic/citedrive/OAuthSessionRegistry.java //SOURCES ../jablib/src/main/java/org/jabref/logic/search/LibrarySearcher.java //SOURCES ../jablib/src/main/java/org/jabref/logic/search/NoOpSearchBackend.java //SOURCES ../jablib/src/main/java/org/jabref/logic/search/SearchBackend.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 747446a65f8a..5e0b2fba7ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We added "Contribute to JabRef" to the Help menu, linking to [contribute.jabref.org](https://contribute.jabref.org). [#17162](https://github.com/JabRef/jabref/pull/17162) - We added EasyOCR, PaddleOCR, and AppleOCR as selectable OCR engines, alongside Tesseract and Docling. [#16866](https://github.com/JabRef/jabref/issues/16866) - We added syntax highlighting to the BibTeX source window of the citations tab. [#17355](https://github.com/JabRef/jabref/issues/17355) +- We added pushing the current library to [CiteDrive](https://www.citedrive.com/), including logging in to CiteDrive. [#14493](https://github.com/JabRef/jabref/pull/14493) ### Changed diff --git a/build-logic/src/main/kotlin/org.jabref.gradle.base.dependency-rules.gradle.kts b/build-logic/src/main/kotlin/org.jabref.gradle.base.dependency-rules.gradle.kts index e0c43ae49166..c0b9b10ed96c 100644 --- a/build-logic/src/main/kotlin/org.jabref.gradle.base.dependency-rules.gradle.kts +++ b/build-logic/src/main/kotlin/org.jabref.gradle.base.dependency-rules.gradle.kts @@ -186,6 +186,31 @@ extraJavaModuleInfo { uses("kong.unirest.core.json.JsonEngine") } module("com.konghq:unirest-modules-gson", "unirest.modules.gson") + + // region nimbusdl + // required by nimbusdl + module("com.github.stephenc.jcip:jcip-annotations", "jcip.annotations") { + exportAllPackages() + } + module("net.minidev:json-smart", "json.smart") { + exportAllPackages() + } + module("com.nimbusds:content-type", "nimbusds.contenttype") { + overrideModuleName() + exportAllPackages() + } + knownModule("com.nimbusds:nimbus-jose-jwt", "com.nimbusds.jose.jwt") + module("com.nimbusds:lang-tag", "nimbusds.langtag") { + overrideModuleName() + exportAllPackages() + } + module("com.nimbusds:oauth2-oidc-sdk", "nimbusds.oauth2.oidc.sdk") { + overrideModuleName() + exportAllPackages() + requireAllDefinedDependencies() + } + // endregion + module("com.squareup.okhttp3:mockwebserver3", "mockwebserver3") { preserveExisting() } diff --git a/docs/code-howtos/http-server.md b/docs/code-howtos/http-server.md index aebae17070e9..2753fd8310be 100644 --- a/docs/code-howtos/http-server.md +++ b/docs/code-howtos/http-server.md @@ -74,6 +74,33 @@ The library name `current` denotes the currently focussed library in JabRef. ./gradlew :jabsrv-cli:run --args="../jablib/src/test/resources/testbib/complex.bib" ``` +## Logging in to CiteDrive without the GUI + +`jabsrv --citedrive-login` prints the CiteDrive login address, waits for the browser to come back to `/callback`, stores the refresh token in the system keyring, and exits. +JabRef's HTTP server port from the preferences has to be the port `jabsrv` listens on, since CiteDrive redirects the browser there. + +```shell +./gradlew :jabsrv-cli:run --args="--citedrive-login" +``` + +### CiteDrive authentication security + +CiteDrive's API and web application addresses must use HTTPS; only the local browser callback uses HTTP. +Each authentication operation captures its server settings, and uploads use the address associated with their access token. +Changing the API address clears the login and prevents pending responses from restoring credentials for the previous configuration. +The keyring stores the API address alongside the refresh token; existing tokens without that binding require another login. + +Concurrent token requests share one authentication operation to support refresh token rotation. +Both token exchange and refresh use a ten-second connection timeout and a thirty-second read timeout, with redirects disabled. +The code exchange runs asynchronously so that a slow token endpoint does not block the callback response. +Temporary errors preserve the stored login; an HTTP 400 `invalid_grant` response starts another browser login. + +The current upload sends all entries from the active library in a single BibTeX request to `/jabref/push/`. +Each upload therefore uses one request from the endpoint's current allowance of 100 requests per hour per user; the server's payload size limit is a separate constraint. +`CiteDrivePushTest` verifies batches of two and one thousand entries by parsing the prepared request back into entries. +Setting the test JVM system property `citedrive.batch.fixture` to a generated 100,000-entry library adds that fixture to the same check. +The test records the payload size and preparation timings without contacting CiteDrive. + ## Developing with IntelliJ IntelliJ Ultimate offers a Markdown-based http-client. You need to open the file `jabsrv/src/test/rest-api.http`. diff --git a/docs/requirements/citedrive.md b/docs/requirements/citedrive.md new file mode 100644 index 000000000000..f5e4df8e8c06 --- /dev/null +++ b/docs/requirements/citedrive.md @@ -0,0 +1,73 @@ +--- +parent: Requirements +--- +# CiteDrive + +## Log in to CiteDrive +`req~citedrive.login~1` + +JabRef logs the user in to CiteDrive in the browser (OAuth 2.0 authorization code flow with PKCE). +The browser returns to JabRef's HTTP server; if it is disabled, JabRef asks the user to enable it and only then opens the browser. +`jabsrv --citedrive-login` offers the same login without the GUI: it prints the login address instead of opening the browser. +A login not completed within ten minutes fails. +The refresh token is stored in the system keyring only, never in the preferences. + +Needs: impl, utest + +## Push the library to CiteDrive +`req~citedrive.push~1` + +"Push to CiteDrive" uploads the current library as BibTeX. +Without a valid refresh token, JabRef logs in first. +A network error while refreshing the token keeps the stored login. + +Needs: impl, utest + +## Open the import page after a push +`req~citedrive.push.import-page~1` + +After a successful push, JabRef opens the CiteDrive page where the user chooses a project and the pushed entries to import. + +Needs: impl + +## CiteDrive credentials must remain bound to their server +`req~citedrive.credentials.server~1` + +Changing the API address invalidates the saved login, including pending authentication responses. +Stored refresh tokens are loaded only for the address that issued them. +An upload uses the API address captured when its access token was requested. + +Needs: impl, utest + +## CiteDrive server addresses must use HTTPS +`req~citedrive.https~1` + +The authorization, token, upload, and web application addresses must use HTTPS. +The browser callback remains on the local HTTP loopback listener. + +Needs: impl, utest + +## Concurrent CiteDrive token requests must share one authentication operation +`req~citedrive.refresh.single-request~1` + +Requests for the same server configuration share an outstanding token request so that rotating refresh tokens are not reused concurrently. + +Needs: impl, utest + +## CiteDrive token requests must have finite network timeouts +`req~citedrive.token.timeout~1` + +Authorization code exchange and refresh use a ten-second connection timeout and a thirty-second read timeout. +Token exchange runs outside the browser callback request thread. + +Needs: impl, utest + +## CiteDrive must preserve the login after retryable refresh failures +`req~citedrive.refresh.failure~1` + +Network, parsing, rate limit, server, and client configuration failures retain the refresh token without starting browser authentication. +Only an `invalid_grant` response with HTTP status 400 clears the token and starts another login. + +Needs: impl, utest + + diff --git a/jabgui/src/main/java/module-info.java b/jabgui/src/main/java/module-info.java index 8f789c269df1..74bef58d9376 100644 --- a/jabgui/src/main/java/module-info.java +++ b/jabgui/src/main/java/module-info.java @@ -179,6 +179,10 @@ requires org.jspecify; + // region nimbusds / OAuth2 + requires nimbusds.oauth2.oidc.sdk; + // endregion + // region: other libraries (alphabetically) // requires cuid; requires com.dlsc.pdfviewfx; diff --git a/jabgui/src/main/java/org/jabref/gui/JabRefGUI.java b/jabgui/src/main/java/org/jabref/gui/JabRefGUI.java index c799b277073f..4a12eac42708 100644 --- a/jabgui/src/main/java/org/jabref/gui/JabRefGUI.java +++ b/jabgui/src/main/java/org/jabref/gui/JabRefGUI.java @@ -37,11 +37,12 @@ import org.jabref.gui.util.DirectoryMonitor; import org.jabref.gui.util.UiTaskExecutor; import org.jabref.gui.walkthrough.WalkthroughPane; -import org.jabref.http.manager.HttpServerManager; +import org.jabref.http.server.manager.HttpServerManager; import org.jabref.languageserver.controller.LanguageServerController; import org.jabref.logic.UiCommand; import org.jabref.logic.ai.AiService; import org.jabref.logic.citation.SearchCitationsRelationsService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.git.GitSsh; import org.jabref.logic.git.util.GitHandlerRegistry; import org.jabref.logic.journals.JournalAbbreviationLoader; @@ -103,6 +104,7 @@ public class JabRefGUI extends Application { private static RemoteListenerServerManager remoteListenerServerManager; private static HttpServerManager httpServerManager; + private static OAuthSessionRegistry oAuthSessionRegistry; private static LanguageServerController languageServerController; private Stage mainStage; @@ -139,7 +141,8 @@ public void start(Stage stage) { clipBoardManager, taskExecutor, gitHandlerRegistry, - journalAbbreviationRepository); + journalAbbreviationRepository, + oAuthSessionRegistry); openWindow(); @@ -228,6 +231,9 @@ public void initialize() { JabRefGUI.httpServerManager = new HttpServerManager(); Injector.setModelOrService(HttpServerManager.class, JabRefGUI.httpServerManager); + JabRefGUI.oAuthSessionRegistry = new OAuthSessionRegistry(); + Injector.setModelOrService(OAuthSessionRegistry.class, JabRefGUI.oAuthSessionRegistry); + JabRefGUI.languageServerController = new LanguageServerController(preferences, journalAbbreviationRepository, entryTypesManager); Injector.setModelOrService(LanguageServerController.class, JabRefGUI.languageServerController); @@ -531,7 +537,7 @@ public void startBackgroundTasks() { } if (remotePreferences.shouldEnableHttpServer()) { - httpServerManager.start(preferences, stateManager, mainFrame, remotePreferences.getHttpServerUri()); + httpServerManager.start(preferences, stateManager, mainFrame, oAuthSessionRegistry, remotePreferences.getHttpServerUri()); } if (remotePreferences.shouldEnableLanguageServer()) { languageServerController.start(cliMessageHandler, remotePreferences.getLanguageServerPort()); @@ -544,7 +550,7 @@ private void setupHttpServerEnabledListener() { // stop in all cases, because the port might have changed httpServerManager.stop(); if (newValue) { - httpServerManager.start(preferences, stateManager, mainFrame, remotePreferences.getHttpServerUri()); + httpServerManager.start(preferences, stateManager, mainFrame, oAuthSessionRegistry, remotePreferences.getHttpServerUri()); } }); } diff --git a/jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java b/jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java index a93373e04ed2..069b9275ca23 100644 --- a/jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java +++ b/jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java @@ -239,7 +239,11 @@ public enum StandardActions implements Action { GIT_PULL(Localization.lang("Pull")), GIT_PUSH(Localization.lang("Push")), GIT_COMMIT(Localization.lang("Commit")), - GIT_SHARE(Localization.lang("Share this library to GitHub")); + GIT_SHARE(Localization.lang("Share this library to GitHub")), + + CITE_DRIVE(Localization.lang("CiteDrive")), + CITE_DRIVE_LOGIN(Localization.lang("Login...")), // "Authorize" would be the more technical term here. + CITE_DRIVE_PUSH(Localization.lang("Push to CiteDrive")); private String text; private final String description; diff --git a/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDriveLoginAction.java b/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDriveLoginAction.java new file mode 100644 index 000000000000..33faac5dca4c --- /dev/null +++ b/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDriveLoginAction.java @@ -0,0 +1,80 @@ +package org.jabref.gui.citedrive; + +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeoutException; + +import org.jabref.gui.DialogService; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.desktop.os.NativeDesktop; +import org.jabref.gui.preferences.GuiPreferences; +import org.jabref.gui.util.UiTaskExecutor; +import org.jabref.logic.citedrive.CiteDriveOAuthService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.remote.RemotePreferences; + +import org.jspecify.annotations.NullMarked; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NullMarked +public class CiteDriveLoginAction extends SimpleCommand { + private static final Logger LOGGER = LoggerFactory.getLogger(CiteDriveLoginAction.class); + private final DialogService dialogService; + private final CiteDriveOAuthService citeDriveOAuthService; + + public CiteDriveLoginAction(DialogService dialogService, GuiPreferences preferences, OAuthSessionRegistry oAuthSessionRegistry) { + this.dialogService = dialogService; + this.citeDriveOAuthService = createOAuthService(dialogService, preferences, oAuthSessionRegistry); + } + + static CiteDriveOAuthService createOAuthService(DialogService dialogService, GuiPreferences preferences, OAuthSessionRegistry oAuthSessionRegistry) { + return new CiteDriveOAuthService( + preferences.getRemotePreferences(), + preferences.getCiteDrivePreferences(), + oAuthSessionRegistry, + uri -> UiTaskExecutor.runInJavaFXThread(() -> { + dialogService.notify(Localization.lang("Opening the CiteDrive login page in the browser")); + NativeDesktop.openBrowserShowPopup(uri.toASCIIString(), dialogService, preferences.getExternalApplicationsPreferences()); + }), + () -> enableHttpServer(dialogService, preferences.getRemotePreferences())); + } + + /// The browser returns to JabRef's HTTP server, which is off by default, so the user is asked to switch it on. + /// + /// @return whether the server is enabled now + private static boolean enableHttpServer(DialogService dialogService, RemotePreferences remotePreferences) { + boolean enable = Boolean.TRUE.equals(UiTaskExecutor.runInJavaFXThread(() -> dialogService.showConfirmationDialogAndWait( + Localization.lang("Log in to CiteDrive"), + Localization.lang("Logging in to CiteDrive needs JabRef's HTTP server, which is currently disabled."), + Localization.lang("Enable HTTP server")))); + if (enable) { + remotePreferences.setEnableHttpServer(true); + } + return enable; + } + + /// User-facing reason of a failed login or push + static String describeFailure(Throwable throwable) { + Throwable cause = (throwable instanceof CompletionException && throwable.getCause() != null) ? throwable.getCause() : throwable; + if (cause instanceof TimeoutException) { + return Localization.lang("Login was not completed in time."); + } + return String.valueOf(cause.getLocalizedMessage()); + } + + @Override + public void execute() { + // The token itself is not needed here: the refresh token is stored in the preferences + citeDriveOAuthService + .authorizeInteractive() + .whenComplete((token, throwable) -> { + if (throwable != null) { + LOGGER.error("CiteDrive login failed", throwable); + dialogService.notify(Localization.lang("CiteDrive login failed: %0", describeFailure(throwable))); + } else if (token.isEmpty()) { + dialogService.notify(Localization.lang("CiteDrive login failed: could not obtain access token.")); + } + }); + } +} diff --git a/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDrivePushAction.java b/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDrivePushAction.java new file mode 100644 index 000000000000..945010883afb --- /dev/null +++ b/jabgui/src/main/java/org/jabref/gui/citedrive/CiteDrivePushAction.java @@ -0,0 +1,78 @@ +package org.jabref.gui.citedrive; + +import java.io.IOException; +import java.util.List; + +import org.jabref.gui.DialogService; +import org.jabref.gui.StateManager; +import org.jabref.gui.actions.ActionHelper; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.desktop.os.NativeDesktop; +import org.jabref.gui.preferences.GuiPreferences; +import org.jabref.gui.util.UiTaskExecutor; +import org.jabref.logic.citedrive.CiteDriveOAuthService; +import org.jabref.logic.citedrive.CiteDrivePush; +import org.jabref.logic.citedrive.OAuthSessionRegistry; +import org.jabref.logic.l10n.Localization; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; + +import org.jspecify.annotations.NullMarked; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NullMarked +public class CiteDrivePushAction extends SimpleCommand { + private static final Logger LOGGER = LoggerFactory.getLogger(CiteDrivePushAction.class); + + private final CiteDriveOAuthService citeDriveOAuthService; + private final DialogService dialogService; + private final StateManager stateManager; + private final GuiPreferences preferences; + + public CiteDrivePushAction(DialogService dialogService, StateManager stateManager, GuiPreferences preferences, OAuthSessionRegistry oAuthSessionRegistry) { + this.dialogService = dialogService; + this.stateManager = stateManager; + this.preferences = preferences; + this.citeDriveOAuthService = CiteDriveLoginAction.createOAuthService(dialogService, preferences, oAuthSessionRegistry); + + this.executable.bind(ActionHelper.needsDatabase(stateManager)); + } + + @Override + public void execute() { + assert this.stateManager.getActiveDatabase().isPresent(); + + BibDatabaseContext database = this.stateManager.getActiveDatabase().get(); + // Snapshot on the FX thread; the push runs in the background + List entries = database.getEntries().stream() + .filter(entry -> !entry.isEmpty()) + .map(BibEntry::new) + .toList(); + + citeDriveOAuthService + .getAuthorizedSession() + // Async: the token may arrive on the HTTP server thread; the push itself is network I/O + .thenAcceptAsync(sessionOpt -> { + if (sessionOpt.isEmpty()) { + dialogService.notify(Localization.lang("CiteDrive push failed: could not obtain access token.")); + return; + } + CiteDriveOAuthService.AuthorizedSession session = sessionOpt.orElseThrow(); + try { + if (CiteDrivePush.push(database, entries, session.accessToken(), preferences, dialogService, session.pushEndpoint())) { + // [impl->req~citedrive.push.import-page~1] + UiTaskExecutor.runInJavaFXThread(() -> NativeDesktop.openBrowserShowPopup(session.importPage().toASCIIString(), dialogService, preferences.getExternalApplicationsPreferences())); + } + } catch (IOException e) { + LOGGER.error("CiteDrive push failed", e); + dialogService.notify(Localization.lang("CiteDrive push failed: %0", e.getMessage())); + } + }) + .exceptionally(throwable -> { + LOGGER.error("CiteDrive push failed", throwable); + dialogService.notify(Localization.lang("CiteDrive push failed: %0", CiteDriveLoginAction.describeFailure(throwable))); + return null; + }); + } +} diff --git a/jabgui/src/main/java/org/jabref/gui/desktop/os/Windows.java b/jabgui/src/main/java/org/jabref/gui/desktop/os/Windows.java index 6f398f08161a..b227562a30b5 100644 --- a/jabgui/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/jabgui/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.util.List; import java.util.Optional; import org.jabref.gui.DialogService; @@ -38,8 +39,17 @@ public void openFile(String filePath, String fileType, ExternalApplicationsPrefe @Override public void openUrlWithSystemHandler(String url) throws IOException { + new ProcessBuilder(urlOpenCommand(url)).start(); + } + + /// Explorer truncates its argument at the Windows path limit of 260 characters, which OAuth login URLs exceed. + /// The URL protocol handler takes the URL unquoted and without that limit. + static List urlOpenCommand(String url) { + if (url.length() > 260) { + return List.of("rundll32.exe", "url.dll,FileProtocolHandler", url); + } // quote String so explorer handles URL query strings correctly - new ProcessBuilder("explorer.exe", "\"" + url + "\"").start(); + return List.of("explorer.exe", "\"" + url + "\""); } @Override diff --git a/jabgui/src/main/java/org/jabref/gui/frame/JabRefFrame.java b/jabgui/src/main/java/org/jabref/gui/frame/JabRefFrame.java index 3a1de359e0f9..a5b5adc96837 100644 --- a/jabgui/src/main/java/org/jabref/gui/frame/JabRefFrame.java +++ b/jabgui/src/main/java/org/jabref/gui/frame/JabRefFrame.java @@ -57,6 +57,7 @@ import org.jabref.logic.UiCommand; import org.jabref.logic.UiMessageHandler; import org.jabref.logic.ai.AiService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.git.util.GitHandlerRegistry; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Localization; @@ -105,6 +106,7 @@ private enum PanelMode { MAIN_TABLE, MAIN_TABLE_AND_ENTRY_EDITOR } private final TaskExecutor taskExecutor; private final GitHandlerRegistry gitHandlerRegistry; private final JournalAbbreviationRepository journalAbbreviationRepository; + private final OAuthSessionRegistry oAuthSessionRegistry; private final JabRefFrameViewModel viewModel; private final GuiPushToApplicationCommand pushToApplicationCommand; @@ -129,7 +131,8 @@ public JabRefFrame(Stage mainStage, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor, GitHandlerRegistry gitHandlerRegistry, - JournalAbbreviationRepository journalAbbreviationRepository) { + JournalAbbreviationRepository journalAbbreviationRepository, + OAuthSessionRegistry oAuthSessionRegistry) { this.mainStage = mainStage; this.dialogService = dialogService; this.fileUpdateMonitor = fileUpdateMonitor; @@ -141,6 +144,7 @@ public JabRefFrame(Stage mainStage, this.taskExecutor = taskExecutor; this.gitHandlerRegistry = gitHandlerRegistry; this.journalAbbreviationRepository = journalAbbreviationRepository; + this.oAuthSessionRegistry = oAuthSessionRegistry; setId("frame"); @@ -250,7 +254,8 @@ private void initLayout() { aiService, entryEditor, gitHandlerRegistry, - journalAbbreviationRepository + journalAbbreviationRepository, + oAuthSessionRegistry ); VBox head = new VBox(0, mainMenu, mainToolBar); diff --git a/jabgui/src/main/java/org/jabref/gui/frame/MainMenu.java b/jabgui/src/main/java/org/jabref/gui/frame/MainMenu.java index 8f7a72307659..97229017e127 100644 --- a/jabgui/src/main/java/org/jabref/gui/frame/MainMenu.java +++ b/jabgui/src/main/java/org/jabref/gui/frame/MainMenu.java @@ -17,6 +17,8 @@ import org.jabref.gui.ai.ClearEmbeddingsAction; import org.jabref.gui.auximport.NewSubLibraryAction; import org.jabref.gui.citationkeypattern.GenerateCitationKeyAction; +import org.jabref.gui.citedrive.CiteDriveLoginAction; +import org.jabref.gui.citedrive.CiteDrivePushAction; import org.jabref.gui.cleanup.CleanupAction; import org.jabref.gui.clipboard.ClipBoardManager; import org.jabref.gui.collab.MergeLibraryAction; @@ -82,6 +84,7 @@ import org.jabref.gui.util.URLs; import org.jabref.gui.util.UiTaskExecutor; import org.jabref.logic.ai.AiService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.git.util.GitHandlerRegistry; import org.jabref.logic.help.HelpFile; import org.jabref.logic.importer.IdFetcher; @@ -114,6 +117,7 @@ public class MainMenu extends MenuBar { private final PreviewControls previewControls; private final GitHandlerRegistry gitHandlerRegistry; private final JournalAbbreviationRepository journalAbbreviationRepository; + private final OAuthSessionRegistry oAuthSessionRegistry; public MainMenu(JabRefFrame frame, FileHistoryMenu fileHistoryMenu, @@ -131,7 +135,8 @@ public MainMenu(JabRefFrame frame, AiService aiService, PreviewControls previewControls, GitHandlerRegistry gitHandlerRegistry, - JournalAbbreviationRepository journalAbbreviationRepository + JournalAbbreviationRepository journalAbbreviationRepository, + OAuthSessionRegistry oAuthSessionRegistry ) { this.frame = frame; this.fileHistoryMenu = fileHistoryMenu; @@ -150,6 +155,7 @@ public MainMenu(JabRefFrame frame, this.previewControls = previewControls; this.gitHandlerRegistry = gitHandlerRegistry; this.journalAbbreviationRepository = journalAbbreviationRepository; + this.oAuthSessionRegistry = oAuthSessionRegistry; createMenu(); } @@ -197,6 +203,7 @@ private void createMenu() { new SeparatorMenuItem(), factory.createMenuItem(StandardActions.GIT_SHARE, new GitShareToGitHubAction(dialogService, stateManager)) ), + citeDriveMenu(factory), factory.createSubMenu(StandardActions.REMOTE_DB, factory.createMenuItem(StandardActions.CONNECT_TO_SHARED_DB, new ConnectToSharedDatabaseCommand(frame, dialogService))), // endregion @@ -438,4 +445,13 @@ private Menu createSendSubMenu(ActionFactory factory, return sendMenu; } + + /// CiteDrive is opt-in: without it the menu is not shown at all + private Menu citeDriveMenu(ActionFactory factory) { + Menu citeDrive = factory.createSubMenu(StandardActions.CITE_DRIVE, + factory.createMenuItem(StandardActions.CITE_DRIVE_PUSH, new CiteDrivePushAction(dialogService, stateManager, preferences, oAuthSessionRegistry)), + factory.createMenuItem(StandardActions.CITE_DRIVE_LOGIN, new CiteDriveLoginAction(dialogService, preferences, oAuthSessionRegistry))); + citeDrive.visibleProperty().bind(preferences.getCiteDrivePreferences().enabledProperty()); + return citeDrive; + } } diff --git a/jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java b/jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java index 78235f05ba11..85a727505c8b 100644 --- a/jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java +++ b/jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java @@ -25,6 +25,7 @@ import org.jabref.gui.preferences.export.ExportTab; import org.jabref.gui.preferences.external.ExternalTab; import org.jabref.gui.preferences.externalfiletypes.ExternalFileTypesTab; +import org.jabref.gui.preferences.externalstorages.ExternalStoragesTab; import org.jabref.gui.preferences.general.GeneralTab; import org.jabref.gui.preferences.git.GitTab; import org.jabref.gui.preferences.groups.GroupsTab; @@ -98,6 +99,7 @@ public PreferencesDialogViewModel(DialogService dialogService, GuiPreferences pr new CustomImporterTab(), new CustomExporterTab(), new NetworkTab(), + new ExternalStoragesTab(), new GitTab() ); } diff --git a/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTab.java b/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTab.java new file mode 100644 index 000000000000..03843434bc15 --- /dev/null +++ b/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTab.java @@ -0,0 +1,37 @@ +package org.jabref.gui.preferences.externalstorages; + +import org.jabref.gui.icon.IconTheme; +import org.jabref.gui.preferences.AbstractPreferenceTabView; +import org.jabref.logic.l10n.Localization; + +import org.jspecify.annotations.NullMarked; + +/// Services a library can be sent to, currently CiteDrive. +@NullMarked +public class ExternalStoragesTab extends AbstractPreferenceTabView { + + public ExternalStoragesTab() { + this.viewModel = new ExternalStoragesTabViewModel(preferences.getCiteDrivePreferences()); + buildView(); + } + + @Override + public String getTabName() { + return Localization.lang("External storages"); + } + + private void buildView() { + setContent(form() + .section(Localization.lang("CiteDrive"), citeDrive -> citeDrive + .checkbox(Localization.lang("Enable CiteDrive"), viewModel.citeDriveEnabledProperty()) + .group(settings -> settings + .checkbox(Localization.lang("Remember the login between sessions"), viewModel.rememberCiteDriveLoginProperty()) + .stringField(Localization.lang("Server address"), viewModel.citeDriveApiBaseUrlProperty(), + field -> field.validate(viewModel.apiBaseUrlValidationStatus())) + .stringField(Localization.lang("Web address"), viewModel.citeDriveAppBaseUrlProperty(), + field -> field.validate(viewModel.appBaseUrlValidationStatus())) + .button(Localization.lang("Reset to default"), IconTheme.JabRefIcons.REFRESH, viewModel::resetCiteDriveAddresses), + settingsGroup -> settingsGroup.disableWhen(viewModel.citeDriveEnabledProperty().not()))) + .build()); + } +} diff --git a/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModel.java b/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModel.java new file mode 100644 index 000000000000..2a72ef93d2c9 --- /dev/null +++ b/jabgui/src/main/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModel.java @@ -0,0 +1,99 @@ +package org.jabref.gui.preferences.externalstorages; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +import org.jabref.gui.preferences.PreferenceTabViewModel; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.net.CiteDrivePreferences; + +import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator; +import de.saxsys.mvvmfx.utils.validation.ValidationMessage; +import de.saxsys.mvvmfx.utils.validation.ValidationStatus; +import de.saxsys.mvvmfx.utils.validation.Validator; +import org.jspecify.annotations.NullMarked; + +@NullMarked +public class ExternalStoragesTabViewModel implements PreferenceTabViewModel { + + private final BooleanProperty citeDriveEnabledProperty = new SimpleBooleanProperty(); + private final BooleanProperty rememberCiteDriveLoginProperty = new SimpleBooleanProperty(); + private final StringProperty citeDriveApiBaseUrlProperty = new SimpleStringProperty(""); + private final StringProperty citeDriveAppBaseUrlProperty = new SimpleStringProperty(""); + + private final CiteDrivePreferences citeDrivePreferences; + + private final Validator apiBaseUrlValidator; + private final Validator appBaseUrlValidator; + + public ExternalStoragesTabViewModel(CiteDrivePreferences citeDrivePreferences) { + this.citeDrivePreferences = citeDrivePreferences; + + apiBaseUrlValidator = urlValidator(citeDriveApiBaseUrlProperty, Localization.lang("Server address")); + appBaseUrlValidator = urlValidator(citeDriveAppBaseUrlProperty, Localization.lang("Web address")); + } + + private static Validator urlValidator(StringProperty url, String fieldName) { + return new FunctionBasedValidator<>( + url, + CiteDrivePreferences::isSecureUrl, + ValidationMessage.error("%s > %s %n %n %s".formatted( + Localization.lang("External storages"), + fieldName, + Localization.lang("Please enter a valid HTTPS URL")))); + } + + @Override + public void setValues() { + citeDriveEnabledProperty.setValue(citeDrivePreferences.isEnabled()); + rememberCiteDriveLoginProperty.setValue(citeDrivePreferences.shouldPersistRefreshToken()); + citeDriveApiBaseUrlProperty.setValue(citeDrivePreferences.getApiBaseUrl()); + citeDriveAppBaseUrlProperty.setValue(citeDrivePreferences.getAppBaseUrl()); + } + + @Override + public void storeSettings() { + citeDrivePreferences.setEnabled(citeDriveEnabledProperty.getValue()); + citeDrivePreferences.setPersistRefreshToken(rememberCiteDriveLoginProperty.getValue()); + citeDrivePreferences.setApiBaseUrl(citeDriveApiBaseUrlProperty.getValue().trim()); + citeDrivePreferences.setAppBaseUrl(citeDriveAppBaseUrlProperty.getValue().trim()); + } + + @Override + public boolean validateSettings() { + return apiBaseUrlValidator.getValidationStatus().isValid() && appBaseUrlValidator.getValidationStatus().isValid(); + } + + /// Back to JabRef's own CiteDrive addresses + public void resetCiteDriveAddresses() { + CiteDrivePreferences defaults = CiteDrivePreferences.getDefault(); + citeDriveApiBaseUrlProperty.setValue(defaults.getApiBaseUrl()); + citeDriveAppBaseUrlProperty.setValue(defaults.getAppBaseUrl()); + } + + public BooleanProperty citeDriveEnabledProperty() { + return citeDriveEnabledProperty; + } + + public BooleanProperty rememberCiteDriveLoginProperty() { + return rememberCiteDriveLoginProperty; + } + + public StringProperty citeDriveApiBaseUrlProperty() { + return citeDriveApiBaseUrlProperty; + } + + public StringProperty citeDriveAppBaseUrlProperty() { + return citeDriveAppBaseUrlProperty; + } + + public ValidationStatus apiBaseUrlValidationStatus() { + return apiBaseUrlValidator.getValidationStatus(); + } + + public ValidationStatus appBaseUrlValidationStatus() { + return appBaseUrlValidator.getValidationStatus(); + } +} diff --git a/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTab.java b/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTab.java index b7018cf887db..a746f6752c04 100644 --- a/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTab.java +++ b/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTab.java @@ -16,9 +16,10 @@ import org.jabref.gui.theme.ThemePreviewView; import org.jabref.gui.util.URLs; import org.jabref.gui.util.ViewModelListCellFactory; -import org.jabref.http.manager.HttpServerManager; +import org.jabref.http.server.manager.HttpServerManager; import org.jabref.languageserver.controller.LanguageServerController; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.help.HelpFile; import org.jabref.logic.l10n.Language; import org.jabref.logic.l10n.Localization; @@ -46,6 +47,7 @@ public GeneralTab() { Injector.instantiateModelOrService(HttpServerManager.class), Injector.instantiateModelOrService(LanguageServerController.class), Injector.instantiateModelOrService(UiMessageHandler.class), + Injector.instantiateModelOrService(OAuthSessionRegistry.class), Injector.instantiateModelOrService(RemoteListenerServerManager.class), Injector.instantiateModelOrService(StateManager.class)); buildView(); diff --git a/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java b/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java index 469ab1f7acb7..37ce9eff1b39 100644 --- a/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java +++ b/jabgui/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java @@ -33,11 +33,12 @@ import org.jabref.gui.theme.ThemePreset; import org.jabref.gui.util.DirectoryDialogConfiguration; import org.jabref.gui.util.FileDialogConfiguration; -import org.jabref.http.manager.HttpServerManager; +import org.jabref.http.server.manager.HttpServerManager; import org.jabref.languageserver.controller.LanguageServerController; import org.jabref.logic.FilePreferences; import org.jabref.logic.LibraryPreferences; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.l10n.Language; import org.jabref.logic.l10n.Localization; import org.jabref.logic.net.ssl.TrustStoreManager; @@ -106,6 +107,7 @@ public class GeneralTabViewModel implements PreferenceTabViewModel { private final HttpServerManager httpServerManager; private final LanguageServerController languageServerController; private final UiMessageHandler uiMessageHandler; + private final OAuthSessionRegistry oAuthSessionRegistry; private final RemoteListenerServerManager remoteListenerServerManager; private final StateManager stateManager; @@ -130,6 +132,7 @@ public GeneralTabViewModel(DialogService dialogService, HttpServerManager httpServerManager, LanguageServerController languageServerController, UiMessageHandler uiMessageHandler, + OAuthSessionRegistry oAuthSessionRegistry, RemoteListenerServerManager remoteListenerServerManager, StateManager stateManager) { this.dialogService = dialogService; @@ -142,6 +145,7 @@ public GeneralTabViewModel(DialogService dialogService, this.httpServerManager = httpServerManager; this.languageServerController = languageServerController; this.uiMessageHandler = uiMessageHandler; + this.oAuthSessionRegistry = oAuthSessionRegistry; this.remoteListenerServerManager = remoteListenerServerManager; this.stateManager = stateManager; @@ -328,7 +332,8 @@ public void storeSettings() { if (enableHttpServerProperty.getValue()) { remotePreferences.setEnableHttpServer(true); URI uri = remotePreferences.getHttpServerUri(); - httpServerManager.start(preferences, stateManager, uiMessageHandler, uri); + + httpServerManager.start(preferences, stateManager, uiMessageHandler, oAuthSessionRegistry, uri); } else { remotePreferences.setEnableHttpServer(false); httpServerManager.stop(); diff --git a/jabgui/src/main/resources/tinylog.properties b/jabgui/src/main/resources/tinylog.properties index 49a1158dfbff..bdd4d2c55f68 100644 --- a/jabgui/src/main/resources/tinylog.properties +++ b/jabgui/src/main/resources/tinylog.properties @@ -2,7 +2,7 @@ level = info writer = gui writerConsole = console -writerConsole.format = {date} [{thread}] {class}.{method}()\n{level}: {message}\n +writerConsole.format = {date} [{thread}] {class}.{method}({file}:{line})\n{level}: {message} # More shrunk exception logs. See https://tinylog.org/v2/configuration/#strip-stack-trace-elements for details exception = strip: jdk.internal @@ -20,3 +20,5 @@ level@org.freedesktop.dbus.connections.transports.TransportBuilder = warn level@org.jabref.logic.externalfiles.LinkedFileTransferHelper = debug #level@org.jabref.gui.JabRefGUI = debug + +level@org.jabref.logic.citedrive.CiteDrivePush = debug diff --git a/jabgui/src/test/java/org/jabref/gui/citedrive/CiteDriveOAuthTest.java b/jabgui/src/test/java/org/jabref/gui/citedrive/CiteDriveOAuthTest.java new file mode 100644 index 000000000000..cce52c020759 --- /dev/null +++ b/jabgui/src/test/java/org/jabref/gui/citedrive/CiteDriveOAuthTest.java @@ -0,0 +1,162 @@ +package org.jabref.gui.citedrive; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; +import java.util.TreeSet; +import java.util.concurrent.ExecutionException; + +import javafx.collections.FXCollections; + +import org.jabref.gui.DialogService; +import org.jabref.gui.desktop.os.NativeDesktop; +import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.gui.frame.ExternalApplicationsPreferences; +import org.jabref.http.SrvStateManager; +import org.jabref.http.server.manager.HttpServerManager; +import org.jabref.logic.citedrive.CiteDriveOAuthService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; +import org.jabref.logic.importer.util.MediaTypes; +import org.jabref.logic.net.CiteDrivePreferences; +import org.jabref.logic.preferences.CliPreferences; +import org.jabref.logic.remote.RemotePreferences; + +import com.nimbusds.oauth2.sdk.token.AccessToken; +import kong.unirest.core.HttpResponse; +import kong.unirest.core.Unirest; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CiteDriveOAuthTest { + + private static final HttpServerManager HTTP_SERVER_MANAGER = new HttpServerManager(); + private static final OAuthSessionRegistry OAUTH_SESSION_REGISTRY = new OAuthSessionRegistry(); + + @BeforeAll + static void startServer() throws URISyntaxException { + CliPreferences cliPreferences = mock(CliPreferences.class); + SrvStateManager srvStateManager = mock(SrvStateManager.class); + URI uri = new URI("http://localhost:23119"); + HTTP_SERVER_MANAGER.start(cliPreferences, srvStateManager, OAUTH_SESSION_REGISTRY, uri); + } + + @AfterAll + static void stopServer() { + HTTP_SERVER_MANAGER.stop(); + } + + @Test + @Timeout(60) + @Disabled + void getToken() throws ExecutionException, InterruptedException { + ExternalApplicationsPreferences externalApplicationsPreferences = mock(ExternalApplicationsPreferences.class); + when(externalApplicationsPreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); + + RemotePreferences remotePreferences = mock(RemotePreferences.class); + when(remotePreferences.getHttpServerUri()).thenReturn(URI.create("http://localhost:23119")); + when(remotePreferences.shouldEnableHttpServer()).thenReturn(true); + + CiteDrivePreferences citeDrivePreferences = CiteDrivePreferences.getDefault(); + + CiteDriveOAuthService citeDriveOAuthService = new CiteDriveOAuthService(remotePreferences, citeDrivePreferences, OAUTH_SESSION_REGISTRY, uri -> NativeDesktop.openBrowserShowPopup(uri.toASCIIString(), mock(DialogService.class), externalApplicationsPreferences), () -> true, URI.create("https://api-dev.citedrive.com/jabref/login/"), URI.create("https://api-dev.citedrive.com/o/token/")); + + Optional actual = citeDriveOAuthService.authorizeInteractive().get(); + assertTrue(actual.isPresent()); + } + + /// Fetches a token and sends some BibTeX to CiteDrive + @Test + @Timeout(60) + @Disabled + void putLibraryOAuthFromScratch() throws ExecutionException, InterruptedException, IOException { + ExternalApplicationsPreferences externalApplicationsPreferences = mock(ExternalApplicationsPreferences.class); + when(externalApplicationsPreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); + + RemotePreferences remotePreferences = mock(RemotePreferences.class); + when(remotePreferences.getHttpServerUri()).thenReturn(URI.create("http://localhost:23119")); + when(remotePreferences.shouldEnableHttpServer()).thenReturn(true); + + CiteDrivePreferences citeDrivePreferences = CiteDrivePreferences.getDefault(); + + CiteDriveOAuthService citeDriveOAuthService = new CiteDriveOAuthService(remotePreferences, citeDrivePreferences, OAUTH_SESSION_REGISTRY, uri -> NativeDesktop.openBrowserShowPopup(uri.toASCIIString(), mock(DialogService.class), externalApplicationsPreferences), () -> true, URI.create("https://api-dev.citedrive.com/jabref/login/"), URI.create("https://api-dev.citedrive.com/o/token/")); + + Optional actual = citeDriveOAuthService.authorizeInteractive().get(); + assertTrue(actual.isPresent()); + + String bibtex = """ + @article{xyz, + author = {Doe, John and Smith, Jane}, + title = {An Example Article}, + journal = {Journal of Examples}, + year = {2024}, + volume = {42}, + number = {1}, + pages = {1-10}, + publisher = {Example Publisher} + } + """; + + HttpResponse response = Unirest.post("https://api-dev.citedrive.com/jabref/push/") + .header("Authorization", "Bearer " + actual.get()) + .header("Content-Type", MediaTypes.APPLICATION_BIBTEX) + .body(bibtex) + .asString(); + + assertEquals(200, response.getStatus()); + // Example response: {"file_size":225,"file_size_kb":0.22,"entry_count":1,"status":"success"} + assertNotEquals("", response.getBody()); + } + + /// Uses the "intended" use of CiteDriveOAuthService w/ token refresh + @Test + @Timeout(60) + @Disabled + void putLibrary() throws ExecutionException, InterruptedException, IOException { + ExternalApplicationsPreferences externalApplicationsPreferences = mock(ExternalApplicationsPreferences.class); + when(externalApplicationsPreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); + + RemotePreferences remotePreferences = mock(RemotePreferences.class); + when(remotePreferences.getHttpServerUri()).thenReturn(URI.create("http://localhost:23119")); + when(remotePreferences.shouldEnableHttpServer()).thenReturn(true); + + CiteDrivePreferences citeDrivePreferences = CiteDrivePreferences.getDefault(); + + CiteDriveOAuthService citeDriveOAuthService = new CiteDriveOAuthService(remotePreferences, citeDrivePreferences, OAUTH_SESSION_REGISTRY, uri -> NativeDesktop.openBrowserShowPopup(uri.toASCIIString(), mock(DialogService.class), externalApplicationsPreferences), () -> true, URI.create("https://api-dev.citedrive.com/jabref/login/"), URI.create("https://api-dev.citedrive.com/o/token/")); + + Optional actual = citeDriveOAuthService.getAccessToken().get(); + assertTrue(actual.isPresent()); + + String bibtex = """ + @article{xyz, + author = {Doe, John and Smith, Jane}, + title = {An Example Article}, + journal = {Journal of Examples}, + year = {2024}, + volume = {42}, + number = {1}, + pages = {1-10}, + publisher = {Example Publisher} + } + """; + + HttpResponse response = Unirest.post("https://api-dev.citedrive.com/jabref/push/") + .header("Authorization", "Bearer " + actual.get()) + .header("Content-Type", MediaTypes.APPLICATION_BIBTEX) + .body(bibtex) + .asString(); + + assertEquals(200, response.getStatus()); + // Example response: {"file_size":225,"file_size_kb":0.22,"entry_count":1,"status":"success"} + assertNotEquals("", response.getBody()); + } +} diff --git a/jabgui/src/test/java/org/jabref/gui/desktop/os/NativeDesktopTest.java b/jabgui/src/test/java/org/jabref/gui/desktop/os/NativeDesktopTest.java index a8343bafbfef..a2c9e63f294f 100644 --- a/jabgui/src/test/java/org/jabref/gui/desktop/os/NativeDesktopTest.java +++ b/jabgui/src/test/java/org/jabref/gui/desktop/os/NativeDesktopTest.java @@ -69,7 +69,7 @@ void openBrowserPassesFullUrlToCustomBrowser() throws IOException, InterruptedEx ExternalApplicationsPreferences preferences = mock(ExternalApplicationsPreferences.class); when(preferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(htmlType)); - NativeDesktop.openBrowser(URL_WITH_QUERY, preferences); + NativeDesktop.openBrowser(URL_WITH_QUERY, preferences, FakeDesktop.NO_FAILURE_EXPECTED, new Windows()); assertEquals(URL_WITH_QUERY, recordedArgument()); } diff --git a/jabgui/src/test/java/org/jabref/gui/desktop/os/WindowsTest.java b/jabgui/src/test/java/org/jabref/gui/desktop/os/WindowsTest.java new file mode 100644 index 000000000000..afd0061fa4fa --- /dev/null +++ b/jabgui/src/test/java/org/jabref/gui/desktop/os/WindowsTest.java @@ -0,0 +1,21 @@ +package org.jabref.gui.desktop.os; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class WindowsTest { + + @Test + void shortUrlGoesToExplorer() { + assertEquals(List.of("explorer.exe", "\"https://example.com/\""), Windows.urlOpenCommand("https://example.com/")); + } + + @Test + void longUrlGoesToTheProtocolHandler() { + String url = "https://example.com/?state=" + "x".repeat(300); + assertEquals(List.of("rundll32.exe", "url.dll,FileProtocolHandler", url), Windows.urlOpenCommand(url)); + } +} diff --git a/jabgui/src/test/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModelTest.java b/jabgui/src/test/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModelTest.java new file mode 100644 index 000000000000..d16c17f9e039 --- /dev/null +++ b/jabgui/src/test/java/org/jabref/gui/preferences/externalstorages/ExternalStoragesTabViewModelTest.java @@ -0,0 +1,26 @@ +package org.jabref.gui.preferences.externalstorages; + +import org.jabref.logic.net.CiteDrivePreferences; + +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.parallel.ResourceLock; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +@NullMarked +@ResourceLock("Localization.lang") +class ExternalStoragesTabViewModelTest { + + @ParameterizedTest + @ValueSource(strings = {"http://api.example/", "http://127.0.0.1:8080/"}) + void rejectsUnencryptedApiAddress(String address) { + ExternalStoragesTabViewModel viewModel = new ExternalStoragesTabViewModel(CiteDrivePreferences.getDefault()); + viewModel.setValues(); + + viewModel.citeDriveApiBaseUrlProperty().set(address); + + assertFalse(viewModel.validateSettings()); + } +} diff --git a/jabgui/src/test/java/org/jabref/gui/preferences/general/GeneralTabViewModelTest.java b/jabgui/src/test/java/org/jabref/gui/preferences/general/GeneralTabViewModelTest.java index f93e38341279..fd0a18d0419d 100644 --- a/jabgui/src/test/java/org/jabref/gui/preferences/general/GeneralTabViewModelTest.java +++ b/jabgui/src/test/java/org/jabref/gui/preferences/general/GeneralTabViewModelTest.java @@ -7,9 +7,10 @@ import org.jabref.gui.preferences.GuiPreferences; import org.jabref.gui.theme.ThemeColorScheme; import org.jabref.gui.theme.ThemePreset; -import org.jabref.http.manager.HttpServerManager; +import org.jabref.http.server.manager.HttpServerManager; import org.jabref.languageserver.controller.LanguageServerController; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.remote.server.RemoteListenerServerManager; import org.junit.jupiter.api.Test; @@ -50,6 +51,7 @@ private GeneralTabViewModel createViewModel(Path tempDir) { mock(HttpServerManager.class), mock(LanguageServerController.class), mock(UiMessageHandler.class), + mock(OAuthSessionRegistry.class), mock(RemoteListenerServerManager.class), mock(StateManager.class)); } diff --git a/jablib-examples/jbang/doi_to_bibtex.java b/jablib-examples/jbang/doi_to_bibtex.java index 6729856480f8..fab39cf3a3a8 100644 --- a/jablib-examples/jbang/doi_to_bibtex.java +++ b/jablib-examples/jbang/doi_to_bibtex.java @@ -16,9 +16,11 @@ //FILES tinylog.properties=tinylog.properties //REPOS mavencentral,mavencentralsnapshots=https://central.sonatype.com/repository/maven-snapshots/ -//DEPS org.jabref:jablib:6.0-SNAPSHOT // JabRef relies on PR https://github.com/unicode-org/icu/pull/2127; for experiments the release version is OK. //DEPS com.ibm.icu:icu4j:78.1 +//DEPS com.nimbusds:oauth2-oidc-sdk:11.30.1 +//DEPS net.minidev:json-smart:2.6.0 +//DEPS org.jabref:jablib:6.0-SNAPSHOT void main() throws Exception { var preferences = JabRefCliPreferences.getInstance(); diff --git a/jablib-examples/jbang/ieee_pdf_references_to_bibtex.java b/jablib-examples/jbang/ieee_pdf_references_to_bibtex.java index c183dea4a410..45e79ffdffa2 100644 --- a/jablib-examples/jbang/ieee_pdf_references_to_bibtex.java +++ b/jablib-examples/jbang/ieee_pdf_references_to_bibtex.java @@ -15,10 +15,12 @@ //FILES tinylog.properties=tinylog.properties //REPOS mavencentral,mavencentralsnapshots=https://central.sonatype.com/repository/maven-snapshots/ -//DEPS org.jabref:jablib:6.0-SNAPSHOT +//DEPS com.fasterxml.jackson.core:jackson-annotations:2.20 // JabRef relies on PR https://github.com/unicode-org/icu/pull/2127; for experiments the release version is OK. //DEPS com.ibm.icu:icu4j:78.1 -//DEPS com.fasterxml.jackson.core:jackson-annotations:2.20 +//DEPS com.nimbusds:oauth2-oidc-sdk:11.30.1 +//DEPS net.minidev:json-smart:2.6.0 +//DEPS org.jabref:jablib:6.0-SNAPSHOT //DEPS tools.jackson.core:jackson-core:3.0.2 //DEPS tools.jackson.core:jackson-databind:3.0.2 //DEPS tools.jackson.dataformat:jackson-dataformat-yaml:3.0.2 diff --git a/jablib/src/main/java/module-info.java b/jablib/src/main/java/module-info.java index b5b0d0e2c924..a5f546823e0f 100644 --- a/jablib/src/main/java/module-info.java +++ b/jablib/src/main/java/module-info.java @@ -131,6 +131,7 @@ exports org.jabref.logic.git.merge.execution; exports org.jabref.model.sciteTallies; exports org.jabref.logic.bibtex.comparator.plausibility; + exports org.jabref.logic.citedrive; exports org.jabref.logic.ocr; // region: AI @@ -232,6 +233,11 @@ requires /*runtime*/ unirest.modules.gson; // endregion + // region nimbusds / OAuth2 + requires json.smart; + requires transitive nimbusds.oauth2.oidc.sdk; + // endregion + // region: SQL databases requires embedded.postgres; diff --git a/jablib/src/main/java/org/jabref/logic/citedrive/CiteDriveOAuthService.java b/jablib/src/main/java/org/jabref/logic/citedrive/CiteDriveOAuthService.java new file mode 100644 index 000000000000..ee1b8213a184 --- /dev/null +++ b/jablib/src/main/java/org/jabref/logic/citedrive/CiteDriveOAuthService.java @@ -0,0 +1,294 @@ +package org.jabref.logic.citedrive; + +import java.io.IOException; +import java.net.URI; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.function.BooleanSupplier; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.jabref.logic.JabRefException; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.net.CiteDrivePreferences; +import org.jabref.logic.remote.RemotePreferences; + +import com.google.common.annotations.VisibleForTesting; +import com.nimbusds.oauth2.sdk.AccessTokenResponse; +import com.nimbusds.oauth2.sdk.AuthorizationCode; +import com.nimbusds.oauth2.sdk.AuthorizationCodeGrant; +import com.nimbusds.oauth2.sdk.AuthorizationGrant; +import com.nimbusds.oauth2.sdk.AuthorizationRequest; +import com.nimbusds.oauth2.sdk.OAuth2Error; +import com.nimbusds.oauth2.sdk.ParseException; +import com.nimbusds.oauth2.sdk.RefreshTokenGrant; +import com.nimbusds.oauth2.sdk.ResponseType; +import com.nimbusds.oauth2.sdk.Scope; +import com.nimbusds.oauth2.sdk.TokenErrorResponse; +import com.nimbusds.oauth2.sdk.TokenRequest; +import com.nimbusds.oauth2.sdk.TokenResponse; +import com.nimbusds.oauth2.sdk.http.HTTPRequest; +import com.nimbusds.oauth2.sdk.http.HTTPResponse; +import com.nimbusds.oauth2.sdk.id.ClientID; +import com.nimbusds.oauth2.sdk.id.State; +import com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod; +import com.nimbusds.oauth2.sdk.pkce.CodeVerifier; +import com.nimbusds.oauth2.sdk.token.AccessToken; +import com.nimbusds.oauth2.sdk.token.RefreshToken; +import org.jspecify.annotations.NullMarked; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/// OAuth 2.0 authorization code flow with PKCE against CiteDrive. +/// +/// The browser redirects back to JabRef's HTTP server ([org.jabref.logic.citedrive.OAuthSessionRegistry] receives the code). +@NullMarked +public class CiteDriveOAuthService { + + private static final Logger LOGGER = LoggerFactory.getLogger(CiteDriveOAuthService.class); + + private static final ClientID CLIENT_ID = new ClientID("jabref-desktop"); + private static final int CONNECT_TIMEOUT_MILLIS = 10_000; + private static final int READ_TIMEOUT_MILLIS = 30_000; + + private final Supplier authEndpoint; + private final Supplier tokenEndpoint; + private final TokenRequestSender tokenRequestSender; + + private final RemotePreferences remotePreferences; + private final CiteDrivePreferences citeDrivePreferences; + private final OAuthSessionRegistry sessionRegistry; + private final Consumer authorizationPageOpener; + private final BooleanSupplier callbackServerEnabler; + + /// @param authorizationPageOpener shows the CiteDrive login page to the user (in the browser) + /// @param callbackServerEnabler makes the HTTP server receiving the browser redirect run; false if it stays off + public CiteDriveOAuthService(RemotePreferences remotePreferences, + CiteDrivePreferences citeDrivePreferences, + OAuthSessionRegistry sessionRegistry, + Consumer authorizationPageOpener, + BooleanSupplier callbackServerEnabler) { + this(remotePreferences, citeDrivePreferences, sessionRegistry, authorizationPageOpener, callbackServerEnabler, + citeDrivePreferences::getAuthorizationEndpoint, citeDrivePreferences::getTokenEndpoint, HTTPRequest::send); + } + + @VisibleForTesting + public CiteDriveOAuthService(RemotePreferences remotePreferences, + CiteDrivePreferences citeDrivePreferences, + OAuthSessionRegistry sessionRegistry, + Consumer authorizationPageOpener, + BooleanSupplier callbackServerEnabler, + URI authEndpoint, + URI tokenEndpoint) { + this(remotePreferences, citeDrivePreferences, sessionRegistry, authorizationPageOpener, callbackServerEnabler, + () -> authEndpoint, () -> tokenEndpoint, HTTPRequest::send); + } + + @VisibleForTesting + CiteDriveOAuthService(RemotePreferences remotePreferences, + CiteDrivePreferences citeDrivePreferences, + OAuthSessionRegistry sessionRegistry, + Consumer authorizationPageOpener, + BooleanSupplier callbackServerEnabler, + TokenRequestSender tokenRequestSender) { + this(remotePreferences, citeDrivePreferences, sessionRegistry, authorizationPageOpener, callbackServerEnabler, + citeDrivePreferences::getAuthorizationEndpoint, citeDrivePreferences::getTokenEndpoint, tokenRequestSender); + } + + private CiteDriveOAuthService(RemotePreferences remotePreferences, + CiteDrivePreferences citeDrivePreferences, + OAuthSessionRegistry sessionRegistry, + Consumer authorizationPageOpener, + BooleanSupplier callbackServerEnabler, + Supplier authEndpoint, + Supplier tokenEndpoint, + TokenRequestSender tokenRequestSender) { + this.remotePreferences = remotePreferences; + this.citeDrivePreferences = citeDrivePreferences; + this.sessionRegistry = sessionRegistry; + this.authorizationPageOpener = authorizationPageOpener; + this.callbackServerEnabler = callbackServerEnabler; + this.authEndpoint = authEndpoint; + this.tokenEndpoint = tokenEndpoint; + this.tokenRequestSender = tokenRequestSender; + } + + /// Lets the user log in in the browser. Also stores the refresh token. + /// + /// [impl->req~citedrive.login~1] + /// + /// Fails if the HTTP server receiving the browser redirect is disabled, or if the user does not finish logging in in time. + public CompletableFuture> authorizeInteractive() { + return authorizeInteractive(snapshotConfiguration()).thenApply(session -> session.map(AuthorizedSession::accessToken)); + } + + private CompletableFuture> authorizeInteractive(ServerConfiguration configuration) { + if (!configuration.isSecure() || configuration.revision() != citeDrivePreferences.getServerRevision()) { + return CompletableFuture.failedFuture(loginFailure("Invalid or changed CiteDrive server configuration")); + } + if (!remotePreferences.shouldEnableHttpServer() && !callbackServerEnabler.getAsBoolean()) { + return CompletableFuture.failedFuture(new JabRefException( + "HTTP server disabled", + Localization.lang("Logging in to CiteDrive needs the HTTP server. Please enable it in the preferences."))); + } + + String state = UUID.randomUUID().toString(); + // One verifier per login attempt: a second attempt must not invalidate the code of the first one + CodeVerifier codeVerifier = new CodeVerifier(); + CompletableFuture codeFuture = sessionRegistry.register(state); + + URI authUrl = new AuthorizationRequest.Builder(ResponseType.CODE, CLIENT_ID) + .endpointURI(configuration.authEndpoint()) + .redirectionURI(configuration.callbackUri()) + .state(new State(state)) + .scope(new Scope("read", "write")) // required for CiteDrive API + .codeChallenge(codeVerifier, CodeChallengeMethod.S256) + .build() + .toURI(); + authorizationPageOpener.accept(authUrl); + + return codeFuture.thenApplyAsync(code -> exchangeCodeForToken(code, codeVerifier, configuration)); + } + + private URI getCallBackUri() { + return remotePreferences.getHttpServerUri().resolve("/callback"); + } + + /// Also stores the refresh token + private Optional exchangeCodeForToken(String code, CodeVerifier codeVerifier, ServerConfiguration configuration) { + if (configuration.revision() != citeDrivePreferences.getServerRevision()) { + return Optional.empty(); + } + AuthorizationGrant codeGrant = new AuthorizationCodeGrant(new AuthorizationCode(code), configuration.callbackUri(), codeVerifier); + TokenResponse response; + try { + response = sendTokenRequest(configuration.tokenEndpoint(), codeGrant); + } catch (ParseException | IOException e) { + LOGGER.error("Could not obtain CiteDrive token", e); + return Optional.empty(); + } + if (!response.indicatesSuccess()) { + TokenErrorResponse errorResponse = response.toErrorResponse(); + LOGGER.error("Could not receive token {}", errorResponse.getErrorObject().toJSONObject()); + return Optional.empty(); + } + AccessTokenResponse successResponse = response.toSuccessResponse(); + if (!citeDrivePreferences.updateRefreshToken(configuration.revision(), Optional.ofNullable(successResponse.getTokens().getRefreshToken()))) { + return Optional.empty(); + } + return Optional.of(configuration.authorizedSession(successResponse.getTokens().getAccessToken())); + } + + /// Refreshes the access token using the stored refresh token. + /// Goes interactive if there is no refresh token or CiteDrive rejects it. + /// + /// Also stores the refresh token + public CompletableFuture> getAccessToken() { + return getAuthorizedSession().thenApply(session -> session.map(AuthorizedSession::accessToken)); + } + + /// Pins the upload and import addresses to the server that issued the access token. + /// + /// [impl->req~citedrive.credentials.server~1] + public CompletableFuture> getAuthorizedSession() { + ServerConfiguration configuration = snapshotConfiguration(); + if (!configuration.isSecure()) { + return CompletableFuture.failedFuture(loginFailure("CiteDrive requires HTTPS server addresses")); + } + + return sessionRegistry.requestToken(citeDrivePreferences, configuration.revision(), () -> { + Optional cachedRefreshToken; + synchronized (citeDrivePreferences) { + if (configuration.revision() != citeDrivePreferences.getServerRevision()) { + return CompletableFuture.failedFuture(loginFailure("CiteDrive server configuration changed")); + } + cachedRefreshToken = Optional.ofNullable(citeDrivePreferences.getRefreshToken()); + } + return cachedRefreshToken.map(refreshToken -> CompletableFuture.supplyAsync(() -> refreshToken(refreshToken, configuration)) + .thenCompose(session -> session.map(value -> CompletableFuture.completedFuture(Optional.of(value))) + .orElseGet(() -> authorizeInteractive(configuration)))) + .orElseGet(() -> authorizeInteractive(configuration)); + }); + } + + /// @return empty if CiteDrive rejected the refresh token (it is then removed) + /// @throws CompletionException on network or parse errors; the refresh token is kept to retry later + /// + /// [impl->req~citedrive.refresh.failure~1] + private Optional refreshToken(RefreshToken refreshToken, ServerConfiguration configuration) { + if (configuration.revision() != citeDrivePreferences.getServerRevision()) { + throw new CompletionException(loginFailure("CiteDrive server configuration changed")); + } + TokenResponse response; + try { + response = sendTokenRequest(configuration.tokenEndpoint(), new RefreshTokenGrant(refreshToken)); + } catch (IOException | ParseException e) { + LOGGER.warn("Could not refresh CiteDrive token", e); + throw new CompletionException(e); + } + + if (!response.indicatesSuccess()) { + var error = response.toErrorResponse().getErrorObject(); + LOGGER.warn("Refresh token failed: {}", error.toJSONObject()); + if (OAuth2Error.INVALID_GRANT.getCode().equals(error.getCode()) && error.getHTTPStatusCode() == HTTPResponse.SC_BAD_REQUEST) { + citeDrivePreferences.updateRefreshToken(configuration.revision(), Optional.empty()); + return Optional.empty(); + } + throw new CompletionException(loginFailure("CiteDrive token endpoint failed: " + error.getCode())); + } + + AccessTokenResponse successResponse = response.toSuccessResponse(); + Optional newRefreshToken = Optional.ofNullable(successResponse.getTokens().getRefreshToken()).or(() -> Optional.of(refreshToken)); + if (!citeDrivePreferences.updateRefreshToken(configuration.revision(), newRefreshToken)) { + throw new CompletionException(loginFailure("CiteDrive server configuration changed")); + } + return Optional.of(configuration.authorizedSession(successResponse.getTokens().getAccessToken())); + } + + // [impl->req~citedrive.token.timeout~1] + private TokenResponse sendTokenRequest(URI endpoint, AuthorizationGrant grant) throws IOException, ParseException { + HTTPRequest request = new TokenRequest.Builder(endpoint, CLIENT_ID, grant).build().toHTTPRequest(); + request.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); + request.setReadTimeout(READ_TIMEOUT_MILLIS); + request.setFollowRedirects(false); + return TokenResponse.parse(tokenRequestSender.send(request)); + } + + private ServerConfiguration snapshotConfiguration() { + synchronized (citeDrivePreferences) { + return new ServerConfiguration(citeDrivePreferences.getServerRevision(), authEndpoint.get(), tokenEndpoint.get(), + getCallBackUri(), citeDrivePreferences.getPushEndpoint(), citeDrivePreferences.getImportPage()); + } + } + + private static JabRefException loginFailure(String message) { + return new JabRefException(message, Localization.lang("Login could not be completed")); + } + + /// An access token can only be uploaded to the address captured with its authentication operation. + @NullMarked + public record AuthorizedSession(AccessToken accessToken, URI pushEndpoint, URI importPage) { + } + + @FunctionalInterface + @NullMarked + interface TokenRequestSender { + HTTPResponse send(HTTPRequest request) throws IOException; + } + + @NullMarked + private record ServerConfiguration(long revision, URI authEndpoint, URI tokenEndpoint, URI callbackUri, URI pushEndpoint, URI importPage) { + boolean isSecure() { + return CiteDrivePreferences.isSecureUrl(authEndpoint.toString()) + && CiteDrivePreferences.isSecureUrl(tokenEndpoint.toString()) + && CiteDrivePreferences.isSecureUrl(pushEndpoint.toString()) + && CiteDrivePreferences.isSecureUrl(importPage.toString()); + } + + AuthorizedSession authorizedSession(AccessToken accessToken) { + return new AuthorizedSession(accessToken, pushEndpoint, importPage); + } + } +} diff --git a/jablib/src/main/java/org/jabref/logic/citedrive/CiteDrivePush.java b/jablib/src/main/java/org/jabref/logic/citedrive/CiteDrivePush.java new file mode 100644 index 000000000000..199d4848bbc9 --- /dev/null +++ b/jablib/src/main/java/org/jabref/logic/citedrive/CiteDrivePush.java @@ -0,0 +1,92 @@ +package org.jabref.logic.citedrive; + +import java.io.IOException; +import java.io.StringWriter; +import java.net.URI; +import java.util.List; + +import org.jabref.logic.exporter.BibDatabaseWriter; +import org.jabref.logic.exporter.BibWriter; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; +import org.jabref.logic.importer.util.MediaTypes; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.net.CiteDrivePreferences; +import org.jabref.logic.preferences.CliPreferences; +import org.jabref.logic.util.NotificationService; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; + +import com.nimbusds.oauth2.sdk.token.AccessToken; +import kong.unirest.core.HttpResponse; +import kong.unirest.core.Unirest; +import kong.unirest.core.json.JSONException; +import kong.unirest.core.json.JSONObject; +import org.jspecify.annotations.NullMarked; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NullMarked +public class CiteDrivePush { + private static final Logger LOGGER = LoggerFactory.getLogger(CiteDrivePush.class); + + /// [impl->req~citedrive.push~1] + /// + /// Uploads the entries as plain BibTeX: JabRef's metadata (local directories etc.) stays local. + /// + /// @param entries copies of the library's entries: writing applies save actions and key generation to them, + /// which must not touch the live library + /// @return true if CiteDrive accepted the library + public static boolean push(BibDatabaseContext context, List entries, AccessToken accessToken, CliPreferences cliPreferences, NotificationService notificationService) throws IOException { + return push(context, entries, accessToken, cliPreferences, notificationService, cliPreferences.getCiteDrivePreferences().getPushEndpoint()); + } + + public static boolean push(BibDatabaseContext context, List entries, AccessToken accessToken, CliPreferences cliPreferences, NotificationService notificationService, URI pushEndpoint) throws IOException { + if (!CiteDrivePreferences.isSecureUrl(pushEndpoint.toString())) { + throw new IOException(Localization.lang("Please enter a valid HTTPS URL")); + } + StringWriter writer = new StringWriter(); + SelfContainedSaveConfiguration exportConfiguration = cliPreferences.getSelfContainedExportConfiguration(); + SelfContainedSaveConfiguration plainBibtex = new SelfContainedSaveConfiguration( + exportConfiguration.getSelfContainedSaveOrder(), false, BibDatabaseWriter.SaveType.PLAIN_BIBTEX, exportConfiguration.shouldReformatFile()); + new BibDatabaseWriter( + new BibWriter(writer, context.getDatabase().getNewLineSeparator()), + plainBibtex, + cliPreferences.getFieldPreferences(), + cliPreferences.getCitationKeyPatternPreferences(), + cliPreferences.getCustomEntryTypesRepository()) + .writePartOfDatabase(context, entries); + + HttpResponse httpResponse = Unirest.post(pushEndpoint.toASCIIString()) + .header("Authorization", accessToken.toAuthorizationHeader()) + .header("Content-Type", MediaTypes.APPLICATION_BIBTEX) + .body(writer.toString()) + .asString(); + httpResponse.ifFailure(response -> { + LOGGER.error("CiteDrive push failed: {} - {}", response.getStatus(), response.getBody()); + notificationService.notify(Localization.lang("CiteDrive push failed: %0", response.getStatus() + " " + response.getBody())); + }); + httpResponse.ifSuccess(response -> { + String body = response.getBody(); + String message; + try { + JSONObject jsonResponse = new JSONObject(body); + int entryCount = jsonResponse.optInt("entry_count", 0); + double fileSizeKb = jsonResponse.optDouble("file_size_kb", 0.0); + + if (entryCount > 0) { + message = Localization.lang("CiteDrive push succeeded: %0 entries (%1 KB)", + String.valueOf(entryCount), + String.valueOf(fileSizeKb)); + } else { + message = Localization.lang("CiteDrive push succeeded: %0 KB", + String.valueOf(fileSizeKb)); + } + } catch (JSONException e) { + LOGGER.warn("Could not parse CiteDrive push response: {}", body, e); + message = Localization.lang("CiteDrive push succeeded."); + } + notificationService.notify(message); + }); + return httpResponse.isSuccess(); + } +} diff --git a/jablib/src/main/java/org/jabref/logic/citedrive/OAuthSessionRegistry.java b/jablib/src/main/java/org/jabref/logic/citedrive/OAuthSessionRegistry.java new file mode 100644 index 000000000000..fb3fccee6afb --- /dev/null +++ b/jablib/src/main/java/org/jabref/logic/citedrive/OAuthSessionRegistry.java @@ -0,0 +1,86 @@ +package org.jabref.logic.citedrive; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.jabref.logic.net.CiteDrivePreferences; + +import org.jspecify.annotations.NullMarked; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/// Pending OAuth logins, keyed by the OAuth `state` parameter +@NullMarked +public class OAuthSessionRegistry { + + /// Time the user has to finish logging in in the browser + static final long TIMEOUT_MINUTES = 10; + + private static final Logger LOGGER = LoggerFactory.getLogger(OAuthSessionRegistry.class); + + private final Map> pending = new ConcurrentHashMap<>(); + private final Map>> tokenRequests = new ConcurrentHashMap<>(); + + // [impl->req~citedrive.refresh.single-request~1] + CompletableFuture> requestToken( + CiteDrivePreferences preferences, long revision, + Supplier>> request) { + TokenRequestKey key = new TokenRequestKey(preferences, revision); + CompletableFuture> candidate = new CompletableFuture<>(); + CompletableFuture> shared = Optional.ofNullable(tokenRequests.putIfAbsent(key, candidate)) + .orElseGet(() -> { + CompletableFuture.completedFuture(true) + .thenCompose(_ -> request.get()) + .whenComplete((session, failure) -> { + tokenRequests.remove(key, candidate); + Optional.ofNullable(failure).ifPresentOrElse(candidate::completeExceptionally, () -> candidate.complete(session)); + }); + return candidate; + }); + return shared.copy(); + } + + @NullMarked + private record TokenRequestKey(CiteDrivePreferences preferences, long revision) { + } + + /// @return the authorization code; fails with a `TimeoutException` if no callback arrives in time + public CompletableFuture register(String state) { + return register(state, TIMEOUT_MINUTES, TimeUnit.MINUTES); + } + + CompletableFuture register(String state, long timeout, TimeUnit unit) { + CompletableFuture future = new CompletableFuture<>(); + pending.put(state, future); + future.orTimeout(timeout, unit) + .whenComplete((_, _) -> pending.remove(state, future)); + return future; + } + + /// @return false if no login is waiting for this state (unknown, timed out, or already completed) + public boolean complete(String state, String code) { + CompletableFuture future = pending.remove(state); + if (future == null) { + LOGGER.warn("No pending OAuth session for the received state"); + return false; + } + return future.complete(code); + } + + public void fail(String state, Throwable t) { + CompletableFuture future = pending.remove(state); + if (future != null) { + future.completeExceptionally(t); + } else { + LOGGER.warn("No pending OAuth session for the received state (fail)"); + } + } + + boolean isPending(String state) { + return pending.containsKey(state); + } +} diff --git a/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java b/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java index acffebe2f5ff..c5055562f44a 100644 --- a/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java +++ b/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java @@ -88,7 +88,7 @@ /// [`OpenDatabase.loadDatabase`](org.jabref.logic.importer.OpenDatabase#loadDatabase(java.nio.file.Path, org.jabref.logic.importer.ImportFormatPreferences, org.jabref.model.util.FileUpdateMonitor)) /// /// **Opposite class:** -/// [`BibDatabaseWriter`](org.jabref.logic.exporter.BibDatabaseWriter) +/// [org.jabref.logic.exporter.BibDatabaseWriter] /// /// FIXME: This class relies on `char`, but should use [java.lang.Character] to be fully Unicode compliant. public class BibtexParser implements Parser { diff --git a/jablib/src/main/java/org/jabref/logic/net/CiteDrivePreferences.java b/jablib/src/main/java/org/jabref/logic/net/CiteDrivePreferences.java new file mode 100644 index 000000000000..abbf5443793e --- /dev/null +++ b/jablib/src/main/java/org/jabref/logic/net/CiteDrivePreferences.java @@ -0,0 +1,186 @@ +package org.jabref.logic.net; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; +import javafx.beans.value.ObservableValue; + +import org.jabref.logic.util.URLUtil; + +import com.nimbusds.oauth2.sdk.token.RefreshToken; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NullMarked +public class CiteDrivePreferences { + + private static final Logger LOGGER = LoggerFactory.getLogger(CiteDrivePreferences.class); + + /// OAuth tokens + /// + /// - Refresh token (long-lived, confidential). -- could be unavailable; therefore "Optional" + /// - Access token (short-lived) - not stored here + private final BooleanProperty enabled; + private final ObjectProperty<@Nullable RefreshToken> refreshToken; + private final BooleanProperty persistRefreshToken; + + /// CiteDrive REST API (OAuth and push endpoints); the dev server until CiteDrive goes live + private final StringProperty apiBaseUrl; + /// CiteDrive web app, where the user completes an import + private final StringProperty appBaseUrl; + private long serverRevision; + + public CiteDrivePreferences(boolean enabled, + @Nullable RefreshToken refreshToken, + boolean persistRefreshToken, + String apiBaseUrl, + String appBaseUrl) { + this.enabled = new SimpleBooleanProperty(enabled); + this.refreshToken = new SimpleObjectProperty<>(refreshToken); + this.persistRefreshToken = new SimpleBooleanProperty(persistRefreshToken); + this.apiBaseUrl = new SimpleStringProperty(apiBaseUrl); + this.appBaseUrl = new SimpleStringProperty(appBaseUrl); + this.apiBaseUrl.addListener((_, _, _) -> invalidateLogin()); + } + + // Creates object with default preference values + private CiteDrivePreferences() { + this( + false, // CiteDrive is opt-in + null, // no refresh token + true, // store in keychain + "https://api-dev.citedrive.com/", + "https://app-dev.citedrive.com/" + ); + } + + private synchronized void invalidateLogin() { + serverRevision++; + refreshToken.setValue(null); + } + + public synchronized long getServerRevision() { + return serverRevision; + } + + /// A response from an earlier server configuration must not overwrite the current login. + public synchronized boolean updateRefreshToken(long revision, Optional token) { + if (revision != serverRevision) { + return false; + } + token.ifPresentOrElse(this::setRefreshToken, () -> refreshToken.setValue(null)); + return true; + } + + // [impl->req~citedrive.https~1] + public static boolean isSecureUrl(String url) { + try { + URI uri = new URI(url); + return "https".equalsIgnoreCase(uri.getScheme()) + && Optional.ofNullable(uri.getHost()).filter(host -> !host.isBlank()).isPresent() + && Optional.ofNullable(uri.getRawUserInfo()).isEmpty(); + } catch (URISyntaxException e) { + LOGGER.debug("Invalid CiteDrive address", e); + return false; + } + } + + public static CiteDrivePreferences getDefault() { + return new CiteDrivePreferences(); + } + + public synchronized void setAll(CiteDrivePreferences preferences) { + this.enabled.set(preferences.isEnabled()); + this.persistRefreshToken.set(preferences.shouldPersistRefreshToken()); + this.apiBaseUrl.set(preferences.getApiBaseUrl()); + this.appBaseUrl.set(preferences.getAppBaseUrl()); + this.refreshToken.set(preferences.getRefreshToken()); + } + + public boolean isEnabled() { + return enabled.get(); + } + + public void setEnabled(boolean enabled) { + this.enabled.set(enabled); + } + + public BooleanProperty enabledProperty() { + return enabled; + } + + public synchronized String getApiBaseUrl() { + return apiBaseUrl.get(); + } + + public StringProperty apiBaseUrlProperty() { + return apiBaseUrl; + } + + public synchronized void setApiBaseUrl(String apiBaseUrl) { + this.apiBaseUrl.set(apiBaseUrl); + } + + public String getAppBaseUrl() { + return appBaseUrl.get(); + } + + public StringProperty appBaseUrlProperty() { + return appBaseUrl; + } + + public void setAppBaseUrl(String appBaseUrl) { + this.appBaseUrl.set(appBaseUrl); + } + + public URI getAuthorizationEndpoint() { + return URLUtil.createUri(getApiBaseUrl()).resolve("jabref/login/"); + } + + public URI getTokenEndpoint() { + return URLUtil.createUri(getApiBaseUrl()).resolve("o/token/"); + } + + public URI getPushEndpoint() { + return URLUtil.createUri(getApiBaseUrl()).resolve("jabref/push/"); + } + + /// Web page where the user picks which of the pushed entries to import into a CiteDrive project + public URI getImportPage() { + return URLUtil.createUri(getAppBaseUrl()).resolve("jabref/import/"); + } + + public final synchronized @Nullable RefreshToken getRefreshToken() { + return refreshToken.get(); + } + + /// NOT called from the GUI preferences page, but from the OAuth flow handling components + public synchronized void setRefreshToken(@Nullable RefreshToken refreshToken) { + this.refreshToken.set(refreshToken); + } + + public final boolean shouldPersistRefreshToken() { + return persistRefreshToken.get(); + } + + public BooleanProperty persistRefreshTokenProperty() { + return persistRefreshToken; + } + + public void setPersistRefreshToken(boolean persistRefreshToken) { + this.persistRefreshToken.set(persistRefreshToken); + } + + public ObservableValue<@Nullable RefreshToken> getRefreshTokenProperty() { + return refreshToken; + } +} diff --git a/jablib/src/main/java/org/jabref/logic/preferences/CliPreferences.java b/jablib/src/main/java/org/jabref/logic/preferences/CliPreferences.java index b0b30af535e5..c47b298ad5cf 100644 --- a/jablib/src/main/java/org/jabref/logic/preferences/CliPreferences.java +++ b/jablib/src/main/java/org/jabref/logic/preferences/CliPreferences.java @@ -24,6 +24,7 @@ import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.layout.format.NameFormatterPreferences; +import org.jabref.logic.net.CiteDrivePreferences; import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.net.ssl.SSLPreferences; import org.jabref.logic.ocr.OcrPreferences; @@ -88,6 +89,8 @@ public interface CliPreferences { ProxyPreferences getProxyPreferences(); + CiteDrivePreferences getCiteDrivePreferences(); + SSLPreferences getSSLPreferences(); CitationKeyPatternPreferences getCitationKeyPatternPreferences(); diff --git a/jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java b/jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java index 8e7f1997969d..99aa7fe4bfa3 100644 --- a/jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java +++ b/jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java @@ -76,6 +76,7 @@ import org.jabref.logic.l10n.Localization; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.layout.format.NameFormatterPreferences; +import org.jabref.logic.net.CiteDrivePreferences; import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.net.ssl.SSLPreferences; import org.jabref.logic.net.ssl.TrustStoreManager; @@ -125,8 +126,12 @@ import com.github.javakeyring.PasswordAccessException; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Splitter; +import com.nimbusds.oauth2.sdk.ParseException; +import com.nimbusds.oauth2.sdk.token.RefreshToken; import com.tobiasdiez.easybind.EasyBind; import jakarta.inject.Singleton; +import net.minidev.json.JSONObject; +import net.minidev.json.parser.JSONParser; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; @@ -346,6 +351,15 @@ public class JabRefCliPreferences implements CliPreferences { private static final String PROXY_PERSIST_PASSWORD = "persistPassword"; // endregion + // CiteDrive + // RefreshToken + private static final String CITE_DRIVE_KEYRING_SERVICE = "org.jabref"; + private static final String CITE_DRIVE_KEYRING_ACCOUNT = "citedrive"; + private static final String CITE_DRIVE_ENABLED = "citeDriveEnabled"; + private static final String CITE_DRIVE_PERSIST_TOKEN = "citeDrivePersistToken"; + private static final String CITE_DRIVE_API_BASE_URL = "citeDriveApiBaseUrl"; + private static final String CITE_DRIVE_APP_BASE_URL = "citeDriveAppBaseUrl"; + // SSL private static final String SSL_TRUSTSTORE_PATH = "truststorePath"; @@ -485,6 +499,7 @@ public class JabRefCliPreferences implements CliPreferences { private FilePreferences filePreferences; private RemotePreferences remotePreferences; private ProxyPreferences proxyPreferences; + private CiteDrivePreferences citeDrivePreferences; private SSLPreferences sslPreferences; private SearchPreferences searchPreferences; private AutoLinkPreferences autoLinkPreferences; @@ -1094,6 +1109,9 @@ public void clear() throws BackingStoreException { initializeAll(); allBindings.forEach(binding -> binding.resetToDefaults().run()); + + // CiteDrive preferences are not registered in allBindings (they persist via the keyring), so reset them explicitly. + getCiteDrivePreferences().setAll(CiteDrivePreferences.getDefault()); } /// Imports Preferences from an XML file. @@ -1107,6 +1125,9 @@ public void importPreferences(Path path) throws JabRefException { initializeAll(); allBindings.forEach(binding -> binding.importFromStore().run()); + + // CiteDrive preferences are not registered in allBindings (the token lives in the keyring), so reload them explicitly. + getCiteDrivePreferences().setAll(getCiteDrivePreferencesFromBackingStore(CiteDrivePreferences.getDefault())); } /// Instantiates every preference group so its bindings are registered in [#allBindings] before a bulk reset/import @@ -1633,6 +1654,97 @@ public CitationKeyPatternPreferences getCitationKeyPatternPreferences() { return citationKeyPatternPreferences; } + // region: CiteDrive Preferences + @Override + public CiteDrivePreferences getCiteDrivePreferences() { + if (citeDrivePreferences != null) { + return citeDrivePreferences; + } + + citeDrivePreferences = getCiteDrivePreferencesFromBackingStore(CiteDrivePreferences.getDefault()); + + EasyBind.listen(citeDrivePreferences.persistRefreshTokenProperty(), (_, _, newValue) -> { + putBoolean(CITE_DRIVE_PERSIST_TOKEN, newValue); + setCiteDriveToken(newValue ? citeDrivePreferences.getRefreshToken() : null); + }); + + EasyBind.listen(citeDrivePreferences.enabledProperty(), (_, _, newValue) -> putBoolean(CITE_DRIVE_ENABLED, newValue)); + EasyBind.listen(citeDrivePreferences.apiBaseUrlProperty(), (_, _, newValue) -> put(CITE_DRIVE_API_BASE_URL, newValue)); + EasyBind.listen(citeDrivePreferences.appBaseUrlProperty(), (_, _, newValue) -> put(CITE_DRIVE_APP_BASE_URL, newValue)); + + EasyBind.listen(citeDrivePreferences.getRefreshTokenProperty(), (_, _, newValue) -> { + if (citeDrivePreferences.shouldPersistRefreshToken()) { + setCiteDriveToken(newValue); + } + }); + + return citeDrivePreferences; + } + + private CiteDrivePreferences getCiteDrivePreferencesFromBackingStore(CiteDrivePreferences defaults) { + boolean persistToken = getBoolean(CITE_DRIVE_PERSIST_TOKEN, defaults.shouldPersistRefreshToken()); + String apiBaseUrl = get(CITE_DRIVE_API_BASE_URL, defaults.getApiBaseUrl()); + return new CiteDrivePreferences( + getBoolean(CITE_DRIVE_ENABLED, defaults.isEnabled()), + persistToken ? getCiteDriveToken(apiBaseUrl) : null, + persistToken, + apiBaseUrl, + get(CITE_DRIVE_APP_BASE_URL, defaults.getAppBaseUrl()) + ); + } + + /// The refresh token lives in the system keyring only: never in the preferences, which can be exported as plain text + private @Nullable RefreshToken getCiteDriveToken(String apiBaseUrl) { + try (final Keyring keyring = Keyring.create()) { + return parseCiteDriveToken(keyring.getPassword(CITE_DRIVE_KEYRING_SERVICE, CITE_DRIVE_KEYRING_ACCOUNT), apiBaseUrl); + } catch (PasswordAccessException _) { + LOGGER.debug("No CiteDrive token stored in keyring"); + } catch (Exception ex) { + LOGGER.warn("Unable to read CiteDrive token from keyring", ex); + } + return null; + } + + /// If the keyring is not available, the token is kept in memory only (login needed after restart) + private void setCiteDriveToken(@Nullable RefreshToken refreshToken) { + try (final Keyring keyring = Keyring.create()) { + if (refreshToken == null) { + keyring.deletePassword(CITE_DRIVE_KEYRING_SERVICE, CITE_DRIVE_KEYRING_ACCOUNT); + } else { + keyring.setPassword(CITE_DRIVE_KEYRING_SERVICE, CITE_DRIVE_KEYRING_ACCOUNT, serializeCiteDriveToken(refreshToken, citeDrivePreferences.getApiBaseUrl())); + } + } catch (PasswordAccessException _) { + LOGGER.debug("No CiteDrive token stored in keyring, nothing to remove"); + } catch (Exception ex) { + LOGGER.warn("Unable to update CiteDrive token in keyring", ex); + } + } + + static String serializeCiteDriveToken(RefreshToken refreshToken, String apiBaseUrl) { + JSONObject json = refreshToken.toJSONObject(); + json.put("api_base_url", apiBaseUrl); + return json.toJSONString(); + } + + // [impl->req~citedrive.credentials.server~1] + static @Nullable RefreshToken parseCiteDriveToken(@Nullable String json, String apiBaseUrl) { + if (StringUtil.isBlank(json)) { + return null; + } + + try { + JSONObject jsonObject = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(json); + if (!apiBaseUrl.equals(jsonObject.get("api_base_url"))) { + return null; + } + return RefreshToken.parse(jsonObject); + } catch (ParseException | net.minidev.json.parser.ParseException e) { + LOGGER.warn("Invalid CiteDrive refresh token JSON", e); + return null; + } + } + // endRegion: CiteDrive Preferences + private @NonNull GlobalCitationKeyPatterns getGlobalCitationKeyPattern(CitationKeyPatternPreferences defaults) { GlobalCitationKeyPatterns citationKeyPattern = GlobalCitationKeyPatterns.fromPattern( get(CITATION_KEY_DEFAULT_PATTERN, defaults.getKeyPatterns().getDefaultValue().stringRepresentation())); @@ -2501,7 +2613,6 @@ private Set getFetcherKeys(Set defaults) { for (int i = 0; i < names.size(); i++) { fetcherApiKeys.add(new FetcherApiKey( names.get(i), - // i < uses.size() ? Boolean.parseBoolean(uses.get(i)) : false (i < uses.size()) && Boolean.parseBoolean(uses.get(i)), i < keys.size() ? keys.get(i) : "")); } diff --git a/jablib/src/main/resources/l10n/JabRef_en.properties b/jablib/src/main/resources/l10n/JabRef_en.properties index ae6be01d4988..dfc03f114dc8 100644 --- a/jablib/src/main/resources/l10n/JabRef_en.properties +++ b/jablib/src/main/resources/l10n/JabRef_en.properties @@ -1918,6 +1918,7 @@ Cannot\ use\ port\ %0\ for\ remote\ operation;\ another\ application\ may\ be\ u Enable\ HTTP\ Server\ (e.g.,\ for\ JabMap)\ on\ port=Enable HTTP Server (e.g., for JabMap) on port Skip\ import\ dialog\ for\ entries\ received\ from\ browser\ extensions=Skip import dialog for entries received from browser extensions HTTP\ Server=HTTP Server +Enable\ HTTP\ server=Enable HTTP server Enable\ LSP\ Server\ on\ port=Enable LSP Server on port LSP\ Server=LSP Server @@ -3686,6 +3687,33 @@ Forward=Forward Export\ chat\ history=Export chat history +# CiteDrive +CiteDrive=CiteDrive +Login...=Login... +Push\ to\ CiteDrive=Push to CiteDrive +Logged\ in\ to\ CiteDrive=Logged in to CiteDrive +External\ storages=External storages +Enable\ CiteDrive=Enable CiteDrive +Remember\ the\ login\ between\ sessions=Remember the login between sessions +Server\ address=Server address +Web\ address=Web address +Please\ enter\ a\ valid\ HTTPS\ URL=Please enter a valid HTTPS URL +Login\ could\ not\ be\ completed=Login could not be completed +Continue\ in\ JabRef.\ You\ can\ close\ this\ window.=Continue in JabRef. You can close this window. +Please\ try\ again\ in\ JabRef.\ You\ can\ close\ this\ window.=Please try again in JabRef. You can close this window. +CiteDrive\ login\ failed\:\ \%0=CiteDrive login failed: %0 +CiteDrive\ push\ failed\:\ could\ not\ obtain\ access\ token.=CiteDrive push failed: could not obtain access token. +CiteDrive\ login\ failed\:\ could\ not\ obtain\ access\ token.=CiteDrive login failed: could not obtain access token. +CiteDrive\ push\ failed\:\ %0=CiteDrive push failed: %0 +CiteDrive\ push\ succeeded.=CiteDrive push succeeded. +Logging\ in\ to\ CiteDrive\ needs\ the\ HTTP\ server.\ Please\ enable\ it\ in\ the\ preferences.=Logging in to CiteDrive needs the HTTP server. Please enable it in the preferences. +Log\ in\ to\ CiteDrive=Log in to CiteDrive +Logging\ in\ to\ CiteDrive\ needs\ JabRef's\ HTTP\ server,\ which\ is\ currently\ disabled.=Logging in to CiteDrive needs JabRef's HTTP server, which is currently disabled. +Opening\ the\ CiteDrive\ login\ page\ in\ the\ browser=Opening the CiteDrive login page in the browser +Login\ was\ not\ completed\ in\ time.=Login was not completed in time. +CiteDrive\ push\ succeeded\:\ %0\ KB=CiteDrive push succeeded: %0 KB +CiteDrive\ push\ succeeded\:\ %0\ entries\ (%1\ KB)=CiteDrive push succeeded: %0 entries (%1 KB) + Select\ citation\ fetcher=Select citation fetcher Parsing\ failed\ because\ more\ than\ one\ entry\ was\ found.\ Please\ check\ your\ BibTeX\ syntax.=Parsing failed because more than one entry was found. Please check your BibTeX syntax. Problem\ with\ parsing\ entry=Problem with parsing entry diff --git a/jablib/src/test/java/org/jabref/logic/citedrive/CiteDriveOAuthServiceTest.java b/jablib/src/test/java/org/jabref/logic/citedrive/CiteDriveOAuthServiceTest.java new file mode 100644 index 000000000000..c247b5ff496d --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/citedrive/CiteDriveOAuthServiceTest.java @@ -0,0 +1,303 @@ +package org.jabref.logic.citedrive; + +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.net.URI; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.jabref.logic.net.CiteDrivePreferences; +import org.jabref.logic.remote.RemotePreferences; + +import com.nimbusds.oauth2.sdk.AuthorizationRequest; +import com.nimbusds.oauth2.sdk.ParseException; +import com.nimbusds.oauth2.sdk.http.HTTPRequest; +import com.nimbusds.oauth2.sdk.http.HTTPResponse; +import com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod; +import com.nimbusds.oauth2.sdk.token.BearerAccessToken; +import com.nimbusds.oauth2.sdk.token.RefreshToken; +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.ArgumentCaptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +// [utest->req~citedrive.login~1] +// [utest->req~citedrive.push~1] +@NullMarked +@ResourceLock("Localization.lang") +class CiteDriveOAuthServiceTest { + + private final RemotePreferences remotePreferences = mock(RemotePreferences.class); + private final CiteDrivePreferences preferences = CiteDrivePreferences.getDefault(); + private final OAuthSessionRegistry registry = new OAuthSessionRegistry(); + private final AtomicReference openedPage = new AtomicReference<>(); + private final CiteDriveOAuthService.TokenRequestSender sender = mock(CiteDriveOAuthService.TokenRequestSender.class); + + @BeforeEach + void setUp() { + when(remotePreferences.shouldEnableHttpServer()).thenReturn(true); + when(remotePreferences.getHttpServerUri()).thenReturn(URI.create("http://127.0.0.1:23119/")); + } + + @Test + void disabledHttpServerFailsWithoutOpeningBrowser() { + when(remotePreferences.shouldEnableHttpServer()).thenReturn(false); + + assertThrows(ExecutionException.class, () -> service().authorizeInteractive().get(5, TimeUnit.SECONDS)); + assertEquals(Optional.empty(), Optional.ofNullable(openedPage.get())); + verifyNoInteractions(sender); + } + + @Test + void networkErrorOnRefreshKeepsStoredLogin() throws IOException { + RefreshToken refreshToken = new RefreshToken("stored"); + preferences.setRefreshToken(refreshToken); + when(sender.send(any())).thenThrow(new SocketTimeoutException("Token endpoint timed out")); + + assertThrows(ExecutionException.class, () -> service().getAccessToken().get(5, TimeUnit.SECONDS)); + assertEquals(refreshToken, preferences.getRefreshToken()); + assertEquals(Optional.empty(), Optional.ofNullable(openedPage.get())); + } + + @Test + void refreshWithoutRotationKeepsStoredRefreshToken() throws Exception { + RefreshToken refreshToken = new RefreshToken("stored"); + preferences.setRefreshToken(refreshToken); + when(sender.send(any())).thenReturn(response(200, """ + {"access_token":"access","token_type":"Bearer"} + """)); + + assertEquals(new BearerAccessToken("access"), service().getAccessToken().get(5, TimeUnit.SECONDS).orElseThrow()); + assertEquals(refreshToken, preferences.getRefreshToken()); + } + + @Test + void interactiveLoginWithoutRefreshTokenDiscardsPreviousAccountToken() throws Exception { + preferences.setRefreshToken(new RefreshToken("previous-account")); + when(sender.send(any())).thenReturn(response(200, """ + {"access_token":"access","token_type":"Bearer"} + """)); + + var login = service().authorizeInteractive(); + AuthorizationRequest request = AuthorizationRequest.parse(openedPage.get()); + registry.complete(request.getState().getValue(), "code"); + + assertEquals(new BearerAccessToken("access"), login.get(5, TimeUnit.SECONDS).orElseThrow()); + assertEquals(Optional.empty(), Optional.ofNullable(preferences.getRefreshToken())); + } + + @ParameterizedTest + @CsvSource({"400,invalid_client", "429,slow_down", "503,temporarily_unavailable", "503,invalid_grant"}) + void retryableOrConfigurationErrorKeepsStoredLogin(int status, String error) throws IOException, ParseException { + // [utest->req~citedrive.refresh.failure~1] + RefreshToken refreshToken = new RefreshToken("stored"); + preferences.setRefreshToken(refreshToken); + when(sender.send(any())).thenReturn(response(status, "{\"error\":\"" + error + "\"}")); + + assertThrows(ExecutionException.class, () -> service().getAccessToken().get(5, TimeUnit.SECONDS)); + assertEquals(refreshToken, preferences.getRefreshToken()); + assertEquals(Optional.empty(), Optional.ofNullable(openedPage.get())); + } + + @Test + void temporaryFailureCanBeRetriedWithoutAnotherLogin() throws Exception { + // [utest->req~citedrive.refresh.failure~1] + preferences.setRefreshToken(new RefreshToken("stored")); + when(sender.send(any())).thenReturn(response(503, "{\"error\":\"temporarily_unavailable\"}"), successResponse()); + + assertThrows(ExecutionException.class, () -> service().getAccessToken().get(5, TimeUnit.SECONDS)); + assertEquals(new BearerAccessToken("access"), service().getAccessToken().get(5, TimeUnit.SECONDS).orElseThrow()); + assertEquals(Optional.empty(), Optional.ofNullable(openedPage.get())); + } + + @Test + void invalidGrantClearsStoredLoginAndStartsInteractiveLogin() throws Exception { + // [utest->req~citedrive.refresh.failure~1] + preferences.setRefreshToken(new RefreshToken("expired")); + when(sender.send(any())).thenReturn(response(400, "{\"error\":\"invalid_grant\"}")); + CompletableFuture browserOpened = new CompletableFuture<>(); + CiteDriveOAuthService service = new CiteDriveOAuthService(remotePreferences, preferences, registry, browserOpened::complete, () -> false, sender); + + CompletableFuture login = service.getAccessToken(); + AuthorizationRequest request = AuthorizationRequest.parse(browserOpened.get(5, TimeUnit.SECONDS)); + + assertEquals(Optional.empty(), Optional.ofNullable(preferences.getRefreshToken())); + registry.fail(request.getState().getValue(), new IOException("Login cancelled by test")); + assertThrows(ExecutionException.class, () -> login.get(5, TimeUnit.SECONDS)); + } + + @Test + void concurrentServicesShareOneRefreshAndStoreRotatedToken() throws Exception { + // [utest->req~citedrive.refresh.single-request~1] + preferences.setRefreshToken(new RefreshToken("stored")); + CompletableFuture received = new CompletableFuture<>(); + CompletableFuture reply = new CompletableFuture<>(); + AtomicInteger requests = new AtomicInteger(); + when(sender.send(any())).thenAnswer(invocation -> { + requests.incrementAndGet(); + received.complete(invocation.getArgument(0)); + return reply.join(); + }); + + try { + var first = service().getAuthorizedSession(); + received.get(5, TimeUnit.SECONDS); + var second = service().getAuthorizedSession(); + reply.complete(successResponse()); + + assertEquals(first.get(5, TimeUnit.SECONDS), second.get(5, TimeUnit.SECONDS)); + assertEquals(1, requests.get()); + assertEquals(new RefreshToken("rotated"), preferences.getRefreshToken()); + } finally { + reply.complete(successResponse()); + } + } + + @Test + void serverChangeUpdatesExistingServiceAndDiscardsOldCredential() throws Exception { + // [utest->req~citedrive.credentials.server~1] + preferences.setRefreshToken(new RefreshToken("old-server-token")); + CiteDriveOAuthService service = service(); + preferences.setApiBaseUrl("https://other.example/"); + when(sender.send(any())).thenReturn(successResponse()); + + var login = service.getAuthorizedSession(); + AuthorizationRequest request = AuthorizationRequest.parse(openedPage.get()); + assertEquals(URI.create("https://other.example/jabref/login/"), request.getEndpointURI()); + assertEquals(Optional.empty(), Optional.ofNullable(preferences.getRefreshToken())); + registry.complete(request.getState().getValue(), "code"); + + var session = login.get(5, TimeUnit.SECONDS).orElseThrow(); + assertEquals(URI.create("https://other.example/jabref/push/"), session.pushEndpoint()); + ArgumentCaptor tokenRequest = ArgumentCaptor.forClass(HTTPRequest.class); + verify(sender).send(tokenRequest.capture()); + assertEquals("https://other.example/o/token/", tokenRequest.getValue().getURL().toString()); + } + + @Test + void serverChangeDuringRefreshCannotRestoreOldCredential() throws Exception { + // [utest->req~citedrive.credentials.server~1] + preferences.setRefreshToken(new RefreshToken("stored")); + CompletableFuture received = new CompletableFuture<>(); + CompletableFuture reply = new CompletableFuture<>(); + when(sender.send(any())).thenAnswer(invocation -> { + received.complete(invocation.getArgument(0)); + return reply.join(); + }); + + try { + var login = service().getAuthorizedSession(); + received.get(5, TimeUnit.SECONDS); + preferences.setApiBaseUrl("https://other.example/"); + reply.complete(successResponse()); + + assertThrows(ExecutionException.class, () -> login.get(5, TimeUnit.SECONDS)); + assertEquals(Optional.empty(), Optional.ofNullable(preferences.getRefreshToken())); + } finally { + reply.complete(successResponse()); + } + } + + @Test + void authorizedSessionKeepsItsOriginalUploadAddress() throws Exception { + // [utest->req~citedrive.credentials.server~1] + preferences.setRefreshToken(new RefreshToken("stored")); + when(sender.send(any())).thenReturn(successResponse()); + var session = service().getAuthorizedSession().get(5, TimeUnit.SECONDS).orElseThrow(); + + preferences.setApiBaseUrl("https://other.example/"); + preferences.setAppBaseUrl("https://other-app.example/"); + + assertEquals(URI.create("https://api-dev.citedrive.com/jabref/push/"), session.pushEndpoint()); + assertEquals(URI.create("https://app-dev.citedrive.com/jabref/import/"), session.importPage()); + } + + @Test + void insecureApiAddressFailsBeforeOpeningBrowserOrSendingToken() { + // [utest->req~citedrive.https~1] + preferences.setApiBaseUrl("http://other.example/"); + preferences.setRefreshToken(new RefreshToken("stored")); + + assertThrows(ExecutionException.class, () -> service().getAccessToken().get(5, TimeUnit.SECONDS)); + assertThrows(ExecutionException.class, () -> service().authorizeInteractive().get(5, TimeUnit.SECONDS)); + verifyNoInteractions(sender); + assertEquals(Optional.empty(), Optional.ofNullable(openedPage.get())); + } + + @Test + void refreshUsesBoundedTimeoutsAndDisablesRedirects() throws Exception { + // [utest->req~citedrive.token.timeout~1] + preferences.setRefreshToken(new RefreshToken("stored")); + when(sender.send(any())).thenReturn(successResponse()); + + assertEquals(new BearerAccessToken("access"), service().getAccessToken().get(5, TimeUnit.SECONDS).orElseThrow()); + assertRequestTimeouts(); + } + + @Test + void codeExchangeRunsOutsideCallbackThreadWithBoundedTimeouts() throws Exception { + // [utest->req~citedrive.token.timeout~1] + CompletableFuture received = new CompletableFuture<>(); + CompletableFuture reply = new CompletableFuture<>(); + when(sender.send(any())).thenAnswer(invocation -> { + received.complete(invocation.getArgument(0)); + return reply.join(); + }); + try { + var login = service().authorizeInteractive(); + AuthorizationRequest request = AuthorizationRequest.parse(openedPage.get()); + assertEquals(CodeChallengeMethod.S256, request.getCodeChallengeMethod()); + assertTrue(registry.complete(request.getState().getValue(), "code")); + received.get(5, TimeUnit.SECONDS); + assertFalse(login.isDone()); + reply.complete(successResponse()); + + assertEquals(new BearerAccessToken("access"), login.get(5, TimeUnit.SECONDS).orElseThrow()); + assertRequestTimeouts(); + } finally { + reply.complete(successResponse()); + } + } + + private CiteDriveOAuthService service() { + return new CiteDriveOAuthService(remotePreferences, preferences, registry, openedPage::set, () -> false, sender); + } + + private void assertRequestTimeouts() throws IOException { + ArgumentCaptor request = ArgumentCaptor.forClass(HTTPRequest.class); + verify(sender).send(request.capture()); + assertEquals(10_000, request.getValue().getConnectTimeout()); + assertEquals(30_000, request.getValue().getReadTimeout()); + assertFalse(request.getValue().getFollowRedirects()); + } + + private HTTPResponse successResponse() throws ParseException { + return response(200, """ + {"access_token":"access","token_type":"Bearer","refresh_token":"rotated"} + """); + } + + private HTTPResponse response(int status, String body) throws ParseException { + HTTPResponse response = new HTTPResponse(status); + response.setContentType("application/json"); + response.setBody(body); + return response; + } +} diff --git a/jablib/src/test/java/org/jabref/logic/citedrive/CiteDrivePushTest.java b/jablib/src/test/java/org/jabref/logic/citedrive/CiteDrivePushTest.java new file mode 100644 index 000000000000..327dc29b083d --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/citedrive/CiteDrivePushTest.java @@ -0,0 +1,212 @@ +package org.jabref.logic.citedrive; + +import java.io.IOException; +import java.io.Reader; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.jabref.logic.bibtex.FieldPreferences; +import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; +import org.jabref.logic.exporter.BibDatabaseWriter; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; +import org.jabref.logic.importer.ImportFormatPreferences; +import org.jabref.logic.importer.fileformat.BibtexParser; +import org.jabref.logic.preferences.CliPreferences; +import org.jabref.logic.util.NotificationService; +import org.jabref.model.database.BibDatabase; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.BibEntryTypesManager; +import org.jabref.model.entry.field.StandardField; +import org.jabref.model.entry.types.StandardEntryType; +import org.jabref.model.metadata.SaveOrder; + +import com.nimbusds.oauth2.sdk.token.BearerAccessToken; +import kong.unirest.core.HttpRequestWithBody; +import kong.unirest.core.HttpResponse; +import kong.unirest.core.RequestBodyEntity; +import kong.unirest.core.Unirest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestReporter; +import org.junit.jupiter.api.parallel.ResourceLock; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Answers; +import org.mockito.ArgumentCaptor; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/// Guards what leaves the machine: the request body is plain BibTeX regardless of the user's export settings. +/// JabRef's metadata (here: the library-specific file directory) must not be uploaded, and the token has to arrive as a bearer header. +/// +/// [utest->req~citedrive.push~1] +@ResourceLock("Localization.lang") +class CiteDrivePushTest { + + private static Stream batches() { + Stream generated = Stream.of(2, 1_000).map(count -> Arguments.of(count, Optional.empty())); + Stream fixture = Optional.ofNullable(System.getProperty("citedrive.batch.fixture")) + .stream() + .map(path -> Arguments.of(100_000, Optional.of(Path.of(path)))); + return Stream.concat(generated, fixture); + } + + @ParameterizedTest + @MethodSource("batches") + @SuppressWarnings("unchecked") + void batchUploadSendsEveryEntryInOneRequest(int count, Optional fixture, TestReporter reporter) throws IOException { + ImportFormatPreferences importPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(importPreferences.bibEntryPreferences().getKeywordSeparator()).thenReturn(','); + long started = System.nanoTime(); + BibDatabaseContext context; + if (fixture.isEmpty()) { + BibDatabase database = new BibDatabase(); + IntStream.range(0, count).mapToObj(index -> new BibEntry(StandardEntryType.Article) + .withCitationKey("batch-" + index) + .withField(StandardField.AUTHOR, "Müller, Jane") + .withField(StandardField.TITLE, "Batch entry " + index) + .withChanged(true)) + .forEach(database::insertEntry); + context = new BibDatabaseContext(database); + } else { + try (Reader reader = Files.newBufferedReader(fixture.orElseThrow(), StandardCharsets.UTF_8)) { + context = new BibtexParser(importPreferences).parse(reader).getDatabaseContext(); + } + } + long loaded = System.nanoTime(); + assertEquals(count, context.getEntries().size()); + List entries = context.getEntries().stream().map(BibEntry::new).toList(); + long copied = System.nanoTime(); + HttpRequestWithBody postRequest = mock(HttpRequestWithBody.class); + RequestBodyEntity bodyEntity = mock(RequestBodyEntity.class); + HttpResponse response = mock(HttpResponse.class); + when(postRequest.header(anyString(), anyString())).thenReturn(postRequest); + when(postRequest.body(anyString())).thenReturn(bodyEntity); + when(bodyEntity.asString()).thenReturn(response); + when(response.isSuccess()).thenReturn(true); + + try (MockedStatic unirestMock = Mockito.mockStatic(Unirest.class)) { + unirestMock.when(() -> Unirest.post("https://example.com/jabref/push/")).thenReturn(postRequest); + + assertTrue(CiteDrivePush.push(context, entries, new BearerAccessToken("access-token"), createPreferences(), + mock(NotificationService.class), URI.create("https://example.com/jabref/push/"))); + long serialized = System.nanoTime(); + + ArgumentCaptor requestBody = ArgumentCaptor.forClass(String.class); + verify(postRequest).body(requestBody.capture()); + unirestMock.verify(() -> Unirest.post("https://example.com/jabref/push/")); + List uploaded = new BibtexParser(importPreferences).parse(Reader.of(requestBody.getValue())).getDatabase().getEntries(); + Comparator byKey = Comparator.comparing(entry -> entry.getCitationKey().orElseThrow()); + assertEquals(entries.stream().sorted(byKey).toList(), uploaded.stream().sorted(byKey).toList()); + assertEquals(context.getEntries().stream().sorted(byKey).toList(), entries.stream().sorted(byKey).toList()); + reporter.publishEntry(Map.of( + "entries", String.valueOf(count), + "bytes", String.valueOf(requestBody.getValue().getBytes(StandardCharsets.UTF_8).length), + "loadMs", String.valueOf((loaded - started) / 1_000_000), + "snapshotMs", String.valueOf((copied - loaded) / 1_000_000), + "serializeAndPrepareMs", String.valueOf((serialized - copied) / 1_000_000))); + } + } + + @Test + void pushRejectsUnencryptedEndpointBeforeSendingCredentials() { + try (MockedStatic unirestMock = Mockito.mockStatic(Unirest.class)) { + BibDatabaseContext context = createDatabaseContext(); + assertThrows(IOException.class, () -> CiteDrivePush.push(context, context.getEntries(), new BearerAccessToken("access-token"), + createPreferences(), mock(NotificationService.class), URI.create("http://api.example/jabref/push/"))); + unirestMock.verifyNoInteractions(); + } + } + + @Test + @SuppressWarnings("unchecked") + void pushSendsSerializedBibtexBody() throws IOException { + HttpRequestWithBody postRequest = mock(HttpRequestWithBody.class); + RequestBodyEntity bodyEntity = mock(RequestBodyEntity.class); + HttpResponse response = mock(HttpResponse.class); + when(postRequest.header(anyString(), anyString())).thenReturn(postRequest); + AtomicReference requestBody = new AtomicReference<>(); + doAnswer(invocation -> { + requestBody.set(invocation.getArgument(0)); + return bodyEntity; + }).when(postRequest).body(anyString()); + when(bodyEntity.asString()).thenReturn(response); + when(response.isSuccess()).thenReturn(true); + + NotificationService notificationService = mock(NotificationService.class); + + try (MockedStatic unirestMock = Mockito.mockStatic(Unirest.class)) { + unirestMock.when(() -> Unirest.post("https://example.com/jabref/push/")).thenReturn(postRequest); + BibDatabaseContext context = createDatabaseContext(); + boolean pushed = CiteDrivePush.push( + context, + context.getEntries(), + new BearerAccessToken("access-token"), + createPreferences(), + notificationService, + URI.create("https://example.com/jabref/push/")); + + verify(postRequest).header("Authorization", "Bearer access-token"); + verify(postRequest).header("Content-Type", "application/x-bibtex"); + String body = requestBody.get(); + assertNotNull(body); + assertEquals(""" + @Article{test-key, + author = {Doe, Jane}, + title = {CiteDrive push}, + } + """.stripLeading(), body); + verify(response).ifSuccess(Mockito.any()); + assertTrue(pushed); + } + } + + private BibDatabaseContext createDatabaseContext() { + BibEntry entry = new BibEntry(StandardEntryType.Article) + .withCitationKey("test-key") + .withField(StandardField.AUTHOR, "Doe, Jane") + .withField(StandardField.TITLE, "CiteDrive push") + .withChanged(true); + + BibDatabase database = new BibDatabase(); + // The writer uses the library's separator; pinning it keeps the expected body the same on Windows + database.setNewLineSeparator("\n"); + database.insertEntry(entry); + BibDatabaseContext context = new BibDatabaseContext(database); + context.getMetaData().setLibrarySpecificFileDirectory("/home/user/papers"); + return context; + } + + private CliPreferences createPreferences() { + CliPreferences preferences = mock(CliPreferences.class, Answers.RETURNS_DEEP_STUBS); + when(preferences.getSelfContainedExportConfiguration()).thenReturn(new SelfContainedSaveConfiguration( + SaveOrder.getDefaultSaveOrder(), + false, + BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, + false)); + when(preferences.getFieldPreferences()).thenReturn(new FieldPreferences(true, List.of(), List.of())); + when(preferences.getCitationKeyPatternPreferences()).thenReturn(mock(CitationKeyPatternPreferences.class, Answers.RETURNS_DEEP_STUBS)); + when(preferences.getCustomEntryTypesRepository()).thenReturn(new BibEntryTypesManager()); + return preferences; + } +} diff --git a/jablib/src/test/java/org/jabref/logic/citedrive/OAuthSessionRegistryTest.java b/jablib/src/test/java/org/jabref/logic/citedrive/OAuthSessionRegistryTest.java new file mode 100644 index 000000000000..374443ffb636 --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/citedrive/OAuthSessionRegistryTest.java @@ -0,0 +1,46 @@ +package org.jabref.logic.citedrive; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/// The registry is what ties a browser callback to the login that started it. +/// Two things must hold or logins silently break: +/// +/// - a callback completes only the login with the matching `state` (a stale or foreign callback must not finish someone else's login) +/// - an abandoned login times out *and* is dropped from the registry, otherwise forgotten logins pile up for the lifetime of the JVM +/// +/// [utest->req~citedrive.login~1] +class OAuthSessionRegistryTest { + + private final OAuthSessionRegistry registry = new OAuthSessionRegistry(); + + @Test + void completeDeliversCodeOfMatchingState() throws Exception { + CompletableFuture first = registry.register("first"); + CompletableFuture second = registry.register("second"); + + registry.complete("second", "code-2"); + + assertEquals("code-2", second.get()); + assertFalse(first.isDone()); + } + + @Test + void abandonedLoginTimesOutAndIsRemoved() { + CompletableFuture future = registry.register("state", 10, TimeUnit.MILLISECONDS); + + ExecutionException exception = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS)); + + assertInstanceOf(TimeoutException.class, exception.getCause()); + assertFalse(registry.isPending("state")); + } +} diff --git a/jablib/src/test/java/org/jabref/logic/net/CiteDrivePreferencesTest.java b/jablib/src/test/java/org/jabref/logic/net/CiteDrivePreferencesTest.java new file mode 100644 index 000000000000..37fded62cf68 --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/net/CiteDrivePreferencesTest.java @@ -0,0 +1,58 @@ +package org.jabref.logic.net; + +import java.util.Optional; + +import com.nimbusds.oauth2.sdk.token.RefreshToken; +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +@NullMarked +class CiteDrivePreferencesTest { + + @ParameterizedTest + @CsvSource({ + "https://api.citedrive.com/,true", + "HTTPS://api.citedrive.com/,true", + "http://api.citedrive.com/,false", + "http://127.0.0.1:8080/,false", + "https:///missing-host,false", + "https://user:password@api.citedrive.com/,false", + "file:///tmp/token,false", + "'not a URL',false" + }) + void serverAddressRequiresHttps(String address, boolean valid) { + // [utest->req~citedrive.https~1] + assertEquals(valid, CiteDrivePreferences.isSecureUrl(address)); + } + + @Test + void changingServerInvalidatesLoginEvenWhenReturningToOriginalAddress() { + // [utest->req~citedrive.credentials.server~1] + CiteDrivePreferences preferences = CiteDrivePreferences.getDefault(); + long revision = preferences.getServerRevision(); + String originalAddress = preferences.getApiBaseUrl(); + preferences.setRefreshToken(new RefreshToken("original")); + + preferences.setApiBaseUrl("https://other.example/"); + preferences.setApiBaseUrl(originalAddress); + + assertFalse(preferences.updateRefreshToken(revision, Optional.of(new RefreshToken("stale")))); + assertEquals(Optional.empty(), Optional.ofNullable(preferences.getRefreshToken())); + } + + @Test + void importingPreferencesKeepsTheLoginForTheImportedServer() { + CiteDrivePreferences preferences = CiteDrivePreferences.getDefault(); + RefreshToken importedToken = new RefreshToken("imported"); + CiteDrivePreferences imported = new CiteDrivePreferences(true, importedToken, true, "https://other.example/", "https://app.example/"); + + preferences.setAll(imported); + + assertEquals(importedToken, preferences.getRefreshToken()); + } +} diff --git a/jablib/src/test/java/org/jabref/logic/preferences/CiteDriveTokenStorageTest.java b/jablib/src/test/java/org/jabref/logic/preferences/CiteDriveTokenStorageTest.java new file mode 100644 index 000000000000..291adb8305f5 --- /dev/null +++ b/jablib/src/test/java/org/jabref/logic/preferences/CiteDriveTokenStorageTest.java @@ -0,0 +1,30 @@ +package org.jabref.logic.preferences; + +import java.util.Optional; + +import com.nimbusds.oauth2.sdk.token.RefreshToken; +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@NullMarked + // [utest->req~citedrive.credentials.server~1] +class CiteDriveTokenStorageTest { + + @Test + void storedTokenCanOnlyBeLoadedForItsOriginalServer() { + RefreshToken token = new RefreshToken("stored"); + String json = JabRefCliPreferences.serializeCiteDriveToken(token, "https://original.example/"); + + assertEquals(token, JabRefCliPreferences.parseCiteDriveToken(json, "https://original.example/")); + assertEquals(Optional.empty(), Optional.ofNullable(JabRefCliPreferences.parseCiteDriveToken(json, "https://other.example/"))); + } + + @Test + void legacyTokenWithoutServerBindingRequiresAnotherLogin() { + String json = new RefreshToken("legacy").toJSONObject().toJSONString(); + + assertEquals(Optional.empty(), Optional.ofNullable(JabRefCliPreferences.parseCiteDriveToken(json, "https://original.example/"))); + } +} diff --git a/jabsrv-cli/src/main/java/org/jabref/http/server/cli/ServerCli.java b/jabsrv-cli/src/main/java/org/jabref/http/server/cli/ServerCli.java index c3e4805bbfa4..6217383e8585 100644 --- a/jabsrv-cli/src/main/java/org/jabref/http/server/cli/ServerCli.java +++ b/jabsrv-cli/src/main/java/org/jabref/http/server/cli/ServerCli.java @@ -8,10 +8,15 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; import org.jabref.architecture.AllowedToUseStandardStreams; import org.jabref.http.server.Server; +import org.jabref.logic.citedrive.CiteDriveOAuthService; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.preferences.JabRefCliPreferences; +import org.jabref.logic.remote.RemotePreferences; import org.glassfish.grizzly.http.server.HttpServer; import org.slf4j.Logger; @@ -21,7 +26,7 @@ @AllowedToUseStandardStreams("This is a CLI application. It resides in the package http.server to be close to the other http server related classes.") @CommandLine.Command(name = "jabsrv", mixinStandardHelpOptions = true, description = "JabSrv - JabRef HTTP server") -public class ServerCli implements Callable { +public class ServerCli implements Callable { private static final Logger LOGGER = LoggerFactory.getLogger(ServerCli.class); @CommandLine.Parameters(arity = "0..*", paramLabel = "FILE", description = "the library files (*.bib) to serve") @@ -33,6 +38,9 @@ public class ServerCli implements Callable { @CommandLine.Option(names = {"-p", "--port"}, description = "the port") private Integer port = 23119; + @CommandLine.Option(names = "--citedrive-login", description = "log in to CiteDrive in the browser, remember the login, and exit") + private boolean citeDriveLogin; + /// Starts an http server serving the last files opened in JabRef /// /// More files can be provided as args. @@ -42,13 +50,17 @@ public static void main(final String[] args) throws InterruptedException { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); - new CommandLine(new ServerCli()).execute(args); + int exitCode = new CommandLine(new ServerCli()).execute(args); + // Only reached after --citedrive-login: serving blocks until the process is killed + System.exit(exitCode); } @Override - public Void call() throws InterruptedException, IOException { + public Integer call() throws InterruptedException, IOException { + JabRefCliPreferences preferences = JabRefCliPreferences.getInstance(); + // The server serves the last opened files (see org.jabref.http.server.resources.LibraryResource.getLibraryPath) - final List filesToServe = new ArrayList<>(JabRefCliPreferences.getInstance().getLastFilesOpenedPreferences().getLastFilesOpened()); + final List filesToServe = new ArrayList<>(preferences.getLastFilesOpenedPreferences().getLastFilesOpened()); // Additionally, files can be provided as args if (files != null) { @@ -68,15 +80,71 @@ public Void call() throws InterruptedException, IOException { uri = new URI(url); } catch (URISyntaxException _) { LOGGER.error("Invalid URL: {}", url); - return null; + return 1; } - Server server = new Server(JabRefCliPreferences.getInstance()); + // Only --citedrive-login registers a pending login here; without it, the callback endpoint + // answers every callback with "login could not be completed" + OAuthSessionRegistry oAuthSessionRegistry = new OAuthSessionRegistry(); + Server server = new Server(preferences, oAuthSessionRegistry); HttpServer httpServer = server.run(filesToServe, uri); + if (citeDriveLogin) { + try { + return logInToCiteDrive(preferences, oAuthSessionRegistry, uri); + } finally { + httpServer.shutdownNow(); + } + } + // Keep the http server running until user kills the process (e.g., presses Ctrl+C) Thread.currentThread().join(); - return null; + return 0; + } + + /// The same flow as in the GUI, with the browser URL printed instead of opened. The refresh token ends up in the keyring, so JabRef itself is logged in afterwards. + /// + /// [impl->req~citedrive.login~1] + /// + /// @return the process exit code + private static int logInToCiteDrive(JabRefCliPreferences preferences, OAuthSessionRegistry oAuthSessionRegistry, URI serverUri) throws InterruptedException { + RemotePreferences remotePreferences = preferences.getRemotePreferences(); + + // CiteDrive redirects the browser to the HTTP server address from the preferences: that has to be this server + int callbackPort = remotePreferences.getHttpServerUri().getPort(); + if (callbackPort != serverUri.getPort()) { + System.err.printf("CiteDrive redirects the browser to port %d (JabRef's HTTP server port), but jabsrv listens on port %d. Use --port %d.%n", callbackPort, serverUri.getPort(), callbackPort); + return 1; + } + + if (!preferences.getCiteDrivePreferences().shouldPersistRefreshToken()) { + System.err.println("JabRef is set to forget the CiteDrive login between sessions, so a login from jabsrv would be lost on exit. Enable \"Remember the login between sessions\" in JabRef's preferences."); + return 1; + } + + CiteDriveOAuthService oAuthService = new CiteDriveOAuthService( + remotePreferences, + preferences.getCiteDrivePreferences(), + oAuthSessionRegistry, + authUrl -> System.out.println("Open this address in your browser to log in to CiteDrive:\n" + authUrl), + // This server already receives the callback, whatever the preferences say + () -> true); + + try { + if (oAuthService.authorizeInteractive().get().isPresent()) { + System.out.println("Logged in to CiteDrive."); + return 0; + } + System.err.println("CiteDrive login failed: could not obtain access token."); + } catch (ExecutionException e) { + if (e.getCause() instanceof TimeoutException) { + System.err.println("CiteDrive login failed: login was not completed in time."); + } else { + LOGGER.error("CiteDrive login failed", e.getCause()); + System.err.println("CiteDrive login failed: " + e.getCause().getLocalizedMessage()); + } + } + return 1; } } diff --git a/jabsrv/src/main/java/module-info.java b/jabsrv/src/main/java/module-info.java index 021783eec1bb..5e6de78ab8af 100644 --- a/jabsrv/src/main/java/module-info.java +++ b/jabsrv/src/main/java/module-info.java @@ -1,27 +1,29 @@ /// HTTP server exposing libraries via a REST API, used by the browser extension and for /// collaborative editing. Built on Jersey/Grizzly. /// -/// Entry points: [org.jabref.http.server.Server], [org.jabref.http.manager.HttpServerManager]. +/// Entry points: [org.jabref.http.server.Server], [org.jabref.http.server.manager.HttpServerManager]. /// /// @see HTTP server code howto module org.jabref.jabsrv { - exports org.jabref.http.server; - exports org.jabref.http.manager; + exports org.jabref.http; exports org.jabref.http.dto to com.google.gson, org.glassfish.hk2.locator; exports org.jabref.http.dto.cayw to com.google.gson; + exports org.jabref.http.server; + exports org.jabref.http.server.cayw to org.glassfish.jersey.core.server; + exports org.jabref.http.server.manager; + exports org.jabref.http.server.resources; + exports org.jabref.http.server.resources.callback to org.glassfish.hk2.locator, org.glassfish.jersey.core.server; + exports org.jabref.http.server.resources.command to org.glassfish.jersey.core.server; + exports org.jabref.http.server.services; + opens org.jabref.http.dto to com.google.gson, org.glassfish.hk2.locator, org.glassfish.hk2.utilities; opens org.jabref.http.server to org.glassfish.hk2.utilities, org.glassfish.hk2.locator; - exports org.jabref.http.server.cayw to org.glassfish.jersey.core.server; - exports org.jabref.http.server.command to org.glassfish.jersey.core.server; opens org.jabref.http.server.cayw to com.google.gson, org.glassfish.hk2.locator, org.glassfish.hk2.utilities; - opens org.jabref.http.dto to com.google.gson, org.glassfish.hk2.locator, org.glassfish.hk2.utilities; - opens org.jabref.http.server.command to com.google.gson, org.glassfish.hk2.locator, org.glassfish.hk2.utilities, tools.jackson.databind; - exports org.jabref.http.server.services; - opens org.jabref.http.server.services to org.glassfish.hk2.locator, org.glassfish.hk2.utilities; - exports org.jabref.http; + opens org.jabref.http.server.resources.callback to org.glassfish.hk2.utilities; opens org.jabref.http.server.resources to org.glassfish.hk2.locator, org.glassfish.hk2.utilities; - exports org.jabref.http.server.resources; + opens org.jabref.http.server.resources.command to com.google.gson, org.glassfish.hk2.locator, org.glassfish.hk2.utilities, tools.jackson.databind; + opens org.jabref.http.server.services to org.glassfish.hk2.locator, org.glassfish.hk2.utilities; requires transitive javafx.base; diff --git a/jabsrv/src/main/java/org/jabref/http/JabrefMediaType.java b/jabsrv/src/main/java/org/jabref/http/JabrefMediaType.java index b1d3598867e0..c10b1a6c5140 100644 --- a/jabsrv/src/main/java/org/jabref/http/JabrefMediaType.java +++ b/jabsrv/src/main/java/org/jabref/http/JabrefMediaType.java @@ -1,6 +1,6 @@ package org.jabref.http; +/// Other mediatypes in [org.jabref.logic.importer.util.MediaTypes] public class JabrefMediaType { - public static final String BIBTEX = "application/x-bibtex"; public static final String JSON_CSL_ITEM = "application/x-bibtex-library-csl+json"; } diff --git a/jabsrv/src/main/java/org/jabref/http/server/Server.java b/jabsrv/src/main/java/org/jabref/http/server/Server.java index 255b6f215d6b..c5cd164f848d 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/Server.java +++ b/jabsrv/src/main/java/org/jabref/http/server/Server.java @@ -18,7 +18,6 @@ import org.jabref.http.dto.GsonMessageBodyWriter; import org.jabref.http.server.cayw.CAYWResource; import org.jabref.http.server.cayw.format.FormatterService; -import org.jabref.http.server.command.CommandResource; import org.jabref.http.server.resources.CitationsResource; import org.jabref.http.server.resources.EntriesResource; import org.jabref.http.server.resources.EntryResource; @@ -28,8 +27,11 @@ import org.jabref.http.server.resources.LibraryResource; import org.jabref.http.server.resources.MapResource; import org.jabref.http.server.resources.RootResource; +import org.jabref.http.server.resources.callback.CallbackResource; +import org.jabref.http.server.resources.command.CommandResource; import org.jabref.http.server.services.CitationCacheService; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.os.OS; import org.jabref.logic.preferences.CliPreferences; import org.jabref.model.database.BibDatabase; @@ -51,9 +53,11 @@ public class Server { private static final Logger LOGGER = LoggerFactory.getLogger(Server.class); private final CliPreferences preferences; + private final OAuthSessionRegistry oAuthSessionRegistry; - public Server(CliPreferences preferences) { + public Server(CliPreferences preferences, OAuthSessionRegistry oAuthSessionRegistry) { this.preferences = preferences; + this.oAuthSessionRegistry = oAuthSessionRegistry; } /// Entry point for the CLI @@ -115,6 +119,8 @@ public HttpServer run(SrvStateManager srvStateManager, UiMessageHandler uiMessag } private HttpServer startServer(ServiceLocator serviceLocator, URI uri) { + // Add remaining services - the difference between CLI and GUI is the SrvStateManager added before + ServiceLocatorUtilities.addOneConstant(serviceLocator, oAuthSessionRegistry); ServiceLocatorUtilities.addOneConstant(serviceLocator, preferences, "preferences", CliPreferences.class); ServiceLocatorUtilities.addOneConstant(serviceLocator, new CitationCacheService()); ServiceLocatorUtilities.addOneConstant(serviceLocator, new FormatterService()); @@ -139,6 +145,7 @@ private HttpServer startServer(ServiceLocator serviceLocator, URI uri) { // Other resources resourceConfig.register(CommandResource.class); + resourceConfig.register(CallbackResource.class); resourceConfig.register(CAYWResource.class); // Supporting classes diff --git a/jabsrv/src/main/java/org/jabref/http/manager/HttpServerManager.java b/jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerManager.java similarity index 73% rename from jabsrv/src/main/java/org/jabref/http/manager/HttpServerManager.java rename to jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerManager.java index 12aa9ab5a2cd..831ebe200af3 100644 --- a/jabsrv/src/main/java/org/jabref/http/manager/HttpServerManager.java +++ b/jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerManager.java @@ -1,9 +1,10 @@ -package org.jabref.http.manager; +package org.jabref.http.server.manager; import java.net.URI; import org.jabref.http.SrvStateManager; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.preferences.CliPreferences; import org.jspecify.annotations.NullMarked; @@ -22,17 +23,20 @@ public class HttpServerManager implements AutoCloseable { @Nullable private HttpServerThread httpServerThread; - public synchronized void start(CliPreferences preferences, SrvStateManager srvStateManager, URI uri) { - start(preferences, srvStateManager, null, uri); + public synchronized void start(CliPreferences preferences, SrvStateManager srvStateManager, OAuthSessionRegistry oAuthSessionRegistry, URI uri) { + start(preferences, srvStateManager, null, oAuthSessionRegistry, uri); } - public synchronized void start(CliPreferences preferences, SrvStateManager srvStateManager, @Nullable UiMessageHandler uiMessageHandler, URI uri) { + public synchronized void start(CliPreferences preferences, SrvStateManager srvStateManager, @Nullable UiMessageHandler uiMessageHandler, OAuthSessionRegistry oAuthSessionRegistry, URI uri) { if (httpServerThread != null) { LOGGER.warn("HTTP server manager already started, cannot start again."); return; } - httpServerThread = new HttpServerThread(preferences, srvStateManager, uiMessageHandler, uri); + httpServerThread = new HttpServerThread(preferences, srvStateManager, uiMessageHandler, oAuthSessionRegistry, uri); + // This enqueues the thread to run in the background + // The JVM will take care of running it at some point in time in the future + // Thus, we cannot check directly if it really runs httpServerThread.start(); LOGGER.debug("Triggered HTTP server start up."); } diff --git a/jabsrv/src/main/java/org/jabref/http/manager/HttpServerThread.java b/jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerThread.java similarity index 87% rename from jabsrv/src/main/java/org/jabref/http/manager/HttpServerThread.java rename to jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerThread.java index bc64e8e2fac7..e7f985798beb 100644 --- a/jabsrv/src/main/java/org/jabref/http/manager/HttpServerThread.java +++ b/jabsrv/src/main/java/org/jabref/http/server/manager/HttpServerThread.java @@ -1,10 +1,11 @@ -package org.jabref.http.manager; +package org.jabref.http.server.manager; import java.net.URI; import org.jabref.http.SrvStateManager; import org.jabref.http.server.Server; import org.jabref.logic.UiMessageHandler; +import org.jabref.logic.citedrive.OAuthSessionRegistry; import org.jabref.logic.preferences.CliPreferences; import jakarta.ws.rs.ProcessingException; @@ -30,11 +31,11 @@ public class HttpServerThread extends Thread { @Nullable private HttpServer httpServer; - public HttpServerThread(CliPreferences cliPreferences, SrvStateManager srvStateManager, UiMessageHandler uiMessageHandler, URI uri) { + public HttpServerThread(CliPreferences cliPreferences, SrvStateManager srvStateManager, UiMessageHandler uiMessageHandler, OAuthSessionRegistry oAuthSessionRegistry, URI uri) { this.srvStateManager = srvStateManager; this.uiMessageHandler = uiMessageHandler; this.uri = uri; - this.server = new Server(cliPreferences); + this.server = new Server(cliPreferences, oAuthSessionRegistry); this.setName("JabSrv - JabRef HTTP Server on " + uri.getHost() + ":" + uri.getPort()); } diff --git a/jabsrv/src/main/java/org/jabref/http/manager/package-info.java b/jabsrv/src/main/java/org/jabref/http/server/manager/package-info.java similarity index 62% rename from jabsrv/src/main/java/org/jabref/http/manager/package-info.java rename to jabsrv/src/main/java/org/jabref/http/server/manager/package-info.java index 6258bb4c371f..3ea9192a127d 100644 --- a/jabsrv/src/main/java/org/jabref/http/manager/package-info.java +++ b/jabsrv/src/main/java/org/jabref/http/server/manager/package-info.java @@ -1,2 +1,2 @@ /// Implemented similar to package org.jabref.logic.remote.server; -package org.jabref.http.manager; +package org.jabref.http.server.manager; diff --git a/jabsrv/src/main/java/org/jabref/http/server/resources/LibraryResource.java b/jabsrv/src/main/java/org/jabref/http/server/resources/LibraryResource.java index 0d2784ec4340..1ae4ad8f03b0 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/resources/LibraryResource.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/LibraryResource.java @@ -11,6 +11,7 @@ import org.jabref.http.dto.BibEntryDTO; import org.jabref.http.server.services.ServerUtils; import org.jabref.logic.citationstyle.JabRefItemDataProvider; +import org.jabref.logic.importer.util.MediaTypes; import org.jabref.logic.preferences.CliPreferences; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; @@ -72,7 +73,7 @@ public String getClsItemJson(@PathParam("id") String id) throws IOException { } @GET - @Produces(JabrefMediaType.BIBTEX) + @Produces(MediaTypes.APPLICATION_BIBTEX) public Response getBibtex(@PathParam("id") String id) { if ("demo".equals(id)) { StreamingOutput stream = output -> { diff --git a/jabsrv/src/main/java/org/jabref/http/server/resources/callback/CallbackResource.java b/jabsrv/src/main/java/org/jabref/http/server/resources/callback/CallbackResource.java new file mode 100644 index 000000000000..c295bc24dc80 --- /dev/null +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/callback/CallbackResource.java @@ -0,0 +1,97 @@ +package org.jabref.http.server.resources.callback; + +import org.jabref.logic.JabRefException; +import org.jabref.logic.citedrive.OAuthSessionRegistry; +import org.jabref.logic.l10n.Localization; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/// The browser is redirected here after logging in at CiteDrive, see [org.jabref.logic.citedrive.CiteDriveOAuthService] +@NullMarked +@Path("/callback") +public class CallbackResource { + + private static final Logger LOGGER = LoggerFactory.getLogger(CallbackResource.class); + + @Inject + OAuthSessionRegistry sessionRegistry; + + @GET + @Produces(MediaType.TEXT_HTML) + public Response citeDriveCallback(@QueryParam("code") @Nullable String code, + @QueryParam("state") @Nullable String state, + @QueryParam("error") @Nullable String error, + @QueryParam("error_description") @Nullable String errorDescription) { + if (state == null || state.isBlank()) { + // Without state, no pending login can be identified + LOGGER.warn("Missing state in CiteDrive callback (error: {})", error); + return Response.serverError().entity(failurePage()).build(); + } + + if (error != null && !error.isBlank()) { + LOGGER.warn("CiteDrive callback error: {} ({})", error, errorDescription); + sessionRegistry.fail(state, new JabRefException("CiteDrive authorization error: " + error, Localization.lang("Login could not be completed"))); + return Response.serverError().entity(failurePage()).build(); + } + + if (code == null || code.isBlank()) { + LOGGER.warn("Missing code in CiteDrive callback"); + sessionRegistry.fail(state, new JabRefException("Missing code in CiteDrive callback", Localization.lang("Login could not be completed"))); + return Response.serverError().entity(failurePage()).build(); + } + + if (!sessionRegistry.complete(state, code)) { + // The login this callback belongs to timed out, or the callback arrived twice + return Response.serverError().entity(failurePage()).build(); + } + return Response.ok(page(Localization.lang("Logged in to CiteDrive"), Localization.lang("Continue in JabRef. You can close this window."))).build(); + } + + private static String failurePage() { + return page(Localization.lang("Login could not be completed"), Localization.lang("Please try again in JabRef. You can close this window.")); + } + + /// The page the user is left with in the browser + private static String page(String heading, String message) { + return """ + + + + + + %s + + + +
+

%s

+

%s

+
+ + + """.formatted(heading, heading, message); + } +} diff --git a/jabsrv/src/main/java/org/jabref/http/server/command/Command.java b/jabsrv/src/main/java/org/jabref/http/server/resources/command/Command.java similarity index 97% rename from jabsrv/src/main/java/org/jabref/http/server/command/Command.java rename to jabsrv/src/main/java/org/jabref/http/server/resources/command/Command.java index 077086fc3f42..8bf6f0a148c5 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/command/Command.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/command/Command.java @@ -1,4 +1,4 @@ -package org.jabref.http.server.command; +package org.jabref.http.server.resources.command; import java.util.List; diff --git a/jabsrv/src/main/java/org/jabref/http/server/command/CommandResource.java b/jabsrv/src/main/java/org/jabref/http/server/resources/command/CommandResource.java similarity index 95% rename from jabsrv/src/main/java/org/jabref/http/server/command/CommandResource.java rename to jabsrv/src/main/java/org/jabref/http/server/resources/command/CommandResource.java index ffbe4b4a7b08..c16342804f6b 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/command/CommandResource.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/command/CommandResource.java @@ -1,4 +1,4 @@ -package org.jabref.http.server.command; +package org.jabref.http.server.resources.command; import jakarta.inject.Inject; import jakarta.ws.rs.Consumes; diff --git a/jabsrv/src/main/java/org/jabref/http/server/command/FocusCommand.java b/jabsrv/src/main/java/org/jabref/http/server/resources/command/FocusCommand.java similarity index 85% rename from jabsrv/src/main/java/org/jabref/http/server/command/FocusCommand.java rename to jabsrv/src/main/java/org/jabref/http/server/resources/command/FocusCommand.java index 37ad47dd9e3a..b8803c9aee4a 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/command/FocusCommand.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/command/FocusCommand.java @@ -1,4 +1,4 @@ -package org.jabref.http.server.command; +package org.jabref.http.server.resources.command; import org.jabref.logic.UiCommand; diff --git a/jabsrv/src/main/java/org/jabref/http/server/command/OpenLibrariesCommand.java b/jabsrv/src/main/java/org/jabref/http/server/resources/command/OpenLibrariesCommand.java similarity index 93% rename from jabsrv/src/main/java/org/jabref/http/server/command/OpenLibrariesCommand.java rename to jabsrv/src/main/java/org/jabref/http/server/resources/command/OpenLibrariesCommand.java index e9d1038123f0..a05404db421a 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/command/OpenLibrariesCommand.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/command/OpenLibrariesCommand.java @@ -1,4 +1,4 @@ -package org.jabref.http.server.command; +package org.jabref.http.server.resources.command; import java.nio.file.Path; import java.util.ArrayList; diff --git a/jabsrv/src/main/java/org/jabref/http/server/command/SelectEntriesCommand.java b/jabsrv/src/main/java/org/jabref/http/server/resources/command/SelectEntriesCommand.java similarity index 98% rename from jabsrv/src/main/java/org/jabref/http/server/command/SelectEntriesCommand.java rename to jabsrv/src/main/java/org/jabref/http/server/resources/command/SelectEntriesCommand.java index 4db39593b773..61c1eca21c1c 100644 --- a/jabsrv/src/main/java/org/jabref/http/server/command/SelectEntriesCommand.java +++ b/jabsrv/src/main/java/org/jabref/http/server/resources/command/SelectEntriesCommand.java @@ -1,4 +1,4 @@ -package org.jabref.http.server.command; +package org.jabref.http.server.resources.command; import java.util.ArrayList; import java.util.List; diff --git a/jabsrv/src/main/resources/META-INF/native-image/org.jabref/jabsrv/reachability-metadata.json b/jabsrv/src/main/resources/META-INF/native-image/org.jabref/jabsrv/reachability-metadata.json index ff433f989bed..837e387e008d 100644 --- a/jabsrv/src/main/resources/META-INF/native-image/org.jabref/jabsrv/reachability-metadata.json +++ b/jabsrv/src/main/resources/META-INF/native-image/org.jabref/jabsrv/reachability-metadata.json @@ -998,31 +998,37 @@ "allDeclaredMethods": true }, { - "type": "org.jabref.http.server.command.CommandResource", + "type": "org.jabref.http.server.resources.command.CommandResource", "allDeclaredConstructors": true, "allDeclaredFields": true, "allDeclaredMethods": true }, { - "type": "org.jabref.http.server.command.Command", + "type": "org.jabref.http.server.resources.command.Command", "allDeclaredConstructors": true, "allDeclaredFields": true, "allDeclaredMethods": true }, { - "type": "org.jabref.http.server.command.FocusCommand", + "type": "org.jabref.http.server.resources.command.FocusCommand", "allDeclaredConstructors": true, "allDeclaredFields": true, "allDeclaredMethods": true }, { - "type": "org.jabref.http.server.command.OpenLibrariesCommand", + "type": "org.jabref.http.server.resources.command.OpenLibrariesCommand", "allDeclaredConstructors": true, "allDeclaredFields": true, "allDeclaredMethods": true }, { - "type": "org.jabref.http.server.command.SelectEntriesCommand", + "type": "org.jabref.http.server.resources.command.SelectEntriesCommand", + "allDeclaredConstructors": true, + "allDeclaredFields": true, + "allDeclaredMethods": true + }, + { + "type": "org.jabref.http.server.resources.callback.CallbackResource", "allDeclaredConstructors": true, "allDeclaredFields": true, "allDeclaredMethods": true diff --git a/jabsrv/src/test/java/org/jabref/http/JabSrvArchitectureTest.java b/jabsrv/src/test/java/org/jabref/http/JabSrvArchitectureTest.java index b901c321ea51..a9359a687ff4 100644 --- a/jabsrv/src/test/java/org/jabref/http/JabSrvArchitectureTest.java +++ b/jabsrv/src/test/java/org/jabref/http/JabSrvArchitectureTest.java @@ -27,17 +27,17 @@ public void doNotUseAfterburnerInjector(JavaClasses classes) { /// `application/json` resources are written by `GsonMessageBodyWriter`, so /// Jackson annotations (`@JsonValue`, `@JsonProperty`, …) silently no-op /// and produce the wrong wire format. The only place Jackson is allowed - /// is `org.jabref.http.server.command`, which still uses + /// is `org.jabref.http.server.resources.command`, which still uses /// `tools.jackson.databind.ObjectMapper` directly for its WebSocket-style /// command dispatch. @ArchTest public void doNotUseJacksonOutsideCommand(JavaClasses classes) { ArchRuleDefinition.noClasses() - .that().resideOutsideOfPackage("org.jabref.http.server.command..") + .that().resideOutsideOfPackage("org.jabref.http.server.resources.command..") .should().dependOnClassesThat().resideInAnyPackage( "com.fasterxml.jackson..", "tools.jackson..") - .because("Gson writes the wire format for resources/DTOs; Jackson annotations are silently ignored. Only org.jabref.http.server.command may use Jackson directly.") + .because("Gson writes the wire format for resources/DTOs; Jackson annotations are silently ignored. Only org.jabref.http.server.resources.command may use Jackson directly.") .check(classes); } } diff --git a/jabsrv/src/test/java/org/jabref/http/server/LibraryResourceTest.java b/jabsrv/src/test/java/org/jabref/http/server/LibraryResourceTest.java index 3b27f24c5933..91fff580525f 100644 --- a/jabsrv/src/test/java/org/jabref/http/server/LibraryResourceTest.java +++ b/jabsrv/src/test/java/org/jabref/http/server/LibraryResourceTest.java @@ -3,6 +3,7 @@ import org.jabref.http.JabrefMediaType; import org.jabref.http.server.resources.LibrariesResource; import org.jabref.http.server.resources.LibraryResource; +import org.jabref.logic.importer.util.MediaTypes; import jakarta.ws.rs.core.Application; import org.glassfish.jersey.server.ResourceConfig; @@ -47,7 +48,7 @@ void getJson() { @Comment{jabref-meta: databaseType:bibtex;} """, - target("/libraries/" + TestBibFile.GENERAL_SERVER_TEST.id).request(JabrefMediaType.BIBTEX).get(String.class)); + target("/libraries/" + TestBibFile.GENERAL_SERVER_TEST.id).request(MediaTypes.APPLICATION_BIBTEX).get(String.class)); } @Test diff --git a/jabsrv/src/test/java/org/jabref/http/server/command/FocusCommandTest.java b/jabsrv/src/test/java/org/jabref/http/server/command/FocusCommandTest.java index cf7833865937..acb1d448f853 100644 --- a/jabsrv/src/test/java/org/jabref/http/server/command/FocusCommandTest.java +++ b/jabsrv/src/test/java/org/jabref/http/server/command/FocusCommandTest.java @@ -1,6 +1,7 @@ package org.jabref.http.server.command; import org.jabref.http.server.ServerTest; +import org.jabref.http.server.resources.command.CommandResource; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Application; diff --git a/versions/build.gradle.kts b/versions/build.gradle.kts index 90af70b35d8b..1f43d6e3463a 100644 --- a/versions/build.gradle.kts +++ b/versions/build.gradle.kts @@ -68,6 +68,8 @@ dependencies.constraints { api("com.knuddels:jtokkit:1.1.0") api("com.konghq:unirest-java-core:4.10.1") api("com.konghq:unirest-modules-gson:4.10.1") + api("com.nimbusds:oauth2-oidc-sdk:11.30.1") + api("com.nimbusds:nimbus-jose-jwt:10.6") api("com.squareup.okhttp3:mockwebserver3:5.5.0") api("com.squareup.okhttp3:okhttp:5.5.0") api("com.squareup.okio:okio-jvm:3.18.2") @@ -108,6 +110,7 @@ dependencies.constraints { api("net.java.dev.jna:jna-platform:5.19.1") api("net.ltgt.errorprone:net.ltgt.errorprone.gradle.plugin:4.3.0") api("net.ltgt.nullaway:nullaway:2.3.0") + api("net.minidev:json-smart:2.6.0") api("org.antlr:antlr4-runtime:4.13.2") api("org.antlr:antlr4:4.13.2") api("org.apache.commons:commons-csv:1.14.1")