|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +""" |
| 14 | +Identity discoverable authorization plugin must be constructed with an |
| 15 | +auhorization URL and a user id, user name or token. A user id or user name |
| 16 | +would also require a password. The arguments that apply to the selected v2 |
| 17 | +or v3 plugin will be used. The rest of the arguments will be ignored. For |
| 18 | +example:: |
| 19 | +
|
| 20 | + from openstack.auth.identity import discoverable |
| 21 | + from openstack import transport |
| 22 | +
|
| 23 | + args = { |
| 24 | + 'password': 'openSesame', |
| 25 | + 'auth_url': 'https://10.1.1.1:5000/v3/', |
| 26 | + 'user_name': 'alibaba', |
| 27 | + } |
| 28 | + auth = discoverable.Auth(**args) |
| 29 | + xport = transport.Transport() |
| 30 | + accessInfo = auth.authorize(xport) |
| 31 | +""" |
| 32 | + |
| 33 | +from openstack.auth.identity import base |
| 34 | +from openstack.auth.identity import v2 |
| 35 | +from openstack.auth.identity import v3 |
| 36 | +from openstack import exceptions |
| 37 | + |
| 38 | + |
| 39 | +class Auth(base.BaseIdentityPlugin): |
| 40 | + |
| 41 | + #: Valid options for this plugin |
| 42 | + valid_options = list(set(v2.Auth.valid_options + v3.Auth.valid_options)) |
| 43 | + |
| 44 | + def __init__(self, auth_url=None, **auth_args): |
| 45 | + """Construct an Identity Authentication Plugin. |
| 46 | +
|
| 47 | + This authorization plugin should be constructed with an auth_url |
| 48 | + and everything needed by either a v2 or v3 identity plugin. |
| 49 | +
|
| 50 | + :param string auth_url: Identity service endpoint for authentication. |
| 51 | +
|
| 52 | + :raises TypeError: if a user_id, user_name or token is not provided. |
| 53 | + """ |
| 54 | + |
| 55 | + super(Auth, self).__init__(auth_url=auth_url) |
| 56 | + |
| 57 | + if not auth_url: |
| 58 | + msg = ("The authorization URL auth_url was not provided.") |
| 59 | + raise exceptions.AuthorizationFailure(msg) |
| 60 | + endpoint_version = auth_url.split('v')[-1][0] |
| 61 | + if endpoint_version == '2': |
| 62 | + plugin = v2.Auth |
| 63 | + else: |
| 64 | + plugin = v3.Auth |
| 65 | + valid_list = plugin.valid_options |
| 66 | + args = dict((n, auth_args[n]) for n in valid_list if n in auth_args) |
| 67 | + self.auth_plugin = plugin(auth_url, **args) |
| 68 | + |
| 69 | + @property |
| 70 | + def token_url(self): |
| 71 | + """The full URL where we will send authentication data.""" |
| 72 | + return self.auth_plugin.token_url |
| 73 | + |
| 74 | + def authorize(self, transport, **kwargs): |
| 75 | + return self.auth_plugin.authorize(transport, **kwargs) |
| 76 | + |
| 77 | + def invalidate(self): |
| 78 | + return self.auth_plugin.invalidate() |
0 commit comments