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();
}
}
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());
}
}
};
}
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);
}
}
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();
}
Aggregations