~maas-committers/maas/trunk

« back to all changes in this revision

Viewing changes to src/maascli/auth.py

  • Committer: Blake Rouse
  • Date: 2017-06-22 18:36:11 UTC
  • Revision ID: blake.rouse@canonical.com-20170622183611-8dgbwu0rejwqn3vs
Remove all code from the bzr branch. Document that Git should be used instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2012-2016 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""MAAS CLI authentication."""
5
 
 
6
 
__all__ = [
7
 
    'obtain_credentials',
8
 
    ]
9
 
 
10
 
from getpass import getpass
11
 
import http.client
12
 
import sys
13
 
from urllib.parse import urljoin
14
 
 
15
 
from apiclient.creds import convert_string_to_tuple
16
 
from maascli.api import (
17
 
    Action,
18
 
    http_request,
19
 
)
20
 
 
21
 
 
22
 
class UnexpectedResponse(Exception):
23
 
    """Unexpected API response."""
24
 
 
25
 
 
26
 
def try_getpass(prompt):
27
 
    """Call `getpass`, ignoring EOF errors."""
28
 
    try:
29
 
        return getpass(prompt)
30
 
    except EOFError:
31
 
        return None
32
 
 
33
 
 
34
 
def obtain_credentials(credentials):
35
 
    """Prompt for credentials if possible.
36
 
 
37
 
    If the credentials are "-" then read from stdin without interactive
38
 
    prompting.
39
 
    """
40
 
    if credentials == "-":
41
 
        credentials = sys.stdin.readline().strip()
42
 
    elif credentials is None:
43
 
        credentials = try_getpass(
44
 
            "API key (leave empty for anonymous access): ")
45
 
    # Ensure that the credentials have a valid form.
46
 
    if credentials and not credentials.isspace():
47
 
        return convert_string_to_tuple(credentials)
48
 
    else:
49
 
        return None
50
 
 
51
 
 
52
 
def check_valid_apikey(url, credentials, insecure=False):
53
 
    """Check for valid apikey.
54
 
 
55
 
    :param credentials: A 3-tuple of credentials.
56
 
    """
57
 
    if '/api/1.0' in url:
58
 
        check_url = urljoin(url, "nodegroups/")
59
 
        uri, body, headers = Action.prepare_payload(
60
 
            op="list", method="GET", uri=check_url, data=[])
61
 
    else:
62
 
        check_url = urljoin(url, "users/")
63
 
        uri, body, headers = Action.prepare_payload(
64
 
            op="whoami", method="GET", uri=check_url, data=[])
65
 
 
66
 
    # Headers are returned as a list, but they must be a dict for
67
 
    # the signing machinery.
68
 
    headers = dict(headers)
69
 
 
70
 
    Action.sign(uri, headers, credentials)
71
 
 
72
 
    response, content = http_request(
73
 
        uri, method="GET", body=body, headers=headers,
74
 
        insecure=insecure)
75
 
 
76
 
    status = int(response['status'])
77
 
    if status == http.client.UNAUTHORIZED:
78
 
        return False
79
 
    elif status == http.client.OK:
80
 
        return True
81
 
    else:
82
 
        raise UnexpectedResponse(
83
 
            "The MAAS server gave an unexpected response: %s" % status)