Exemplo n.º 1
0
def authentify():
    config.logger.info("authentify()")
    try:
        if config.app_window is not None and config.app_window.plus_account is not None:  # @UndefinedVariable
            # fetch passwd
            if config.passwd is None:
                setKeyring()
                try:
                    config.passwd = keyring.get_password(
                        config.app_name,
                        config.app_window.plus_account)  # @UndefinedVariable
                except:
                    pass
            if config.passwd is None:
                config.logger.debug("connection: -> password not found")
                clearCredentials()
                return False
            else:
                config.logger.debug(
                    "connection: -> authentifying %s",
                    config.app_window.plus_account)  # @UndefinedVariable
                data = {
                    "email": config.app_window.plus_account,
                    "password": config.passwd
                }  # @UndefinedVariable
                r = postData(config.auth_url, data, False)
                # returns 404: login wrong and 401: passwd wrong
                res = r.json()
                if "success" in res and res[
                        "success"] and "result" in res and "user" in res[
                            "result"] and "token" in res["result"]["user"]:
                    config.logger.debug(
                        "connection: -> authentified, token received")
                    if "nickname" in res["result"]["user"]:
                        nickname = res["result"]["user"]["nickname"]
                    else:
                        nickname = None
                    setToken(res["result"]["user"]["token"], nickname)
                    if "account_id" in res["result"]["user"] and "_id" in res[
                            "result"]["user"]["account_id"]:
                        account_nr = account.setAccount(
                            res["result"]["user"]["account_id"]["_id"])
                        config.account_nr = account_nr
                        config.logger.debug("connection: -> account: %s",
                                            account_nr)
                    return True
                else:
                    config.logger.debug("connection: -> authentication failed")
                    if "error" in res:
                        config.app_window.sendmessage(
                            res["error"])  # @UndefinedVariable
                    clearCredentials()
                    return False
    except requests.exceptions.RequestException as e:
        config.logger.error("connection: -> RequestException: %s", e)
        raise (e)
    except Exception as e:
        config.logger.debug("connection: -> Exception: %s", e)
        clearCredentials()
        raise (e)
Exemplo n.º 2
0
def authentify():
    config.logger.info("authentify()")
    try:
        if config.app_window is not None and config.app_window.plus_account is not None:  # @UndefinedVariable
            # fetch passwd
            if config.passwd is None:
                setKeyring()
                try:
                    config.passwd = keyring.get_password(
                        config.app_name,
                        config.app_window.plus_account)  # @UndefinedVariable
                except:
                    pass
            if config.passwd is None:
                config.logger.debug("connection: -> password not found")
                clearCredentials()
                return False
            else:
                config.logger.debug(
                    "connection: -> authentifying %s",
                    config.app_window.plus_account)  # @UndefinedVariable
                data = {
                    "email": config.app_window.plus_account,
                    "password": config.passwd
                }  # @UndefinedVariable
                r = postData(config.auth_url, data, False)
                # returns 404: login wrong and 401: passwd wrong
                res = r.json()
                if "success" in res and res[
                        "success"] and "result" in res and "user" in res[
                            "result"] and "token" in res["result"]["user"]:
                    config.logger.debug(
                        "connection: -> authentified, token received")
                    # extract in user/account data
                    nickname = extractUserInfo(res["result"]["user"],
                                               "nickname", None)
                    config.app_window.plus_language = extractUserInfo(
                        res["result"]["user"], "language", "en")
                    config.app_window.plus_paidUntil = None
                    config.app_window.plus_subscription = None
                    config.app_window.plus_paidUntil = None
                    if "account" in res["result"]["user"]:
                        config.app_window.plus_subscription = extractUserInfo(
                            res["result"]["user"]["account"], "subscription",
                            None)
                        paidUntil = extractUserInfo(
                            res["result"]["user"]["account"], "paidUntil",
                            None)
                        try:
                            if paidUntil is not None:
                                config.app_window.plus_paidUntil = util.ISO86012datetime(
                                    paidUntil)
                        except Exception:
                            pass
                    if config.app_window.plus_paidUntil is not None and \
                            (config.app_window.plus_paidUntil.date() - datetime.datetime.now().date()).days < (- config.expired_subscription_max_days):
                        config.logger.debug(
                            "connection: -> authentication failed due to long expired subscription"
                        )
                        if "error" in res:
                            config.app_window.sendmessage(
                                res["error"])  # @UndefinedVariable
                        clearCredentials()
                        return False
                    else:
                        if "readonly" in res["result"]["user"] and isinstance(
                                res["result"]["user"]["readonly"], bool):
                            config.app_window.plus_readonly = res["result"][
                                "user"]["readonly"]
                        else:
                            config.app_window.plus_readonly = False
                        #
                        setToken(res["result"]["user"]["token"], nickname)
                        if "account" in res["result"]["user"] and "_id" in res[
                                "result"]["user"]["account"]:
                            account_nr = account.setAccount(
                                res["result"]["user"]["account"]["_id"])
                            config.account_nr = account_nr
                            config.logger.debug("connection: -> account: %s",
                                                account_nr)
                        return True
                else:
                    config.logger.debug("connection: -> authentication failed")
                    if "error" in res:
                        config.app_window.sendmessage(
                            res["error"])  # @UndefinedVariable
                    clearCredentials()
                    return False
    except requests.exceptions.RequestException as e:
        config.logger.error("connection: -> RequestException: %s", e)
        raise (e)
    except Exception as e:
        config.logger.debug("connection: -> Exception: %s", e)
        clearCredentials()
        raise (e)