Search in sources :

Example 1 with GoogleLoginState

use of com.google.gdt.eclipse.login.common.GoogleLoginState in project google-cloud-intellij by GoogleCloudPlatform.

the class DefaultGoogleApiClientFactory method getCloudStorageApiClient.

@Override
public Storage getCloudStorageApiClient(@NotNull String projectId, @NotNull CredentialedUser credentialedUser) {
    GoogleLoginState loginState = credentialedUser.getGoogleLoginState();
    String clientId = loginState.fetchOAuth2ClientId();
    String clientSecret = loginState.fetchOAuth2ClientSecret();
    String refreshToken = loginState.fetchOAuth2RefreshToken();
    return StorageOptions.newBuilder().setProjectId(projectId).setCredentials(new UserCredentials(clientId, clientSecret, refreshToken)).build().getService();
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) UserCredentials(com.google.auth.oauth2.UserCredentials)

Example 2 with GoogleLoginState

use of com.google.gdt.eclipse.login.common.GoogleLoginState in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessHandlerTest method setupMocks.

private void setupMocks() {
    GoogleLoginState googleLoginState = mock(GoogleLoginState.class);
    CredentialedUser credentialedUser = mock(CredentialedUser.class);
    when(credentialedUser.getGoogleLoginState()).thenReturn(googleLoginState);
    LinkedHashMap<String, CredentialedUser> users = new LinkedHashMap<String, CredentialedUser>();
    users.put("mockUser@foo.bar", credentialedUser);
    when(mockLoginService.getAllUsers()).thenReturn(users);
    CloudDebugProcessState cloudDebugProcessState = mock(CloudDebugProcessState.class);
    when(cloudDebugProcessState.getUserEmail()).thenReturn("mockUser@foo.bar");
    CloudDebugProcess cloudDebugProcess = mock(CloudDebugProcess.class);
    when(cloudDebugProcess.getProcessState()).thenReturn(cloudDebugProcessState);
    handler = new CloudDebugProcessHandler(cloudDebugProcess);
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with GoogleLoginState

use of com.google.gdt.eclipse.login.common.GoogleLoginState in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudAttachDialogTest method mockCredentials.

private void mockCredentials() throws Exception {
    IntegratedGoogleLoginService mockLoginService = TestUtils.installMockService(IntegratedGoogleLoginService.class);
    GoogleLoginState googleLoginState = mock(GoogleLoginState.class);
    Credential credential = mock(Credential.class);
    this.user = mock(CredentialedUser.class);
    LinkedHashMap<String, CredentialedUser> allusers = new LinkedHashMap<String, CredentialedUser>();
    when(this.user.getCredential()).thenReturn(credential);
    when(this.user.getEmail()).thenReturn(USER);
    when(this.user.getGoogleLoginState()).thenReturn(googleLoginState);
    when(googleLoginState.fetchAccessToken()).thenReturn(PASSWORD);
    when(mockLoginService.getAllUsers()).thenReturn(allusers);
    allusers.put(USER, this.user);
}
Also used : IntegratedGoogleLoginService(com.google.cloud.tools.intellij.login.IntegratedGoogleLoginService) GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) Credential(com.google.api.client.auth.oauth2.Credential) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with GoogleLoginState

use of com.google.gdt.eclipse.login.common.GoogleLoginState in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudSdkAppEngineHelper method doStageCredentials.

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).toPath();
        Files.write(credentialsPath, jsonCredential.getBytes(Charsets.UTF_8));
        return Optional.of(credentialsPath);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) Gson(com.google.gson.Gson) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) IOException(java.io.IOException)

Example 5 with GoogleLoginState

use of com.google.gdt.eclipse.login.common.GoogleLoginState in project google-cloud-intellij by GoogleCloudPlatform.

the class IntegratedIntellijGoogleLoginService method logIn.

/**
 * Opens an external browser to allow the user to sign in. If the user is already signed in, this
 * updates the user's credentials. If the logging process fails, a message dialog will pop up to
 * notify the user. If the logging process succeeds, a logging event will be fired.
 *
 * @param message if not null, then this message is displayed above the login dialog. This is for
 *     when the user is presented the login dialog from doing something other than logging in,
 *     such as accessing Google API services. It should say something like "Importing a project
 *     from Google Project Hosting requires signing in."
 * @param loginCompletedCallback if not null, then this callback is called when the login either
 *     succeeds or fails
 */
@Override
public void logIn(@Nullable final String message, @Nullable final IGoogleLoginCompletedCallback loginCompletedCallback) {
    UsageTrackerProvider.getInstance().trackEvent(LoginTracking.LOGIN_START).ping();
    final CredentialedUser lastActiveUser = users.getActiveUser();
    users.removeActiveUser();
    final GoogleLoginState state = createGoogleLoginState(false);
    // We pass in the current project, which causes intelliJ to properly figure out the
    // parent window. This keeps the cancel dialog on top and visible.
    new Task.Modal(getCurrentProject(), AccountMessageBundle.message("login.service.sign.in.via.browser.modal.text"), true) {

        private boolean loggedIn = false;

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            if (!(indicator instanceof ProgressIndicatorEx)) {
                return;
            }
            ((ProgressIndicatorEx) indicator).addStateDelegate(new AbstractProgressIndicatorExBase() {

                @Override
                public void cancel() {
                    assert uiFacade != null;
                    uiFacade.stop();
                    super.cancel();
                }
            });
            loggedIn = state != null && state.logInWithLocalServer(message);
        }

        @Override
        public void onCancel() {
            notifyOnComplete();
        }

        @Override
        public void onSuccess() {
            notifyOnComplete();
        }

        private void notifyOnComplete() {
            // TODO: add user preference to chose to use pop-up copy and paste dialog
            if (loggedIn) {
                users.addUser(new CredentialedUser(state, () -> {
                    if (loginCompletedCallback != null) {
                        loginCompletedCallback.onLoginCompleted();
                    }
                }));
            } else {
                // Login failed (or aborted), so restore the last active user, if any
                restoreLastActiveUser();
                if (loginCompletedCallback != null) {
                    loginCompletedCallback.onLoginCompleted();
                }
            }
        }

        private void restoreLastActiveUser() {
            if (lastActiveUser != null) {
                setActiveUser(lastActiveUser.getEmail());
            }
        }
    }.queue();
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) Task(com.intellij.openapi.progress.Task) AbstractProgressIndicatorExBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorExBase) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProgressIndicatorEx(com.intellij.openapi.wm.ex.ProgressIndicatorEx)

Aggregations

GoogleLoginState (com.google.gdt.eclipse.login.common.GoogleLoginState)7 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)5 LinkedHashMap (java.util.LinkedHashMap)4 IntegratedGoogleLoginService (com.google.cloud.tools.intellij.login.IntegratedGoogleLoginService)3 Credential (com.google.api.client.auth.oauth2.Credential)2 UserCredentials (com.google.auth.oauth2.UserCredentials)1 Gson (com.google.gson.Gson)1 MockProjectEx (com.intellij.mock.MockProjectEx)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 AbstractProgressIndicatorExBase (com.intellij.openapi.progress.util.AbstractProgressIndicatorExBase)1 ProgressIndicatorEx (com.intellij.openapi.wm.ex.ProgressIndicatorEx)1 PsiManager (com.intellij.psi.PsiManager)1 IOException (java.io.IOException)1 Before (org.junit.Before)1