diff --git a/src/caosdb/connection/authentication/input.py b/src/caosdb/connection/authentication/input.py new file mode 100644 index 0000000000000000000000000000000000000000..f8bcefb8cf5eaf5adab211c266055e601946b113 --- /dev/null +++ b/src/caosdb/connection/authentication/input.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +# +# ** header v3.0 +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +# ** end header +# +"""input. + +A CredentialsProvider which reads the password from the input line. +""" +from __future__ import absolute_import, unicode_literals, print_function +from .interface import CredentialsProvider, CredentialsAuthenticator + +import getpass + +def get_authentication_provider(): + """get_authentication_provider. + + Return an authenticator which uses the input for username/password credentials. + + Returns + ------- + CredentialsAuthenticator + with an InputCredentialsProvider as back-end. + """ + return CredentialsAuthenticator(InputCredentialsProvider()) + + +class InputCredentialsProvider(CredentialsProvider): + """InputCredentialsProvider. + + A class for obtaining the password directly from the user. + + Methods + ------- + configure + + Attributes + ---------- + password + username + """ + + def __init__(self): + super(InputCredentialsProvider, self).__init__() + self._password = None + self._username = None + + def configure(self, **config): + """configure. + + Parameters + ---------- + **config + Keyword arguments containing at least keywords "username" and "password". + + Returns + ------- + None + """ + if config.get("username"): + self._username = config["username"] + else: + self._username = input("Please enter the user name: ") + + self._password = getpass.getpass("Please enter the password: ") + + @property + def password(self): + return self._password + + @property + def username(self): + return self._username diff --git a/src/caosdb/connection/authentication/interface.py b/src/caosdb/connection/authentication/interface.py index 745517a2ab1b884a1fad5e0627ffd9de26853ed6..ff5fb95c0210031f26130e1f92cb808388df8cd1 100644 --- a/src/caosdb/connection/authentication/interface.py +++ b/src/caosdb/connection/authentication/interface.py @@ -22,7 +22,10 @@ # ** end header # """This module provides the interfaces for authenticating requests to the -caosdb server.""" +caosdb server. + +Implementing modules muts provide a `get_authentication_provider()` method. +""" from abc import ABCMeta, abstractmethod, abstractproperty import logging from caosdb.connection.utils import urlencode diff --git a/src/caosdb/connection/connection.py b/src/caosdb/connection/connection.py index b14620c31365173687d2a6f4d78a763fcd7d2301..38674eb9841a85a1997d6b4b45395964f88de128 100644 --- a/src/caosdb/connection/connection.py +++ b/src/caosdb/connection/connection.py @@ -145,8 +145,10 @@ def _make_conf(*conf): return result -_DEFAULT_CONF = {"password_method": "plain", "implementation": - _DefaultCaosDBServerConnection} +_DEFAULT_CONF = { + "password_method": "plain", + "implementation": _DefaultCaosDBServerConnection, +} def _get_authenticator(**config): @@ -182,6 +184,12 @@ def configure_connection(**kwargs): 1) The plain text password 2) A function which returns the password. timeout A connection timeout in seconds. + password_method The method to use for obtaining the password. Can be for + example: + - "plain" Need username and password arguments. + - "pass" Uses the `pass` password manager. + - "keyring" Uses the `keyring` library. + - "input" Asks for the password. implementation A class which implements CaosDBServerConnection. (Default: _DefaultCaosDBServerConnection) ssl_insecure Whether SSL certificate warnings should be ignored. Only use