Exemple #1
0
    def create_new_app(self, request):
        """
        Creates a new app.
        """
        username    = utils.get_reqvar(request, "username")
        appname     = utils.get_reqvar(request, "appname")

        userkey     = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)
        
        appkey      = get_app_key(username, appname)
        if appkey in self.tyrant:
            return errors.json_error_page(request, errors.APP_ALREADY_EXISTS)

        self.tyrant[appkey] = json_encode({'username': username,
                                           'appname': appname,
                                           'dev_pkeyfile': "",
                                           'dev_certfile': "",
                                           'prod_pkeyfile': "",
                                           'prod_certfile': "",
                                           'dev_pkeypasswd': "",
                                           'dev_certpasswd': "",
                                           'prod_pkeypasswd': "",
                                           'prod_certpasswd': "",
                                           'created': datetime.datetime.now()})

        # create the folder for the app so we have 
        # certificates and other things in there
        # <cert_folder>/<alphabet>/<appkey>/
        try:
            self.create_app_cert_folder(username, appname)
        except OSError, e:
            return errors.json_error_page(request, errors.OS_ERROR, str(e))
Exemple #2
0
    def delete_app(self, request):
        """
        Deletes an app.
        """
        username = utils.get_reqvar(request, "username")
        appname = utils.get_reqvar(request, "appname")
        
        appkey      = get_app_key(username, appname)
        if appkey not in self.tyrant:
            return errors.json_error_page(request, errors.APP_DOES_NOT_EXIST)

        del self.tyrant[appkey]

        try:
            shutil.rmtree(self.get_app_folder(username, appname))
        except OSError, e:
            pass
Exemple #3
0
    def delete_app(self, request):
        """
        Deletes an app.
        """
        username = utils.get_reqvar(request, "username")
        appname = utils.get_reqvar(request, "appname")

        appkey = get_app_key(username, appname)
        if appkey not in self.tyrant:
            return errors.json_error_page(request, errors.APP_DOES_NOT_EXIST)

        del self.tyrant[appkey]

        try:
            shutil.rmtree(self.get_app_folder(username, appname))
        except OSError, e:
            pass
Exemple #4
0
    def change_user_password(self, request):
        username    = utils.get_reqvar(request, "username")
        newpassword = utils.get_reqvar(request, "newpassword")

        userkey     = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)
        
        if not is_password_valid(newpassword):
            return errors.json_error_page(request, errors.PASSWORD_INVALID)

        userdata                = json_decode(self.tyrant[userkey])
        userdata['pwdreset']    = False
        userdata['password']    = newpassword
        self.tyrant[userkey]    = json_encode(userdata)

        return json_response(request, 0, "OK")
Exemple #5
0
    def create_new_user(self, request):
        username    = utils.get_reqvar(request, "username")
        password    = utils.get_reqvar(request, "password")

        userkey     = get_user_key(username)
        if userkey in self.tyrant:
            return errors.json_error_page(request, errors.USER_ALREADY_EXISTS)
        
        if not is_password_valid(password):
            return errors.json_error_page(request, errors.PASSWORD_INVALID)

        self.tyrant[userkey] = json_encode({'username': username,
                                            'pwdreset': False,
                                            'password': password,
                                            'created': datetime.datetime.now()})

        return json_response(request, 0, "OK")
Exemple #6
0
    def change_user_password(self, request):
        username = utils.get_reqvar(request, "username")
        newpassword = utils.get_reqvar(request, "newpassword")

        userkey = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)

        if not is_password_valid(newpassword):
            return errors.json_error_page(request, errors.PASSWORD_INVALID)

        userdata = json_decode(self.tyrant[userkey])
        userdata['pwdreset'] = False
        userdata['password'] = newpassword
        self.tyrant[userkey] = json_encode(userdata)

        return json_response(request, 0, "OK")
Exemple #7
0
    def delete_user(self, request):
        username    = utils.get_reqvar(request, "username")

        userkey     = 'user_' + username
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)

        del self.tyrant[userkey]

        return json_response(request, 0, "OK")
Exemple #8
0
    def delete_user(self, request):
        username = utils.get_reqvar(request, "username")

        userkey = 'user_' + username
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)

        del self.tyrant[userkey]

        return json_response(request, 0, "OK")
