o b7@sddlmZmZmZddlmZddlmZmZm Z dZ dZ dZ dZ e dZe d Zd d ZGd d d ejZGdddZGdddejZGdddZGdddejZGdddejZdddZGdddejZdS))errorsasl xmlstream)JID)domishutilityxpathz#urn:ietf:params:xml:ns:xmpp-streamsz urn:ietf:params:xml:ns:xmpp-bindz#urn:ietf:params:xml:ns:xmpp-sessionz"http://jabber.org/features/iq-authz/iq/query/digestz/iq/query/passwordcCst||}t|SN)BasicAuthenticatorrXmlStreamFactory)jidsecretarG/usr/lib/python3/dist-packages/twisted/words/protocols/jabber/client.pybasicClientFactorys  rc@s4eZdZdZd ddZddZd dd Zd d ZdS)IQa Wrapper for a Info/Query packet. This provides the necessary functionality to send IQs and get notified when a result comes back. It's a subclass from L{domish.Element}, so you can use the standard DOM manipulation calls to add data to the outbound request. @type callbacks: L{utility.CallbackList} @cvar callbacks: Callback list to be notified when response comes back setcCs2tj|d|||d<||_t|_dS)z @type xmlstream: L{xmlstream.XmlStream} @param xmlstream: XmlStream to use for transmission of this IQ @type type: C{str} @param type: IQ type identifier ('get' or 'set') ) jabber:clientiqtypeN)rElement__init__ addUniqueId _xmlstreamr CallbackList callbacks)selfrrrrrr&s  z IQ.__init__cOs |jjd|g|Ri|dS)zW Register a callback for notification when the IQ result is available. TN)r addCallback)rfnargskwargsrrrr5s zIQ.addCallbackNcCs8|dkr||d<|jd|d|j|j|dS)a? Call this method to send this IQ request via the associated XmlStream. @param to: Jabber ID of the entity to send the request to @type to: C{str} @returns: Callback list for this IQ. Any callbacks added to this list will be fired when the result comes back. Ntoz /iq[@id='%s']id)raddOnetimeObserver _resultEventsend)rr"rrrr&<s  zIQ.sendcCs|j|d|_dSr )rcallbackrrrrrr%Ms  zIQ._resultEvent)rr )__name__ __module__ __qualname____doc__rrr&r%rrrrrs    rc@sHeZdZdZdZdZddZddZdd Zd d Z d d Z ddZ dS)IQAuthInitializera Non-SASL Authentication initializer for the initiating entity. This protocol is defined in U{JEP-0078} and mainly serves for compatibility with pre-XMPP-1.0 server implementations. @cvar INVALID_USER_EVENT: Token to signal that authentication failed, due to invalid username. @type INVALID_USER_EVENT: L{str} @cvar AUTH_FAILED_EVENT: Token to signal that authentication failed, due to invalid password. @type AUTH_FAILED_EVENT: L{str} z$//event/client/basicauth/invaliduserz#//event/client/basicauth/authfailedcC ||_dSr rrxsrrrrf zIQAuthInitializer.__init__cCsPt|jd}|d|jjj}|jjd|jd|}||j |j |S)Ngetzjabber:iq:authqueryusernamecontent) rr addElement authenticatorr r5userr& addCallbacks _cbAuthQuery _ebAuthQuery)rrr drrr initializeis  zIQAuthInitializer.initializecCs|jjj}|jjj}t|jd}|d|jjd|jd|jjd|jdt |r@t |jj |}|jjdt |dn|jjd|d|}||j|j|S)Nrr4r6r7resourcedigestpassword)rr:r rCrr9r5r;rA DigestAuthQrymatches hashPasswordsidstrr&r<_cbAuth_ebAuth)rrr rCreplyrBr?rrrr=ts    zIQAuthInitializer._cbAuthQuerycCsH|tj|j}|jdkr|j|j|j|S|j|j|j |S)Nznot-authorized) trapr StanzaErrorvalue conditionrdispatchstanzaINVALID_USER_EVENTAUTH_FAILED_EVENT)rfailureerrrr>s  zIQAuthInitializer._ebAuthQuerycCsdSr rr(rrrrIszIQAuthInitializer._cbAuthcCs$|tj|j|jj|j|Sr )rLrrMrrPrNrQrS)rrTrrrrJs zIQAuthInitializer._ebAuthN) r)r*r+r,rRrSrr@r=r>rIrJrrrrr-Rs  r-c@sFeZdZdZdZejZejZdZddZ ddZ d d d Z d d Z dS)r aO Authenticates an XmlStream against a Jabber server as a Client. This only implements non-SASL authentication, per U{JEP-0078}. Additionally, this authenticator provides the ability to perform inline registration, per U{JEP-0077}. Under normal circumstances, the BasicAuthenticator generates the L{xmlstream.STREAM_AUTHD_EVENT} once the stream has authenticated. However, it can also generate other events, such as: - L{INVALID_USER_EVENT} : Authentication failed, due to invalid username - L{AUTH_FAILED_EVENT} : Authentication failed, due to invalid password - L{REGISTER_FAILED_EVENT} : Registration failed If authentication fails for any reason, you can attempt to register by calling the L{registerAccount} method. If the registration succeeds, a L{xmlstream.STREAM_AUTHD_EVENT} will be fired. Otherwise, one of the above errors will be generated (again). @cvar INVALID_USER_EVENT: See L{IQAuthInitializer.INVALID_USER_EVENT}. @type INVALID_USER_EVENT: L{str} @cvar AUTH_FAILED_EVENT: See L{IQAuthInitializer.AUTH_FAILED_EVENT}. @type AUTH_FAILED_EVENT: L{str} @cvar REGISTER_FAILED_EVENT: Token to signal that registration failed. @type REGISTER_FAILED_EVENT: L{str} rz'//event/client/basicauth/registerfailedcCs tj||j||_||_dSr )rConnectAuthenticatorrhostr rC)rr rCrrrrs zBasicAuthenticator.__init__cCs0d|_tj||tj|ddt|g|_dS)N)rrFrequired)versionrrVassociateWithStreamTLSInitiatingInitializerr- initializersr0rrrr[s   z&BasicAuthenticator.associateWithStreamNcCsj|r||j_|r ||_t|jd}|d|jjd|jjd|jjd|jd||j| dS)Nr)zjabber:iq:registerr5r6r7rC) r r;rCrrr9r5r_registerResultEventr&)rr6rCrrrrregisterAccounts    z"BasicAuthenticator.registerAccountcCs,|ddkr |dS|j||jdS)Nrresult) streamStartedrrPREGISTER_FAILED_EVENTr(rrrr^s  z'BasicAuthenticator._registerResultEvent)NN) r)r*r+r, namespacer-rRrSrbrr[r_r^rrrrr s  r c@s eZdZdZddZddZdS)CheckVersionInitializerzU Initializer that checks if the minimum common stream version number is 1.0. cCr.r r/r0rrrrr2z CheckVersionInitializer.__init__cCs|jjdkr tddS)N)rzunsupported-version)rrZr StreamError)rrrrr@s  z"CheckVersionInitializer.initializeN)r)r*r+r,rr@rrrrrds rdc@s(eZdZdZedfZddZddZdS)BindInitializerz Initializer that implements Resource Binding for the initiating entity. This protocol is documented in U{RFC 3920, section 7}. bindcCsRt|jd}|tdf}|jjjj}|r|jd|d|}||j |S)NrrhrAr7) rrr9 NS_XMPP_BINDr:r rAr&ronBind)rrrhrAr?rrrstarts  zBindInitializer.startcCs$|jrtt|jj|jj_dSdSr )rhrrHr rr:r(rrrrj szBindInitializer.onBindN)r)r*r+r,rifeaturerkrjrrrrrgs  rgc@s eZdZdZedfZddZdS)SessionInitializerz Initializer that implements session establishment for the initiating entity. This protocol is defined in U{RFC 3921, section 3}. sessioncCs$t|jd}|tdf|S)Nrrn)rrr9NS_XMPP_SESSIONr&r(rrrrkszSessionInitializer.startN)r)r*r+r,rorlrkrrrrrms rmNcCst|||d}t|S)a Client factory for XMPP 1.0 (only). This returns a L{xmlstream.XmlStreamFactory} with an L{XMPPAuthenticator} object to perform the stream initialization steps (such as authentication). @see: The notes at L{XMPPAuthenticator} describe how the C{jid} and C{password} parameters are to be used. @param jid: Jabber ID to connect with. @type jid: L{jid.JID} @param password: password to authenticate with. @type password: L{unicode} @param configurationForTLS: An object which creates appropriately configured TLS connections. This is passed to C{startTLS} on the transport and is preferably created using L{twisted.internet.ssl.optionsForClientTLS}. If L{None}, the default is to verify the server certificate against the trust roots as provided by the platform. See L{twisted.internet._sslverify.platformTrust}. @type configurationForTLS: L{IOpenSSLClientConnectionCreator} or L{None} @return: XML stream factory. @rtype: L{xmlstream.XmlStreamFactory} )configurationForTLS)XMPPAuthenticatorrr )r rCrprrrrXMPPClientFactory"s rrc@s&eZdZdZdZdddZddZdS) rqa Initializes an XmlStream connecting to an XMPP server as a Client. This authenticator performs the initialization steps needed to start exchanging XML stanzas with an XMPP server as an XMPP client. It checks if the server advertises XML stream version 1.0, negotiates TLS (when available), performs SASL authentication, binds a resource and establishes a session. Upon successful stream initialization, the L{xmlstream.STREAM_AUTHD_EVENT} event will be dispatched through the XML stream object. Otherwise, the L{xmlstream.INIT_FAILED_EVENT} event will be dispatched with a failure object. After inspection of the failure, initialization can then be restarted by calling L{ConnectAuthenticator.initializeStream}. For example, in case of authentication failure, a user may be given the opportunity to input the correct password. By setting the L{password} instance variable and restarting initialization, the stream authentication step is then retried, and subsequent steps are performed if successful. @ivar jid: Jabber ID to authenticate with. This may contain a resource part, as a suggestion to the server for resource binding. A server may override this, though. If the resource part is left off, the server will generate a unique resource identifier. The server will always return the full Jabber ID in the resource binding step, and this is stored in this instance variable. @type jid: L{jid.JID} @ivar password: password to be used during SASL authentication. @type password: L{unicode} rNcCs&tj||j||_||_||_dS)a3 @param configurationForTLS: An object which creates appropriately configured TLS connections. This is passed to C{startTLS} on the transport and is preferably created using L{twisted.internet.ssl.optionsForClientTLS}. If C{None}, the default is to verify the server certificate against the trust roots as provided by the platform. See L{twisted.internet._sslverify.platformTrust}. @type configurationForTLS: L{IOpenSSLClientConnectionCreator} or C{None} N)rrVrrWr rC_configurationForTLS)rr rCrprrrrfs  zXMPPAuthenticator.__init__cCsNtj||t|tj|d|jdtj|ddt|ddt |ddg|_ dS)z Register with the XML stream. Populates stream's list of initializers, along with their requiredness. This list is used by L{ConnectAuthenticator.initializeStream} to perform the initialization steps. T)rYrprXFN) rrVr[rdr\rsrSASLInitiatingInitializerrgrmr]r0rrrr[ws     z%XMPPAuthenticator.associateWithStreamr )r)r*r+r,rcrr[rrrrrqAs "  rqr )twisted.words.protocols.jabberrrr"twisted.words.protocols.jabber.jidrtwisted.words.xishrrrNS_XMPP_STREAMSriroNS_IQ_AUTH_FEATURE internQueryrDPlaintextAuthQryrrrr-rVr rd BaseFeatureInitiatingInitializerrgrmrrrqrrrrs$   9JO