private void loadRepositories(@Nullable Runnable onComplete) { if (cloudProject == null) { return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { return; } if (repositories == null) { repositories = new ProjectRepositoriesModelItem(); } repositories.loadRepositories(cloudProject.projectId(), user.get(), onComplete); }
private void mockUserLoggedIn(String username) { when(mockLoginService.isLoggedIn()).thenReturn(true); CredentialedUser mockUser = mock(CredentialedUser.class); when(mockLoginService.getLoggedInUser(TEST_PROJECT.googleUsername())) .thenReturn(Optional.of(mockUser)); when(mockUser.getEmail()).thenReturn(username); } }
/** Mocks list of currently signed in users returned by login service. First user is active. */ private void mockUserList(List<CredentialedUser> userList) { Map<String, CredentialedUser> emailUserMap = userList .stream() .collect(Collectors.toMap(CredentialedUser::getEmail, Function.identity())); when(googleLoginService.getAllUsers()).thenReturn(emailUserMap); when(googleLoginService.getActiveUser()).thenReturn(userList.get(0)); for (CredentialedUser user : userList) { when(googleLoginService.ensureLoggedIn(user.getEmail())).thenReturn(true); when(googleLoginService.getLoggedInUser(user.getEmail())).thenReturn(Optional.of(user)); } }
@VisibleForTesting void setSelectedProject(CloudProject cloudProject) { if (cloudProject != null && !Strings.isNullOrEmpty(cloudProject.googleUsername())) { Optional<CredentialedUser> loggedInUser = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (loggedInUser.isPresent()) { selectedProjectsByAccount.put(loggedInUser.get(), cloudProject.projectName()); accountComboBox.setSelectedItem(loggedInUser.get()); } } else { // no project set, show active user projects by default. accountComboBox.setSelectedItem(Services.getLoginService().getActiveUser()); } }
@Override public void connect(@NotNull ConnectionCallback<AppEngineDeploymentConfiguration> callback) { Services.getLoginService().logInIfNot(); if (!Services.getLoginService().isLoggedIn()) { callback.errorOccurred( AppEngineMessageBundle.message("appengine.deployment.error.not.logged.in")); return; } callback.connected(new AppEngineRuntimeInstance()); } }
private void mockLoggedInUser() { when(Services.getLoginService().isLoggedIn()).thenReturn(true); }
private static boolean needsToSignIn() { Map<String, CredentialedUser> users = Services.getLoginService().getAllUsers(); return users == null || users.isEmpty(); }
@Test public void testStageCredentials_withValidCreds() throws Exception { String username = "jones@gmail.com"; String clientId = "clientId"; String clientSecret = "clientSecret"; String refreshToken = "refreshToken"; when(googleLoginService.ensureLoggedIn(username)).thenReturn(true); when(googleLoginService.getLoggedInUser(username)).thenReturn(Optional.of(credentialedUser)); when(credentialedUser.getGoogleLoginState()).thenReturn(loginState); when(loginState.fetchOAuth2ClientId()).thenReturn(clientId); when(loginState.fetchOAuth2ClientSecret()).thenReturn(clientSecret); when(loginState.fetchOAuth2RefreshToken()).thenReturn(refreshToken); helper.stageCredentials(username); Path credentialFile = helper.getCredentialsPath(); Reader credentialFileReader = Files.newBufferedReader(credentialFile, Charset.defaultCharset()); Map jsonMap = new Gson().fromJson(credentialFileReader, Map.class); credentialFileReader.close(); assertEquals(clientId, jsonMap.get("client_id")); assertEquals(clientSecret, jsonMap.get("client_secret")); assertEquals(refreshToken, jsonMap.get("refresh_token")); assertEquals("authorized_user", jsonMap.get("type")); Files.delete(credentialFile); }
(event) -> { if (event.getEventType() == EventType.ACTIVATED) { if (Services.getLoginService().isLoggedIn()) { handleOpenProjectSelectionDialog(); } else { Services.getLoginService().logIn();
private static String getCustomUserKey(String key) { CredentialedUser activeUser = Services.getLoginService().getActiveUser(); if (activeUser == null) { return key; } return key + "_" + activeUser.getEmail(); }
@Override public Optional<Path> stageCredentials(String googleUserName) { if (Services.getLoginService().ensureLoggedIn(googleUserName)) { return doStageCredentials(googleUserName); } return Optional.empty(); }
@Test public void testInitComponent_loginServiceIsInitialized() { testComponent.initComponent(); verify(googleLoginService).loadPersistedCredentials(); } }
private void mockLoggedOutUser() { when(Services.getLoginService().isLoggedIn()).thenReturn(false); }
private static boolean needsToSignIn() { Map<String, CredentialedUser> users = Services.getLoginService().getAllUsers(); return users.isEmpty(); }
Services.getLoginService().logInIfNot(); if (!Services.getLoginService().isLoggedIn()) { callback.errorOccurred( AppEngineMessageBundle.message("appengine.deployment.error.not.logged.in"));
/** * Retrieves the persistently stored {@link OAuthData} object for the active user, if any. * * @return the persistently stored {@code OAuthData} object for the active user if it exists or an * {@code OAuthData} object all of whose getters return {@code null} . */ public static OAuthData loadOAuthData() { String refreshToken = null; String storedEmail = null; SortedSet<String> storedScopes = new TreeSet<String>(); if (Services.getLoginService().getActiveUser() != null) { Preferences prefs = getPrefs(); refreshToken = prefs.get(getCustomUserKey(OAUTH_DATA_REFRESH_TOKEN_KEY), null); storedEmail = prefs.get(getCustomUserKey(OAUTH_DATA_EMAIL_KEY), null); String storedScopesString = prefs.get(getCustomUserKey(OAUTH_SCOPES_KEY), ""); // Use a set to ensure uniqueness. for (String scope : Splitter.on(DELIMITER).split(storedScopesString)) { storedScopes.add(scope); } } return new OAuthData(null, refreshToken, storedEmail, storedScopes, 0); }
@Test public void testStageCredentials_withoutValidCreds() throws Exception { String username = "jones@gmail.com"; when(googleLoginService.ensureLoggedIn(username)).thenReturn(false); assertFalse(helper.stageCredentials(username).isPresent()); }
@Override public void initComponent() { if (!ApplicationManager.getApplication().isUnitTestMode()) { configureUsageTracking(); } Services.getLoginService().loadPersistedCredentials(); PluginConfigurationService pluginConfigurationService = ServiceManager.getService(PluginConfigurationService.class); PluginInfoService pluginInfoService = ServiceManager.getService(PluginInfoService.class); if (pluginInfoService.shouldEnableErrorFeedbackReporting()) { initErrorReporting(pluginConfigurationService, pluginInfoService); } new ConflictingAppEnginePluginCheck().notifyIfConflicting(); new GoogleAccountPluginUninstaller().uninstallIfPresent(); }
private Optional<Path> doStageCredentials(String googleUsername) { Optional<CredentialedUser> projectUser = Services.getLoginService().getLoggedInUser(googleUsername); GoogleLoginState googleLoginState; if (projectUser.isPresent()) { googleLoginState = projectUser.get().getGoogleLoginState(); } else { return Optional.empty(); } String clientId = googleLoginState.fetchOAuth2ClientId(); String clientSecret = googleLoginState.fetchOAuth2ClientSecret(); String refreshToken = googleLoginState.fetchOAuth2RefreshToken(); Map<String, String> credentialMap = ImmutableMap.of( CLIENT_ID_LABEL, clientId, CLIENT_SECRET_LABEL, clientSecret, REFRESH_TOKEN_LABEL, refreshToken, GCLOUD_USER_TYPE_LABEL, GCLOUD_USER_TYPE); String jsonCredential = new Gson().toJson(credentialMap); try { credentialsPath = FileUtil.createTempFile( "tmp_google_application_default_credential", "json", true /* deleteOnExit */) .toPath(); Files.write(credentialsPath, jsonCredential.getBytes(Charsets.UTF_8)); return Optional.of(credentialsPath); } catch (IOException ex) { throw new RuntimeException(ex); } }
void refresh() { showNotificationPanel(); if (!Services.getLoginService().isLoggedIn()) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.not.logged.in")); } else if (projectSelector.getSelectedProject() == null) { notificationLabel.setText( GoogleCloudStorageMessageBundle.message("gcs.panel.bucket.listing.no.project.selected")); } else { CloudProject cloudProject = projectSelector.getSelectedProject(); Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (user.isPresent()) { loadAndDisplayBuckets(cloudProject.projectId(), user.get()); } else { notificationLabel.setText( GoogleCloudStorageMessageBundle.message( "gcs.panel.bucket.listing.error.loading.buckets")); log.warn("Cloud not load credentialed user for GCS operation. User may not be logged."); } } }