Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gnome keyring functionality #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions devices/linux/Files/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,42 @@
import base64
import getpass
import os
import pathlib
import platform
import re
import string
import subprocess
import sys
import tempfile
import uuid
import venv
from shutil import copyfile
from typing import List, Type, Union

NM_AVAILABLE = True
CRYPTO_AVAILABLE = True
DEBUG_ON = False

KEYRING_TEMPLATE = '''\
#!${venv_executable}

import argparse

import keyring

def main():
parser = argparse.ArgumentParser()
parser.add_argument('password', action="store", type=str)
result = parser.parse_args()

keyring.set_password("eduroam", result.password)


if __name__ == "__main__":
main()

'''


def debug(msg) -> None:
"""Print debugging messages to stdout"""
Expand Down Expand Up @@ -898,3 +922,42 @@ def add_connections(self, user_data: Type[InstallerData]):
self.__add_connection(ssid)
for ssid in Config.del_ssids:
self.__delete_existing_connection(ssid)

def use_keyring(self, password: str) -> bool:
""" create a virtual environment to use the gnome keyring """
with tempfile.TemporaryDirectory() as target_dir_path:
debug(f"Create temporary directory '{target_dir_path}'.")

venv_builder = EnvBuilder(with_pip=True)
venv_builder.create(target_dir_path)
venv_context = venv_builder.context
requirements = ['keyring']

pip_install_command = [
venv_context.env_exe,
'-m',
'pip',
'install',
*requirements,
]
subprocess.run(pip_install_command)

script_substitutions = {'venv_executable': venv_context.env_exe}
script = (string.Template(KEYRING_TEMPLATE).substitute(script_substitutions))
script_path = pathlib.Path(target_dir_path).joinpath('save_password.py')
script_path.write_text(script)
script_command = [venv_context.env_exe, str(script_path), password]
run_script_process = subprocess.run(script_command)
if run_script_process.returncode == 0:
return True
return False


class EnvBuilder(venv.EnvBuilder):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.context = None

def post_setup(self, context):
self.context = context