Search in sources :

Example 1 with UserAccount

use of com.owlplug.auth.model.UserAccount in project OwlPlug by DropSnorz.

the class AuthenticationService method createAccountAndAuth.

/**
 * Creates a new account by starting the Authentication flow.
 *
 * @throws AuthenticationException if an error occurs during Authentication
 *                                   flow.
 */
public void createAccountAndAuth() throws AuthenticationException {
    String clientId = owlPlugCredentials.getGoogleAppId();
    String clientSecret = owlPlugCredentials.getGoogleSecret();
    ArrayList<String> scopes = new ArrayList<>();
    scopes.add("https://www.googleapis.com/auth/drive");
    scopes.add("https://www.googleapis.com/auth/userinfo.profile");
    try {
        NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        DataStoreFactory dataStore = new JPADataStoreFactory(googleCredentialDAO);
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientId, clientSecret, scopes).setDataStoreFactory(dataStore).setAccessType("offline").setApprovalPrompt("force").build();
        UserAccount userAccount = new UserAccount();
        userAccountDAO.save(userAccount);
        receiver = new LocalServerReceiver();
        AuthorizationCodeInstalledApp authCodeAccess = new AuthorizationCodeInstalledApp(flow, receiver);
        Credential credential = authCodeAccess.authorize(userAccount.getKey());
        Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("OwlPlug").build();
        Userinfoplus userinfo = oauth2.userinfo().get().execute();
        userAccount.setName(userinfo.getName());
        userAccount.setIconUrl(userinfo.getPicture());
        userAccount.setAccountProvider(UserAccountProvider.GOOGLE);
        userAccount.setCredential(googleCredentialDAO.findByKey(userAccount.getKey()));
        userAccountDAO.save(userAccount);
        this.getPreferences().putLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, userAccount.getId());
    } catch (GeneralSecurityException | IOException e) {
        log.error("Error during authentication", e);
        throw new AuthenticationException(e);
    } finally {
        // Delete accounts without complete setup
        userAccountDAO.deleteInvalidAccounts();
    }
}
Also used : Userinfoplus(com.google.api.services.oauth2.model.Userinfoplus) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) AuthenticationException(com.owlplug.auth.utils.AuthenticationException) Oauth2(com.google.api.services.oauth2.Oauth2) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) IOException(java.io.IOException) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) UserAccount(com.owlplug.auth.model.UserAccount) DataStoreFactory(com.google.api.client.util.store.DataStoreFactory) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory)

Example 2 with UserAccount

use of com.owlplug.auth.model.UserAccount in project OwlPlug by DropSnorz.

the class AccountCellFactory method call.

@Override
public ListCell<AccountItem> call(ListView<AccountItem> l) {
    return new ListCell<AccountItem>() {

        @Override
        protected void updateItem(AccountItem item, boolean empty) {
            super.updateItem(item, empty);
            setAlignment(align);
            if (item instanceof UserAccount) {
                UserAccount account = (UserAccount) item;
                HBox cell = new HBox();
                cell.setSpacing(5);
                cell.setAlignment(align);
                if (account.getIconUrl() != null) {
                    Image image = imageCache.get(account.getIconUrl());
                    ImageView imageView = new ImageView(image);
                    imageView.setFitWidth(32);
                    imageView.setFitHeight(32);
                    cell.getChildren().add(imageView);
                }
                Label label = new Label(account.getName());
                cell.getChildren().add(label);
                if (showDeleteButton) {
                    Region growingArea = new Region();
                    HBox.setHgrow(growingArea, Priority.ALWAYS);
                    cell.getChildren().add(growingArea);
                    Hyperlink deleteButton = new Hyperlink("X");
                    deleteButton.getStyleClass().add("hyperlink-button");
                    cell.getChildren().add(deleteButton);
                    deleteButton.setOnAction(e -> {
                        authenticationService.deleteAccount(account);
                    });
                }
                setGraphic(cell);
                setText(null);
                return;
            }
            if (item instanceof AccountMenuItem) {
                AccountMenuItem accountMenuItem = (AccountMenuItem) item;
                setGraphic(null);
                setText(accountMenuItem.getText());
            }
        }
    };
}
Also used : HBox(javafx.scene.layout.HBox) ListCell(javafx.scene.control.ListCell) Label(javafx.scene.control.Label) Region(javafx.scene.layout.Region) ImageView(javafx.scene.image.ImageView) Image(javafx.scene.image.Image) UserAccount(com.owlplug.auth.model.UserAccount) Hyperlink(javafx.scene.control.Hyperlink)

Example 3 with UserAccount

use of com.owlplug.auth.model.UserAccount in project OwlPlug by DropSnorz.

the class MainController method refreshAccounts.

/**
 * Refresh Account comboBox.
 */
