diff options
| author | Michael Biebl <biebl@debian.org> | 2025-08-01 19:15:18 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2025-08-01 19:15:18 +0200 |
| commit | 43a9f1ee48d7f7b9f4447e9fe17a4274c6fad67f (patch) | |
| tree | 5f570d79dfc6eb4c736b708a99b3b2e5ed7608d1 /examples/python | |
| parent | e0a3970cd0ea64b36773deae96366bbe7d1efbee (diff) | |
| parent | 7e9091a5960f67a84787c03153324915220e4816 (diff) | |
Update upstream source from tag 'upstream/1.54.0'
Update to upstream version '1.54.0' with Debian dir efd253369d835d8f9c404eef4f97d88deb9625fe
Diffstat (limited to 'examples/python')
| -rwxr-xr-x | examples/python/gi/gmaincontext.py | 2 | ||||
| -rwxr-xr-x | examples/python/gi/secret-agent.py | 92 |
2 files changed, 93 insertions, 1 deletions
diff --git a/examples/python/gi/gmaincontext.py b/examples/python/gi/gmaincontext.py index fcbcd61b..da8c89c5 100755 --- a/examples/python/gi/gmaincontext.py +++ b/examples/python/gi/gmaincontext.py @@ -188,7 +188,7 @@ def create_nmc(dbus_connection): # which has an overhead. # # Also, split the GObject creation and the init_async() call in two. - # That allows to pass construct-only parameters, in particular like + # That allows one to pass construct-only parameters, in particular like # the instance_flags. # Create a separate context for the NMClient. The NMClient is strongly diff --git a/examples/python/gi/secret-agent.py b/examples/python/gi/secret-agent.py new file mode 100755 index 00000000..ed3272ab --- /dev/null +++ b/examples/python/gi/secret-agent.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: LGPL-2.1-or-later + +import gi + +gi.require_version("NM", "1.0") +from gi.repository import GLib, NM, Gio + +# This example shows how to implement a very simple secret agent for +# NetworkManager. The secret agent registers to the NM daemon and can +# provide missing secrets like Wi-Fi or VPN passwords. Set environment +# variable "LIBNM_CLIENT_DEBUG=trace" to enable libnm verbose logging. + + +class SecretAgent(NM.SecretAgentOld): + def __init__(self): + super().__init__(identifier="MySecretAgent") + super().init() + + def do_get_secrets( + self, + connection, + connection_path, + setting_name, + hints, + flags, + callback, + callback_data, + ): + print( + "get_secrets for '{}', interface '{}', setting '{}'".format( + connection.get_id(), connection.get_interface_name(), setting_name + ) + ) + + # Implement here the logic to retrieve the secrets. + # As an example, we return a hardcoded Wi-Fi PSK. + if ( + connection.get_connection_type() == "802-11-wireless" + and setting_name == "802-11-wireless-security" + ): + s_wifi = connection.get_setting_wireless() + ssid = NM.utils_ssid_to_utf8(s_wifi.get_ssid().get_data()) + + if ssid == "home": + secrets = GLib.Variant( + "a{sa{sv}}", + { + "802-11-wireless-security": { + "psk": GLib.Variant("s", "abcd1234") + } + }, + ) + print("Sending secrets {}".format(secrets)) + callback(self, connection, secrets, None) + return + + # We don't have the secret, NM will ask to another agent or fail + callback( + self, + connection, + None, + GLib.GError.new_literal( + NM.SecretAgentError.quark(), + "No secrets found", + NM.SecretAgentError.NOSECRETS, + ), + ) + + def do_cancel_get_secrets(self, connection_path, connection_name): + pass + + def do_save_secrets(self, connection, connection_path, callback, callback_data): + # Implement this if you want to store "agent-owned" secrets + callback(self, connection, None) + + def do_delete_secrets(self, connection, connection_path, callback, callback_data): + # Implement this if you want to store "agent-owned" secrets + callback(self, connection, None) + + +def main(): + agent = SecretAgent() + loop = GLib.MainLoop() + try: + loop.run() + except KeyboardInterrupt: + print("Exiting Secret Agent...") + + +if __name__ == "__main__": + main() |