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

Open-MSS / MSS / 17923574015

22 Sep 2025 05:36PM UTC coverage: 69.881% (+0.08%) from 69.804%
17923574015

Pull #2895

github

web-flow
Merge c048e0cf5 into ced3151f8
Pull Request #2895: Bump prefix-dev/setup-pixi from 0.9.0 to 0.9.1

14480 of 20721 relevant lines covered (69.88%)

0.7 hits per line

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

68.63
/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-2025 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
import keyring
1✔
35

36

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

47
from mslib.msui import constants
1✔
48

49

50
NAME = __name__
1✔
51

52

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

63

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

82

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

93

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