1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""MAAS CLI authentication."""
6
from __future__ import (
17
from getpass import getpass
20
from apiclient.creds import convert_string_to_tuple
23
def try_getpass(prompt):
24
"""Call `getpass`, ignoring EOF errors."""
26
return getpass(prompt)
31
def obtain_credentials(credentials):
32
"""Prompt for credentials if possible.
34
If the credentials are "-" then read from stdin without interactive
37
if credentials == "-":
38
credentials = sys.stdin.readline().strip()
39
elif credentials is None:
40
credentials = try_getpass(
41
"API key (leave empty for anonymous access): ")
42
# Ensure that the credentials have a valid form.
43
if credentials and not credentials.isspace():
44
return convert_string_to_tuple(credentials)