feat: Rework how the negotiator system works

We can now return what exactly made a connection attempt fail.
This commit is contained in:
PapaTutuWawa 2022-11-19 21:48:28 +01:00
parent 6b106fe365
commit 2e3472d88f
13 changed files with 154 additions and 170 deletions

View File

@ -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';
@ -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 {
@ -345,12 +346,8 @@ 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');
}
// Whenever we encounter an error that would trigger a reconnection attempt while // Whenever we encounter an error that would trigger a reconnection attempt while
// the connection result is being awaited, don't attempt a reconnection but instead // the connection result is being awaited, don't attempt a reconnection but instead
@ -358,7 +355,12 @@ class XmppConnection {
if (_connectionCompleter != null) { if (_connectionCompleter != null) {
_log.info('Not triggering reconnection since connection result is being awaited'); _log.info('Not triggering reconnection since connection result is being awaited');
await _disconnect(triggeredByUser: false, state: XmppConnectionState.error); await _disconnect(triggeredByUser: false, state: XmppConnectionState.error);
_connectionCompleter?.complete(const XmppConnectionResult(false)); _connectionCompleter?.complete(
XmppConnectionResult(
false,
error: error,
),
);
_connectionCompleter = null; _connectionCompleter = null;
return; return;
} }
@ -370,7 +372,7 @@ class XmppConnection {
/// 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) {
if (_socketClosureTriggersReconnect) { if (_socketClosureTriggersReconnect) {
_log.fine('Received XmppSocketClosureEvent. Reconnecting...'); _log.fine('Received XmppSocketClosureEvent. Reconnecting...');
@ -525,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() {
@ -750,19 +752,33 @@ class XmppConnection {
await getPresenceManager().sendInitialPresence(); await getPresenceManager().sendInitialPresence();
} }
/// To be called after _currentNegotiator!.negotiate(..) has been called. Checks the Future<void> _executeCurrentNegotiator(XMLNode nonza) async {
/// state of the negotiator and picks the next negotiatior, ends negotiation or // If we don't have a negotiator get one
/// waits, depending on what the negotiator did. _currentNegotiator ??= getNextNegotiator(_streamFeatures);
Future<void> _checkCurrentNegotiator() async { if (_currentNegotiator == null && _isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
if (_currentNegotiator!.state == NegotiatorState.done) { _log.finest('Negotiations done!');
_log.finest('Negotiator ${_currentNegotiator!.id} 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;
@ -772,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);
@ -782,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!');
@ -804,24 +820,18 @@ 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');
await handleError(null);
} }
} }
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 {
// Check if we received a stream error // Check if we received a stream error
@ -829,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;
} }
@ -849,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:
@ -927,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) {
@ -1053,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');

View 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 {}

View File

@ -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() {

View File

@ -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);
} }
} }

View File

@ -0,0 +1,3 @@
import 'package:moxxmpp/src/negotiators/negotiator.dart';
class SaslFailedError extends NegotiatorError {}

View File

@ -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;
} }
} }
} }

View File

@ -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';
@ -89,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,
@ -197,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 == '') {
@ -211,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();
@ -230,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
@ -248,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;
} }
} }

View File

@ -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;
} }
} }

View File

@ -10,6 +10,7 @@ import 'package:moxxmpp/src/negotiators/negotiator.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/error.dart';
import 'package:moxxmpp/src/types/result.dart';
const rosterErrorNoQuery = 1; const rosterErrorNoQuery = 1;
const rosterErrorNonResult = 2; const rosterErrorNonResult = 2;
@ -53,11 +54,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

View File

@ -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;
} }
} }

View File

@ -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

View File

@ -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>''',
), ),
], ],

View File

@ -128,9 +128,9 @@ void main() {
'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF\$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=', 'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF\$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=',
); );
await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==</success>")); final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==</success>"));
expect(negotiator.state, NegotiatorState.done); expect(result.get<NegotiatorState>(), NegotiatorState.done);
}); });
test('Test a positive server signature check', () async { test('Test a positive server signature check', () async {
@ -150,9 +150,9 @@ void main() {
await negotiator.negotiate(scramSha1StreamFeatures); await negotiator.negotiate(scramSha1StreamFeatures);
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>")); await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>")); final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
expect(negotiator.state, NegotiatorState.done); expect(result.get<NegotiatorState>(), NegotiatorState.done);
}); });
test('Test a negative server signature check', () async { test('Test a negative server signature check', () async {
@ -170,11 +170,15 @@ void main() {
), ),
); );
await negotiator.negotiate(scramSha1StreamFeatures); var result;
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>")); result = await negotiator.negotiate(scramSha1StreamFeatures);
await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>")); expect(result.isType<NegotiatorState>(), true);
expect(negotiator.state, NegotiatorState.error); 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 { test('Test a resetting the SCRAM negotiator', () async {
@ -194,14 +198,14 @@ void main() {
await negotiator.negotiate(scramSha1StreamFeatures); await negotiator.negotiate(scramSha1StreamFeatures);
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>")); await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>")); final result1 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
expect(negotiator.state, NegotiatorState.done); expect(result1.get<NegotiatorState>(), NegotiatorState.done);
// Reset and try again // Reset and try again
negotiator.reset(); negotiator.reset();
await negotiator.negotiate(scramSha1StreamFeatures); await negotiator.negotiate(scramSha1StreamFeatures);
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>")); await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>")); final result2 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
expect(negotiator.state, NegotiatorState.done); expect(result2.get<NegotiatorState>(), NegotiatorState.done);
}); });
} }