Advertisement
Guest User

gkgetsecret.py

a guest
Aug 17th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.62 KB | Source Code | 0 0
  1. '''
  2. gkgetsecret.py - This provides a handful of functions for retrieving
  3. secrets from GNOME Keyring using the libsecret API.
  4. Copyright (C) 2018  Charles Bos
  5.  
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU Affero General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Affero General Public License
  17. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. '''
  19.  
  20. from gi import require_version
  21. require_version('Secret', '1')
  22. from gi.repository import Secret
  23.  
  24. def get_pw_from_desc(pw_desc) :
  25.     '''
  26.    This function returns the password for an item in the default keyring
  27.    which contains the description provided.
  28.    Use this function if you created a password using the dialogue in Seahorse
  29.    '''
  30.     # Get service
  31.     service = Secret.Service.get_sync(Secret.ServiceFlags.LOAD_COLLECTIONS)
  32.  
  33.     # Get default keyring
  34.     keyring = Secret.Collection.for_alias_sync(service, "default", \
  35.           Secret.CollectionFlags.NONE, None)
  36.  
  37.     # Get keyring items
  38.     items = keyring.get_items()
  39.  
  40.     # Load secrets
  41.     Secret.Item.load_secrets_sync(items)
  42.  
  43.     # Loop through items, find the matching one and return its password
  44.     password = None
  45.     for item in items :
  46.         if item.get_label() == pw_desc :
  47.             password = item.get_secret().get_text()
  48.             break
  49.  
  50.     # Close connection
  51.     service.disconnect()
  52.  
  53.     return password
  54.  
  55. def get_pw_from_attrs(*attr_val_pairs) :
  56.     '''
  57.    This function returns the password for an item in the default keyring
  58.    which contains all of the attribute value pairs provided as arguments.
  59.    Use this function if you created a password using the secret-tool command
  60.    or another such program that interfaces with libsecret
  61.    '''
  62.     # Check the list of attr-val pairs is present and contains an even number
  63.     # of elements
  64.     if attr_val_pairs == () :
  65.         raise TypeError("get_pw_from_attrs() at least 1 attribute-value pair " \
  66.                 "must be supplied")
  67.     if len(attr_val_pairs) % 2 != 0 :
  68.         raise TypeError("get_pw_from_attrs() incomplete attribute-value " \
  69.                 "pair was supplied")
  70.  
  71.     # Get service
  72.     service = Secret.Service.get_sync(Secret.ServiceFlags.LOAD_COLLECTIONS)
  73.  
  74.     # Get default keyring
  75.     keyring = Secret.Collection.for_alias_sync(service, "default", \
  76.           Secret.CollectionFlags.NONE, None)
  77.  
  78.     # Get keyring items
  79.     items = keyring.get_items()
  80.  
  81.     # Load secrets
  82.     Secret.Item.load_secrets_sync(items)
  83.  
  84.     # Loop through items, find the one which contains all supplied attr_val
  85.     # pairs and return its password
  86.     password = None
  87.     for item in items :
  88.         attrs = item.get_attributes()
  89.         match = True
  90.         for x in range(0, len(attr_val_pairs), 2) :
  91.             key = attr_val_pairs[x]
  92.             value = attr_val_pairs[x + 1]
  93.             try :
  94.                 if attrs[key] != value :
  95.                     match = False
  96.                     break
  97.             except KeyError :
  98.                 match = False
  99.                 break
  100.         if match :
  101.             password = item.get_secret().get_text()
  102.             break
  103.  
  104.     # Close connection
  105.     service.disconnect()
  106.  
  107.     return password
  108.  
  109. def get_val_from_attrs(attr, *attr_val_pairs) :
  110.     '''
  111.    This function returns the value for a given attribute. The first item
  112.    found that contains that attribute will be the one that is used. To ensure
  113.    that the correct item is chosen, any number of attribute-value pairs can
  114.    be optionally supplied as arguments and only the item which contains all
  115.    of those attr-val pairs (along with the main attr) will be used.
  116.    Use this function if you created a password using the secret-tool command
  117.    or another such program that interfaces with libsecret
  118.    '''
  119.     # Check the list of attr-val pairs contains an even number of elements
  120.     # if it exists
  121.     if attr_val_pairs != () :
  122.         if len(attr_val_pairs) % 2 != 0 :
  123.             raise TypeError("get_val_from_attrs() incomplete attribute-value " \
  124.                     "pair was supplied")
  125.  
  126.     # Get service
  127.     service = Secret.Service.get_sync(Secret.ServiceFlags.LOAD_COLLECTIONS)
  128.  
  129.     # Get default keyring
  130.     keyring = Secret.Collection.for_alias_sync(service, "default", \
  131.           Secret.CollectionFlags.NONE, None)
  132.  
  133.     # Get keyring items
  134.     items = keyring.get_items()
  135.  
  136.     # Loop through items, find the one which contains the supplied attribute
  137.     # (plus any attr_val pairs if specified) and return that attribute's
  138.     # value
  139.     attr_value = None
  140.     for item in items :
  141.         attrs = item.get_attributes()
  142.         try :
  143.             attrs[attr]
  144.         except KeyError :
  145.             continue
  146.         match = True
  147.         for x in range(0, len(attr_val_pairs), 2) :
  148.             key = attr_val_pairs[x]
  149.             value = attr_val_pairs[x + 1]
  150.             try :
  151.                 if attrs[key] != value :
  152.                     match = False
  153.                     break
  154.             except KeyError :
  155.                 match = False
  156.                 break
  157.         if match :
  158.             attr_value = attrs[attr]
  159.             break
  160.  
  161.     # Close connection
  162.     service.disconnect()
  163.  
  164.     return attr_value
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement