Skip to content

Commit 1807529

Browse files
authored
Added precondition format checks for App-Id and Api-Key (firebase#1470)
* Added precondition format checks for App-Id and Api-Key * Make Firebase Project id/number mandatory. GCM sender ID can no longer replace it. * code formatting
1 parent 205b523 commit 1807529

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,22 @@ private void preConditionChecks() {
146146
Preconditions.checkNotEmpty(getApplicationId());
147147
Preconditions.checkNotEmpty(getProjectIdentifier());
148148
Preconditions.checkNotEmpty(getApiKey());
149+
Preconditions.checkArgument(
150+
Utils.isValidAppIdFormat(getApplicationId()),
151+
"Please set your Application ID. A valid Firebase App ID is required to communicate "
152+
+ "with Firebase server APIs: It identifies your application with Firebase."
153+
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
154+
Preconditions.checkArgument(
155+
Utils.isValidApiKeyFormat(getApiKey()),
156+
"Please set a valid API key. A Firebase API key is required to communicate with "
157+
+ "Firebase server APIs: It authenticates your project with Google."
158+
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
149159
}
150160

151161
/** Returns the Project Id or Project Number for the Firebase Project. */
152162
@Nullable
153163
String getProjectIdentifier() {
154-
return TextUtils.isEmpty(firebaseApp.getOptions().getProjectId())
155-
? firebaseApp.getOptions().getGcmSenderId()
156-
: firebaseApp.getOptions().getProjectId();
164+
return firebaseApp.getOptions().getProjectId();
157165
}
158166

159167
/**

firebase-installations/src/main/java/com/google/firebase/installations/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
package com.google.firebase.installations;
1616

1717
import android.text.TextUtils;
18+
import androidx.annotation.Nullable;
1819
import com.google.firebase.installations.local.PersistedInstallationEntry;
1920
import java.util.concurrent.TimeUnit;
21+
import java.util.regex.Pattern;
2022

2123
/** Util methods used for {@link FirebaseInstallations} */
2224
class Utils {
2325
public static final long AUTH_TOKEN_EXPIRATION_BUFFER_IN_SECS = TimeUnit.HOURS.toSeconds(1);
26+
private static final String APP_ID_IDENTIFICATION_SUBSTRING = ":";
27+
private static final Pattern API_KEY_FORMAT = Pattern.compile("\\AA[\\w-]{38}\\z");
2428

2529
/**
2630
* Checks if the FIS Auth token is expired or going to expire in next 1 hour {@link
@@ -41,4 +45,12 @@ public boolean isAuthTokenExpired(PersistedInstallationEntry entry) {
4145
public long currentTimeInSecs() {
4246
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
4347
}
48+
49+
static boolean isValidAppIdFormat(@Nullable String appId) {
50+
return appId.contains(APP_ID_IDENTIFICATION_SUBSTRING);
51+
}
52+
53+
static boolean isValidApiKeyFormat(@Nullable String apiKey) {
54+
return API_KEY_FORMAT.matcher(apiKey).matches();
55+
}
4456
}

0 commit comments

Comments
 (0)