lightmelody

changeset 691:038f620f4f2a objectify

Early work on rewriting Lm.SASL
author Arc Riley <arcriley@gmail.com>
date Thu, 27 Dec 2012 17:35:26 -0500
parents 523537516ba4
children 7bef73e76352
files src/SASL.gs
diffstat 1 files changed, 88 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SASL.gs	Thu Dec 27 17:35:26 2012 -0500
     1.3 @@ -0,0 +1,88 @@
     1.4 +/*
     1.5 + *  LightMelody - Lightweight XMPP/Jingle Client Library
     1.6 + *  Copyright (C) 2012 Copyleft Games Group
     1.7 + *
     1.8 + *  This program is free software; you can redistribute it and/or modify
     1.9 + *  it under the terms of the GNU Affero General Public License as published
    1.10 + *  by the Free Software Foundation, either version 3 of the License, or
    1.11 + *  (at your option) any later version.
    1.12 + *
    1.13 + *  This program is distributed in the hope that it will be useful,
    1.14 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *  GNU Affero General Public License for more details.
    1.17 + *
    1.18 + *  You should have received a copy of the GNU Affero General Public License
    1.19 + *  along with this program; if not, see http://www.gnu.org/licenses
    1.20 + *
    1.21 + */
    1.22 +
    1.23 +[indent=4]
    1.24 +
    1.25 +class Lm.SASLA
    1.26 +    connection : Connection
    1.27 +    features_received : bool
    1.28 +    start_auth : bool
    1.29 +    auth_type : int
    1.30 +    features_cb : MessageHandler
    1.31 +
    1.32 +    [CCode (cprefix="AUTH_TYPE_")]
    1.33 +    enum AuthType
    1.34 +        PLAIN  = 1
    1.35 +        DIGEST = 2
    1.36 +        WEB = 4
    1.37 +
    1.38 +    [CCode (has_target=false)]
    1.39 +    delegate ResultHandler (sasl : SASLA, connection : Connection,
    1.40 +                            success : bool, reason : string)
    1.41 +
    1.42 +    construct (connection : Connection)
    1.43 +        self.connection = connection
    1.44 +        self.features_received = false
    1.45 +        self.start_auth = false
    1.46 +
    1.47 +        // Create and register handler for stream features
    1.48 +        self.features_cb = new MessageHandler (self._features_handler)
    1.49 +        connection.register_message_handler(self.features_cb,
    1.50 +                                            MessageType.STREAM_FEATURES,
    1.51 +                                            HandlerPriority.LAST)
    1.52 +
    1.53 +    def authenticate (auth_params : AuthParameters, server : string,
    1.54 +                      handler : ResultHandler)
    1.55 +        return
    1.56 +
    1.57 +    def inline _is_sasl (mechanisms : MessageNode?) : bool
    1.58 +        sasl_ns : string = "urn:ietf:params:xml:ns:xmpp-sasl"
    1.59 +        if mechanisms is null
    1.60 +            return false
    1.61 +        return mechanisms.get_attribute("xmlns") == sasl_ns
    1.62 +
    1.63 +    def _features_handler (handler : MessageHandler, connection : Connection,
    1.64 +                           message : Message): HandlerResult
    1.65 +        mechanism : MessageNode?
    1.66 +        mechanisms : MessageNode? = message.node.find_child ("mechanisms")
    1.67 +        if self._is_sasl(mechanisms)
    1.68 +            self.features_received = true
    1.69 +            self.auth_type = 0
    1.70 +
    1.71 +            mechanism = mechanisms.children
    1.72 +            while mechanism is not null
    1.73 +                case mechanism.value
    1.74 +                    when "PLAIN"
    1.75 +                        self.auth_type |= AuthType.PLAIN
    1.76 +                    when "DIGEST-MD5"
    1.77 +                        self.auth_type |= AuthType.DIGEST
    1.78 +                    when "WEB"
    1.79 +                        self.auth_type |= AuthType.WEB
    1.80 +
    1.81 +                // Iterate to next mechanism
    1.82 +                mechanism = mechanism.next
    1.83 +
    1.84 +            if self.start_auth
    1.85 +                pass // self.authenticate()
    1.86 +
    1.87 +        return HandlerResult.ALLOW_MORE_HANDLERS
    1.88 +
    1.89 +
    1.90 +
    1.91 +