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