Skip to content

Commit ef08161

Browse files
committed
Reading the iid token from iid shared prefs.
1 parent f801a7e commit ef08161

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

firebase-installations/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package com.google.firebase.installations.local {
3232
public class IidStore {
3333
ctor public IidStore();
3434
method @Nullable public String readIid();
35+
method @Nullable public String readToken();
3536
}
3637

3738
public class PersistedInstallation {

firebase-installations/src/main/java/com/google/firebase/installations/local/IidStore.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.security.PublicKey;
3131
import java.security.spec.InvalidKeySpecException;
3232
import java.security.spec.X509EncodedKeySpec;
33+
import org.json.JSONException;
34+
import org.json.JSONObject;
3335

3436
/**
3537
* Read existing iid only for default (first initialized) instance of this firebase application.*
@@ -38,17 +40,79 @@ public class IidStore {
3840
private static final String IID_SHARED_PREFS_NAME = "com.google.android.gms.appid";
3941
private static final String STORE_KEY_PUB = "|S||P|";
4042
private static final String STORE_KEY_ID = "|S|id";
43+
private static final String STORE_KEY_TOKEN = "|T|";
44+
private static final String DEFAULT_SCOPE = "|*";
45+
private static final String JSON_TOKEN_KEY = "token";
46+
private static final String JSON_ENCODED_PREFIX = "{";
4147

4248
@GuardedBy("iidPrefs")
4349
private final SharedPreferences iidPrefs;
4450

51+
private final String defaultSenderId;
52+
4553
public IidStore() {
4654
// Different FirebaseApp in the same Android application should have the same application
4755
// context and same dir path. We only read existing Iids for the default firebase application.
4856
iidPrefs =
4957
FirebaseApp.getInstance()
5058
.getApplicationContext()
5159
.getSharedPreferences(IID_SHARED_PREFS_NAME, Context.MODE_PRIVATE);
60+
61+
defaultSenderId = getDefaultSenderId(FirebaseApp.getInstance());
62+
}
63+
64+
private static String getDefaultSenderId(FirebaseApp app) {
65+
// Check for an explicit sender id
66+
String senderId = app.getOptions().getGcmSenderId();
67+
if (senderId != null) {
68+
return senderId;
69+
}
70+
String appId = app.getOptions().getApplicationId();
71+
if (!appId.startsWith("1:")) {
72+
// Not v1, server should be updated to accept the full app ID now
73+
return appId;
74+
}
75+
// For v1 app IDs, fall back to parsing the project number out
76+
@SuppressWarnings("StringSplitter")
77+
String[] parts = appId.split(":");
78+
if (parts.length < 2) {
79+
return null; // Invalid format
80+
}
81+
String projectNumber = parts[1];
82+
if (projectNumber.isEmpty()) {
83+
return null; // No project number
84+
}
85+
return projectNumber;
86+
}
87+
88+
private String createTokenKey(@NonNull String senderId) {
89+
return STORE_KEY_TOKEN + senderId + DEFAULT_SCOPE;
90+
}
91+
92+
@Nullable
93+
public String readToken() {
94+
synchronized (iidPrefs) {
95+
String token = iidPrefs.getString(createTokenKey(defaultSenderId), null);
96+
if (token.isEmpty()) {
97+
return null;
98+
}
99+
100+
if (token.startsWith(JSON_ENCODED_PREFIX)) {
101+
return parseIidTokenFromJson(token);
102+
}
103+
// Legacy value, token is whole string
104+
return token;
105+
}
106+
}
107+
108+
private String parseIidTokenFromJson(String token) {
109+
// Encoded as JSON
110+
try {
111+
JSONObject json = new JSONObject(token);
112+
return json.getString(JSON_TOKEN_KEY);
113+
} catch (JSONException e) {
114+
return null;
115+
}
52116
}
53117

54118
@Nullable

0 commit comments

Comments
 (0)