mirror of
https://github.com/TeamVanced/VancedMicroG
synced 2025-01-17 14:47:32 +01:00
Update GCM/IID client code
This commit is contained in:
parent
050afb8f87
commit
e3b042ccd7
@ -30,13 +30,10 @@ import org.microg.gms.gcm.GcmConstants;
|
|||||||
|
|
||||||
import java.io.IOException;
|
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.ACTION_C2DM_RECEIVE;
|
||||||
import static org.microg.gms.gcm.GcmConstants.EXTRA_DELAY;
|
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_ID;
|
||||||
import static org.microg.gms.gcm.GcmConstants.EXTRA_MESSAGE_TYPE;
|
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;
|
||||||
import static org.microg.gms.gcm.GcmConstants.EXTRA_SENDER_LEGACY;
|
import static org.microg.gms.gcm.GcmConstants.EXTRA_SENDER_LEGACY;
|
||||||
import static org.microg.gms.gcm.GcmConstants.EXTRA_SEND_FROM;
|
import static org.microg.gms.gcm.GcmConstants.EXTRA_SEND_FROM;
|
||||||
@ -317,6 +314,6 @@ public class GoogleCloudMessaging {
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
to = to.substring(0, i);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,7 +154,7 @@ public class InstanceID {
|
|||||||
*/
|
*/
|
||||||
public long getCreationTime() {
|
public long getCreationTime() {
|
||||||
if (creationTime == 0) {
|
if (creationTime == 0) {
|
||||||
String s = storeInstance.get(subtype, "cre");
|
String s = storeInstance.getSecret(subtype, "cre");
|
||||||
if (s != null) {
|
if (s != null) {
|
||||||
creationTime = Long.parseLong(s);
|
creationTime = Long.parseLong(s);
|
||||||
}
|
}
|
||||||
@ -211,7 +211,14 @@ public class InstanceID {
|
|||||||
public String getToken(String authorizedEntity, String scope, Bundle extras) throws IOException {
|
public String getToken(String authorizedEntity, String scope, Bundle extras) throws IOException {
|
||||||
if (Looper.getMainLooper() == Looper.myLooper()) throw new IOException(ERROR_MAIN_THREAD);
|
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);
|
rsaGenerator.initialize(RSA_KEY_SIZE);
|
||||||
keyPair = rsaGenerator.generateKeyPair();
|
keyPair = rsaGenerator.generateKeyPair();
|
||||||
creationTime = System.currentTimeMillis();
|
creationTime = System.currentTimeMillis();
|
||||||
storeInstance.put(subtype, keyPair, creationTime);
|
storeInstance.putKeyPair(subtype, keyPair, creationTime);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
Log.w(TAG, e);
|
Log.w(TAG, e);
|
||||||
}
|
}
|
||||||
|
@ -38,21 +38,29 @@ public class InstanceIdStore {
|
|||||||
this.sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
|
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);
|
return sharedPreferences.getString(key, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(String subtype, String key) {
|
public synchronized long getLong(String key) {
|
||||||
return get(subtype + "|S|" + key);
|
return sharedPreferences.getLong(key, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(String subtype, String authorizedEntity, String scope) {
|
public String getSecret(String subtype, String key) {
|
||||||
return get(subtype + "|T|" + authorizedEntity + "|" + scope);
|
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) {
|
public KeyPair getKeyPair(String subtype) {
|
||||||
String pub = get(subtype, "|P|");
|
String pub = getSecret(subtype, "|P|");
|
||||||
String priv = get(subtype, "|K|");
|
String priv = getSecret(subtype, "|K|");
|
||||||
if (pub == null || priv == null) {
|
if (pub == null || priv == null) {
|
||||||
return 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();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
editor.putString(key, value);
|
editor.putString(key, value);
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String subtype, String key, String value) {
|
public synchronized void putLong(String key, long value) {
|
||||||
put(subtype + "|S|" + key, value);
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putLong(key, value);
|
||||||
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String subtype, String authorizedEntity, String scope, String value) {
|
public void putSecret(String subtype, String key, String value) {
|
||||||
put(subtype + "|T|" + authorizedEntity + "|" + scope, value);
|
putString(subtype + "|S|" + key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void put(String subtype, KeyPair keyPair, long timestamp) {
|
public void putToken(String subtype, String authorizedEntity, String scope, String token) {
|
||||||
put(subtype, "|P|", Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
|
putString(subtype + "|T|" + authorizedEntity + "|" + scope, token);
|
||||||
put(subtype, "|K|", Base64.encodeToString(keyPair.getPrivate().getEncoded(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
|
putLong(subtype + "|T-timestamp|" + authorizedEntity + "|" + scope, System.currentTimeMillis());
|
||||||
put(subtype, "cre", Long.toString(timestamp));
|
}
|
||||||
|
|
||||||
|
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() {
|
public synchronized void delete() {
|
||||||
@ -106,6 +121,7 @@ public class InstanceIdStore {
|
|||||||
public synchronized void delete(String subtype, String authorizedEntity, String scope) {
|
public synchronized void delete(String subtype, String authorizedEntity, String scope) {
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
editor.remove(subtype + "|T|" + authorizedEntity + "|" + scope);
|
editor.remove(subtype + "|T|" + authorizedEntity + "|" + scope);
|
||||||
|
editor.remove(subtype + "|T-timestamp|" + authorizedEntity + "|" + scope);
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user