Search code examples
javascriptauthenticationjavafxpushbulletwebengine

JavaFX WebEngine and Pushbullet authentication


I am working on a small JavaFX app that aims to open the Pushbullet authentication Webpage in order to get an OAuth access token. Writing it in JavaFX was really simple. However, when I fill the form and validate it using Google account, authentication fails. Since the same works when I open the URL in my native Chrome browser, I think the issue is related to the limited capability of JavaFX WebEngine implementation but I cannot figure out what it is.

Below is the piece of code I used:

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class GraphicalAuthorizationHelper extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception {
        BorderPane borderPane = new BorderPane();

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();

        borderPane.setCenter(browser);
        webEngine.documentProperty().addListener((prop, oldDoc, newDoc) -> {
            //enableFirebug(webEngine);
        });

        String url = "https://www.pushbullet.com/authorize?client_id=hjT07gVHUYnzN1iVlWIFU7K1Sxype0bf&redirect_uri=http://oauth.yfiton.com/callback&response_type=token";

        webEngine.load(url);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        Scene scene = new Scene(
                borderPane,
                primaryScreenBounds.getWidth() * 0.55,
                primaryScreenBounds.getHeight() * 0.65);

        stage.setScene(scene);
        stage.show();
    }

    private static void enableFirebug(final WebEngine engine) {
        engine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
    }

}

I have noticed that the code can be used to get an Oauth access token for Twitter or Slack with success but not with Pushbullet authentication mechanism. When the form is submitted, I get error Error signing in to your account: Could not connect to server:

Pushbullet authentication error

I have sent an email to Pushbullet team about two weeks ago but I got no answer. In the meantime, I have tried to use firebug to understand what could be the issue but without success:

enter image description here

Any idea, comment, etc. is welcome.


Solution

  • As suggested by Chris Pushbullet, the issue was related to CORS. I succeeded to solve it by setting Java system property sun.net.http.allowRestrictedHeaders to true on the JVM that runs the JavaFX WebEngine instance.