Skip to content

Add firebase auth destroy check before tenant operations. #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,13 @@ private void checkNotDestroyed() {
}
}

void destroy() {
final void destroy() {
synchronized (lock) {
doDestroy();
destroyed.set(true);
}
}

/** Performs any additional required clean up. */
protected abstract void doDestroy();
}
11 changes: 11 additions & 0 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.auth.internal.FirebaseTokenFactory;
import com.google.firebase.internal.FirebaseService;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* This class is the entry point for all server-side Firebase Authentication actions.
Expand All @@ -37,6 +38,7 @@ public class FirebaseAuth extends AbstractFirebaseAuth {
private static final String SERVICE_ID = FirebaseAuth.class.getName();

private final Supplier<TenantManager> tenantManager;
private final AtomicBoolean tenantManagerCreated = new AtomicBoolean(false);

private FirebaseAuth(final Builder builder) {
super(
Expand All @@ -47,6 +49,7 @@ private FirebaseAuth(final Builder builder) {
tenantManager = threadSafeMemoize(new Supplier<TenantManager>() {
@Override
public TenantManager get() {
tenantManagerCreated.set(true);
return new TenantManager(builder.firebaseApp, getUserManager());
}
});
Expand Down Expand Up @@ -80,6 +83,14 @@ public static synchronized FirebaseAuth getInstance(FirebaseApp app) {
return service.getInstance();
}

@Override
protected void doDestroy() {
// Only destroy the tenant manager if it has been created.
if (tenantManagerCreated.get()) {
getTenantManager().destroy();
}
}

private static FirebaseAuth fromApp(final FirebaseApp app) {
return FirebaseAuth.builder()
.setFirebaseApp(app)
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/com/google/firebase/auth/TenantManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.api.client.json.JsonFactory;
import com.google.api.core.ApiFuture;
Expand All @@ -31,6 +32,7 @@
import com.google.firebase.internal.CallableOperation;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* This class can be used to perform a variety of tenant-related operations, including creating,
Expand All @@ -40,6 +42,9 @@
*/
public final class TenantManager {

private final Object lock = new Object();
private final AtomicBoolean destroyed = new AtomicBoolean(false);

private final FirebaseApp firebaseApp;
private final FirebaseUserManager userManager;

Expand Down Expand Up @@ -74,7 +79,7 @@ public ApiFuture<Tenant> getTenantAsync(@NonNull String tenantId) {
}

private CallableOperation<Tenant, FirebaseAuthException> getTenantOp(final String tenantId) {
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
checkNotDestroyed();
checkArgument(!Strings.isNullOrEmpty(tenantId), "tenantId must not be null or empty");
return new CallableOperation<Tenant, FirebaseAuthException>() {
@Override
Expand Down Expand Up @@ -142,7 +147,7 @@ public ApiFuture<ListTenantsPage> listTenantsAsync(@Nullable String pageToken, i

private CallableOperation<ListTenantsPage, FirebaseAuthException> listTenantsOp(
@Nullable final String pageToken, final int maxResults) {
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
checkNotDestroyed();
final TenantSource tenantSource = new DefaultTenantSource(userManager);
final PageFactory factory = new PageFactory(tenantSource, maxResults, pageToken);
return new CallableOperation<ListTenantsPage, FirebaseAuthException>() {
Expand Down Expand Up @@ -180,7 +185,7 @@ public ApiFuture<Tenant> createTenantAsync(@NonNull CreateRequest request) {

private CallableOperation<Tenant, FirebaseAuthException> createTenantOp(
final CreateRequest request) {
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
checkNotDestroyed();
checkNotNull(request, "create request must not be null");
return new CallableOperation<Tenant, FirebaseAuthException>() {
@Override
Expand Down Expand Up @@ -218,7 +223,7 @@ public ApiFuture<Tenant> updateTenantAsync(@NonNull UpdateRequest request) {

private CallableOperation<Tenant, FirebaseAuthException> updateTenantOp(
final UpdateRequest request) {
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
checkNotDestroyed();
checkNotNull(request, "update request must not be null");
return new CallableOperation<Tenant, FirebaseAuthException>() {
@Override
Expand Down Expand Up @@ -253,7 +258,7 @@ public ApiFuture<Void> deleteTenantAsync(String tenantId) {
}

private CallableOperation<Void, FirebaseAuthException> deleteTenantOp(final String tenantId) {
// TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
checkNotDestroyed();
checkArgument(!Strings.isNullOrEmpty(tenantId), "tenantId must not be null or empty");
return new CallableOperation<Void, FirebaseAuthException>() {
@Override
Expand All @@ -263,4 +268,17 @@ protected Void execute() throws FirebaseAuthException {
}
};
}

void checkNotDestroyed() {
synchronized (lock) {
checkState(
!destroyed.get(),
"TenantManager instance is no longer alive. This happens when "
+ "the parent FirebaseApp instance has been deleted.");
}
}

protected void destroy() {
destroyed.set(true);
}
}