保存Kivy应用程序的登录屏幕用户名和密码

保存Kivy应用程序的登录屏幕用户名和密码

问题描述:

我正在开发适用于iOS和Android的Kivy应用程序,即使在应用程序关闭或终止后,也需要帮助保持用户持久登录。我正在使用Parse来存储用户凭据。

I am working on a Kivy app for iOS and Android and need help with keeping the user persistently logged in, even after the app is closed or killed. I am using Parse to store user credentials.

我已经在App类中添加了一个on_pause方法,但这只会让用户在应用程序关闭时保持登录状态但没有被杀死。是否有最佳做法可以安全地允许持久用户使用Kivy登录,即使在应用程序被杀后也是如此?

I've already added an on_pause method to the App class, but this only keeps the user logged in if the app is closed but not killed. Is there a best practice for securely allowing persistent user login with Kivy, even after an app is killed?

编辑:我更喜欢适用于Android的单个Kivy解决方案应用程序和iOS应用程序,无需编辑/添加iOS或Android特定代码。

I prefer a single Kivy solution that works for both an Android app and an iOS app, without the need to edit/add iOS or Android specific code.

以下是我们的代码最终用于存储登录信息,该信息使用了Kivy的JsonStore。然后还可以使用Python加密库对凭证进行加密。

Below is the code that we ended up using to store the login info, which employs Kivy's JsonStore. The credentials can also then be encrypted using Python encryption libraries.

from kivy.storage.jsonstore import JsonStore

from os.path import join


class AppScreen(ScreenManager):
    data_dir = App().user_data_dir
    store = JsonStore(join(data_dir, 'storage.json'))
    ...
    def login(self):
        username = self.login_username.text
        password = self.login_password.text
        AppScreen.store.put('credentials', username=username, password=password)

这是检索凭据的代码:

try:
    store.get('credentials')['username']
except KeyError:
    username = ""
else:
    username = store.get('credentials')['username']

try:
    store.get('credentials')['password']
except KeyError:
    password = ""
else:
    password = store.get('credentials')['password']

在.kv文件中,用户名和密码TextInput小部件如下所示:

In the .kv file, the username and password TextInput widgets look like this:

TextInput:
    id: login_username
    text: root.username
    on_enter_key: root.login()

TextInput:
    id: login_password
    text: root.password
    on_enter_key: root.login()