diff --git a/lib/xmpp/connection.dart b/lib/xmpp/connection.dart index cec80a2c..cd6a7fcf 100644 --- a/lib/xmpp/connection.dart +++ b/lib/xmpp/connection.dart @@ -222,33 +222,34 @@ class XmppConnection { break; } } else { - /* - final bool supportsPlain = saslMechanisms.findElements("mechanism").any( - (node) => node.innerText == "PLAIN" + final bool supportsPlain = saslMechanisms.findTags("mechanism").any( + (node) => node.innerText() == "PLAIN" ); - */ - final bool supportsScramSha1 = saslMechanisms.findTags("mechanism").any( (node) => node.innerText() == "SCRAM-SHA-1" ); - if (!supportsScramSha1) { - print("ERROR: Server does not support SCRAM-SHA-1"); + if (supportsScramSha1) { + print("Proceeding with SASL SCRAM-SHA-1 authentication"); + this._authenticator = SaslScramSha1Negotiator( + settings: this.settings, + clientNonce: "", + initialMessageNoGS2: "", + send: (data) => this._socket.write(data), + sendStreamHeader: this._sendStreamHeader + ); + this._routingState = await this._authenticator.next(null); + return; + } else if (supportsPlain && this.settings.allowPlainAuth) { + print("Proceeding with SASL PLAIN authentication"); + this._authenticator = SaslPlainNegotiator(settings: this.settings, send: (data) => this._socket.write(data), sendStreamHeader: this._sendStreamHeader); + this._routingState = await this._authenticator.next(null); + return; + } else { + print("ERROR: No supported authentication mechanisms"); this._setConnectionState(ConnectionState.ERROR); return; } - - print("Proceeding with SASL SCRAM-SHA-1 authentication"); - //this._authenticator = SaslPlainNegotiator(settings: this.settings, send: (data) => this._socket.write(data), sendStreamHeader: this._sendStreamHeader); - this._authenticator = SaslScramSha1Negotiator( - settings: this.settings, - clientNonce: "", - initialMessageNoGS2: "", - send: (data) => this._socket.write(data), - sendStreamHeader: this._sendStreamHeader - ); - this._routingState = await this._authenticator.next(null); - // Proceed with PLAIN } } diff --git a/lib/xmpp/settings.dart b/lib/xmpp/settings.dart index 07e02664..b730f2be 100644 --- a/lib/xmpp/settings.dart +++ b/lib/xmpp/settings.dart @@ -4,6 +4,7 @@ class ConnectionSettings { final BareJID jid; final String password; final bool useDirectTLS; + final bool allowPlainAuth; - ConnectionSettings({ required this.jid, required this.password, required this.useDirectTLS}); + ConnectionSettings({ required this.jid, required this.password, required this.useDirectTLS, required this.allowPlainAuth }); } diff --git a/test/xmpp_test.dart b/test/xmpp_test.dart index 42eff3b3..c77fa12b 100644 --- a/test/xmpp_test.dart +++ b/test/xmpp_test.dart @@ -76,7 +76,7 @@ class FakeSocket implements SocketWrapper { break; case 4: { this.state++; - expect(str, "show"); + expect(str, "show"); this._streamController.add(""); } @@ -91,7 +91,8 @@ void main() { final XmppConnection conn = XmppConnection(socket: fakeSocket, settings: ConnectionSettings( jid: BareJID.fromString("polynomdivision@test.server"), password: "aaaa", - useDirectTLS: true + useDirectTLS: true, + allowPlainAuth: true )); await conn.connect(); await Future.delayed(Duration(seconds: 3), () { @@ -107,7 +108,7 @@ void main() { expect(challenge.iterations, 4096); final negotiator = SaslScramSha1Negotiator( - settings: ConnectionSettings(jid: BareJID.fromString("user@server"), password: "pencil", useDirectTLS: true), + settings: ConnectionSettings(jid: BareJID.fromString("user@server"), password: "pencil", useDirectTLS: true, allowPlainAuth: true), clientNonce: "fyko+d2lbbFgONRv9qkxdawL", initialMessageNoGS2: "n=user,r=fyko+d2lbbFgONRv9qkxdawL", send: (data) {}, @@ -170,7 +171,8 @@ void main() { XmlDocument doc = builder.buildDocument(); final element = doc.getElement("root"); - expect(XMLNode.fromXmlElement(element!).toXml(), ""); + // TODO: Not sure about this one + expect(XMLNode.fromXmlElement(element!).toXml(), ""); }); test("Test bare JIDs", () {