-
Notifications
You must be signed in to change notification settings - Fork 616
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
Changes from 42 commits
d39159b
e321d7d
c25ca83
6c54e73
1923730
e79e3ad
bb41402
2851062
a2f7f64
cde2320
a27b347
53dc90b
a7982e1
7285408
19c5573
892fddf
8d3dc73
4ad27d7
7f45186
1b1766f
4ac3956
da9aac6
e9c10c2
ef6cf7d
284ab52
4332d7f
113216e
936e2f8
1f427eb
5462241
aa21227
23ff7f2
7da76c0
5152bf0
97f461a
124227c
1ccde5e
0a79342
71c90f0
d11417b
a2e0dba
8706286
34848c6
f4c44b0
44bef2f
e5e0d67
429581e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(byte) | ||
(FID_4BIT_PREFIX | ||
| (ByteBuffer.allocate(8).putLong(uuid.getLeastSignificantBits()).array()[7] | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
& REMOVE_PREFIX_MASK)); | ||
|
||
bb.put(fidPrefixWithLast4bitsOfUUID); | ||
bb.putLong(uuid.getMostSignificantBits()); | ||
bb.putLong(uuid.getLeastSignificantBits()); | ||
|
||
return bb.array(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.