From 941c3e4fd8c0cbdf046131702cb4349b51ccff85 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Mon, 12 Aug 2024 23:05:38 +0200 Subject: [PATCH] test(xep): Test SCRAM-SHA-1 with SASL2 --- .../lib/src/rfcs/rfc_6120/sasl/scram.dart | 3 + packages/moxxmpp/test/xeps/xep_0388_test.dart | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/packages/moxxmpp/lib/src/rfcs/rfc_6120/sasl/scram.dart b/packages/moxxmpp/lib/src/rfcs/rfc_6120/sasl/scram.dart index e753c53..c210b9b 100644 --- a/packages/moxxmpp/lib/src/rfcs/rfc_6120/sasl/scram.dart +++ b/packages/moxxmpp/lib/src/rfcs/rfc_6120/sasl/scram.dart @@ -246,6 +246,9 @@ class SaslScramNegotiator extends Sasl2AuthenticationNegotiator { bool _checkSignature(String base64Signature) { final signature = parseKeyValue(utf8.decode(base64.decode(base64Signature))); + _log.finest( + 'Expecting signature: "$_serverSignature", got: "${signature["v"]}"', + ); return signature['v']! == _serverSignature; } diff --git a/packages/moxxmpp/test/xeps/xep_0388_test.dart b/packages/moxxmpp/test/xeps/xep_0388_test.dart index 8fa81ab..6deff25 100644 --- a/packages/moxxmpp/test/xeps/xep_0388_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0388_test.dart @@ -216,6 +216,89 @@ void main() { expect(result.isType(), false); }); + test('Test SCRAM-SHA-1 SASL2 negotiation with a valid signature', () async { + final fakeSocket = StubTCPSocket([ + StringExpectation( + "", + ''' + + + + PLAIN + SCRAM-SHA-1 + + + PLAIN + SCRAM-SHA-1 + + + + + ''', + ), + StanzaExpectation( + "moxxmppPapaTutuWawa's awesome devicebiwsbj11c2VyLHI9ZnlrbytkMmxiYkZnT05Sdjlxa3hkYXdM", + ''' +cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng== + ''', + ), + StanzaExpectation( + 'Yz1iaXdzLHI9ZnlrbytkMmxiYkZnT05Sdjlxa3hkYXdMM3JmY05IWUpZMVpWdldWczdqLHA9djBYOHYzQnoyVDBDSkdiSlF5RjBYK0hJNFRzPQ==', + 'dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9user@server', + ), + StanzaExpectation( + "", + ''' +'polynomdivision@test.server/MU29eEZn', + ''', + adjustId: true, + ignoreId: true, + ), + ]); + final conn = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + ClientToServerNegotiator(), + fakeSocket, + )..connectionSettings = ConnectionSettings( + jid: JID.fromString('user@server'), + password: 'pencil', + ); + await conn.registerManagers([ + PresenceManager(), + RosterManager(TestingRosterStateManager('', [])), + DiscoManager([]), + ]); + await conn.registerFeatureNegotiators([ + SaslPlainNegotiator(), + SaslScramNegotiator( + 10, + 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', + 'fyko+d2lbbFgONRv9qkxdawL', + ScramHashType.sha1, + ), + ResourceBindingNegotiator(), + Sasl2Negotiator() + ..userAgent = const UserAgent( + id: 'd4565fa7-4d72-4749-b3d3-740edbf87770', + software: 'moxxmpp', + device: "PapaTutuWawa's awesome device", + ), + ]); + + final result = await conn.connect( + waitUntilLogin: true, + shouldReconnect: false, + enableReconnectOnSuccess: false, + ); + expect(result.isType(), false); + }); + test('Test SCRAM-SHA-1 SASL2 negotiation with an invalid signature', () async { final fakeSocket = StubTCPSocket([