public void refreshAccounts() {
    ArrayList<UserAccount> accounts = new ArrayList<UserAccount>();
    for (UserAccount account : authenticationService.getAccounts()) {
        accounts.add(account);
    }
    accountComboBox.hide();
    accountComboBox.getItems().clear();
    accountComboBox.getItems().setAll(accounts);
    accountComboBox.getItems().add(new AccountMenuItem(" + New Account"));
    long selectedAccountId = this.getPreferences().getLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, -1);
    if (selectedAccountId != -1) {
        Optional<UserAccount> selectedAccount = authenticationService.getUserAccountById(selectedAccountId);
        if (selectedAccount.isPresent()) {
            // Bug workaround. The only way to pre-select the account is to find its
            // index in the list
            // If not, the selected cell is not rendered correctly
            accountComboBox.getItems().stream().filter(account -> account.getId().equals(selectedAccount.get().getId())).findAny().ifPresent(accountComboBox.getSelectionModel()::select);
        } else {
            accountComboBox.setValue(null);
        }
    } else {
        accountComboBox.setValue(null);
    }
}
Also used : AccountMenuItem(com.owlplug.auth.ui.AccountMenuItem) ArrayList(java.util.ArrayList) UserAccount(com.owlplug.auth.model.UserAccount)

Example 4 with UserAccount

use of com.owlplug.auth.model.UserAccount in project OwlPlug by DropSnorz.

the class MainController method initialize.

/**
 * FXML initialize method.
 */
@FXML
public void initialize() {
    viewRegistry.preload();
    this.tabPaneHeader.getSelectionModel().selectedIndexProperty().addListener((options, oldValue, newValue) -> {
        tabPaneContent.getSelectionModel().select(newValue.intValue());
        leftDrawer.close();
        // store tab.
        if (newValue.intValue() == 2) {
            storeController.requestLayout();
        }
    });
    accountComboBox.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
        if (newValue instanceof AccountMenuItem) {
            accountController.show();
            // Delay comboBox selector change
            Platform.runLater(() -> accountComboBox.setValue(oldValue));
        }
        if (newValue instanceof UserAccount) {
            UserAccount userAccount = (UserAccount) newValue;
            this.getPreferences().putLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, userAccount.getId());
        }
        accountComboBox.hide();
    });
    accountComboBox.setButtonCell(new AccountCellFactory(imageCache, Pos.CENTER_RIGHT).call(null));
    accountComboBox.setCellFactory(new AccountCellFactory(authenticationService, imageCache, true));
    JFXComboBoxListViewSkin<AccountItem> accountCBSkin = new JFXComboBoxListViewSkin<AccountItem>(accountComboBox);
    accountCBSkin.setHideOnClick(false);
    accountComboBox.setSkin(accountCBSkin);
    refreshAccounts();
    downloadUpdateButton.setOnAction(e -> {
        PlatformUtils.openDefaultBrowser(this.getApplicationDefaults().getUpdateDownloadUrl());
    });
    updatePane.setVisible(false);
    Task<Boolean> retrieveUpdateStatusTask = new Task<Boolean>() {

        @Override
        protected Boolean call() throws Exception {
            return updateService.isUpToDate();
        }
    };
    retrieveUpdateStatusTask.setOnSucceeded(e -> {
        if (!retrieveUpdateStatusTask.getValue()) {
            updatePane.setVisible(true);
        }
    });
    new Thread(retrieveUpdateStatusTask).start();
}
Also used : Task(javafx.concurrent.Task) AccountMenuItem(com.owlplug.auth.ui.AccountMenuItem) JFXComboBoxListViewSkin(com.jfoenix.skins.JFXComboBoxListViewSkin) AccountItem(com.owlplug.auth.ui.AccountItem) UserAccount(com.owlplug.auth.model.UserAccount) AccountCellFactory(com.owlplug.auth.ui.AccountCellFactory) FXML(javafx.fxml.FXML)

Aggregations

UserAccount (com.owlplug.auth.model.UserAccount)4 AccountMenuItem (com.owlplug.auth.ui.AccountMenuItem)2 ArrayList (java.util.ArrayList)2 Credential (com.google.api.client.auth.oauth2.Credential)1 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)1 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)1 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 DataStoreFactory (com.google.api.client.util.store.DataStoreFactory)1 Oauth2 (com.google.api.services.oauth2.Oauth2)1 Userinfoplus (com.google.api.services.oauth2.model.Userinfoplus)1 JFXComboBoxListViewSkin (com.jfoenix.skins.JFXComboBoxListViewSkin)1 JPADataStoreFactory (com.owlplug.auth.JPADataStoreFactory)1 AccountCellFactory (com.owlplug.auth.ui.AccountCellFactory)1 AccountItem (com.owlplug.auth.ui.AccountItem)1 AuthenticationException (com.owlplug.auth.utils.AuthenticationException)1 IOException (java.io.IOException)1