Passed
Pull Request — master (#126)
by
unknown
01:38
created

gvmtools.helper.authenticate()   B

Complexity

Conditions 6

Size

Total Lines 37
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nop 3
dl 0
loc 37
rs 8.6666
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# 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 General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import getpass
20
21
from gvm.errors import GvmError
22
from gvm.xml import pretty_print
23
24
25
__all__ = ['authenticate', 'pretty_print']
26
27
28
def authenticate(gmp, username=None, password=None):
29
    """Authentication helper
30
31
    Tries to get authentication username and password from arguments and if not
32
    present asks the username and/or password from the terminal.
33
34
    Arguments:
35
        gmp: A protocol instance
36
        username (:obj:`str`, optional): Username to authenticate with. If None,
37
            username will be read from terminal.
38
        password (:obj:`str`, optional): Password to authenticate with. If None,
39
            password will be read from the terminal.
40
41
    Returns:
42
        tuple: (username, password) tuple
43
44
    Raises:
45
        GmpError: Raises GmpError if authentication fails.
46
    """
47
    if gmp.is_authenticated():
48
        return
49
50
    # Ask for login credentials if none are given.
51
    if not username:
52
        while len(username) == 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
53
            username = input('Enter username: ')
54
55
    if not password:
56
        password = getpass.getpass(
57
            'Enter password for {0}: '.format(username))
58
59
    try:
60
        gmp.authenticate(username, password)
61
        return (username, password,)
62
    except GvmError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
63
        print('Could not authenticate. Please check your credentials.')
64
        raise e
65