简体   繁体   English

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

[英]Saving login screen username and password for Kivy app

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. 我正在开发适用于iOS和Android的Kivy应用程序,即使应用程序关闭或被杀死,也需要帮助保持用户持久登录。 I am using Parse to store user credentials. 我正在使用Parse来存储用户凭据。

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. 我已经在App类中添加了一个on_pause方法,但这只会让用户在应用程序关闭但未被杀死时保持登录状态。 Is there a best practice for securely allowing persistent user login with Kivy, even after an app is killed? 是否有最佳实践可以安全地允许持久用户使用Kivy登录,即使应用程序被杀死后也是如此?

Edit: 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. 编辑:我更喜欢适用于Android应用和iOS应用的单一Kivy解决方案,无需编辑/添加iOS或Android特定代码。

Below is the code that we ended up using to store the login info, which employs Kivy's JsonStore. 下面是我们最终用于存储登录信息的代码,该信息使用了Kivy的JsonStore。 The credentials can also then be encrypted using Python encryption libraries. 然后,还可以使用Python加密库对凭据进行加密。

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)

And this is the code to retrieve the credentials: 这是检索凭据的代码:

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']

In the .kv file, the username and password TextInput widgets look like this: 在.kv文件中,用户名和密码TextInput小部件如下所示:

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

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

The answer is essentially that you must simply save the data somewhere. 答案基本上是你必须简单地将数据保存在某个地方。 The details will depend on your requirements, and aren't specific to kivy - you can look up normal android or python practices. 细节将取决于您的要求,并不是特定于kivy - 您可以查找正常的android或python实践。

I'm not sure exactly what android guarantees about permissions, but storing stuff in your app directory (on the main partition, not 'external' storage) should be inaccessible to other apps, though anything with root can view them. 我不确定android确保权限是什么,但是存储在app目录中的东西(在主分区上,而不是'外部'存储)应该是其他应用程序无法访问的,尽管有root用户可以查看它们。 You can use normal python tools if you want to encrypt the data, and manage your own kivy gui for getting a decryption key/password from the user. 如果要加密数据,可以使用普通的python工具,并管理自己的kivy gui以获取用户的解密密钥/密码。

You could use the application configuration for that 您可以使用应用程序配置

edit for the comment: basically, additionally to the normal def build(self) : method you also add: 编辑评论:基本上,除了正常的def build(self) :方法,你还添加:

def build_config(self, config):
    config.setdefaults('login', {'username': '', 'password': ''})

then, in your build method you can do the following: 然后,在您的构建方法中,您可以执行以下操作:

def build(self):
    config = self.config
    self.username = config.get('login', 'username')
    self.password = config.get('login', 'password')

now, after the user logged in successfully you just need to write the new username/password to the config: 现在,用户登录成功后,您只需要将新的用户名/密码写入配置:

self.config.set('login', 'username', input1)
self.config.set('login', 'password', input2)

Take in mind that this will save the password unencrypted. 请记住,这将保存未加密的密码。 Perhaps you may want to add encryption for the password on your phone. 也许您可能想要在手机上添加密码加密。 It will save the files to /sdcard/<appname>.ini . 它会将文件保存到/sdcard/<appname>.ini So you should really encrypt it because everyone with access to the sdcard will be able to read it! 所以你应该加密它,因为每个有权访问SD卡的人都能读取它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM