aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/moon_manager/api/users.py
blob: 9de78ff3c60e48a4a4d5fd75396f627efd2632b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.

"""
Users
"""
import hug
import logging
import getpass
from tinydb import Query
from moon_utilities.auth_functions import db, init_db, add_user, get_api_key, change_password

LOGGER = logging.getLogger("moon.manager.api." + __name__)

UsersAPI = hug.API('users')


@hug.object(name='users', version='1.0.0', api=UsersAPI)
class UsersCLI(object):
    """An example of command like calls via an Object"""

    @staticmethod  # nosec
    @hug.object.cli
    def add(username, password: hug.types.text = ""):
        """
        Add a user to the database
        """
        return add_user(username, password)

    @staticmethod  # nosec
    @hug.object.cli
    def change_password(username, password: hug.types.text = "", new_password: hug.types.text = ""):
        """
        Authenticate a username and password against our database
        """
        result = change_password(username, password, new_password)
        if not result:
            return "Wrong password"
        return result

    @staticmethod  # nosec
    @hug.object.cli
    def key(username, password: hug.types.text = ""):
        """
        Authenticate a username and password against our database
        """
        if password == "":
            password = getpass.getpass()
        return get_api_key(username, password)

    @staticmethod
    @hug.object.cli
    def list(human: bool = False):
        """
        List users from the database
        """
        global db
        if db is None:
            init_db()
        user_model = Query()
        users = db.search(user_model.username.matches('.*'))
        if human:
            result = "Users"
            if users:
                for user in users:
                    result += f"\n{user['username']} : \n"
                    result += f"\tusername : {user['username']}\n"
                    result += f"\tapi_key : {user['api_key']}"
            else:
                result += f"\nNo user"
            return result
        else:
            result = []
            if users:
                for user in users:
                    result.append({
                        'username': user['username'],
                        'api_key': user['api_key']
                    })
            return {'users': result}