@@ -36,22 +36,24 @@ #import #import #import "XMPPConnection.h" +#import "XMPPANONYMOUSAuth.h" #import "XMPPCallback.h" #import "XMPPEXTERNALAuth.h" -#import "XMPPSCRAMAuth.h" -#import "XMPPPLAINAuth.h" -#import "XMPPStanza.h" +#import "XMPPExceptions.h" +#import "XMPPIQ.h" #import "XMPPJID.h" -#import "XMPPIQ.h" #import "XMPPMessage.h" +#import "XMPPMulticastDelegate.h" +#import "XMPPPLAINAuth.h" #import "XMPPPresence.h" -#import "XMPPMulticastDelegate.h" -#import "XMPPExceptions.h" +#import "XMPPSCRAMAuth.h" +#import "XMPPStanza.h" #import "XMPPXMLElementBuilder.h" + #import "namespaces.h" #import #define BUFFER_LENGTH 512 @@ -97,10 +99,11 @@ @end @implementation XMPPConnection @synthesize username = _username, resource = _resource, server = _server; @synthesize domain = _domain, password = _password, JID = _JID, port = _port; +@synthesize usesAnonymousAuthentication = _usesAnonymousAuthentication; @synthesize language = _language, privateKeyFile = _privateKeyFile; @synthesize certificateFile = _certificateFile, socket = _socket; @synthesize encryptionRequired = _encryptionRequired, encrypted = _encrypted; @synthesize supportsRosterVersioning = _supportsRosterVersioning; @synthesize supportsStreamManagement = _supportsStreamManagement; @@ -1011,10 +1014,22 @@ _supportsStreamManagement = true; if (mechs != nil) { for (OFXMLElement *mech in [mechs children]) [mechanisms addObject: [mech stringValue]]; + + if (_usesAnonymousAuthentication) { + if (![mechanisms containsObject: @"ANONYMOUS"]) + @throw [XMPPAuthFailedException + exceptionWithConnection: self + reason: @"No supported " + @"auth mechanism"]; + + _authModule = [[XMPPANONYMOUSAuth alloc] init]; + [self xmpp_sendAuth: @"ANONYMOUS"]; + return; + } if (_privateKeyFile != nil && _certificateFile != nil && [mechanisms containsObject: @"EXTERNAL"]) { _authModule = [[XMPPEXTERNALAuth alloc] init]; [self xmpp_sendAuth: @"EXTERNAL"]; @@ -1049,11 +1064,14 @@ password: _password]; [self xmpp_sendAuth: @"PLAIN"]; return; } - assert(0); + @throw [XMPPAuthFailedException + exceptionWithConnection: self + reason: @"No supported auth mechanism"]; + } if (session != nil && [session elementForName: @"optional" namespace: XMPP_NS_SESSION] == nil) _needsSession = true; @@ -1073,11 +1091,11 @@ authTag = [OFXMLElement elementWithName: @"auth" namespace: XMPP_NS_SASL]; [authTag addAttributeWithName: @"mechanism" stringValue: authName]; - if (initialMessage) { + if (initialMessage != nil) { if ([initialMessage count] == 0) [authTag setStringValue: @"="]; else [authTag setStringValue: [initialMessage stringByBase64Encoding]];