Skip to content

Commit 8706286

Browse files
committed
Addressing Rayo's comments
1 parent a2e0dba commit 8706286

File tree

7 files changed

+38
-25
lines changed

7 files changed

+38
-25
lines changed

firebase-installations-interop/src/main/java/com/google/firebase/installations/InstallationTokenResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
@AutoValue
2222
public abstract class InstallationTokenResult {
2323

24-
/** A new FIS Auth-Token, created for this firebase installation. */
24+
/** A new FIS Auth-Token, created for this Firebase Installation. */
2525
@NonNull
2626
public abstract String getToken();
2727
/**
28-
* The amount of time, in milliseconds, before the auth-token expires for this firebase
29-
* installation.
28+
* The amount of time, in milliseconds, before the auth-token expires for this Firebase
29+
* Installation.
3030
*/
3131
@NonNull
3232
public abstract long getTokenExpirationTimestampMillis();

firebase-installations/src/androidTest/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<!-- limitations under the License. -->
1515

1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17-
package="com.google.firebase.installation">
17+
package="com.google.firebase.installations">
1818

1919
<application>
2020
<uses-library android:name="android.test.runner"/>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package com.google.firebase.installation;
15+
package com.google.firebase.installations;
1616

1717
import androidx.test.runner.AndroidJUnit4;
1818
import org.junit.FixMethodOrder;
@@ -26,4 +26,4 @@
2626
*/
2727
@RunWith(AndroidJUnit4.class)
2828
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
29-
public class FirebaseInstallationInstrumentedTest {}
29+
public class FirebaseInstallationsInstrumentedTest {}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package com.google.firebase.installation.local;
15+
package com.google.firebase.installations.local;
1616

1717
import static com.google.common.truth.Truth.assertThat;
1818
import static org.junit.Assert.assertNull;
@@ -22,8 +22,6 @@
2222
import androidx.test.runner.AndroidJUnit4;
2323
import com.google.firebase.FirebaseApp;
2424
import com.google.firebase.FirebaseOptions;
25-
import com.google.firebase.installations.local.FiidCache;
26-
import com.google.firebase.installations.local.FiidCacheEntryValue;
2725
import org.junit.After;
2826
import org.junit.Before;
2927
import org.junit.Test;
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import com.google.firebase.FirebaseException;
1919

2020
/** The class for all Exceptions thrown by {@link FirebaseInstallations}. */
21-
public class FirebaseInstallationException extends FirebaseException {
21+
public class FirebaseInstallationsException extends FirebaseException {
2222

23+
// TODO(ankitagj): Improve clear exception handling.
2324
public enum Status {
2425
SDK_INTERNAL_ERROR,
2526

@@ -28,23 +29,23 @@ public enum Status {
2829

2930
@NonNull private final Status status;
3031

31-
public FirebaseInstallationException(@NonNull Status status) {
32+
public FirebaseInstallationsException(@NonNull Status status) {
3233
this.status = status;
3334
}
3435

35-
public FirebaseInstallationException(@NonNull String message, @NonNull Status status) {
36+
public FirebaseInstallationsException(@NonNull String message, @NonNull Status status) {
3637
super(message);
3738
this.status = status;
3839
}
3940

40-
public FirebaseInstallationException(
41+
public FirebaseInstallationsException(
4142
@NonNull String message, @NonNull Status status, @NonNull Throwable cause) {
4243
super(message, cause);
4344
this.status = status;
4445
}
4546

4647
/**
47-
* Gets the status status for the operation that failed.
48+
* Gets the status for the operation that failed.
4849
*
4950
* @return the status for the FirebaseInstallationsException
5051
*/

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.installations;
1616

1717
import androidx.annotation.NonNull;
18+
import java.nio.ByteBuffer;
1819
import java.nio.charset.Charset;
1920
import java.util.UUID;
2021

@@ -34,7 +35,7 @@ class Utils {
3435
*/
3536
public static final byte REMOVE_PREFIX_MASK = Byte.parseByte("00001111", 2);
3637

37-
/** Length of new-format FIDs. */
38+
/** Length of new-format FIDs as introduced in 2019. */
3839
public static final int FID_LENGTH = 22;
3940

4041
/**
@@ -49,13 +50,8 @@ class Utils {
4950
@NonNull
5051
public static String createRandomFid() {
5152
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
52-
// We create 17 random bytes and ignore the last base64 character later.
53-
byte[] bytes = UUID.randomUUID().toString().substring(0, 17).getBytes(Charset.defaultCharset());
54-
55-
// Replace the first 4 random bits with the constant FID header of 0x7 (0b0111).
56-
bytes[0] = (byte) (FID_4BIT_PREFIX | (bytes[0] & REMOVE_PREFIX_MASK));
57-
58-
return encodeFidBase64UrlSafe(bytes);
53+
byte[] uuidBytes = getBytesFromUUID(UUID.randomUUID());
54+
return encodeFidBase64UrlSafe(uuidBytes);
5955
}
6056

6157
/**
@@ -78,4 +74,22 @@ private static String encodeFidBase64UrlSafe(byte[] rawValue) {
7874
Charset.defaultCharset())
7975
.substring(0, FID_LENGTH);
8076
}
77+
78+
private static byte[] getBytesFromUUID(UUID uuid) {
79+
ByteBuffer bb = ByteBuffer.wrap(new byte[17]);
80+
81+
// The first 4 bits with the constant FID header of 0x7 (0b0111) followed by the last 4 random
82+
// bits of the UUID.
83+
byte fidPrefixWithLast4bitsOfUUID =
84+
(byte)
85+
(FID_4BIT_PREFIX
86+
| (ByteBuffer.allocate(8).putLong(uuid.getLeastSignificantBits()).array()[7]
87+
& REMOVE_PREFIX_MASK));
88+
89+
bb.put(fidPrefixWithLast4bitsOfUUID);
90+
bb.putLong(uuid.getMostSignificantBits());
91+
bb.putLong(uuid.getLeastSignificantBits());
92+
93+
return bb.array();
94+
}
8195
}

firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public class FirebaseInstallationServiceClient {
5050

5151
@NonNull
5252
public InstallationResponse createFirebaseInstallation(
53-
@NonNull String projectID,
5453
@NonNull String apiKey,
54+
@NonNull String projectID,
5555
@NonNull String firebaseInstallationId,
5656
@NonNull String appId)
5757
throws FirebaseInstallationServiceException {
@@ -115,8 +115,8 @@ private static JSONObject buildCreateFirebaseInstallationRequestBody(String fid,
115115

116116
@NonNull
117117
public void deleteFirebaseInstallation(
118-
@NonNull String projectID,
119118
@NonNull String apiKey,
119+
@NonNull String projectID,
120120
@NonNull String fid,
121121
@NonNull String refreshToken)
122122
throws FirebaseInstallationServiceException {
@@ -159,8 +159,8 @@ public void deleteFirebaseInstallation(
159159

160160
@NonNull
161161
public InstallationTokenResult generateAuthToken(
162-
@NonNull String projectID,
163162
@NonNull String apiKey,
163+
@NonNull String projectID,
164164
@NonNull String fid,
165165
@NonNull String refreshToken)
166166
throws FirebaseInstallationServiceException {

0 commit comments

Comments
 (0)