• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-MSS / MSS / 24879295624

24 Apr 2026 08:09AM UTC coverage: 69.793% (+0.08%) from 69.714%
24879295624

Pull #3033

github

web-flow
Merge 14f074e8b into 110d6eec1
Pull Request #3033: refactor server.py for using Blueprint

593 of 984 new or added lines in 16 files covered. (60.26%)

235 existing lines in 6 files now uncovered.

14579 of 20889 relevant lines covered (69.79%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.08
/mslib/utils/auth.py
1
# -*- coding: utf-8 -*-
2
"""
3

4
    mslib.utils.auth
5
    ~~~~~~~~~~~~~~~~
6

7
    handles passwords from the keyring for login and http_auuth
8

9

10
    To better understand of the code, look at the 'ships' example from
11
    chapter 14/16 of 'Rapid GUI Programming with Python and Qt: The
12
    Definitive Guide to PyQt Programming' (Mark Summerfield).
13

14
    This file is part of MSS.
15

16
    :copyright: Copyright 2023 Reimar Bauer
17
    :copyright: Copyright 2023-2026 by the MSS team, see AUTHORS.
18
    :license: APACHE-2.0, see LICENSE for details.
19

20
    Licensed under the Apache License, Version 2.0 (the "License");
21
    you may not use this file except in compliance with the License.
22
    You may obtain a copy of the License at
23

24
       http://www.apache.org/licenses/LICENSE-2.0
25

26
    Unless required by applicable law or agreed to in writing, software
27
    distributed under the License is distributed on an "AS IS" BASIS,
28
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
    See the License for the specific language governing permissions and
30
    limitations under the License.
31
"""
32

33
import logging
1✔
34

35
import keyring
1✔
36
from flask import current_app
1✔
37
from flask_mail import Message
1✔
38

39

40
try:
1✔
41
    from jeepney.wrappers import DBusErrorResponse
1✔
42
except (ImportError, ModuleNotFoundError):
1✔
43
    class DBusErrorResponse(Exception):
1✔
44
        """
45
        Fallback definition on not DBus systems
46
        """
47
        def __init__(self, message):
1✔
48
            super().__init__(message)
×
49

50
from mslib.msui import constants
1✔
51

52

53
NAME = __name__
1✔
54

55

56
def del_password_from_keyring(service_name=NAME, username=""):
1✔
57
    """
58
    removes an entry by username and service_name from the keyring
59
    """
60
    if username.strip() != "":
1✔
61
        try:
1✔
62
            keyring.delete_password(service_name=service_name, username=username)
1✔
63
        except (keyring.errors.NoKeyringError, keyring.errors.PasswordDeleteError) as ex:
×
64
            logging.warning("Can't use Keyring on your system: %s" % ex)
×
65

66

67
def get_password_from_keyring(service_name=NAME, username=""):
1✔
68
    """
69
    When we request a username we use this function to fill in a form field with a password
70
    In this case by none existing credentials in the keyring we have to return an empty string
71
    """
72
    if username.strip() != "":
1✔
73
        try:
1✔
74
            cred = keyring.get_credential(service_name=service_name, username=username)
1✔
75
            if username is not None and cred is None:
1✔
76
                return ""
×
77
            elif cred is None:
1✔
78
                return None
×
79
            else:
80
                return cred.password
1✔
81
        except (keyring.errors.KeyringLocked, keyring.errors.InitError, DBusErrorResponse) as ex:
×
82
            logging.warning(ex)
×
83
            return None
×
84

85

86
def save_password_to_keyring(service_name=NAME, username="", password=""):
1✔
87
    """
88
    save a username and password for a given service_name
89
    """
90
    if "" not in (username.strip(), password.strip()):
1✔
91
        try:
1✔
92
            keyring.set_password(service_name=service_name, username=username, password=password)
1✔
93
        except keyring.errors.NoKeyringError as ex:
×
94
            logging.info("Can't use Keyring on your system: %s" % ex)
×
95

96

97
def get_auth_from_url_and_name(server_url, http_auth, overwrite_login_cache=True):
1✔
98
    """
99
    gets auth_username from http_auth and password from keyring for a given server_url
100
    """
101
    name = ""
1✔
102
    for url, auth_name in http_auth.items():
1✔
103
        if server_url == url:
1✔
104
            try:
1✔
105
                password = get_password_from_keyring(service_name=url, username=auth_name)
1✔
106
            except keyring.errors.NoKeyringError as ex:
×
107
                password = None
×
108
                logging.info("Can't use Keyring on your system: %s" % ex)
×
109
            if overwrite_login_cache and password is not None and password.strip() != "":
1✔
110
                constants.AUTH_LOGIN_CACHE[server_url] = (auth_name, password)
1✔
111
            name = auth_name
1✔
112
            break
1✔
113
    if name == "":
1✔
114
        name = None
1✔
115
    auth = constants.AUTH_LOGIN_CACHE.get(server_url, (name, None))
1✔
116
    return auth
1✔
117

118

119
def send_email(to, subject, template):
1✔
NEW
120
    if current_app.config['MAIL_DEFAULT_SENDER'] is not None:
×
NEW
121
        msg = Message(
×
122
            subject,
123
            recipients=[to],
124
            html=template,
125
            sender=current_app.config['MAIL_DEFAULT_SENDER']
126
        )
NEW
127
        try:
×
NEW
128
            from mslib.mscolab.server import get_mail
×
NEW
129
            mail = get_mail()
×
NEW
130
            mail.send(msg)
×
NEW
131
        except IOError:
×
NEW
132
            logging.error("Can't send email to %s", to)
×
133
    else:
NEW
134
        logging.debug("setup user verification by email")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc