| # Copyright 2012 Canonical Ltd. This software is licensed under the |
| # GNU Affero General Public License version 3 (see the file LICENSE). |
| |
| """Handling of MAAS API credentials. |
| |
| The API client deals with credentials consisting of 3 elements: consumer |
| key, resource token, and resource secret. These are in OAuth, but the |
| consumer secret is hardwired to the empty string. |
| |
| Credentials are represented internally as tuples of these three elements, |
| but can also be converted to a colon-separated string format for easy |
| transport between processes. |
| """ |
| |
| from __future__ import ( |
| absolute_import, |
| print_function, |
| unicode_literals, |
| ) |
| |
| str = None |
| |
| __metaclass__ = type |
| __all__ = [ |
| 'convert_string_to_tuple', |
| 'convert_tuple_to_string', |
| ] |
| |
| |
| def convert_tuple_to_string(creds_tuple): |
| """Represent a MAAS API credentials tuple as a colon-separated string.""" |
| if len(creds_tuple) != 3: |
| raise ValueError( |
| "Credentials tuple does not consist of 3 elements as expected; " |
| "it contains %d." |
| % len(creds_tuple)) |
| return ':'.join(creds_tuple) |
| |
| |
| def convert_string_to_tuple(creds_string): |
| """Recreate a MAAS API credentials tuple from a colon-separated string.""" |
| creds_tuple = tuple(creds_string.split(':')) |
| if len(creds_tuple) != 3: |
| raise ValueError( |
| "Malformed credentials string. Expected 3 colon-separated items, " |
| "got %r." |
| % creds_string) |
| return creds_tuple |