Skip to content

Instantly share code, notes, and snippets.

@nnsense
Created June 5, 2022 12:32
Show Gist options
  • Save nnsense/274614918c84be304b7f607b975289c3 to your computer and use it in GitHub Desktop.
Save nnsense/274614918c84be304b7f607b975289c3 to your computer and use it in GitHub Desktop.
Testing the keyring usage with python
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import keyring
from keyring.backends import macOS
import platform
from getpass import getpass
from os import getenv
from keyrings.cryptfile.cryptfile import CryptFileKeyring
if "Darwin" in platform.system():
print("MacOS")
kr = macOS.Keyring()
else:
print("Linux")
kr = CryptFileKeyring()
try:
kr.keyring_key = getenv("KEYRING_PASSWORD") or getpass("Please enter password for encrypted keyring: ")
except ValueError:
print("FATAL: Wrong password provided")
exit()
except:
print("ERROR")
exit()
keyring.set_keyring(kr)
username = "testing"
user_passwd = keyring.get_password(username, "user_password")
if user_passwd:
print("Password found: " + user_passwd)
else:
print("No pass found, setting a new one")
new_pass = getpass()
keyring.set_password(username, "user_password", new_pass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment