Update GCM/IID client code

This commit is contained in:
Marvin W 2022-01-18 18:47:24 +01:00
parent 050afb8f87
commit e3b042ccd7
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
3 changed files with 44 additions and 24 deletions

View File

@ -30,13 +30,10 @@ import org.microg.gms.gcm.GcmConstants;
import java.io.IOException;
import static org.microg.gms.common.Constants.GMS_PACKAGE_NAME;
import static org.microg.gms.gcm.GcmConstants.ACTION_C2DM_RECEIVE;
import static org.microg.gms.gcm.GcmConstants.EXTRA_DELAY;
import static org.microg.gms.gcm.GcmConstants.EXTRA_ERROR;
import static org.microg.gms.gcm.GcmConstants.EXTRA_MESSAGE_ID;
import static org.microg.gms.gcm.GcmConstants.EXTRA_MESSAGE_TYPE;
import static org.microg.gms.gcm.GcmConstants.EXTRA_REGISTRATION_ID;
import static org.microg.gms.gcm.GcmConstants.EXTRA_SENDER;
import static org.microg.gms.gcm.GcmConstants.EXTRA_SENDER_LEGACY;
import static org.microg.gms.gcm.GcmConstants.EXTRA_SEND_FROM;
@ -317,6 +314,6 @@ public class GoogleCloudMessaging {
if (i > 0) {
to = to.substring(0, i);
}
return InstanceID.getInstance(context).getStore().get("", to, INSTANCE_ID_SCOPE);
return InstanceID.getInstance(context).getStore().getToken("", to, INSTANCE_ID_SCOPE);
}
}

View File

@ -154,7 +154,7 @@ public class InstanceID {
*/
public long getCreationTime() {
if (creationTime == 0) {
String s = storeInstance.get(subtype, "cre");
String s = storeInstance.getSecret(subtype, "cre");
if (s != null) {
creationTime = Long.parseLong(s);
}
@ -211,7 +211,14 @@ public class InstanceID {
public String getToken(String authorizedEntity, String scope, Bundle extras) throws IOException {
if (Looper.getMainLooper() == Looper.myLooper()) throw new IOException(ERROR_MAIN_THREAD);
throw new UnsupportedOperationException();
long tokenTimestamp = storeInstance.getTokenTimestamp(subtype, authorizedEntity, scope);
if (tokenTimestamp > System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000L) {
String token = storeInstance.getToken(subtype, authorizedEntity, scope);
if (token != null) return token;
}
String token = requestToken(authorizedEntity, scope, extras);
storeInstance.putToken(subtype, authorizedEntity, scope, token);
return token;
}
/**
@ -252,7 +259,7 @@ public class InstanceID {
rsaGenerator.initialize(RSA_KEY_SIZE);
keyPair = rsaGenerator.generateKeyPair();
creationTime = System.currentTimeMillis();
storeInstance.put(subtype, keyPair, creationTime);
storeInstance.putKeyPair(subtype, keyPair, creationTime);
} catch (NoSuchAlgorithmException e) {
Log.w(TAG, e);
}
@ -272,4 +279,4 @@ public class InstanceID {
return null;
}
}
}
}

View File

@ -38,21 +38,29 @@ public class InstanceIdStore {
this.sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public synchronized String get(String key) {
public synchronized String getString(String key) {
return sharedPreferences.getString(key, null);
}
public String get(String subtype, String key) {
return get(subtype + "|S|" + key);
public synchronized long getLong(String key) {
return sharedPreferences.getLong(key, -1);
}
public String get(String subtype, String authorizedEntity, String scope) {
return get(subtype + "|T|" + authorizedEntity + "|" + scope);
public String getSecret(String subtype, String key) {
return getString(subtype + "|S|" + key);
}
public String getToken(String subtype, String authorizedEntity, String scope) {
return getString(subtype + "|T|" + authorizedEntity + "|" + scope);
}
public long getTokenTimestamp(String subtype, String authorizedEntity, String scope) {
return getLong(subtype + "|T-timestamp|" + authorizedEntity + "|" + scope);
}
public KeyPair getKeyPair(String subtype) {
String pub = get(subtype, "|P|");
String priv = get(subtype, "|K|");
String pub = getSecret(subtype, "|P|");
String priv = getSecret(subtype, "|K|");
if (pub == null || priv == null) {
return null;
}
@ -67,24 +75,31 @@ public class InstanceIdStore {
}
}
public synchronized void put(String key, String value) {
public synchronized void putString(String key, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
public void put(String subtype, String key, String value) {
put(subtype + "|S|" + key, value);
public synchronized void putLong(String key, long value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.apply();
}
public void put(String subtype, String authorizedEntity, String scope, String value) {
put(subtype + "|T|" + authorizedEntity + "|" + scope, value);
public void putSecret(String subtype, String key, String value) {
putString(subtype + "|S|" + key, value);
}
public synchronized void put(String subtype, KeyPair keyPair, long timestamp) {
put(subtype, "|P|", Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
put(subtype, "|K|", Base64.encodeToString(keyPair.getPrivate().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
put(subtype, "cre", Long.toString(timestamp));
public void putToken(String subtype, String authorizedEntity, String scope, String token) {
putString(subtype + "|T|" + authorizedEntity + "|" + scope, token);
putLong(subtype + "|T-timestamp|" + authorizedEntity + "|" + scope, System.currentTimeMillis());
}
public synchronized void putKeyPair(String subtype, KeyPair keyPair, long timestamp) {
putSecret(subtype, "|P|", Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
putSecret(subtype, "|K|", Base64.encodeToString(keyPair.getPrivate().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
putSecret(subtype, "cre", Long.toString(timestamp));
}
public synchronized void delete() {
@ -106,6 +121,7 @@ public class InstanceIdStore {
public synchronized void delete(String subtype, String authorizedEntity, String scope) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(subtype + "|T|" + authorizedEntity + "|" + scope);
editor.remove(subtype + "|T-timestamp|" + authorizedEntity + "|" + scope);
editor.apply();
}
}