Skip to content

Commit

Permalink
#105 Migrate to newer jiotty API to fix double-login
Browse files Browse the repository at this point in the history
  • Loading branch information
ylexus committed Jun 15, 2021
1 parent 61d4199 commit 284454c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ private Bindings() {
@Retention(RUNTIME)
public @interface SettingsRoot {
}

@BindingAnnotation
@Target({FIELD, PARAMETER, METHOD})
@Retention(RUNTIME)
@interface GoogleAuthRootDir {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.yudichev.jiotty.common.lang.TypedBuilder;
import net.yudichev.jiotty.common.time.TimeModule;
import net.yudichev.jiotty.connector.google.common.GoogleApiAuthSettings;
import net.yudichev.jiotty.connector.google.common.GoogleAuthorization;
import net.yudichev.jiotty.connector.google.common.GoogleAuthorizationModule;
import net.yudichev.jiotty.connector.google.drive.GoogleDriveModule;
import net.yudichev.jiotty.connector.google.photos.GooglePhotosModule;

Expand All @@ -16,7 +18,10 @@
import static com.google.api.services.drive.DriveScopes.DRIVE_APPDATA;
import static com.google.common.base.Preconditions.checkNotNull;
import static net.yudichev.googlephotosupload.core.AppGlobals.APP_TITLE;
import static net.yudichev.googlephotosupload.core.Bindings.GoogleAuthRootDir;
import static net.yudichev.jiotty.common.inject.BindingSpec.boundTo;
import static net.yudichev.jiotty.common.inject.BindingSpec.providedBy;
import static net.yudichev.jiotty.connector.google.photos.GooglePhotosScopes.SCOPE_PHOTOS_LIBRARY;

public final class DependenciesModule extends AbstractModule {
private final Path appSettingsRootDir;
Expand All @@ -39,7 +44,7 @@ protected void configure() {

var authDataStoreRootDir = appSettingsRootDir.resolve("auth");
bind(Restarter.class).to(RestarterImpl.class);
bind(Path.class).annotatedWith(RestarterImpl.GoogleAuthRootDir.class).toInstance(authDataStoreRootDir);
bind(Path.class).annotatedWith(GoogleAuthRootDir.class).toInstance(authDataStoreRootDir);

bind(CustomCredentialsManagerImpl.class).in(Singleton.class);
bind(CustomCredentialsManager.class).to(CustomCredentialsManagerImpl.class);
Expand All @@ -49,12 +54,15 @@ protected void configure() {
.setCredentialsUrl(providedBy(CustomCredentialsManagerImpl.class));
googleApiSettingsCustomiser.accept(googleApiSettingsBuilder);
var settings = googleApiSettingsBuilder.build();
install(GooglePhotosModule.builder()
install(GoogleAuthorizationModule.builder()
.setSettings(settings)
.addRequiredScopes(DRIVE_APPDATA, SCOPE_PHOTOS_LIBRARY)
.build());
install(GooglePhotosModule.builder()
.setAuthorization(boundTo(GoogleAuthorization.class))
.build());
install(GoogleDriveModule.builder()
.setSettings(settings)
.addScopes(DRIVE_APPDATA)
.setAuthorization(boundTo(GoogleAuthorization.class))
.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.yudichev.googlephotosupload.core;

import com.google.common.io.MoreFiles;
import net.yudichev.jiotty.common.inject.BaseLifecycleComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Path;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE;
import static net.yudichev.googlephotosupload.core.Bindings.GoogleAuthRootDir;

final class LegacyAuthCleaner extends BaseLifecycleComponent {
private static final Logger logger = LoggerFactory.getLogger(LegacyAuthCleaner.class);
private final Path googleAuthRootDir;

@Inject
LegacyAuthCleaner(@GoogleAuthRootDir Path googleAuthRootDir) {
this.googleAuthRootDir = checkNotNull(googleAuthRootDir);
}

@Override
protected void doStart() {
var legacyAuthDir = googleAuthRootDir.resolve("photos");
try {
MoreFiles.deleteRecursively(legacyAuthDir, ALLOW_INSECURE);
} catch (IOException e) {
logger.warn("Unable to remove legacy auth dir {}", legacyAuthDir, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package net.yudichev.googlephotosupload.core;

import com.google.inject.BindingAnnotation;
import net.yudichev.jiotty.common.app.ApplicationLifecycleControl;

import javax.inject.Inject;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.nio.file.Files;
import java.nio.file.Path;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.MoreFiles.deleteRecursively;
import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static net.yudichev.googlephotosupload.core.Bindings.GoogleAuthRootDir;
import static net.yudichev.jiotty.common.lang.MoreThrowables.asUnchecked;

final class RestarterImpl implements Restarter {
Expand Down Expand Up @@ -43,10 +39,4 @@ public void initiateLogoutAndRestart() {
public void initiateRestart() {
applicationLifecycleControl.initiateRestart();
}

@BindingAnnotation
@Target({FIELD, PARAMETER, METHOD})
@Retention(RUNTIME)
@interface GoogleAuthRootDir {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public UploadPhotosModule() {
protected void configure() {
bind(BuildVersion.class).asEagerSingleton();
boundLifecycleComponent(LegacyLogCleaner.class);
boundLifecycleComponent(LegacyAuthCleaner.class);

bind(ExecutorService.class).annotatedWith(Backpressured.class)
.toProvider(boundLifecycleComponent(BackpressuredExecutorServiceProvider.class));
Expand Down

0 comments on commit 284454c

Please sign in to comment.