Compare commits
12 Commits
moxxmpp-v0
...
edc86a10b3
| Author | SHA1 | Date | |
|---|---|---|---|
| edc86a10b3 | |||
| 39e9c55fae | |||
| 1b2c567787 | |||
| d3955479f7 | |||
| 300a52f9fe | |||
| 2e3472d88f | |||
| 6b106fe365 | |||
| bfd28c281e | |||
| c307567025 | |||
| 5dd96f518b | |||
| 6d9010b11c | |||
| 9cc735d854 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -10,3 +10,6 @@ build/
|
|||||||
# Omit committing pubspec.lock for library packages; see
|
# Omit committing pubspec.lock for library packages; see
|
||||||
# https://dart.dev/guides/libraries/private-files#pubspeclock.
|
# https://dart.dev/guides/libraries/private-files#pubspeclock.
|
||||||
pubspec.lock
|
pubspec.lock
|
||||||
|
|
||||||
|
# Omit pubspec override files generated by melos
|
||||||
|
**/pubspec_overrides.yaml
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
CSINegotiator(),
|
CSINegotiator(),
|
||||||
RosterFeatureNegotiator(),
|
RosterFeatureNegotiator(),
|
||||||
SaslPlainNegotiator(),
|
SaslPlainNegotiator(),
|
||||||
|
SaslScramNegotiator(10, '', '', ScramHashType.sha512),
|
||||||
SaslScramNegotiator(9, '', '', ScramHashType.sha256),
|
SaslScramNegotiator(9, '', '', ScramHashType.sha256),
|
||||||
SaslScramNegotiator(8, '', '', ScramHashType.sha1),
|
SaslScramNegotiator(8, '', '', ScramHashType.sha1),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ dependencies:
|
|||||||
version: 0.1.4+1
|
version: 0.1.4+1
|
||||||
moxxmpp:
|
moxxmpp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: 0.1.2+1
|
version: 0.1.3+1
|
||||||
moxxmpp_socket_tcp:
|
moxxmpp_socket_tcp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: 0.1.2+1
|
version: 0.1.2+5
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
1
packages/moxxmpp/.pubignore
Normal file
1
packages/moxxmpp/.pubignore
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pubspec_overrides.yaml
|
||||||
@@ -1,3 +1,21 @@
|
|||||||
|
## 0.1.3+1
|
||||||
|
|
||||||
|
- **FIX**: Expose the error classes.
|
||||||
|
|
||||||
|
## 0.1.3
|
||||||
|
|
||||||
|
- **REFACTOR**: Replace MayFail by Result.
|
||||||
|
- **FIX**: Remove the old Results API.
|
||||||
|
- **FEAT**: Rework how the negotiator system works.
|
||||||
|
|
||||||
|
## 0.1.2+3
|
||||||
|
|
||||||
|
- **FIX**: SASL SCRAM-SHA-{256,512} should now work.
|
||||||
|
|
||||||
|
## 0.1.2+2
|
||||||
|
|
||||||
|
- **FIX**: Fix reconnections when the connection is awaited.
|
||||||
|
|
||||||
## 0.1.2+1
|
## 0.1.2+1
|
||||||
|
|
||||||
- **FIX**: A certificate rejection does not crash the connection.
|
- **FIX**: A certificate rejection does not crash the connection.
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Logger.root.level = Level.ALL;
|
||||||
|
Logger.root.onRecord.listen((record) {
|
||||||
|
print('${record.level.name}: ${record.time}: ${record.message}');
|
||||||
|
});
|
||||||
|
final log = Logger('FailureReconnectionTest');
|
||||||
|
|
||||||
|
test('Failing an awaited connection with TestingSleepReconnectionPolicy', () async {
|
||||||
|
var errors = 0;
|
||||||
|
final connection = XmppConnection(
|
||||||
|
TestingSleepReconnectionPolicy(10),
|
||||||
|
TCPSocketWrapper(false),
|
||||||
|
);
|
||||||
|
connection.registerFeatureNegotiators([
|
||||||
|
StartTlsNegotiator(),
|
||||||
|
]);
|
||||||
|
connection.registerManagers([
|
||||||
|
DiscoManager(),
|
||||||
|
RosterManager(),
|
||||||
|
PingManager(),
|
||||||
|
MessageManager(),
|
||||||
|
PresenceManager('http://moxxmpp.example'),
|
||||||
|
]);
|
||||||
|
connection.asBroadcastStream().listen((event) {
|
||||||
|
if (event is ConnectionStateChangedEvent) {
|
||||||
|
if (event.state == XmppConnectionState.error) {
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.setConnectionSettings(
|
||||||
|
ConnectionSettings(
|
||||||
|
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
|
||||||
|
password: 'abc123',
|
||||||
|
useDirectTLS: true,
|
||||||
|
allowPlainAuth: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = await connection.connectAwaitable();
|
||||||
|
log.info('Connection failed as expected');
|
||||||
|
expect(result.success, false);
|
||||||
|
expect(errors, 1);
|
||||||
|
|
||||||
|
log.info('Waiting 20 seconds for unexpected reconnections');
|
||||||
|
await Future.delayed(const Duration(seconds: 20));
|
||||||
|
expect(errors, 1);
|
||||||
|
}, timeout: Timeout.factor(2));
|
||||||
|
|
||||||
|
test('Failing an awaited connection with ExponentialBackoffReconnectionPolicy', () async {
|
||||||
|
var errors = 0;
|
||||||
|
final connection = XmppConnection(
|
||||||
|
ExponentialBackoffReconnectionPolicy(1),
|
||||||
|
TCPSocketWrapper(false),
|
||||||
|
);
|
||||||
|
connection.registerFeatureNegotiators([
|
||||||
|
StartTlsNegotiator(),
|
||||||
|
]);
|
||||||
|
connection.registerManagers([
|
||||||
|
DiscoManager(),
|
||||||
|
RosterManager(),
|
||||||
|
PingManager(),
|
||||||
|
MessageManager(),
|
||||||
|
PresenceManager('http://moxxmpp.example'),
|
||||||
|
]);
|
||||||
|
connection.asBroadcastStream().listen((event) {
|
||||||
|
if (event is ConnectionStateChangedEvent) {
|
||||||
|
if (event.state == XmppConnectionState.error) {
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.setConnectionSettings(
|
||||||
|
ConnectionSettings(
|
||||||
|
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
|
||||||
|
password: 'abc123',
|
||||||
|
useDirectTLS: true,
|
||||||
|
allowPlainAuth: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = await connection.connectAwaitable();
|
||||||
|
log.info('Connection failed as expected');
|
||||||
|
expect(result.success, false);
|
||||||
|
expect(errors, 1);
|
||||||
|
|
||||||
|
log.info('Waiting 20 seconds for unexpected reconnections');
|
||||||
|
await Future.delayed(const Duration(seconds: 20));
|
||||||
|
expect(errors, 1);
|
||||||
|
}, timeout: Timeout.factor(2));
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
library moxxmpp;
|
library moxxmpp;
|
||||||
|
|
||||||
export 'package:moxxmpp/src/connection.dart';
|
export 'package:moxxmpp/src/connection.dart';
|
||||||
|
export 'package:moxxmpp/src/errors.dart';
|
||||||
export 'package:moxxmpp/src/events.dart';
|
export 'package:moxxmpp/src/events.dart';
|
||||||
export 'package:moxxmpp/src/iq.dart';
|
export 'package:moxxmpp/src/iq.dart';
|
||||||
export 'package:moxxmpp/src/jid.dart';
|
export 'package:moxxmpp/src/jid.dart';
|
||||||
@@ -16,6 +17,7 @@ export 'package:moxxmpp/src/negotiators/manager.dart';
|
|||||||
export 'package:moxxmpp/src/negotiators/namespaces.dart';
|
export 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/negotiator.dart';
|
export 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/resource_binding.dart';
|
export 'package:moxxmpp/src/negotiators/resource_binding.dart';
|
||||||
|
export 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/plain.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/plain.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/scram.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/scram.dart';
|
||||||
@@ -25,13 +27,13 @@ export 'package:moxxmpp/src/presence.dart';
|
|||||||
export 'package:moxxmpp/src/reconnect.dart';
|
export 'package:moxxmpp/src/reconnect.dart';
|
||||||
export 'package:moxxmpp/src/rfcs/rfc_2782.dart';
|
export 'package:moxxmpp/src/rfcs/rfc_2782.dart';
|
||||||
export 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
export 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
||||||
export 'package:moxxmpp/src/roster.dart';
|
export 'package:moxxmpp/src/roster/errors.dart';
|
||||||
|
export 'package:moxxmpp/src/roster/roster.dart';
|
||||||
export 'package:moxxmpp/src/settings.dart';
|
export 'package:moxxmpp/src/settings.dart';
|
||||||
export 'package:moxxmpp/src/socket.dart';
|
export 'package:moxxmpp/src/socket.dart';
|
||||||
export 'package:moxxmpp/src/stanza.dart';
|
export 'package:moxxmpp/src/stanza.dart';
|
||||||
export 'package:moxxmpp/src/stringxml.dart';
|
export 'package:moxxmpp/src/stringxml.dart';
|
||||||
export 'package:moxxmpp/src/types/error.dart';
|
export 'package:moxxmpp/src/types/result.dart';
|
||||||
export 'package:moxxmpp/src/types/resultv2.dart';
|
|
||||||
export 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
export 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
||||||
export 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
|
export 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0004.dart';
|
export 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
@@ -61,7 +63,8 @@ export 'package:moxxmpp/src/xeps/xep_0333.dart';
|
|||||||
export 'package:moxxmpp/src/xeps/xep_0334.dart';
|
export 'package:moxxmpp/src/xeps/xep_0334.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0352.dart';
|
export 'package:moxxmpp/src/xeps/xep_0352.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0359.dart';
|
export 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0363.dart';
|
export 'package:moxxmpp/src/xeps/xep_0363/errors.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0363/xep_0363.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0380.dart';
|
export 'package:moxxmpp/src/xeps/xep_0380.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0384/errors.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/errors.dart';
|
||||||
@@ -70,6 +73,7 @@ export 'package:moxxmpp/src/xeps/xep_0384/types.dart';
|
|||||||
export 'package:moxxmpp/src/xeps/xep_0384/xep_0384.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/xep_0384.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0385.dart';
|
export 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0414.dart';
|
export 'package:moxxmpp/src/xeps/xep_0414.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0446.dart';
|
export 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0447.dart';
|
export 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0448.dart';
|
export 'package:moxxmpp/src/xeps/xep_0448.dart';
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxxmpp/src/buffer.dart';
|
import 'package:moxxmpp/src/buffer.dart';
|
||||||
|
import 'package:moxxmpp/src/errors.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/iq.dart';
|
import 'package:moxxmpp/src/iq.dart';
|
||||||
import 'package:moxxmpp/src/managers/attributes.dart';
|
import 'package:moxxmpp/src/managers/attributes.dart';
|
||||||
@@ -14,7 +15,7 @@ import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/presence.dart';
|
import 'package:moxxmpp/src/presence.dart';
|
||||||
import 'package:moxxmpp/src/reconnect.dart';
|
import 'package:moxxmpp/src/reconnect.dart';
|
||||||
import 'package:moxxmpp/src/roster.dart';
|
import 'package:moxxmpp/src/roster/roster.dart';
|
||||||
import 'package:moxxmpp/src/routing.dart';
|
import 'package:moxxmpp/src/routing.dart';
|
||||||
import 'package:moxxmpp/src/settings.dart';
|
import 'package:moxxmpp/src/settings.dart';
|
||||||
import 'package:moxxmpp/src/socket.dart';
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
@@ -61,14 +62,14 @@ class XmppConnectionResult {
|
|||||||
const XmppConnectionResult(
|
const XmppConnectionResult(
|
||||||
this.success,
|
this.success,
|
||||||
{
|
{
|
||||||
this.reason,
|
this.error,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
final bool success;
|
final bool success;
|
||||||
// NOTE: [reason] is not human-readable, but the type of SASL error.
|
// If a connection attempt fails, i.e. success is false, then this indicates the
|
||||||
// See sasl/errors.dart
|
// reason the connection failed.
|
||||||
final String? reason;
|
final XmppError? error;
|
||||||
}
|
}
|
||||||
|
|
||||||
class XmppConnection {
|
class XmppConnection {
|
||||||
@@ -163,6 +164,8 @@ class XmppConnection {
|
|||||||
/// Completers for certain actions
|
/// Completers for certain actions
|
||||||
// ignore: use_late_for_private_fields_and_variables
|
// ignore: use_late_for_private_fields_and_variables
|
||||||
Completer<XmppConnectionResult>? _connectionCompleter;
|
Completer<XmppConnectionResult>? _connectionCompleter;
|
||||||
|
/// Controls whether an XmppSocketClosureEvent triggers a reconnection.
|
||||||
|
bool _socketClosureTriggersReconnect = true;
|
||||||
|
|
||||||
/// Negotiators
|
/// Negotiators
|
||||||
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators;
|
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators;
|
||||||
@@ -343,25 +346,40 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Called when a stream ending error has occurred
|
/// Called when a stream ending error has occurred
|
||||||
Future<void> handleError(Object? error) async {
|
Future<void> handleError(XmppError error) async {
|
||||||
if (error != null) {
|
_log.severe('handleError called with ${error.toString()}');
|
||||||
_log.severe('handleError: $error');
|
|
||||||
} else {
|
|
||||||
_log.severe('handleError: Called with null');
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(Unknown): This may be too harsh for every error
|
// Whenever we encounter an error that would trigger a reconnection attempt while
|
||||||
await _setConnectionState(XmppConnectionState.notConnected);
|
// the connection result is being awaited, don't attempt a reconnection but instead
|
||||||
|
// try to gracefully disconnect.
|
||||||
|
if (_connectionCompleter != null) {
|
||||||
|
_log.info('Not triggering reconnection since connection result is being awaited');
|
||||||
|
await _disconnect(triggeredByUser: false, state: XmppConnectionState.error);
|
||||||
|
_connectionCompleter?.complete(
|
||||||
|
XmppConnectionResult(
|
||||||
|
false,
|
||||||
|
error: error,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
_connectionCompleter = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _setConnectionState(XmppConnectionState.error);
|
||||||
await _reconnectionPolicy.onFailure();
|
await _reconnectionPolicy.onFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called whenever the socket creates an event
|
/// Called whenever the socket creates an event
|
||||||
Future<void> _handleSocketEvent(XmppSocketEvent event) async {
|
Future<void> _handleSocketEvent(XmppSocketEvent event) async {
|
||||||
if (event is XmppSocketErrorEvent) {
|
if (event is XmppSocketErrorEvent) {
|
||||||
await handleError(event.error);
|
await handleError(SocketError(event));
|
||||||
} else if (event is XmppSocketClosureEvent) {
|
} else if (event is XmppSocketClosureEvent) {
|
||||||
_log.fine('Received XmppSocketClosureEvent. Reconnecting...');
|
if (_socketClosureTriggersReconnect) {
|
||||||
await _reconnectionPolicy.onFailure();
|
_log.fine('Received XmppSocketClosureEvent. Reconnecting...');
|
||||||
|
await _reconnectionPolicy.onFailure();
|
||||||
|
} else {
|
||||||
|
_log.fine('Received XmppSocketClosureEvent. No reconnection attempt since _socketClosureTriggersReconnect is false...');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,7 +527,7 @@ class XmppConnection {
|
|||||||
/// Called when we timeout during connecting
|
/// Called when we timeout during connecting
|
||||||
Future<void> _onConnectingTimeout() async {
|
Future<void> _onConnectingTimeout() async {
|
||||||
_log.severe('Connection stuck in "connecting". Causing a reconnection...');
|
_log.severe('Connection stuck in "connecting". Causing a reconnection...');
|
||||||
await handleError('Connecting timeout');
|
await handleError(TimeoutError());
|
||||||
}
|
}
|
||||||
|
|
||||||
void _destroyConnectingTimer() {
|
void _destroyConnectingTimer() {
|
||||||
@@ -733,20 +751,34 @@ class XmppConnection {
|
|||||||
// Send out initial presence
|
// Send out initial presence
|
||||||
await getPresenceManager().sendInitialPresence();
|
await getPresenceManager().sendInitialPresence();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To be called after _currentNegotiator!.negotiate(..) has been called. Checks the
|
|
||||||
/// state of the negotiator and picks the next negotiatior, ends negotiation or
|
|
||||||
/// waits, depending on what the negotiator did.
|
|
||||||
Future<void> _checkCurrentNegotiator() async {
|
|
||||||
if (_currentNegotiator!.state == NegotiatorState.done) {
|
|
||||||
_log.finest('Negotiator ${_currentNegotiator!.id} done');
|
|
||||||
|
|
||||||
|
Future<void> _executeCurrentNegotiator(XMLNode nonza) async {
|
||||||
|
// If we don't have a negotiator get one
|
||||||
|
_currentNegotiator ??= getNextNegotiator(_streamFeatures);
|
||||||
|
if (_currentNegotiator == null && _isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
|
_log.finest('Negotiations done!');
|
||||||
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _onNegotiationsDone();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = await _currentNegotiator!.negotiate(nonza);
|
||||||
|
if (result.isType<NegotiatorError>()) {
|
||||||
|
_log.severe('Negotiator returned an error');
|
||||||
|
await handleError(result.get<NegotiatorError>());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final state = result.get<NegotiatorState>();
|
||||||
|
_currentNegotiator!.state = state;
|
||||||
|
switch (state) {
|
||||||
|
case NegotiatorState.ready: return;
|
||||||
|
case NegotiatorState.done:
|
||||||
if (_currentNegotiator!.sendStreamHeaderWhenDone) {
|
if (_currentNegotiator!.sendStreamHeaderWhenDone) {
|
||||||
_currentNegotiator = null;
|
_currentNegotiator = null;
|
||||||
_streamFeatures.clear();
|
_streamFeatures.clear();
|
||||||
_sendStreamHeader();
|
_sendStreamHeader();
|
||||||
} else {
|
} else {
|
||||||
// Track what features we still have
|
|
||||||
_streamFeatures
|
_streamFeatures
|
||||||
.removeWhere((node) {
|
.removeWhere((node) {
|
||||||
return node.attributes['xmlns'] == _currentNegotiator!.negotiatingXmlns;
|
return node.attributes['xmlns'] == _currentNegotiator!.negotiatingXmlns;
|
||||||
@@ -756,7 +788,6 @@ class XmppConnection {
|
|||||||
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
_log.finest('Negotiations done!');
|
_log.finest('Negotiations done!');
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else {
|
} else {
|
||||||
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
||||||
@@ -766,15 +797,16 @@ class XmppConnection {
|
|||||||
tag: 'stream:features',
|
tag: 'stream:features',
|
||||||
children: _streamFeatures,
|
children: _streamFeatures,
|
||||||
);
|
);
|
||||||
await _currentNegotiator!.negotiate(fakeStanza);
|
|
||||||
await _checkCurrentNegotiator();
|
await _executeCurrentNegotiator(fakeStanza);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.retryLater) {
|
break;
|
||||||
|
case NegotiatorState.retryLater:
|
||||||
_log.finest('Negotiator wants to continue later. Picking new one...');
|
_log.finest('Negotiator wants to continue later. Picking new one...');
|
||||||
|
|
||||||
_currentNegotiator!.state = NegotiatorState.ready;
|
_currentNegotiator!.state = NegotiatorState.ready;
|
||||||
|
|
||||||
|
|
||||||
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
_log.finest('Negotiations done!');
|
_log.finest('Negotiations done!');
|
||||||
|
|
||||||
@@ -788,29 +820,17 @@ class XmppConnection {
|
|||||||
tag: 'stream:features',
|
tag: 'stream:features',
|
||||||
children: _streamFeatures,
|
children: _streamFeatures,
|
||||||
);
|
);
|
||||||
await _currentNegotiator!.negotiate(fakeStanza);
|
await _executeCurrentNegotiator(fakeStanza);
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
}
|
}
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.skipRest) {
|
break;
|
||||||
|
case NegotiatorState.skipRest:
|
||||||
_log.finest('Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!');
|
_log.finest('Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!');
|
||||||
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.error) {
|
break;
|
||||||
_log.severe('Negotiator returned an error');
|
|
||||||
|
|
||||||
_updateRoutingState(RoutingState.error);
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
|
||||||
_connectionCompleter?.complete(const XmppConnectionResult(false));
|
|
||||||
_connectionCompleter = null;
|
|
||||||
|
|
||||||
_closeSocket();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _closeSocket() {
|
|
||||||
_socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Called whenever we receive data that has been parsed as XML.
|
/// Called whenever we receive data that has been parsed as XML.
|
||||||
Future<void> handleXmlStream(XMLNode node) async {
|
Future<void> handleXmlStream(XMLNode node) async {
|
||||||
@@ -819,7 +839,7 @@ class XmppConnection {
|
|||||||
_log
|
_log
|
||||||
..finest('<== ${node.toXml()}')
|
..finest('<== ${node.toXml()}')
|
||||||
..severe('Received a stream error! Attempting reconnection');
|
..severe('Received a stream error! Attempting reconnection');
|
||||||
await handleError('Stream error');
|
await handleError(StreamError());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -839,53 +859,14 @@ class XmppConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_currentNegotiator != null) {
|
if (node.tag == 'stream:features') {
|
||||||
// If we already have a negotiator, just let it do its thing
|
// Store the received stream features
|
||||||
_log.finest('Negotiator currently active...');
|
|
||||||
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
} else {
|
|
||||||
_streamFeatures
|
_streamFeatures
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(node.children);
|
..addAll(node.children);
|
||||||
|
|
||||||
// We need to pick a new one
|
|
||||||
if (_isMandatoryNegotiationDone(node.children)) {
|
|
||||||
// Mandatory features are done but can we still negotiate more?
|
|
||||||
if (_isNegotiationPossible(node.children)) {// We can still negotiate features, so do that.
|
|
||||||
_log.finest('All required stream features done! Continuing negotiation');
|
|
||||||
_currentNegotiator = getNextNegotiator(node.children);
|
|
||||||
_log.finest('Chose $_currentNegotiator as next negotiator');
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
} else {
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// There still are mandatory features
|
|
||||||
if (!_isNegotiationPossible(node.children)) {
|
|
||||||
_log.severe('Mandatory negotiations not done but continuation not possible');
|
|
||||||
_updateRoutingState(RoutingState.error);
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
|
||||||
|
|
||||||
// Resolve the connection completion future
|
|
||||||
_connectionCompleter?.complete(
|
|
||||||
const XmppConnectionResult(
|
|
||||||
false,
|
|
||||||
reason: 'Could not complete connection negotiations',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_connectionCompleter = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_currentNegotiator = getNextNegotiator(node.children);
|
|
||||||
_log.finest('Chose $_currentNegotiator as next negotiator');
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await _executeCurrentNegotiator(node);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case RoutingState.handleStanzas:
|
case RoutingState.handleStanzas:
|
||||||
@@ -917,20 +898,6 @@ class XmppConnection {
|
|||||||
} else if (event is AuthenticationSuccessEvent) {
|
} else if (event is AuthenticationSuccessEvent) {
|
||||||
_log.finest('Received AuthenticationSuccessEvent. Setting _isAuthenticated to true');
|
_log.finest('Received AuthenticationSuccessEvent. Setting _isAuthenticated to true');
|
||||||
_isAuthenticated = true;
|
_isAuthenticated = true;
|
||||||
} else if (event is AuthenticationFailedEvent) {
|
|
||||||
_log.finest('Failed authentication');
|
|
||||||
_updateRoutingState(RoutingState.error);
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
|
||||||
|
|
||||||
// Resolve the connection completion future
|
|
||||||
_connectionCompleter?.complete(
|
|
||||||
XmppConnectionResult(
|
|
||||||
false,
|
|
||||||
reason: 'Authentication failed: ${event.saslError}',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_connectionCompleter = null;
|
|
||||||
_closeSocket();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final manager in _xmppManagers.values) {
|
for (final manager in _xmppManagers.values) {
|
||||||
@@ -965,17 +932,32 @@ class XmppConnection {
|
|||||||
|
|
||||||
/// Attempt to gracefully close the session
|
/// Attempt to gracefully close the session
|
||||||
Future<void> disconnect() async {
|
Future<void> disconnect() async {
|
||||||
_reconnectionPolicy.setShouldReconnect(false);
|
await _disconnect(state: XmppConnectionState.notConnected);
|
||||||
getPresenceManager().sendUnavailablePresence();
|
|
||||||
_socket.prepareDisconnect();
|
|
||||||
sendRawString('</stream:stream>');
|
|
||||||
await _setConnectionState(XmppConnectionState.notConnected);
|
|
||||||
_socket.close();
|
|
||||||
|
|
||||||
// Clear Stream Management state, if available
|
|
||||||
await getStreamManagementManager()?.resetState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _disconnect({required XmppConnectionState state, bool triggeredByUser = true}) async {
|
||||||
|
_reconnectionPolicy.setShouldReconnect(false);
|
||||||
|
_socketClosureTriggersReconnect = false;
|
||||||
|
|
||||||
|
if (triggeredByUser) {
|
||||||
|
getPresenceManager().sendUnavailablePresence();
|
||||||
|
}
|
||||||
|
|
||||||
|
_socket.prepareDisconnect();
|
||||||
|
|
||||||
|
if (triggeredByUser) {
|
||||||
|
sendRawString('</stream:stream>');
|
||||||
|
}
|
||||||
|
|
||||||
|
await _setConnectionState(state);
|
||||||
|
_socket.close();
|
||||||
|
|
||||||
|
if (triggeredByUser) {
|
||||||
|
// Clear Stream Management state, if available
|
||||||
|
await getStreamManagementManager()?.resetState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Make sure that all required managers are registered
|
/// Make sure that all required managers are registered
|
||||||
void _runPreConnectionAssertions() {
|
void _runPreConnectionAssertions() {
|
||||||
assert(_xmppManagers.containsKey(presenceManager), 'A PresenceManager is mandatory');
|
assert(_xmppManagers.containsKey(presenceManager), 'A PresenceManager is mandatory');
|
||||||
@@ -1009,7 +991,7 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _reconnectionPolicy.reset();
|
await _reconnectionPolicy.reset();
|
||||||
|
_socketClosureTriggersReconnect = true;
|
||||||
await _sendEvent(ConnectingEvent());
|
await _sendEvent(ConnectingEvent());
|
||||||
|
|
||||||
final smManager = getStreamManagementManager();
|
final smManager = getStreamManagementManager();
|
||||||
@@ -1028,7 +1010,7 @@ class XmppConnection {
|
|||||||
port: port,
|
port: port,
|
||||||
);
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
await handleError(null);
|
await handleError(NoConnectionError());
|
||||||
} else {
|
} else {
|
||||||
await _reconnectionPolicy.onSuccess();
|
await _reconnectionPolicy.onSuccess();
|
||||||
_log.fine('Preparing the internal state for a connection attempt');
|
_log.fine('Preparing the internal state for a connection attempt');
|
||||||
|
|||||||
20
packages/moxxmpp/lib/src/errors.dart
Normal file
20
packages/moxxmpp/lib/src/errors.dart
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
|
|
||||||
|
/// An internal error class
|
||||||
|
abstract class XmppError {}
|
||||||
|
|
||||||
|
/// Returned if we could not establish a TCP connection
|
||||||
|
/// to the server.
|
||||||
|
class NoConnectionError extends XmppError {}
|
||||||
|
|
||||||
|
/// Returned if a socket error occured
|
||||||
|
class SocketError extends XmppError {
|
||||||
|
SocketError(this.event);
|
||||||
|
final XmppSocketErrorEvent event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned if we time out
|
||||||
|
class TimeoutError extends XmppError {}
|
||||||
|
|
||||||
|
/// Returned if we received a stream error
|
||||||
|
class StreamError extends XmppError {}
|
||||||
@@ -8,6 +8,7 @@ import 'package:moxxmpp/src/xeps/xep_0066.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
@@ -71,6 +72,7 @@ class MessageEvent extends XmppEvent {
|
|||||||
this.fun,
|
this.fun,
|
||||||
this.funReplacement,
|
this.funReplacement,
|
||||||
this.funCancellation,
|
this.funCancellation,
|
||||||
|
this.messageRetraction,
|
||||||
});
|
});
|
||||||
final String body;
|
final String body;
|
||||||
final JID fromJid;
|
final JID fromJid;
|
||||||
@@ -90,6 +92,7 @@ class MessageEvent extends XmppEvent {
|
|||||||
final String? funReplacement;
|
final String? funReplacement;
|
||||||
final String? funCancellation;
|
final String? funCancellation;
|
||||||
final bool encrypted;
|
final bool encrypted;
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
final Map<String, dynamic> other;
|
final Map<String, dynamic> other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import 'package:moxxmpp/src/xeps/xep_0203.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
@@ -55,6 +56,9 @@ class StanzaHandlerData with _$StanzaHandlerData {
|
|||||||
// This is for stanza handlers that are not part of the XMPP library but still need
|
// This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
@Default(<String, dynamic>{}) Map<String, dynamic> other,
|
@Default(<String, dynamic>{}) Map<String, dynamic> other,
|
||||||
|
// If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? messageRetraction,
|
||||||
}
|
}
|
||||||
) = _StanzaHandlerData;
|
) = _StanzaHandlerData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,11 @@ mixin _$StanzaHandlerData {
|
|||||||
DelayedDelivery? get delayedDelivery =>
|
DelayedDelivery? get delayedDelivery =>
|
||||||
throw _privateConstructorUsedError; // This is for stanza handlers that are not part of the XMPP library but still need
|
throw _privateConstructorUsedError; // This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
Map<String, dynamic> get other => throw _privateConstructorUsedError;
|
Map<String, dynamic> get other =>
|
||||||
|
throw _privateConstructorUsedError; // If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? get messageRetraction =>
|
||||||
|
throw _privateConstructorUsedError;
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
$StanzaHandlerDataCopyWith<StanzaHandlerData> get copyWith =>
|
$StanzaHandlerDataCopyWith<StanzaHandlerData> get copyWith =>
|
||||||
@@ -87,7 +91,8 @@ abstract class $StanzaHandlerDataCopyWith<$Res> {
|
|||||||
bool encrypted,
|
bool encrypted,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other});
|
Map<String, dynamic> other,
|
||||||
|
MessageRetractionData? messageRetraction});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @nodoc
|
/// @nodoc
|
||||||
@@ -122,6 +127,7 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
|
Object? messageRetraction = freezed,
|
||||||
}) {
|
}) {
|
||||||
return _then(_value.copyWith(
|
return _then(_value.copyWith(
|
||||||
done: done == freezed
|
done: done == freezed
|
||||||
@@ -208,6 +214,10 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value.other
|
? _value.other
|
||||||
: other // ignore: cast_nullable_to_non_nullable
|
: other // ignore: cast_nullable_to_non_nullable
|
||||||
as Map<String, dynamic>,
|
as Map<String, dynamic>,
|
||||||
|
messageRetraction: messageRetraction == freezed
|
||||||
|
? _value.messageRetraction
|
||||||
|
: messageRetraction // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageRetractionData?,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,7 +250,8 @@ abstract class _$$_StanzaHandlerDataCopyWith<$Res>
|
|||||||
bool encrypted,
|
bool encrypted,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other});
|
Map<String, dynamic> other,
|
||||||
|
MessageRetractionData? messageRetraction});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @nodoc
|
/// @nodoc
|
||||||
@@ -277,6 +288,7 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
|
Object? messageRetraction = freezed,
|
||||||
}) {
|
}) {
|
||||||
return _then(_$_StanzaHandlerData(
|
return _then(_$_StanzaHandlerData(
|
||||||
done == freezed
|
done == freezed
|
||||||
@@ -363,6 +375,10 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value._other
|
? _value._other
|
||||||
: other // ignore: cast_nullable_to_non_nullable
|
: other // ignore: cast_nullable_to_non_nullable
|
||||||
as Map<String, dynamic>,
|
as Map<String, dynamic>,
|
||||||
|
messageRetraction: messageRetraction == freezed
|
||||||
|
? _value.messageRetraction
|
||||||
|
: messageRetraction // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageRetractionData?,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -387,7 +403,8 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
this.encrypted = false,
|
this.encrypted = false,
|
||||||
this.encryptionType,
|
this.encryptionType,
|
||||||
this.delayedDelivery,
|
this.delayedDelivery,
|
||||||
final Map<String, dynamic> other = const <String, dynamic>{}})
|
final Map<String, dynamic> other = const <String, dynamic>{},
|
||||||
|
this.messageRetraction})
|
||||||
: _other = other;
|
: _other = other;
|
||||||
|
|
||||||
// Indicates to the runner that processing is now done. This means that all
|
// Indicates to the runner that processing is now done. This means that all
|
||||||
@@ -463,9 +480,14 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
return EqualUnmodifiableMapView(_other);
|
return EqualUnmodifiableMapView(_other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
@override
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return 'StanzaHandlerData(done: $done, cancel: $cancel, cancelReason: $cancelReason, stanza: $stanza, retransmitted: $retransmitted, sims: $sims, sfs: $sfs, oob: $oob, stableId: $stableId, reply: $reply, chatState: $chatState, isCarbon: $isCarbon, deliveryReceiptRequested: $deliveryReceiptRequested, isMarkable: $isMarkable, fun: $fun, funReplacement: $funReplacement, funCancellation: $funCancellation, encrypted: $encrypted, encryptionType: $encryptionType, delayedDelivery: $delayedDelivery, other: $other)';
|
return 'StanzaHandlerData(done: $done, cancel: $cancel, cancelReason: $cancelReason, stanza: $stanza, retransmitted: $retransmitted, sims: $sims, sfs: $sfs, oob: $oob, stableId: $stableId, reply: $reply, chatState: $chatState, isCarbon: $isCarbon, deliveryReceiptRequested: $deliveryReceiptRequested, isMarkable: $isMarkable, fun: $fun, funReplacement: $funReplacement, funCancellation: $funCancellation, encrypted: $encrypted, encryptionType: $encryptionType, delayedDelivery: $delayedDelivery, other: $other, messageRetraction: $messageRetraction)';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -501,7 +523,9 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
.equals(other.encryptionType, encryptionType) &&
|
.equals(other.encryptionType, encryptionType) &&
|
||||||
const DeepCollectionEquality()
|
const DeepCollectionEquality()
|
||||||
.equals(other.delayedDelivery, delayedDelivery) &&
|
.equals(other.delayedDelivery, delayedDelivery) &&
|
||||||
const DeepCollectionEquality().equals(other._other, this._other));
|
const DeepCollectionEquality().equals(other._other, this._other) &&
|
||||||
|
const DeepCollectionEquality()
|
||||||
|
.equals(other.messageRetraction, messageRetraction));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -527,7 +551,8 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
const DeepCollectionEquality().hash(encrypted),
|
const DeepCollectionEquality().hash(encrypted),
|
||||||
const DeepCollectionEquality().hash(encryptionType),
|
const DeepCollectionEquality().hash(encryptionType),
|
||||||
const DeepCollectionEquality().hash(delayedDelivery),
|
const DeepCollectionEquality().hash(delayedDelivery),
|
||||||
const DeepCollectionEquality().hash(_other)
|
const DeepCollectionEquality().hash(_other),
|
||||||
|
const DeepCollectionEquality().hash(messageRetraction)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
@@ -556,7 +581,8 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
final bool encrypted,
|
final bool encrypted,
|
||||||
final ExplicitEncryptionType? encryptionType,
|
final ExplicitEncryptionType? encryptionType,
|
||||||
final DelayedDelivery? delayedDelivery,
|
final DelayedDelivery? delayedDelivery,
|
||||||
final Map<String, dynamic> other}) = _$_StanzaHandlerData;
|
final Map<String, dynamic> other,
|
||||||
|
final MessageRetractionData? messageRetraction}) = _$_StanzaHandlerData;
|
||||||
|
|
||||||
@override // Indicates to the runner that processing is now done. This means that all
|
@override // Indicates to the runner that processing is now done. This means that all
|
||||||
// pre-processing is done and no other handlers should be consulted.
|
// pre-processing is done and no other handlers should be consulted.
|
||||||
@@ -606,6 +632,9 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
@override // This is for stanza handlers that are not part of the XMPP library but still need
|
@override // This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
Map<String, dynamic> get other;
|
Map<String, dynamic> get other;
|
||||||
|
@override // If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? get messageRetraction;
|
||||||
@override
|
@override
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
_$$_StanzaHandlerDataCopyWith<_$_StanzaHandlerData> get copyWith =>
|
_$$_StanzaHandlerDataCopyWith<_$_StanzaHandlerData> get copyWith =>
|
||||||
|
|||||||
@@ -24,3 +24,4 @@ const omemoManager = 'org.moxxmpp.omemomanager';
|
|||||||
const emeManager = 'org.moxxmpp.ememanager';
|
const emeManager = 'org.moxxmpp.ememanager';
|
||||||
const cryptographicHashManager = 'org.moxxmpp.cryptographichashmanager';
|
const cryptographicHashManager = 'org.moxxmpp.cryptographichashmanager';
|
||||||
const delayedDeliveryManager = 'org.moxxmpp.delayeddeliverymanager';
|
const delayedDeliveryManager = 'org.moxxmpp.delayeddeliverymanager';
|
||||||
|
const messageRetractionManager = 'org.moxxmpp.messageretractionmanager';
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0184.dart';
|
import 'package:moxxmpp/src/xeps/xep_0184.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0333.dart';
|
import 'package:moxxmpp/src/xeps/xep_0333.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0448.dart';
|
import 'package:moxxmpp/src/xeps/xep_0448.dart';
|
||||||
|
|
||||||
class MessageDetails {
|
class MessageDetails {
|
||||||
|
|
||||||
const MessageDetails({
|
const MessageDetails({
|
||||||
required this.to,
|
required this.to,
|
||||||
this.body,
|
this.body,
|
||||||
@@ -35,6 +35,7 @@ class MessageDetails {
|
|||||||
this.funReplacement,
|
this.funReplacement,
|
||||||
this.funCancellation,
|
this.funCancellation,
|
||||||
this.shouldEncrypt = false,
|
this.shouldEncrypt = false,
|
||||||
|
this.messageRetraction,
|
||||||
});
|
});
|
||||||
final String to;
|
final String to;
|
||||||
final String? body;
|
final String? body;
|
||||||
@@ -51,6 +52,7 @@ class MessageDetails {
|
|||||||
final String? funReplacement;
|
final String? funReplacement;
|
||||||
final String? funCancellation;
|
final String? funCancellation;
|
||||||
final bool shouldEncrypt;
|
final bool shouldEncrypt;
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
}
|
}
|
||||||
|
|
||||||
class MessageManager extends XmppManagerBase {
|
class MessageManager extends XmppManagerBase {
|
||||||
@@ -95,6 +97,7 @@ class MessageManager extends XmppManagerBase {
|
|||||||
funReplacement: state.funReplacement,
|
funReplacement: state.funReplacement,
|
||||||
funCancellation: state.funCancellation,
|
funCancellation: state.funCancellation,
|
||||||
encrypted: state.encrypted,
|
encrypted: state.encrypted,
|
||||||
|
messageRetraction: state.messageRetraction,
|
||||||
other: state.other,
|
other: state.other,
|
||||||
),);
|
),);
|
||||||
|
|
||||||
@@ -159,6 +162,8 @@ class MessageManager extends XmppManagerBase {
|
|||||||
} else if (firstSource is StatelessFileSharingEncryptedSource) {
|
} else if (firstSource is StatelessFileSharingEncryptedSource) {
|
||||||
body = firstSource.source.url;
|
body = firstSource.source.url;
|
||||||
}
|
}
|
||||||
|
} else if (details.messageRetraction?.fallback != null) {
|
||||||
|
body = details.messageRetraction!.fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
stanza.addChild(
|
stanza.addChild(
|
||||||
@@ -216,6 +221,33 @@ class MessageManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (details.messageRetraction != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'apply-to',
|
||||||
|
xmlns: fasteningXmlns,
|
||||||
|
attributes: <String, String>{
|
||||||
|
'id': details.messageRetraction!.id,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'retract',
|
||||||
|
xmlns: messageRetractionXmlns,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (details.messageRetraction!.fallback != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'fallback',
|
||||||
|
xmlns: fallbackIndicationXmlns,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getAttributes().sendStanza(stanza, awaitable: false);
|
getAttributes().sendStanza(stanza, awaitable: false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,6 +114,15 @@ const simsXmlns = 'urn:xmpp:sims:1';
|
|||||||
// XEP-0420
|
// XEP-0420
|
||||||
const sceXmlns = 'urn:xmpp:sce:1';
|
const sceXmlns = 'urn:xmpp:sce:1';
|
||||||
|
|
||||||
|
// XEP-0422
|
||||||
|
const fasteningXmlns = 'urn:xmpp:fasten:0';
|
||||||
|
|
||||||
|
// XEP-0424
|
||||||
|
const messageRetractionXmlns = 'urn:xmpp:message-retract:0';
|
||||||
|
|
||||||
|
// XEp-0428
|
||||||
|
const fallbackIndicationXmlns = 'urn:xmpp:fallback:0';
|
||||||
|
|
||||||
// XEP-0446
|
// XEP-0446
|
||||||
const fileMetadataXmlns = 'urn:xmpp:file:metadata:0';
|
const fileMetadataXmlns = 'urn:xmpp:file:metadata:0';
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import 'package:moxlib/moxlib.dart';
|
import 'package:moxlib/moxlib.dart';
|
||||||
|
import 'package:moxxmpp/src/errors.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
import 'package:moxxmpp/src/settings.dart';
|
import 'package:moxxmpp/src/settings.dart';
|
||||||
import 'package:moxxmpp/src/socket.dart';
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
/// The state a negotiator is currently in
|
/// The state a negotiator is currently in
|
||||||
enum NegotiatorState {
|
enum NegotiatorState {
|
||||||
@@ -14,15 +16,15 @@ enum NegotiatorState {
|
|||||||
done,
|
done,
|
||||||
// Cancel the current attempt but we are not done
|
// Cancel the current attempt but we are not done
|
||||||
retryLater,
|
retryLater,
|
||||||
// The negotiator is in an error state
|
|
||||||
error,
|
|
||||||
// Skip the rest of the negotiation and assume the stream ready. Only use this when
|
// Skip the rest of the negotiation and assume the stream ready. Only use this when
|
||||||
// using stream restoration XEPs, like Stream Management.
|
// using stream restoration XEPs, like Stream Management.
|
||||||
skipRest,
|
skipRest,
|
||||||
}
|
}
|
||||||
|
|
||||||
class NegotiatorAttributes {
|
/// A base class for all errors that may occur during feature negotiation
|
||||||
|
abstract class NegotiatorError extends XmppError {}
|
||||||
|
|
||||||
|
class NegotiatorAttributes {
|
||||||
const NegotiatorAttributes(
|
const NegotiatorAttributes(
|
||||||
this.sendNonza,
|
this.sendNonza,
|
||||||
this.getConnectionSettings,
|
this.getConnectionSettings,
|
||||||
@@ -97,7 +99,7 @@ abstract class XmppFeatureNegotiatorBase {
|
|||||||
/// must switch some internal state to prevent getting matched immediately again.
|
/// must switch some internal state to prevent getting matched immediately again.
|
||||||
/// If ready is returned, then the negotiator indicates that it is not done with
|
/// If ready is returned, then the negotiator indicates that it is not done with
|
||||||
/// negotiation.
|
/// negotiation.
|
||||||
Future<void> negotiate(XMLNode nonza);
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza);
|
||||||
|
|
||||||
/// Reset the negotiator to a state that negotation can happen again.
|
/// Reset the negotiator to a state that negotation can happen again.
|
||||||
void reset() {
|
void reset() {
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
class ResourceBindingFailedError extends NegotiatorError {}
|
||||||
|
|
||||||
class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
||||||
|
|
||||||
ResourceBindingNegotiator() : _requestSent = false, super(0, false, bindXmlns, resourceBindingNegotiator);
|
ResourceBindingNegotiator() : _requestSent = false, super(0, false, bindXmlns, resourceBindingNegotiator);
|
||||||
@@ -23,7 +26,7 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
if (!_requestSent) {
|
if (!_requestSent) {
|
||||||
final stanza = XMLNode.xmlns(
|
final stanza = XMLNode.xmlns(
|
||||||
tag: 'iq',
|
tag: 'iq',
|
||||||
@@ -42,10 +45,10 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
_requestSent = true;
|
_requestSent = true;
|
||||||
attributes.sendNonza(stanza);
|
attributes.sendNonza(stanza);
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
} else {
|
} else {
|
||||||
if (nonza.tag != 'iq' || nonza.attributes['type'] != 'result') {
|
if (nonza.tag != 'iq' || nonza.attributes['type'] != 'result') {
|
||||||
state = NegotiatorState.error;
|
return Result(ResourceBindingFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final bind = nonza.firstTag('bind')!;
|
final bind = nonza.firstTag('bind')!;
|
||||||
@@ -53,7 +56,7 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
final resource = jid.innerText().split('/')[1];
|
final resource = jid.innerText().split('/')[1];
|
||||||
|
|
||||||
await attributes.sendEvent(ResourceBindingSuccessEvent(resource: resource));
|
await attributes.sendEvent(ResourceBindingSuccessEvent(resource: resource));
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
packages/moxxmpp/lib/src/negotiators/sasl/errors.dart
Normal file
3
packages/moxxmpp/lib/src/negotiators/sasl/errors.dart
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
|
||||||
|
class SaslFailedError extends NegotiatorError {}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
class SaslPlainAuthNonza extends SaslAuthNonza {
|
class SaslPlainAuthNonza extends SaslAuthNonza {
|
||||||
SaslPlainAuthNonza(String username, String password) : super(
|
SaslPlainAuthNonza(String username, String password) : super(
|
||||||
@@ -15,7 +16,6 @@ class SaslPlainAuthNonza extends SaslAuthNonza {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SaslPlainNegotiator extends SaslNegotiator {
|
class SaslPlainNegotiator extends SaslNegotiator {
|
||||||
|
|
||||||
SaslPlainNegotiator()
|
SaslPlainNegotiator()
|
||||||
: _authSent = false,
|
: _authSent = false,
|
||||||
_log = Logger('SaslPlainNegotiator'),
|
_log = Logger('SaslPlainNegotiator'),
|
||||||
@@ -41,7 +41,7 @@ class SaslPlainNegotiator extends SaslNegotiator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
if (!_authSent) {
|
if (!_authSent) {
|
||||||
final settings = attributes.getConnectionSettings();
|
final settings = attributes.getConnectionSettings();
|
||||||
attributes.sendNonza(
|
attributes.sendNonza(
|
||||||
@@ -49,17 +49,17 @@ class SaslPlainNegotiator extends SaslNegotiator {
|
|||||||
redact: SaslPlainAuthNonza('******', '******').toXml(),
|
redact: SaslPlainAuthNonza('******', '******').toXml(),
|
||||||
);
|
);
|
||||||
_authSent = true;
|
_authSent = true;
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
} else {
|
} else {
|
||||||
final tag = nonza.tag;
|
final tag = nonza.tag;
|
||||||
if (tag == 'success') {
|
if (tag == 'success') {
|
||||||
await attributes.sendEvent(AuthenticationSuccessEvent());
|
await attributes.sendEvent(AuthenticationSuccessEvent());
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
} else {
|
} else {
|
||||||
// We assume it's a <failure/>
|
// We assume it's a <failure/>
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
|
return Result(SaslFailedError());
|
||||||
state = NegotiatorState.error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math' show Random;
|
import 'dart:math' show Random;
|
||||||
|
|
||||||
import 'package:cryptography/cryptography.dart';
|
import 'package:cryptography/cryptography.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:random_string/random_string.dart';
|
import 'package:random_string/random_string.dart';
|
||||||
import 'package:saslprep/saslprep.dart';
|
import 'package:saslprep/saslprep.dart';
|
||||||
|
|
||||||
@@ -30,6 +31,17 @@ HashAlgorithm hashFromType(ScramHashType type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int pbkdfBitsFromHash(ScramHashType type) {
|
||||||
|
switch (type) {
|
||||||
|
// NOTE: SHA1 is 20 octets long => 20 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha1: return 160;
|
||||||
|
// NOTE: SHA256 is 32 octets long => 32 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha256: return 256;
|
||||||
|
// NOTE: SHA512 is 64 octets long => 64 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha512: return 512;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const scramSha1Mechanism = 'SCRAM-SHA-1';
|
const scramSha1Mechanism = 'SCRAM-SHA-1';
|
||||||
const scramSha256Mechanism = 'SCRAM-SHA-256';
|
const scramSha256Mechanism = 'SCRAM-SHA-256';
|
||||||
const scramSha512Mechanism = 'SCRAM-SHA-512';
|
const scramSha512Mechanism = 'SCRAM-SHA-512';
|
||||||
@@ -78,7 +90,6 @@ enum ScramState {
|
|||||||
const gs2Header = 'n,,';
|
const gs2Header = 'n,,';
|
||||||
|
|
||||||
class SaslScramNegotiator extends SaslNegotiator {
|
class SaslScramNegotiator extends SaslNegotiator {
|
||||||
|
|
||||||
// NOTE: NEVER, and I mean, NEVER set clientNonce or initalMessageNoGS2. They are just there for testing
|
// NOTE: NEVER, and I mean, NEVER set clientNonce or initalMessageNoGS2. They are just there for testing
|
||||||
SaslScramNegotiator(
|
SaslScramNegotiator(
|
||||||
int priority,
|
int priority,
|
||||||
@@ -106,7 +117,7 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
final pbkdf2 = Pbkdf2(
|
final pbkdf2 = Pbkdf2(
|
||||||
macAlgorithm: Hmac(_hash),
|
macAlgorithm: Hmac(_hash),
|
||||||
iterations: iterations,
|
iterations: iterations,
|
||||||
bits: 160, // NOTE: RFC says 20 octets => 20 octets * 8 bits/octet
|
bits: pbkdfBitsFromHash(hashType),
|
||||||
);
|
);
|
||||||
|
|
||||||
final saltedPasswordRaw = await pbkdf2.deriveKey(
|
final saltedPasswordRaw = await pbkdf2.deriveKey(
|
||||||
@@ -186,7 +197,7 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
switch (_scramState) {
|
switch (_scramState) {
|
||||||
case ScramState.preSent:
|
case ScramState.preSent:
|
||||||
if (clientNonce == null || clientNonce == '') {
|
if (clientNonce == null || clientNonce == '') {
|
||||||
@@ -200,15 +211,14 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
SaslScramAuthNonza(body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)), type: hashType),
|
SaslScramAuthNonza(body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)), type: hashType),
|
||||||
redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(),
|
redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(),
|
||||||
);
|
);
|
||||||
break;
|
return const Result(NegotiatorState.ready);
|
||||||
case ScramState.initialMessageSent:
|
case ScramState.initialMessageSent:
|
||||||
if (nonza.tag != 'challenge') {
|
if (nonza.tag != 'challenge') {
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
|
|
||||||
state = NegotiatorState.error;
|
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
return;
|
return Result(SaslFailedError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final challengeBase64 = nonza.innerText();
|
final challengeBase64 = nonza.innerText();
|
||||||
@@ -219,15 +229,14 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
SaslScramResponseNonza(body: responseBase64),
|
SaslScramResponseNonza(body: responseBase64),
|
||||||
redact: SaslScramResponseNonza(body: '******').toXml(),
|
redact: SaslScramResponseNonza(body: '******').toXml(),
|
||||||
);
|
);
|
||||||
return;
|
return const Result(NegotiatorState.ready);
|
||||||
case ScramState.challengeResponseSent:
|
case ScramState.challengeResponseSent:
|
||||||
if (nonza.tag != 'success') {
|
if (nonza.tag != 'success') {
|
||||||
// We assume it's a <failure />
|
// We assume it's a <failure />
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This assumes that the string is always "v=..." and contains no other parameters
|
// NOTE: This assumes that the string is always "v=..." and contains no other parameters
|
||||||
@@ -237,16 +246,13 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
//final error = nonza.children.first.tag;
|
//final error = nonza.children.first.tag;
|
||||||
//attributes.sendEvent(AuthenticationFailedEvent(error));
|
//attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await attributes.sendEvent(AuthenticationSuccessEvent());
|
await attributes.sendEvent(AuthenticationSuccessEvent());
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
return;
|
|
||||||
case ScramState.error:
|
case ScramState.error:
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
enum _StartTlsState {
|
enum _StartTlsState {
|
||||||
ready,
|
ready,
|
||||||
requested
|
requested
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StartTLSFailedError extends NegotiatorError {}
|
||||||
|
|
||||||
class StartTLSNonza extends XMLNode {
|
class StartTLSNonza extends XMLNode {
|
||||||
StartTLSNonza() : super.xmlns(
|
StartTLSNonza() : super.xmlns(
|
||||||
tag: 'starttls',
|
tag: 'starttls',
|
||||||
@@ -27,18 +30,17 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
final Logger _log;
|
final Logger _log;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
switch (_state) {
|
switch (_state) {
|
||||||
case _StartTlsState.ready:
|
case _StartTlsState.ready:
|
||||||
_log.fine('StartTLS is available. Performing StartTLS upgrade...');
|
_log.fine('StartTLS is available. Performing StartTLS upgrade...');
|
||||||
_state = _StartTlsState.requested;
|
_state = _StartTlsState.requested;
|
||||||
attributes.sendNonza(StartTLSNonza());
|
attributes.sendNonza(StartTLSNonza());
|
||||||
break;
|
return const Result(NegotiatorState.ready);
|
||||||
case _StartTlsState.requested:
|
case _StartTlsState.requested:
|
||||||
if (nonza.tag != 'proceed' || nonza.attributes['xmlns'] != startTlsXmlns) {
|
if (nonza.tag != 'proceed' || nonza.attributes['xmlns'] != startTlsXmlns) {
|
||||||
_log.severe('Failed to perform StartTLS negotiation');
|
_log.severe('Failed to perform StartTLS negotiation');
|
||||||
state = NegotiatorState.error;
|
return Result(StartTLSFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Securing socket');
|
_log.fine('Securing socket');
|
||||||
@@ -46,13 +48,11 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
.secure(attributes.getConnectionSettings().jid.domain);
|
.secure(attributes.getConnectionSettings().jid.domain);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
_log.severe('Failed to secure stream');
|
_log.severe('Failed to secure stream');
|
||||||
state = NegotiatorState.error;
|
return Result(StartTLSFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Stream is now TLS secured');
|
_log.fine('Stream is now TLS secured');
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,10 +80,11 @@ abstract class ReconnectionPolicy {
|
|||||||
/// for every failed attempt.
|
/// for every failed attempt.
|
||||||
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
||||||
|
|
||||||
ExponentialBackoffReconnectionPolicy()
|
ExponentialBackoffReconnectionPolicy(this._maxBackoffTime)
|
||||||
: _counter = 0,
|
: _counter = 0,
|
||||||
_log = Logger('ExponentialBackoffReconnectionPolicy'),
|
_log = Logger('ExponentialBackoffReconnectionPolicy'),
|
||||||
super();
|
super();
|
||||||
|
final int _maxBackoffTime;
|
||||||
int _counter;
|
int _counter;
|
||||||
Timer? _timer;
|
Timer? _timer;
|
||||||
final Logger _log;
|
final Logger _log;
|
||||||
@@ -93,6 +94,7 @@ class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
|||||||
final isReconnecting = await isReconnectionRunning();
|
final isReconnecting = await isReconnectionRunning();
|
||||||
if (shouldReconnect) {
|
if (shouldReconnect) {
|
||||||
if (!isReconnecting) {
|
if (!isReconnecting) {
|
||||||
|
await setIsReconnecting(true);
|
||||||
await performReconnect!();
|
await performReconnect!();
|
||||||
} else {
|
} else {
|
||||||
// Should never happen.
|
// Should never happen.
|
||||||
@@ -117,14 +119,13 @@ class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
|||||||
Future<void> onFailure() async {
|
Future<void> onFailure() async {
|
||||||
_log.finest('Failure occured. Starting exponential backoff');
|
_log.finest('Failure occured. Starting exponential backoff');
|
||||||
_counter++;
|
_counter++;
|
||||||
await setIsReconnecting(true);
|
|
||||||
|
|
||||||
if (_timer != null) {
|
if (_timer != null) {
|
||||||
_timer!.cancel();
|
_timer!.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait at max 80 seconds.
|
// Wait at max 80 seconds.
|
||||||
final seconds = min(pow(2, _counter).toInt(), 80);
|
final seconds = min(min(pow(2, _counter).toInt(), 80), _maxBackoffTime);
|
||||||
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,3 +149,23 @@ class TestingReconnectionPolicy extends ReconnectionPolicy {
|
|||||||
@override
|
@override
|
||||||
Future<void> reset() async {}
|
Future<void> reset() async {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A reconnection policy for tests that waits a constant number of seconds before
|
||||||
|
/// attempting a reconnection.
|
||||||
|
@visibleForTesting
|
||||||
|
class TestingSleepReconnectionPolicy extends ReconnectionPolicy {
|
||||||
|
TestingSleepReconnectionPolicy(this._sleepAmount) : super();
|
||||||
|
final int _sleepAmount;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onSuccess() async {}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onFailure() async {
|
||||||
|
await Future<void>.delayed(Duration(seconds: _sleepAmount));
|
||||||
|
await performReconnect!();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> reset() async {}
|
||||||
|
}
|
||||||
|
|||||||
7
packages/moxxmpp/lib/src/roster/errors.dart
Normal file
7
packages/moxxmpp/lib/src/roster/errors.dart
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
abstract class RosterError {}
|
||||||
|
|
||||||
|
/// Returned when the server's response did not contain a <query /> element
|
||||||
|
class NoQueryError extends RosterError {}
|
||||||
|
|
||||||
|
/// Unspecified error
|
||||||
|
class UnknownError extends RosterError {}
|
||||||
@@ -7,15 +7,12 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/roster/errors.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/error.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
const rosterErrorNoQuery = 1;
|
|
||||||
const rosterErrorNonResult = 2;
|
|
||||||
|
|
||||||
class XmppRosterItem {
|
class XmppRosterItem {
|
||||||
|
|
||||||
XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
|
XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
|
||||||
final String jid;
|
final String jid;
|
||||||
final String? name;
|
final String? name;
|
||||||
@@ -31,14 +28,12 @@ enum RosterRemovalResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RosterRequestResult {
|
class RosterRequestResult {
|
||||||
|
|
||||||
RosterRequestResult({ required this.items, this.ver });
|
RosterRequestResult({ required this.items, this.ver });
|
||||||
List<XmppRosterItem> items;
|
List<XmppRosterItem> items;
|
||||||
String? ver;
|
String? ver;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RosterPushEvent extends XmppEvent {
|
class RosterPushEvent extends XmppEvent {
|
||||||
|
|
||||||
RosterPushEvent({ required this.item, this.ver });
|
RosterPushEvent({ required this.item, this.ver });
|
||||||
final XmppRosterItem item;
|
final XmppRosterItem item;
|
||||||
final String? ver;
|
final String? ver;
|
||||||
@@ -53,11 +48,11 @@ class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
bool get isSupported => _supported;
|
bool get isSupported => _supported;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when the negotiator matched, meaning the server
|
// negotiate is only called when the negotiator matched, meaning the server
|
||||||
// advertises roster versioning.
|
// advertises roster versioning.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -148,7 +143,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
/// Shared code between requesting rosters without and with roster versioning, if
|
/// Shared code between requesting rosters without and with roster versioning, if
|
||||||
/// the server deems a regular roster response more efficient than n roster pushes.
|
/// the server deems a regular roster response more efficient than n roster pushes.
|
||||||
Future<MayFail<RosterRequestResult>> _handleRosterResponse(XMLNode? query) async {
|
Future<Result<RosterRequestResult, RosterError>> _handleRosterResponse(XMLNode? query) async {
|
||||||
final List<XmppRosterItem> items;
|
final List<XmppRosterItem> items;
|
||||||
if (query != null) {
|
if (query != null) {
|
||||||
items = query.children.map((item) => XmppRosterItem(
|
items = query.children.map((item) => XmppRosterItem(
|
||||||
@@ -166,7 +161,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.warning('Server response to roster request without roster versioning does not contain a <query /> element, while the type is not error. This violates RFC6121');
|
logger.warning('Server response to roster request without roster versioning does not contain a <query /> element, while the type is not error. This violates RFC6121');
|
||||||
return MayFail.failure(rosterErrorNoQuery);
|
return Result(NoQueryError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final ver = query.attributes['ver'] as String?;
|
final ver = query.attributes['ver'] as String?;
|
||||||
@@ -175,7 +170,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
await commitLastRosterVersion(ver);
|
await commitLastRosterVersion(ver);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MayFail.success(
|
return Result(
|
||||||
RosterRequestResult(
|
RosterRequestResult(
|
||||||
items: items,
|
items: items,
|
||||||
ver: ver,
|
ver: ver,
|
||||||
@@ -185,7 +180,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Requests the roster following RFC 6121 without using roster versioning.
|
/// Requests the roster following RFC 6121 without using roster versioning.
|
||||||
Future<MayFail<RosterRequestResult>> requestRoster() async {
|
Future<Result<RosterRequestResult, RosterError>> requestRoster() async {
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
final response = await attrs.sendStanza(
|
final response = await attrs.sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
@@ -201,7 +196,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
if (response.attributes['type'] != 'result') {
|
if (response.attributes['type'] != 'result') {
|
||||||
logger.warning('Error requesting roster without roster versioning: ${response.toXml()}');
|
logger.warning('Error requesting roster without roster versioning: ${response.toXml()}');
|
||||||
return MayFail.failure(rosterErrorNonResult);
|
return Result(UnknownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final query = response.firstTag('query', xmlns: rosterXmlns);
|
final query = response.firstTag('query', xmlns: rosterXmlns);
|
||||||
@@ -210,7 +205,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
/// Requests a series of roster pushes according to RFC6121. Requires that the server
|
/// Requests a series of roster pushes according to RFC6121. Requires that the server
|
||||||
/// advertises urn:xmpp:features:rosterver in the stream features.
|
/// advertises urn:xmpp:features:rosterver in the stream features.
|
||||||
Future<MayFail<RosterRequestResult?>> requestRosterPushes() async {
|
Future<Result<RosterRequestResult?, RosterError>> requestRosterPushes() async {
|
||||||
if (_rosterVersion == null) {
|
if (_rosterVersion == null) {
|
||||||
await loadLastRosterVersion();
|
await loadLastRosterVersion();
|
||||||
}
|
}
|
||||||
@@ -233,7 +228,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
if (result.attributes['type'] != 'result') {
|
if (result.attributes['type'] != 'result') {
|
||||||
logger.warning('Requesting roster pushes failed: ${result.toXml()}');
|
logger.warning('Requesting roster pushes failed: ${result.toXml()}');
|
||||||
return MayFail.failure(rosterErrorNonResult);
|
return Result(UnknownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final query = result.firstTag('query', xmlns: rosterXmlns);
|
final query = result.firstTag('query', xmlns: rosterXmlns);
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/// A wrapper class that can be used to indicate that a function may return a valid
|
|
||||||
/// instance of [T] but may also fail.
|
|
||||||
/// The way [MayFail] is intended to be used to to have function specific - or application
|
|
||||||
/// specific - error codes that can be either handled by code or be translated into a
|
|
||||||
/// localised error message for the user.
|
|
||||||
class MayFail<T> {
|
|
||||||
|
|
||||||
MayFail({ this.result, this.errorCode });
|
|
||||||
MayFail.success(this.result);
|
|
||||||
MayFail.failure(this.errorCode);
|
|
||||||
T? result;
|
|
||||||
int? errorCode;
|
|
||||||
|
|
||||||
bool isError() => result == null && errorCode != null;
|
|
||||||
|
|
||||||
T getValue() => result!;
|
|
||||||
|
|
||||||
int getErrorCode() => errorCode!;
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
/// Class that is supposed to by used with a state type S and a value type V.
|
class Result<T, V> {
|
||||||
/// The state indicates if an action was successful or not, while the value
|
|
||||||
/// type indicates the return value, i.e. a result in a computation or the
|
|
||||||
/// actual error description.
|
|
||||||
class Result<S, V> {
|
|
||||||
|
|
||||||
Result(S state, V value) : _state = state, _value = value;
|
const Result(this._data) : assert(_data is T || _data is V, 'Invalid data type: Must be either $T or $V');
|
||||||
final S _state;
|
final dynamic _data;
|
||||||
final V _value;
|
|
||||||
|
|
||||||
S getState() => _state;
|
bool isType<S>() => _data is S;
|
||||||
V getValue() => _value;
|
|
||||||
|
S get<S>() {
|
||||||
|
assert(_data is S, 'Data is not $S');
|
||||||
|
|
||||||
|
return _data as S;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
class Result<T, V> {
|
|
||||||
|
|
||||||
const Result(this._data) : assert(_data is T || _data is V, 'Invalid data type: Must be either $T or $V');
|
|
||||||
final dynamic _data;
|
|
||||||
|
|
||||||
bool isType<S>() => _data is S;
|
|
||||||
|
|
||||||
S get<S>() {
|
|
||||||
assert(_data is S, 'Data is not $S');
|
|
||||||
|
|
||||||
return _data as S;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/presence.dart';
|
import 'package:moxxmpp/src/presence.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/nonzas.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/nonzas.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/state.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/state.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
||||||
@@ -23,7 +24,6 @@ enum _StreamManagementNegotiatorState {
|
|||||||
/// StreamManagementManager at least once before connecting, if stream resumption
|
/// StreamManagementManager at least once before connecting, if stream resumption
|
||||||
/// is wanted.
|
/// is wanted.
|
||||||
class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
||||||
|
|
||||||
StreamManagementNegotiator()
|
StreamManagementNegotiator()
|
||||||
: _state = _StreamManagementNegotiatorState.ready,
|
: _state = _StreamManagementNegotiatorState.ready,
|
||||||
_supported = false,
|
_supported = false,
|
||||||
@@ -59,7 +59,7 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when we matched the stream feature, so we know
|
// negotiate is only called when we matched the stream feature, so we know
|
||||||
// that the server advertises it.
|
// that the server advertises it.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
@@ -80,7 +80,8 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
_state = _StreamManagementNegotiatorState.enableRequested;
|
_state = _StreamManagementNegotiatorState.enableRequested;
|
||||||
attributes.sendNonza(StreamManagementEnableNonza());
|
attributes.sendNonza(StreamManagementEnableNonza());
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
case _StreamManagementNegotiatorState.resumeRequested:
|
case _StreamManagementNegotiatorState.resumeRequested:
|
||||||
if (nonza.tag == 'resumed') {
|
if (nonza.tag == 'resumed') {
|
||||||
_log.finest('Stream Management resumption successful');
|
_log.finest('Stream Management resumption successful');
|
||||||
@@ -97,7 +98,7 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
_resumeFailed = false;
|
_resumeFailed = false;
|
||||||
_isResumed = true;
|
_isResumed = true;
|
||||||
state = NegotiatorState.skipRest;
|
return const Result(NegotiatorState.skipRest);
|
||||||
} else {
|
} else {
|
||||||
// We assume it is <failed />
|
// We assume it is <failed />
|
||||||
_log.info('Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...');
|
_log.info('Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...');
|
||||||
@@ -113,9 +114,8 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
_resumeFailed = true;
|
_resumeFailed = true;
|
||||||
_isResumed = false;
|
_isResumed = false;
|
||||||
_state = _StreamManagementNegotiatorState.ready;
|
_state = _StreamManagementNegotiatorState.ready;
|
||||||
state = NegotiatorState.retryLater;
|
return const Result(NegotiatorState.retryLater);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case _StreamManagementNegotiatorState.enableRequested:
|
case _StreamManagementNegotiatorState.enableRequested:
|
||||||
if (nonza.tag == 'enabled') {
|
if (nonza.tag == 'enabled') {
|
||||||
_log.finest('Stream Management enabled');
|
_log.finest('Stream Management enabled');
|
||||||
@@ -133,14 +133,12 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
} else {
|
} else {
|
||||||
// We assume a <failed />
|
// We assume a <failed />
|
||||||
_log.warning('Stream Management enablement failed');
|
_log.warning('Stream Management enablement failed');
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
class CSIActiveNonza extends XMLNode {
|
class CSIActiveNonza extends XMLNode {
|
||||||
CSIActiveNonza() : super(
|
CSIActiveNonza() : super(
|
||||||
@@ -32,11 +33,11 @@ class CSINegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
bool get isSupported => _supported;
|
bool get isSupported => _supported;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when the negotiator matched, meaning the server
|
// negotiate is only called when the negotiator matched, meaning the server
|
||||||
// advertises CSI.
|
// advertises CSI.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
10
packages/moxxmpp/lib/src/xeps/xep_0363/errors.dart
Normal file
10
packages/moxxmpp/lib/src/xeps/xep_0363/errors.dart
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
abstract class HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Returned when we don't know what JID to ask for an upload slot
|
||||||
|
class NoEntityKnownError extends HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Returned when the file we want to upload is too big
|
||||||
|
class FileTooBigError extends HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Unspecified errors
|
||||||
|
class UnknownHttpFileUploadError extends HttpFileUploadError {}
|
||||||
@@ -7,19 +7,15 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/error.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0363/errors.dart';
|
||||||
const errorNoUploadServer = 1;
|
|
||||||
const errorFileTooBig = 2;
|
|
||||||
const errorGeneric = 3;
|
|
||||||
|
|
||||||
const allowedHTTPHeaders = [ 'authorization', 'cookie', 'expires' ];
|
const allowedHTTPHeaders = [ 'authorization', 'cookie', 'expires' ];
|
||||||
|
|
||||||
class HttpFileUploadSlot {
|
class HttpFileUploadSlot {
|
||||||
|
|
||||||
const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers);
|
const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers);
|
||||||
final String putUrl;
|
final String putUrl;
|
||||||
final String getUrl;
|
final String getUrl;
|
||||||
@@ -45,7 +41,6 @@ Map<String, String> prepareHeaders(Map<String, String> headers) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HttpFileUploadManager extends XmppManagerBase {
|
class HttpFileUploadManager extends XmppManagerBase {
|
||||||
|
|
||||||
HttpFileUploadManager() : _gotSupported = false, _supported = false, super();
|
HttpFileUploadManager() : _gotSupported = false, _supported = false, super();
|
||||||
JID? _entityJid;
|
JID? _entityJid;
|
||||||
int? _maxUploadSize;
|
int? _maxUploadSize;
|
||||||
@@ -119,17 +114,17 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
/// the file's size in octets. [contentType] is optional and refers to the file's
|
/// the file's size in octets. [contentType] is optional and refers to the file's
|
||||||
/// Mime type.
|
/// Mime type.
|
||||||
/// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise.
|
/// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise.
|
||||||
Future<MayFail<HttpFileUploadSlot>> requestUploadSlot(String filename, int filesize, { String? contentType }) async {
|
Future<Result<HttpFileUploadSlot, HttpFileUploadError>> requestUploadSlot(String filename, int filesize, { String? contentType }) async {
|
||||||
if (!(await isSupported())) return MayFail.failure(errorNoUploadServer);
|
if (!(await isSupported())) return Result(NoEntityKnownError());
|
||||||
|
|
||||||
if (_entityJid == null) {
|
if (_entityJid == null) {
|
||||||
logger.warning('Attempted to request HTTP File Upload slot but no entity is known to send this request to.');
|
logger.warning('Attempted to request HTTP File Upload slot but no entity is known to send this request to.');
|
||||||
return MayFail.failure(errorNoUploadServer);
|
return Result(NoEntityKnownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_maxUploadSize != null && filesize > _maxUploadSize!) {
|
if (_maxUploadSize != null && filesize > _maxUploadSize!) {
|
||||||
logger.warning('Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit');
|
logger.warning('Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit');
|
||||||
return MayFail.failure(errorFileTooBig);
|
return Result(FileTooBigError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
@@ -154,7 +149,7 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
if (response.attributes['type']! != 'result') {
|
if (response.attributes['type']! != 'result') {
|
||||||
logger.severe('Failed to request HTTP File Upload slot.');
|
logger.severe('Failed to request HTTP File Upload slot.');
|
||||||
// TODO(Unknown): Be more precise
|
// TODO(Unknown): Be more precise
|
||||||
return MayFail.failure(errorGeneric);
|
return Result(UnknownHttpFileUploadError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final slot = response.firstTag('slot', xmlns: httpFileUploadXmlns)!;
|
final slot = response.firstTag('slot', xmlns: httpFileUploadXmlns)!;
|
||||||
@@ -169,7 +164,7 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return MayFail.success(
|
return Result(
|
||||||
HttpFileUploadSlot(
|
HttpFileUploadSlot(
|
||||||
putUrl,
|
putUrl,
|
||||||
getUrl,
|
getUrl,
|
||||||
@@ -12,7 +12,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
@@ -44,7 +44,6 @@ const _doNotEncryptList = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
abstract class OmemoManager extends XmppManagerBase {
|
abstract class OmemoManager extends XmppManagerBase {
|
||||||
|
|
||||||
OmemoManager() : _handlerLock = Lock(), _handlerFutures = {}, super();
|
OmemoManager() : _handlerLock = Lock(), _handlerFutures = {}, super();
|
||||||
|
|
||||||
final Lock _handlerLock;
|
final Lock _handlerLock;
|
||||||
|
|||||||
59
packages/moxxmpp/lib/src/xeps/xep_0424.dart
Normal file
59
packages/moxxmpp/lib/src/xeps/xep_0424.dart
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
|
||||||
|
class MessageRetractionData {
|
||||||
|
MessageRetractionData(this.id, this.fallback);
|
||||||
|
final String? fallback;
|
||||||
|
final String id;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageRetractionManager extends XmppManagerBase {
|
||||||
|
@override
|
||||||
|
String getName() => 'MessageRetractionManager';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getId() => messageRetractionManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<String> getDiscoFeatures() => [ messageRetractionXmlns ];
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
StanzaHandler(
|
||||||
|
stanzaTag: 'message',
|
||||||
|
callback: _onMessage,
|
||||||
|
// Before the MessageManager
|
||||||
|
priority: -99,
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
|
||||||
|
final applyTo = message.firstTag('apply-to', xmlns: fasteningXmlns);
|
||||||
|
if (applyTo == null) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
final retract = applyTo.firstTag('retract', xmlns: messageRetractionXmlns);
|
||||||
|
if (retract == null) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
final isFallbackBody = message.firstTag('fallback', xmlns: fallbackIndicationXmlns) != null;
|
||||||
|
|
||||||
|
return state.copyWith(
|
||||||
|
messageRetraction: MessageRetractionData(
|
||||||
|
applyTo.attributes['id']! as String,
|
||||||
|
isFallbackBody ?
|
||||||
|
message.firstTag('body')?.innerText() :
|
||||||
|
null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: moxxmpp
|
name: moxxmpp
|
||||||
description: A pure-Dart XMPP library
|
description: A pure-Dart XMPP library
|
||||||
version: 0.1.2+1
|
version: 0.1.3+1
|
||||||
homepage: https://codeberg.org/moxxy/moxxmpp
|
homepage: https://codeberg.org/moxxy/moxxmpp
|
||||||
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
|
||||||
@@ -29,5 +29,8 @@ dependencies:
|
|||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
build_runner: ^2.1.11
|
build_runner: ^2.1.11
|
||||||
|
moxxmpp_socket_tcp:
|
||||||
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
version: ^0.1.2+5
|
||||||
test: ^1.16.0
|
test: ^1.16.0
|
||||||
very_good_analysis: ^3.0.1
|
very_good_analysis: ^3.0.1
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import 'package:test/test.dart';
|
|||||||
import 'helpers/logging.dart';
|
import 'helpers/logging.dart';
|
||||||
import 'helpers/xmpp.dart';
|
import 'helpers/xmpp.dart';
|
||||||
|
|
||||||
const exampleXmlns1 = 'im:moxxy:example1';
|
const exampleXmlns1 = 'im:moxxmpp:example1';
|
||||||
const exampleNamespace1 = 'im.moxxy.test.example1';
|
const exampleNamespace1 = 'im.moxxmpp.test.example1';
|
||||||
const exampleXmlns2 = 'im:moxxy:example2';
|
const exampleXmlns2 = 'im:moxxmpp:example2';
|
||||||
const exampleNamespace2 = 'im.moxxy.test.example2';
|
const exampleNamespace2 = 'im.moxxmpp.test.example2';
|
||||||
|
|
||||||
class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
||||||
StubNegotiator1() : called = false, super(1, false, exampleXmlns1, exampleNamespace1);
|
StubNegotiator1() : called = false, super(1, false, exampleXmlns1, exampleNamespace1);
|
||||||
@@ -14,9 +14,9 @@ class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
|||||||
bool called;
|
bool called;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
called = true;
|
called = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,9 +26,9 @@ class StubNegotiator2 extends XmppFeatureNegotiatorBase {
|
|||||||
bool called;
|
bool called;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
called = true;
|
called = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,8 +47,8 @@ void main() {
|
|||||||
from="test.server"
|
from="test.server"
|
||||||
xml:lang="en">
|
xml:lang="en">
|
||||||
<stream:features xmlns="http://etherx.jabber.org/streams">
|
<stream:features xmlns="http://etherx.jabber.org/streams">
|
||||||
<example1 xmlns="im:moxxy:example1" />
|
<example1 xmlns="im:moxxmpp:example1" />
|
||||||
<example2 xmlns="im:moxxy:example2" />
|
<example2 xmlns="im:moxxmpp:example2" />
|
||||||
</stream:features>''',
|
</stream:features>''',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
27
packages/moxxmpp/test/sasl/kv_test.dart
Normal file
27
packages/moxxmpp/test/sasl/kv_test.dart
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test the Key-Value parser', () {
|
||||||
|
final result1 = parseKeyValue('n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL');
|
||||||
|
expect(result1.length, 2);
|
||||||
|
expect(result1['n']!, 'user');
|
||||||
|
expect(result1['r']!, 'fyko+d2lbbFgONRv9qkxdawL');
|
||||||
|
|
||||||
|
final result2 = parseKeyValue('r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096');
|
||||||
|
expect(result2.length, 3);
|
||||||
|
expect(result2['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j');
|
||||||
|
expect(result2['s']!, 'QSXCR+Q6sek8bf92');
|
||||||
|
expect(result2['i']!, '4096');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Test the Key-Value parser with '=' as a value", () {
|
||||||
|
final result = parseKeyValue('c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=,o=123');
|
||||||
|
expect(result.length, 4);
|
||||||
|
expect(result['c']!, 'biws');
|
||||||
|
expect(result['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j');
|
||||||
|
expect(result['p']!, 'v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=');
|
||||||
|
expect(result['o']!, '123');
|
||||||
|
});
|
||||||
|
}
|
||||||
211
packages/moxxmpp/test/sasl/scram_test.dart
Normal file
211
packages/moxxmpp/test/sasl/scram_test.dart
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:hex/hex.dart';
|
||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import '../helpers/xmpp.dart';
|
||||||
|
|
||||||
|
final scramSha1StreamFeatures = XMLNode(
|
||||||
|
tag: 'stream:features',
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'mechanisms',
|
||||||
|
xmlns: saslXmlns,
|
||||||
|
children: [
|
||||||
|
XMLNode(
|
||||||
|
tag: 'mechanism',
|
||||||
|
text: 'SCRAM-SHA-1',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
final scramSha256StreamFeatures = XMLNode(
|
||||||
|
tag: 'stream:features',
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'mechanisms',
|
||||||
|
xmlns: saslXmlns,
|
||||||
|
children: [
|
||||||
|
XMLNode(
|
||||||
|
tag: 'mechanism',
|
||||||
|
text: 'SCRAM-SHA-256',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final fakeSocket = StubTCPSocket(play: []);
|
||||||
|
test('Test SASL SCRAM-SHA-1', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
HEX.encode(await negotiator.calculateSaltedPassword('QSXCR+Q6sek8bf92', 4096)),
|
||||||
|
'1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateClientKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')),
|
||||||
|
),
|
||||||
|
'e234c47bf6c36696dd6d852b99aaa2ba26555728',
|
||||||
|
);
|
||||||
|
const authMessage = 'n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j';
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateClientSignature(authMessage, HEX.decode('e9d94660c39d65c38fbad91c358f14da0eef2bd6')),
|
||||||
|
),
|
||||||
|
'5d7138c486b0bfabdf49e3e2da8bd6e5c79db613',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
negotiator.calculateClientProof(HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613')),
|
||||||
|
),
|
||||||
|
'bf45fcbf7073d93d022466c94321745fe1c8e13b',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateServerSignature(authMessage, HEX.decode('0fe09258b3ac852ba502cc62ba903eaacdbf7d31')),
|
||||||
|
),
|
||||||
|
'ae617da6a57c4bbb2e0286568dae1d251905b0a4',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateServerKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')),
|
||||||
|
),
|
||||||
|
'0fe09258b3ac852ba502cc62ba903eaacdbf7d31',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
negotiator.calculateClientProof(
|
||||||
|
HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'),
|
||||||
|
HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'bf45fcbf7073d93d022466c94321745fe1c8e13b',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(await negotiator.calculateChallengeResponse('cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng=='), 'c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test SASL SCRAM-SHA-256', () async {
|
||||||
|
String? lastMessage;
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=rOprNGfwEbeRWgbNEkqO', 'rOprNGfwEbeRWgbNEkqO', ScramHashType.sha256);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode n, {String? redact}) => lastMessage = n.innerText(),
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha256StreamFeatures);
|
||||||
|
expect(
|
||||||
|
utf8.decode(base64Decode(lastMessage!)),
|
||||||
|
'n,,n=user,r=rOprNGfwEbeRWgbNEkqO',
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=</challenge>"));
|
||||||
|
expect(
|
||||||
|
utf8.decode(base64Decode(lastMessage!)),
|
||||||
|
'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF\$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=',
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==</success>"));
|
||||||
|
|
||||||
|
expect(result.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a positive server signature check', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
|
||||||
|
expect(result.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a negative server signature check', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
var result;
|
||||||
|
result = await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
expect(result.isType<NegotiatorState>(), true);
|
||||||
|
|
||||||
|
result = await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
expect(result.isType<NegotiatorState>(), true);
|
||||||
|
|
||||||
|
result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result.isType<NegotiatorError>(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a resetting the SCRAM negotiator', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result1 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result1.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
|
||||||
|
// Reset and try again
|
||||||
|
negotiator.reset();
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result2 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result2.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
}
|
||||||
1
packages/moxxmpp_socket_tcp/.pubignore
Normal file
1
packages/moxxmpp_socket_tcp/.pubignore
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pubspec_overrides.yaml
|
||||||
@@ -1,3 +1,19 @@
|
|||||||
|
## 0.1.2+5
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+4
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+3
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+2
|
||||||
|
|
||||||
|
- **FIX**: Fix reconnections when the connection is awaited.
|
||||||
|
|
||||||
## 0.1.2+1
|
## 0.1.2+1
|
||||||
|
|
||||||
- **FIX**: A certificate rejection does not crash the connection.
|
- **FIX**: A certificate rejection does not crash the connection.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: moxxmpp_socket_tcp
|
name: moxxmpp_socket_tcp
|
||||||
description: A socket for moxxmpp using TCP that implements the RFC6120 connection algorithm and XEP-0368
|
description: A socket for moxxmpp using TCP that implements the RFC6120 connection algorithm and XEP-0368
|
||||||
version: 0.1.2+1
|
version: 0.1.2+5
|
||||||
homepage: https://codeberg.org/moxxy/moxxmpp
|
homepage: https://codeberg.org/moxxy/moxxmpp
|
||||||
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ dependencies:
|
|||||||
meta: ^1.6.0
|
meta: ^1.6.0
|
||||||
moxxmpp:
|
moxxmpp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: ^0.1.2+1
|
version: ^0.1.3+1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
lints: ^2.0.0
|
lints: ^2.0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user