Exemple #9
0
    def create_new_user(self, request):
        username = utils.get_reqvar(request, "username")
        password = utils.get_reqvar(request, "password")

        userkey = get_user_key(username)
        if userkey in self.tyrant:
            return errors.json_error_page(request, errors.USER_ALREADY_EXISTS)

        if not is_password_valid(password):
            return errors.json_error_page(request, errors.PASSWORD_INVALID)

        self.tyrant[userkey] = json_encode({
            'username': username,
            'pwdreset': False,
            'password': password,
            'created': datetime.datetime.now()
        })

        return json_response(request, 0, "OK")
Exemple #10
0
    def upload_app_certificate(self, request):
        """
        Uploads a dev or provisioning certificate for an app.
        """
        username = utils.get_reqvar(request, "username")
        appname = utils.get_reqvar(request, "appname")
        certtype = utils.get_reqvar(request, "certtype")
        certfile = utils.get_reqvar(request, "certfile")
        certpasswd = urllib2.unquote(utils.get_reqvar(request, "certpasswd"))
        pkeypasswd = urllib2.unquote(utils.get_reqvar(request, "pkeypasswd"))
        certcontlen = int(utils.get_reqvar(request, "certcontlen"))
        pkeycontlen = int(utils.get_reqvar(request, "pkeycontlen"))

        appkey = get_app_key(username, appname)
        if appkey not in self.tyrant:
            return errors.json_error_page(request, errors.APP_DOES_NOT_EXIST)

        contlength = request.getHeader("content-length")
        logging.debug("Headers: " + str(request.getAllHeaders()))
        content = request.content.read()
        logging.debug("Encoded Contents: " + str(type(content)) + ", " +
                      str(len(content)))
        content = content.decode("zlib")
        logging.debug("Decoded Contents: " + str(type(content)) + ", " +
                      str(len(content)))
        logging.debug("Passwords: %s, %s" % (certpasswd, pkeypasswd))

        # strip the certfile and the pkeyfile
        fmt = "%ds%ds" % (certcontlen, pkeycontlen)
        cert_contents, pkey_contents = struct.unpack(fmt, content)

        logging.debug("Cert Content Length: " + str(len(cert_contents)))
        logging.debug("Pkey Content Length: " + str(len(pkey_contents)))

        # so how should the files be saved?
        # for now our certfolder will be
        cert_folder = self.create_app_cert_folder(username, appname)

        try:
            cert_pem_file = self.p12_to_pem(
                cert_contents, certpasswd,
                "%s/%s_certificate" % (cert_folder, certtype), True)
            pkey_pem_file = self.p12_to_pem(
                pkey_contents, pkeypasswd,
                "%s/%s_privatekey" % (cert_folder, certtype), False)
        except IOError, e:
            return errors.json_error_page(request, errors.IO_ERROR, str(e))
Exemple #11
0
    def create_new_app(self, request):
        """
        Creates a new app.
        """
        username = utils.get_reqvar(request, "username")
        appname = utils.get_reqvar(request, "appname")

        userkey = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)

        appkey = get_app_key(username, appname)
        if appkey in self.tyrant:
            return errors.json_error_page(request, errors.APP_ALREADY_EXISTS)

        self.tyrant[appkey] = json_encode({
            'username': username,
            'appname': appname,
            'dev_pkeyfile': "",
            'dev_certfile': "",
            'prod_pkeyfile': "",
            'prod_certfile': "",
            'dev_pkeypasswd': "",
            'dev_certpasswd': "",
            'prod_pkeypasswd': "",
            'prod_certpasswd': "",
            'created': datetime.datetime.now()
        })

        # create the folder for the app so we have
        # certificates and other things in there
        # <cert_folder>/<alphabet>/<appkey>/
        try:
            self.create_app_cert_folder(username, appname)
        except OSError, e:
            return errors.json_error_page(request, errors.OS_ERROR, str(e))
