xmpp: Make PLAIN auth configurable

This commit is contained in:
PapaTutuWawa 2021-12-30 23:26:03 +01:00
parent df258df2e4
commit 293af5b360
3 changed files with 28 additions and 24 deletions

View File

@ -222,24 +222,15 @@ class XmppConnection {
break; break;
} }
} else { } else {
/* final bool supportsPlain = saslMechanisms.findTags("mechanism").any(
final bool supportsPlain = saslMechanisms.findElements("mechanism").any( (node) => node.innerText() == "PLAIN"
(node) => node.innerText == "PLAIN"
); );
*/
final bool supportsScramSha1 = saslMechanisms.findTags("mechanism").any( final bool supportsScramSha1 = saslMechanisms.findTags("mechanism").any(
(node) => node.innerText() == "SCRAM-SHA-1" (node) => node.innerText() == "SCRAM-SHA-1"
); );
if (!supportsScramSha1) { if (supportsScramSha1) {
print("ERROR: Server does not support SCRAM-SHA-1");
this._setConnectionState(ConnectionState.ERROR);
return;
}
print("Proceeding with SASL SCRAM-SHA-1 authentication"); 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( this._authenticator = SaslScramSha1Negotiator(
settings: this.settings, settings: this.settings,
clientNonce: "", clientNonce: "",
@ -248,7 +239,17 @@ class XmppConnection {
sendStreamHeader: this._sendStreamHeader sendStreamHeader: this._sendStreamHeader
); );
this._routingState = await this._authenticator.next(null); this._routingState = await this._authenticator.next(null);
// Proceed with PLAIN 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;
}
} }
} }

View File

@ -4,6 +4,7 @@ class ConnectionSettings {
final BareJID jid; final BareJID jid;
final String password; final String password;
final bool useDirectTLS; 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 });
} }

View File

@ -76,7 +76,7 @@ class FakeSocket implements SocketWrapper {
break; break;
case 4: { case 4: {
this.state++; this.state++;
expect(str, "<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show >show</show></presence>"); expect(str, "<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show>show</show></presence>");
this._streamController.add("<presence /><message />"); this._streamController.add("<presence /><message />");
} }
@ -91,7 +91,8 @@ void main() {
final XmppConnection conn = XmppConnection(socket: fakeSocket, settings: ConnectionSettings( final XmppConnection conn = XmppConnection(socket: fakeSocket, settings: ConnectionSettings(
jid: BareJID.fromString("polynomdivision@test.server"), jid: BareJID.fromString("polynomdivision@test.server"),
password: "aaaa", password: "aaaa",
useDirectTLS: true useDirectTLS: true,
allowPlainAuth: true
)); ));
await conn.connect(); await conn.connect();
await Future.delayed(Duration(seconds: 3), () { await Future.delayed(Duration(seconds: 3), () {
@ -107,7 +108,7 @@ void main() {
expect(challenge.iterations, 4096); expect(challenge.iterations, 4096);
final negotiator = SaslScramSha1Negotiator( 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", clientNonce: "fyko+d2lbbFgONRv9qkxdawL",
initialMessageNoGS2: "n=user,r=fyko+d2lbbFgONRv9qkxdawL", initialMessageNoGS2: "n=user,r=fyko+d2lbbFgONRv9qkxdawL",
send: (data) {}, send: (data) {},
@ -170,7 +171,8 @@ void main() {
XmlDocument doc = builder.buildDocument(); XmlDocument doc = builder.buildDocument();
final element = doc.getElement("root"); final element = doc.getElement("root");
expect(XMLNode.fromXmlElement(element!).toXml(), "<root owo='uwu' />"); // TODO: Not sure about this one
expect(XMLNode.fromXmlElement(element!).toXml(), "<root owo='uwu'></root>");
}); });
test("Test bare JIDs", () { test("Test bare JIDs", () {