feat: add encrypted push

This commit is contained in:
Sorunome 2021-10-28 11:14:47 +02:00
parent 81164316b5
commit ff19deb7fd
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
3 changed files with 32 additions and 80 deletions

View File

@ -131,67 +131,22 @@ class BackgroundPush {
await _fcmSharedIsolate?.requestPermission(); await _fcmSharedIsolate?.requestPermission();
} }
final clientName = PlatformInfos.clientName; final clientName = PlatformInfos.clientName;
oldTokens ??= <String>{};
final pushers = await client.getPushers().catchError((e) {
Logs().w('[Push] Unable to request pushers', e);
return <Pusher>[];
});
var setNewPusher = false;
// Just the plain app id, we add the .data_message suffix later // Just the plain app id, we add the .data_message suffix later
var appId = AppConfig.pushNotificationsAppId; var appId = AppConfig.pushNotificationsAppId;
// we need the deviceAppId to remove potential legacy UP pusher // we need the deviceAppId to remove potential legacy UP pusher
var deviceAppId = '$appId.${client.deviceID}'; var deviceAppId = '$appId.${client.deviceID}';
// appId may only be up to 64 chars as per spec // appId may only be up to 64 chars as per spec
if (deviceAppId.length > 64) { final maxLength = 64 - '.data_message'.length;
deviceAppId = deviceAppId.substring(0, 64); if (deviceAppId.length > maxLength) {
deviceAppId = deviceAppId.substring(0, maxLength);
} }
if (!useDeviceSpecificAppId && PlatformInfos.isAndroid) { if (!useDeviceSpecificAppId && PlatformInfos.isAndroid) {
appId += '.data_message'; appId += '.data_message';
} }
final thisAppId = useDeviceSpecificAppId ? deviceAppId : appId; final thisAppId = useDeviceSpecificAppId ? deviceAppId : appId;
if (gatewayUrl != null && token != null && clientName != null) { await client.setupPusher(
final currentPushers = pushers.where((pusher) => pusher.pushkey == token); oldTokens: oldTokens,
if (currentPushers.length == 1 && pusher: Pusher(
currentPushers.first.kind == 'http' &&
currentPushers.first.appId == thisAppId &&
currentPushers.first.appDisplayName == clientName &&
currentPushers.first.deviceDisplayName == client.deviceName &&
currentPushers.first.lang == 'en' &&
currentPushers.first.data.url.toString() == gatewayUrl &&
currentPushers.first.data.format ==
AppConfig.pushNotificationsPusherFormat) {
Logs().i('[Push] Pusher already set');
} else {
Logs().i('Need to set new pusher');
oldTokens.add(token);
if (client.isLogged()) {
setNewPusher = true;
}
}
} else {
Logs().w('[Push] Missing required push credentials');
}
for (final pusher in pushers) {
if ((token != null &&
pusher.pushkey != token &&
deviceAppId == pusher.appId) ||
oldTokens.contains(pusher.pushkey)) {
pusher.kind = null;
try {
await client.postPusher(
pusher,
append: true,
);
Logs().i('[Push] Removed legacy pusher for this device');
} catch (err) {
Logs().w('[Push] Failed to remove old pusher', err);
}
}
}
if (setNewPusher) {
try {
await client.postPusher(
Pusher(
pushkey: token, pushkey: token,
appId: thisAppId, appId: thisAppId,
appDisplayName: clientName, appDisplayName: clientName,
@ -203,12 +158,7 @@ class BackgroundPush {
), ),
kind: 'http', kind: 'http',
), ),
append: false,
); );
} catch (e, s) {
Logs().e('[Push] Unable to set pushers', e, s);
}
}
} }
bool _wentToRoomOnStartup = false; bool _wentToRoomOnStartup = false;
@ -325,7 +275,7 @@ class BackgroundPush {
Logs().v('[Push] Foreground message received'); Logs().v('[Push] Foreground message received');
Map<String, dynamic> data; Map<String, dynamic> data;
try { try {
data = Map<String, dynamic>.from(message['data'] ?? message); data = await client.processPushPayload(Map<String, dynamic>.from(message));
await _onMessage(data); await _onMessage(data);
} catch (e, s) { } catch (e, s) {
Logs().e('[Push] Error while processing notification', e, s); Logs().e('[Push] Error while processing notification', e, s);
@ -392,7 +342,7 @@ class BackgroundPush {
Future<void> _onUpMessage(String message) async { Future<void> _onUpMessage(String message) async {
Map<String, dynamic> data; Map<String, dynamic> data;
try { try {
data = Map<String, dynamic>.from(json.decode(message)['notification']); data = await client.processPushPayload(json.decode(message));
await _onMessage(data); await _onMessage(data);
} catch (e, s) { } catch (e, s) {
Logs().e('[Push] Error while processing notification', e, s); Logs().e('[Push] Error while processing notification', e, s);

View File

@ -815,17 +815,17 @@ packages:
matrix: matrix:
dependency: "direct main" dependency: "direct main"
description: description:
name: matrix path: "/home/sorunome/repos/famedly/famedlysdk"
url: "https://pub.dartlang.org" relative: false
source: hosted source: path
version: "0.7.0" version: "0.6.2"
matrix_api_lite: matrix_api_lite:
dependency: transitive dependency: transitive
description: description:
name: matrix_api_lite path: "/home/sorunome/repos/famedly/frontend/libraries/matrix_api_lite"
url: "https://pub.dartlang.org" relative: false
source: hosted source: path
version: "0.5.3" version: "0.4.4"
matrix_link_text: matrix_link_text:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -48,7 +48,9 @@ dependencies:
intl: any intl: any
localstorage: ^4.0.0+1 localstorage: ^4.0.0+1
lottie: ^1.2.1 lottie: ^1.2.1
matrix: ^0.7.0 #matrix: ^0.7.0
matrix:
path: /home/sorunome/repos/famedly/famedlysdk
matrix_link_text: ^1.0.2 matrix_link_text: ^1.0.2
open_noti_settings: ^0.3.0 open_noti_settings: ^0.3.0
package_info_plus: ^1.2.1 package_info_plus: ^1.2.1