Command-line client for Nextcloud’s Passman.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

62 lines
2.0 KiB

# pebble - Passman client
# Copyright (C) 2018 Damien Goutte-Gattat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests
import base64
class Server:
def __init__(self, host, user, password):
self.host = host
self.user = user
if hasattr(password, '__call__'):
self._get_password = password
else:
self._get_password = lambda h, u: password
self._auth = None
@property
def _auth_header(self):
if not self._auth:
self._auth = 'Basic {}'.format(
base64.b64encode(
'{}:{}'.format(
self.user,
self._get_password(self.host, self.user)
)
)
)
return self._auth
def _do_get(self, endpoint):
url = 'https://{}/index.php/apps/passman/api/v2/{}'.format(
self.host, endpoint)
hdrs = { 'Authorization': self._auth_header }
return requests.get(url, headers=hdrs).json()
def get_vaults(self):
return self._do_get('vaults')
def get_vault(self, guid):
return self._do_get('vaults/{}'.format(guid))
def find_vault(self, name):
vaults = self.get_vaults()
for v in vaults:
if v['name'] == name:
return v
return None