Beispiel #1
0
    def process_hello(self, data):
        """Process a hello message"""
        # This must be a helo, or we kick the client
        cmd = data.get("messageType")
        if cmd != "hello":
            return self.sendClose()

        if self.uaid:
            return self.returnError("hello", "duplicate hello", 401)

        uaid = data.get("uaid")
        _, uaid = validate_uaid(uaid)
        self.uaid = uaid

        self.transport.pauseProducing()
        user_item = dict(
            uaid=self.uaid,
            node_id=self.ap_settings.router_url,
            connected_at=self.connected_at,
        )
        d = self.deferToThread(self.ap_settings.router.register_user,
                               user_item)
        d.addCallback(self._check_other_nodes)
        d.addErrback(self.err_hello)
        self._register = d
        return d
Beispiel #2
0
    def _validate_auth(self, uaid):
        """Validates the Authorization header in a request

        Validate the given request bearer token
        """

        test, _ = validate_uaid(uaid)
        if not test:
            return False
        header = self.request.headers.get("Authorization")
        if header is None:
            return False
        try:
            token_type, rtoken = re.sub(r' +', ' ',
                                        header.strip()).split(" ", 2)
        except ValueError:
            return False
        if "bearer" != token_type.lower():
            return False
        if self.ap_settings.auth_key:
            for key in self.ap_settings.auth_key:
                token = generate_hash(key, uaid)
                if rtoken == token:
                    return True
            return False
        else:
            return True
Beispiel #3
0
    def _validate_auth(self, uaid):
        """Validates the Authorization header in a request

        Validate the given request bearer token

        """
        test, _ = validate_uaid(uaid)
        if not test:
            return False
        header = self.request.headers.get("Authorization")
        if header is None:
            return False
        try:
            token_type, rtoken = re.sub(r' +', ' ',
                                        header.strip()).split(" ", 2)
        except ValueError:
            return False
        if AUTH_SCHEME != token_type.lower():
            return False
        if self.ap_settings.bear_hash_key:
            for key in self.ap_settings.bear_hash_key:
                token = generate_hash(key, uaid)
                if rtoken == token:
                    return True
            return False
        else:
            return True
Beispiel #4
0
    def process_hello(self, data):
        """Process a hello message"""
        # This must be a helo, or we kick the client
        cmd = data.get("messageType")
        if cmd != "hello":
            return self.sendClose()

        if self.ps.uaid:
            return self.returnError("hello", "duplicate hello", 401)

        uaid = data.get("uaid")
        self.ps.use_webpush = data.get("use_webpush", False)
        self.ps._base_tags.append("use_webpush:%s" % self.ps.use_webpush)
        self.ps.router_type = "webpush" if self.ps.use_webpush\
                              else "simplepush"
        if self.ps.use_webpush:
            self.ps.updates_sent = defaultdict(lambda: [])
            self.ps.direct_updates = defaultdict(lambda: [])

        existing_user, uaid = validate_uaid(uaid)
        self.ps.uaid = uaid
        self.ps.uaid_hash = hasher(uaid)
        # Check for the special wakeup commands
        if "wakeup_host" in data and "mobilenetwork" in data:
            wakeup_host = data.get("wakeup_host")
            if "ip" in wakeup_host and "port" in wakeup_host:
                mobilenetwork = data.get("mobilenetwork")
                # Normalize the wake info to a single object.
                wake_data = dict(
                    data=dict(ip=wakeup_host["ip"],
                              port=wakeup_host["port"],
                              mcc=mobilenetwork.get("mcc", ''),
                              mnc=mobilenetwork.get("mnc", ''),
                              netid=mobilenetwork.get("netid", '')))
                self.ps.wake_data = wake_data

        self.transport.pauseProducing()

        d = self._register_user(existing_user)
        d.addCallback(self._copy_new_data)
        d.addCallback(self._check_collision)
        d.addErrback(self.trap_cancel)
        d.addErrback(self.err_overload, "hello")
        d.addErrback(self.err_hello)
        self.ps._register = d
        return d
Beispiel #5
0
    def process_hello(self, data):
        """Process a hello message"""
        # This must be a helo, or we kick the client
        cmd = data.get("messageType")
        if cmd != "hello":
            return self.sendClose()

        if self.ps.uaid:
            return self.returnError("hello", "duplicate hello", 401)

        uaid = data.get("uaid")
        self.ps.use_webpush = data.get("use_webpush", False)
        self.ps._base_tags.append("use_webpush:%s" %
                                  self.ps.use_webpush)
        self.ps.router_type = "webpush" if self.ps.use_webpush\
                              else "simplepush"
        if self.ps.use_webpush:
            self.ps.updates_sent = defaultdict(lambda: [])
            self.ps.direct_updates = defaultdict(lambda: [])

        existing_user, uaid = validate_uaid(uaid)
        self.ps.uaid = uaid
        self.ps.uaid_hash = hasher(uaid)
        # Check for the special wakeup commands
        if "wakeup_host" in data and "mobilenetwork" in data:
            wakeup_host = data.get("wakeup_host")
            if "ip" in wakeup_host and "port" in wakeup_host:
                mobilenetwork = data.get("mobilenetwork")
                # Normalize the wake info to a single object.
                wake_data = dict(data=dict(ip=wakeup_host["ip"],
                                 port=wakeup_host["port"],
                                 mcc=mobilenetwork.get("mcc", ''),
                                 mnc=mobilenetwork.get("mnc", ''),
                                 netid=mobilenetwork.get("netid", '')))
                self.ps.wake_data = wake_data

        self.transport.pauseProducing()

        d = self._register_user(existing_user)
        d.addCallback(self._copy_new_data)
        d.addCallback(self._check_collision)
        d.addErrback(self.trap_cancel)
        d.addErrback(self.err_overload, "hello")
        d.addErrback(self.err_hello)
        self.ps._register = d
        return d
Beispiel #6
0
    def _validate_auth(self, uaid):
        """Validates the Authorization header in a request

        Validate the given request using HAWK.
        """

        test, _ = validate_uaid(uaid)
        if not test:
            return False
        for key in self.ap_settings.crypto_key:
            secret = generate_hash(key, uaid)
            fReq = prequests.Request(
                self.request.method,
                "%s://%s%s" % (self.request.protocol, self.request.host,
                               self.request.uri),
                headers=self.request.headers,
                data=self.request.body).prepare()
            if hawkauthlib.check_signature(fReq, secret):
                return True
        return False
Beispiel #7
0
    def _validate_auth(self, uaid):
        """Validates the Authorization header in a request

        Validate the given request using HAWK.
        """

        test, _ = validate_uaid(uaid)
        if not test:
            return False
        for key in self.ap_settings.crypto_key:
            secret = generate_hash(key, uaid)
            fReq = prequests.Request(
                self.request.method,
                "%s://%s%s" %
                (self.request.protocol, self.request.host, self.request.uri),
                headers=self.request.headers,
                data=self.request.body).prepare()
            if hawkauthlib.check_signature(fReq, secret):
                return True
        return False