Skip to content

Adding Util class for FIrebaseInstallations APIs. #676

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 47 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d39159b
Adding Firebase Installations module
ankitaj224 Jul 22, 2019
e321d7d
Readding .idea files that were deleted in previous commit
ankitaj224 Jul 22, 2019
c25ca83
Revert "Adding Firebase Installations module"
ankitaj224 Jul 22, 2019
6c54e73
Revert "Readding .idea files that were deleted in previous commit"
ankitaj224 Jul 22, 2019
1923730
Adding Firebase Installations module.
ankitaj224 Jul 22, 2019
e79e3ad
Readding .idea files that were deleted in previous commit
ankitaj224 Jul 22, 2019
bb41402
Revert "Readding .idea files that were deleted in previous commit"
ankitaj224 Jul 22, 2019
2851062
Revert "Adding Firebase Installations module" with hidden files
ankitaj224 Jul 22, 2019
a2f7f64
Addressing review comments.
ankitaj224 Jul 23, 2019
cde2320
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
a27b347
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
53dc90b
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
a7982e1
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Jul 29, 2019
7285408
Initial Code structure for FIS Android SDK (#648)
ankitaj224 Jul 24, 2019
19c5573
Adding Firebase Installations module
ankitaj224 Jul 22, 2019
892fddf
Readding .idea files that were deleted in previous commit
ankitaj224 Jul 22, 2019
8d3dc73
Revert "Adding Firebase Installations module"
ankitaj224 Jul 22, 2019
4ad27d7
Revert "Readding .idea files that were deleted in previous commit"
ankitaj224 Jul 22, 2019
7f45186
Adding Firebase Installations module.
ankitaj224 Jul 22, 2019
1b1766f
Readding .idea files that were deleted in previous commit
ankitaj224 Jul 22, 2019
4ac3956
Revert "Readding .idea files that were deleted in previous commit"
ankitaj224 Jul 22, 2019
da9aac6
Revert "Adding Firebase Installations module" with hidden files
ankitaj224 Jul 22, 2019
e9c10c2
Addressing review comments.
ankitaj224 Jul 23, 2019
ef6cf7d
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
284ab52
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
4332d7f
Http client to call FIS backend service.
ankitaj224 Jul 24, 2019
113216e
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Jul 29, 2019
936e2f8
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Jul 29, 2019
1f427eb
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Jul 29, 2019
5462241
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Jul 29, 2019
aa21227
Addresing comments and introducing new FirebaseInstallationService
ankitaj224 Jul 30, 2019
23ff7f2
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Jul 30, 2019
7da76c0
Implementing getId method in FirebaseInstallation to call backend.
ankitaj224 Jul 31, 2019
5152bf0
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Jul 31, 2019
97f461a
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Jul 31, 2019
124227c
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Jul 31, 2019
1ccde5e
1. Adding instrumentation tests.
ankitaj224 Aug 6, 2019
0a79342
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Aug 7, 2019
71c90f0
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Aug 7, 2019
d11417b
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 7, 2019
a2e0dba
Addressing Di's comments.
ankitaj224 Aug 7, 2019
8706286
Addressing Rayo's comments
ankitaj224 Aug 12, 2019
34848c6
Updating parameters order in ServiceClient.
ankitaj224 Aug 14, 2019
f4c44b0
Updating createRandomFid as per Rayo's comments.
ankitaj224 Aug 14, 2019
44bef2f
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Aug 29, 2019
e5e0d67
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Aug 30, 2019
429581e
getId() implementation with instrumented tests. (#703)
ankitaj224 Aug 30, 2019
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 @@ -21,12 +21,12 @@
@AutoValue
public abstract class InstallationTokenResult {

/** A new FIS Auth-Token, created for this firebase installation. */
/** A new FIS Auth-Token, created for this Firebase Installation. */
@NonNull
public abstract String getAuthToken();
public abstract String getToken();
/**
* The amount of time, in milliseconds, before the auth-token expires for this firebase
* installation.
* The amount of time, in milliseconds, before the auth-token expires for this Firebase
* Installation.
*/
@NonNull
public abstract long getTokenExpirationTimestampMillis();
Expand All @@ -43,7 +43,7 @@ public static InstallationTokenResult.Builder builder() {
@AutoValue.Builder
public abstract static class Builder {
@NonNull
public abstract Builder setAuthToken(@NonNull String value);
public abstract Builder setToken(@NonNull String value);

@NonNull
public abstract Builder setTokenExpirationTimestampMillis(@NonNull long value);
Expand Down
2 changes: 1 addition & 1 deletion firebase-installations/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- limitations under the License. -->

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

<application>
<uses-library android:name="android.test.runner"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installations;

import androidx.test.runner.AndroidJUnit4;
import org.junit.FixMethodOrder;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FirebaseInstallationsInstrumentedTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installation.local;
package com.google.firebase.installations.local;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;
Expand All @@ -22,8 +22,6 @@
import androidx.test.runner.AndroidJUnit4;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.installations.local.FiidCache;
import com.google.firebase.installations.local.FiidCacheEntryValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down
3 changes: 3 additions & 0 deletions firebase-installations/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installations;

import androidx.annotation.NonNull;
import com.google.firebase.FirebaseException;

/** The class for all Exceptions thrown by {@link FirebaseInstallations}. */
public class FirebaseInstallationsException extends FirebaseException {

// TODO(ankitagj): Improve clear exception handling.
public enum Status {
SDK_INTERNAL_ERROR,

CLIENT_ERROR
}

@NonNull private final Status status;

public FirebaseInstallationsException(@NonNull Status status) {
this.status = status;
}

public FirebaseInstallationsException(@NonNull String message, @NonNull Status status) {
super(message);
this.status = status;
}

public FirebaseInstallationsException(
@NonNull String message, @NonNull Status status, @NonNull Throwable cause) {
super(message, cause);
this.status = status;
}

/**
* Gets the status for the operation that failed.
*
* @return the status for the FirebaseInstallationsException
*/
@NonNull
public Status getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.installations;

import androidx.annotation.NonNull;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.UUID;

/** Util methods used for {@link FirebaseInstallations} */
class Utils {

/**
* 1 Byte with the first 4 header-bits set to the identifying FID prefix 0111 (0x7). Use this
* constant to create FIDs or check the first byte of FIDs. This prefix is also used in legacy
* Instance-IDs
*/
public static final byte FID_4BIT_PREFIX = Byte.parseByte("01110000", 2);

/**
* Byte mask to remove the 4 header-bits of a given Byte. Use this constant with Java's Binary AND
* Operator in order to remove the first 4 bits of a Byte and replacing it with the FID prefix.
*/
public static final byte REMOVE_PREFIX_MASK = Byte.parseByte("00001111", 2);

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

/**
* Creates a random FID of valid format without checking if the FID is already in use by any
* Firebase Installation.
*
* <p>Note: Even though this method does not check with the FIS database if the returned FID is
* already in use, the probability of collision is extremely and negligibly small!
*
* @return random FID value
*/
@NonNull
public static String createRandomFid() {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
byte[] uuidBytes = getBytesFromUUID(UUID.randomUUID());
return encodeFidBase64UrlSafe(uuidBytes);
}

/**
* Converts a given byte-array (assumed to be an FID value) to base64-url-safe encoded
* String-representation.
*
* <p>Note: The returned String has at most 22 characters, the length of FIDs. Thus, it is
* recommended to deliver a byte-array containing at least 16.5 bytes.
*
* @param rawValue FID value to be encoded
* @return (22-character or shorter) String containing the base64-encoded value
*/
private static String encodeFidBase64UrlSafe(byte[] rawValue) {
return new String(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I don't know enough of what is actually happening here, but why do we have to call "new String" here?
Does android.util.Base64.encode not already return a String? What else does it return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

android.util.Base64.encode returns bytes.

android.util.Base64.encode(
rawValue,
android.util.Base64.URL_SAFE
| android.util.Base64.NO_PADDING
| android.util.Base64.NO_WRAP),
Charset.defaultCharset())
.substring(0, FID_LENGTH);
}

private static byte[] getBytesFromUUID(UUID uuid) {
ByteBuffer bb = ByteBuffer.wrap(new byte[17]);

// The first 4 bits with the constant FID header of 0x7 (0b0111) followed by the last 4 random
// bits of the UUID.
byte fidPrefixWithLast4bitsOfUUID =
(byte)
(FID_4BIT_PREFIX
| (ByteBuffer.allocate(8).putLong(uuid.getLeastSignificantBits()).array()[7]
& REMOVE_PREFIX_MASK));

bb.put(fidPrefixWithLast4bitsOfUUID);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());

return bb.array();
}
}
Loading