use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudRepositoryService method list.
@NotNull
public ListReposResponse list(CredentialedUser user, String cloudProject) throws CloudRepositoryServiceException {
try {
Credential credential = user.getCredential();
HttpRequestInitializer initializer = httpRequest -> {
HttpHeaders headers = new HttpHeaders();
httpRequest.setConnectTimeout(LIST_TIMEOUT_MS);
httpRequest.setReadTimeout(LIST_TIMEOUT_MS);
httpRequest.setHeaders(headers);
credential.initialize(httpRequest);
};
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
Source source = new Source.Builder(httpTransport, JacksonFactory.getDefaultInstance(), initializer).setRootUrl(CLOUD_SOURCE_API_ROOT_URL).setServicePath("").setApplicationName(userAgent).build();
return new CustomUrlSourceRequest(source, cloudProject).execute();
} catch (IOException | GeneralSecurityException ex) {
throw new CloudRepositoryServiceException();
}
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class GcpHttpAuthDataProvider method getAuthData.
@Nullable
@Override
public AuthData getAuthData(@NotNull String url) {
final Project currentProject = getCurrentProject();
if ((currentProject != null || Context.currentContext != null) && isGcpUrl(url)) {
Context currentContext = // always prefer context over project setting.
Context.currentContext;
String userEmail = currentContext != null ? currentContext.userName : null;
if (Strings.isNullOrEmpty(userEmail) && currentProject != null) {
userEmail = PropertiesComponent.getInstance(currentProject).getValue(GCP_USER, "");
}
CredentialedUser targetUser = getUserFromEmail(userEmail);
if (targetUser == null) {
// show a dialog allowing the user to select a login. (new project recognized)
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
@Override
public void run() {
SelectUserDialog dialog = new SelectUserDialog(currentProject, GctBundle.getString("httpauthprovider.chooselogin"));
DialogManager.show(dialog);
chooseManualLogin = !dialog.isOK();
selectedUser = dialog.getSelectedUser();
}
}, ModalityState.defaultModalityState());
if (chooseManualLogin) {
return null;
}
userEmail = selectedUser;
targetUser = getUserFromEmail(userEmail);
if (targetUser != null && currentProject != null && Context.currentContext == null) {
PropertiesComponent.getInstance(currentProject).setValue(GCP_USER, userEmail);
}
}
if (targetUser != null) {
try {
return new AuthData(targetUser.getEmail(), targetUser.getGoogleLoginState().fetchAccessToken());
} catch (IOException ex) {
LOG.error("IOException creating authdata:" + ex.toString());
}
}
}
return null;
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class ProjectRepositoriesModelItem method loadRepositories.
@SuppressWarnings("FutureReturnValueIgnored")
public void loadRepositories(@NotNull String cloudProject, @NotNull CredentialedUser user, @Nullable Runnable onComplete) {
setUserObject(cloudProject);
cloudRepositoryService.listAsync(user, cloudProject).thenAccept(response -> {
removeAllChildren();
List<Repo> repositories = response.getRepos();
if (!response.isEmpty() && repositories != null) {
repositories.forEach(repo -> {
Object name = repo.get("name");
if (name != null) {
add(new RepositoryModelItem(name.toString()));
}
});
} else {
add(new ResourceEmptyModelItem(GctBundle.message("cloud.repository.list.empty")));
}
if (onComplete != null) {
onComplete.run();
}
}).exceptionally(response -> {
removeAllChildren();
add(new ResourceErrorModelItem(GctBundle.message("cloud.repository.list.error")));
if (onComplete != null) {
onComplete.run();
}
return null;
});
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudDebuggerClient method getClient.
/**
* Returns a cloud debugger connection given a user email to indicate the credentials to use. The
* function may return null if the user is not logged in.
*/
@Nullable
private static Debugger getClient(@Nullable final String userEmail, final int timeout) {
if (Strings.isNullOrEmpty(userEmail)) {
LOG.warn("unexpected null email in controller initialize.");
return null;
}
final String hashkey = userEmail + timeout;
Debugger cloudDebuggerClient = debuggerClientsFromUserEmail.get(hashkey);
if (cloudDebuggerClient == null) {
try {
final CredentialedUser user = Services.getLoginService().getAllUsers().get(userEmail);
final Credential credential = (user != null ? user.getCredential() : null);
if (credential != null) {
user.getGoogleLoginState().addLoginListener(new LoginListener() {
@Override
public void statusChanged(boolean login) {
if (!login) {
// aggressively remove the cached item on any status change.
debuggerClientsFromUserEmail.remove(hashkey);
} else {
// NOPMD
// user logged in, should we do something?
}
}
});
HttpRequestInitializer initializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
HttpHeaders headers = new HttpHeaders();
httpRequest.setConnectTimeout(timeout);
httpRequest.setReadTimeout(timeout);
httpRequest.setHeaders(headers);
credential.initialize(httpRequest);
}
};
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
cloudDebuggerClient = new Builder(httpTransport, JSON_FACTORY, initializer).setRootUrl(ROOT_URL).setApplicationName(userAgent).build().debugger();
}
} catch (IOException ex) {
LOG.warn("Error connecting to Cloud Debugger API", ex);
} catch (GeneralSecurityException ex) {
LOG.warn("Error connecting to Cloud Debugger API", ex);
}
if (cloudDebuggerClient != null) {
debuggerClientsFromUserEmail.put(hashkey, cloudDebuggerClient);
}
}
return cloudDebuggerClient;
}
use of com.google.cloud.tools.intellij.login.CredentialedUser in project google-cloud-intellij by GoogleCloudPlatform.
the class UsersListCellRenderer method getListCellRendererComponent.
@Nullable
@Override
public Component getListCellRendererComponent(JList<? extends UsersListItem> list, UsersListItem usersListItem, int index, boolean isSelected, boolean cellHasFocus) {
if (usersListItem instanceof NoUsersListItem) {
return createNoUserDisplay();
}
final CredentialedUser activeUser = Services.getLoginService().getActiveUser();
final boolean isActiveUserSelected = activeUser != null && usersListItem.getUserEmail().equals(activeUser.getEmail());
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, HGAP, VGAP));
mainPanel.setMinimumSize(isActiveUserSelected ? activeMainPanelDimension : mainPanelDimension);
mainPanel.setAlignmentX(LEFT_ALIGNMENT);
// Update colors
final Color bg = isActiveUserSelected ? activeColor : inactiveColor;
final Color fg = isActiveUserSelected ? UIUtil.getListSelectionForeground() : UIUtil.getListForeground();
mainPanel.setBackground(bg);
mainPanel.setForeground(fg);
Image image = usersListItem.getUserPicture();
if (image == null) {
// Use default profile image.
image = Toolkit.getDefaultToolkit().getImage(UsersListCellRenderer.class.getResource(DEFAULT_AVATAR));
}
final int imageWidth;
final int imageHeight;
final Map<String, Image> userImageCache;
if (isActiveUserSelected) {
imageWidth = ACTIVE_USER_IMAGE_WIDTH;
imageHeight = ACTIVE_USER_IMAGE_HEIGHT;
userImageCache = userLargeImageCache;
} else {
imageWidth = PLAIN_USER_IMAGE_WIDTH;
imageHeight = PLAIN_USER_IMAGE_HEIGHT;
userImageCache = userSmallImageCache;
}
final Image scaledImage;
if (!userImageCache.containsKey(usersListItem.getUserEmail())) {
scaledImage = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
userImageCache.put(usersListItem.getUserEmail(), scaledImage);
} else {
scaledImage = userImageCache.get(usersListItem.getUserEmail());
}
final JComponent textPanel;
if (isActiveUserSelected) {
textPanel = createActiveTextDisplay(usersListItem);
} else {
textPanel = createTextDisplay(false, usersListItem);
}
mainPanel.add(new JLabel(new ImageIcon(scaledImage)));
mainPanel.add(textPanel);
mainPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, JBColor.border()));
return mainPanel;
}
Aggregations