Exemple #12
0
    def upload_app_certificate(self, request):
        """
        Uploads a dev or provisioning certificate for an app.
        """
        username    = utils.get_reqvar(request, "username")
        appname     = utils.get_reqvar(request, "appname")
        certtype    = utils.get_reqvar(request, "certtype")
        certfile    = utils.get_reqvar(request, "certfile")
        certpasswd  = urllib2.unquote(utils.get_reqvar(request, "certpasswd"))
        pkeypasswd  = urllib2.unquote(utils.get_reqvar(request, "pkeypasswd"))
        certcontlen = int(utils.get_reqvar(request, "certcontlen"))
        pkeycontlen = int(utils.get_reqvar(request, "pkeycontlen"))
        
        appkey      = get_app_key(username, appname)
        if appkey not in self.tyrant:
            return errors.json_error_page(request, errors.APP_DOES_NOT_EXIST)

        contlength  = request.getHeader("content-length")
        logging.debug("Headers: " + str(request.getAllHeaders()))
        content     = request.content.read()
        logging.debug("Encoded Contents: " + str(type(content)) + ", " + str(len(content)))
        content     = content.decode("zlib")
        logging.debug("Decoded Contents: " + str(type(content)) + ", " + str(len(content)))
        logging.debug("Passwords: %s, %s" % (certpasswd, pkeypasswd))

        # strip the certfile and the pkeyfile
        fmt = "%ds%ds" % (certcontlen, pkeycontlen)
        cert_contents, pkey_contents = struct.unpack(fmt, content)

        logging.debug("Cert Content Length: " + str(len(cert_contents)))
        logging.debug("Pkey Content Length: " + str(len(pkey_contents)))

        # so how should the files be saved?
        # for now our certfolder will be 
        cert_folder = self.create_app_cert_folder(username, appname)

        try:
            cert_pem_file = self.p12_to_pem(cert_contents, certpasswd,
                                "%s/%s_certificate" % (cert_folder, certtype), True)
            pkey_pem_file = self.p12_to_pem(pkey_contents, pkeypasswd, 
                                "%s/%s_privatekey" % (cert_folder, certtype), False)
        except IOError, e:
            return errors.json_error_page(request, errors.IO_ERROR, str(e))
Exemple #13
0
    def render(self, request):
        """
        We handle GET or POST requests to this handler.  This will be of
        the form:
        /apps/<appname>/<device_token>/?args
        """
        parts = request.path.split("/")[2:]
        if len(parts) < 2:
            return errors.no_resource_error(request)

        appname     = parts[0]
        dev_token   = parts[1]
        username    = utils.get_reqvar(request, "username")
        content     = request.content.read()
        badge       = utils.get_reqvar(request, "badge")
        sound       = utils.get_reqvar(request, "sound")
        alert       = utils.get_reqvar(request, "alert")
        env         = utils.get_reqvar(request, "env")
        identifier  = utils.get_reqvar(request, "identifier")
        expiry      = utils.get_reqvar(request, "expiry")

        if content:
            payload = json_decode(content)
            if 'aps' not in payload:
                payload['aps'] = {}
        else:
            payload = {'aps': {}}

        if badge:
            payload['aps']['badge'] = int(badge)
        if sound:
            payload['aps']['sound'] = sound
        if alert:
            payload['aps']['alert'] = alert

        logging.debug("Payload: " + json_encode(payload))

        appkey = "%s$%s$%s" % (env, username, appname)
        self.apns_daemon.sendMessage(appname, dev_token, json_encode(payload), identifier, expiry)

        return json_response(request, 0, "OK")
Exemple #14
0
    def render(self, request):
        """
        We handle GET or POST requests to this handler.  This will be of
        the form:
        /apps/<appname>/<device_token>/?args
        """
        parts = request.path.split("/")[2:]
        if len(parts) < 2:
            return errors.no_resource_error(request)

        appname = parts[0]
        dev_token = parts[1]
        username = utils.get_reqvar(request, "username")
        content = request.content.read()
        badge = utils.get_reqvar(request, "badge")
        sound = utils.get_reqvar(request, "sound")
        alert = utils.get_reqvar(request, "alert")
        env = utils.get_reqvar(request, "env")
        identifier = utils.get_reqvar(request, "identifier")
        expiry = utils.get_reqvar(request, "expiry")

        if content:
            payload = json_decode(content)
            if "aps" not in payload:
                payload["aps"] = {}
        else:
            payload = {"aps": {}}

        if badge:
            payload["aps"]["badge"] = int(badge)
        if sound:
            payload["aps"]["sound"] = sound
        if alert:
            payload["aps"]["alert"] = alert

        logging.debug("Payload: " + json_encode(payload))

        appkey = "%s$%s$%s" % (env, username, appname)
        self.apns_daemon.sendMessage(appname, dev_token, json_encode(payload), identifier, expiry)

        return json_response(request, 0, "OK")