AuthInfo use dependency injection

AuthInfo didn't use dependency injection so the managers were
created multiple times.

The dependency injection doesn't happen until after __init__(),
so users can't call methods using the dependencies in __init__().

Change-Id: I4532a2219385f2e303eb6f9a7bd7edb76046e331
This commit is contained in:
Brant Knudson 2013-10-31 13:52:26 -05:00
parent 9307dee27b
commit 949df90014
4 changed files with 19 additions and 20 deletions

View File

@ -14,17 +14,14 @@
# License for the specific language governing permissions and limitations
# under the License.
from keystone import assignment
from keystone.common import controller
from keystone.common import dependency
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone import identity
from keystone.openstack.common import importutils
from keystone.openstack.common import log as logging
from keystone import token
from keystone import trust
LOG = logging.getLogger(__name__)
@ -49,14 +46,17 @@ def get_auth_method(method_name):
return AUTH_METHODS[method_name]
@dependency.requires('assignment_api')
@dependency.requires('assignment_api', 'identity_api', 'trust_api')
class AuthInfo(object):
"""Encapsulation of "auth" request."""
@staticmethod
def create(context, auth=None):
auth_info = AuthInfo(context, auth=auth)
auth_info._validate_and_normalize_auth_data()
return auth_info
def __init__(self, context, auth=None):
self.assignment_api = assignment.Manager()
self.identity_api = identity.Manager()
self.trust_api = trust.Manager()
self.context = context
self.auth = auth
self._scope_data = (None, None, None)
@ -65,7 +65,6 @@ class AuthInfo(object):
# domain scope: (domain_id, None, None)
# trust scope: (None, None, trust_ref)
# unscoped: (None, None, None)
self._validate_and_normalize_auth_data()
def _assert_project_is_enabled(self, project_ref):
# ensure the project is enabled
@ -284,7 +283,7 @@ class Auth(controller.V3Controller):
include_catalog = 'nocatalog' not in context['query_string']
try:
auth_info = AuthInfo(context, auth=auth)
auth_info = AuthInfo.create(context, auth=auth)
auth_context = {'extras': {}, 'method_names': [], 'bind': {}}
self.authenticate(context, auth_info, auth_context)
if auth_context.get('access_token_id'):

View File

@ -62,7 +62,7 @@ class TestAuthPlugin(tests.TestCase):
auth_data[method_name] = {'test': 'test'}
auth_data = {'identity': auth_data}
self.assertRaises(exception.AuthMethodNotSupported,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -71,7 +71,7 @@ class TestAuthPlugin(tests.TestCase):
auth_data['simple-challenge-response'] = {
'test': 'test'}
auth_data = {'identity': auth_data}
auth_info = auth.controllers.AuthInfo(None, auth_data)
auth_info = auth.controllers.AuthInfo.create(None, auth_data)
auth_context = {'extras': {}, 'method_names': []}
try:
self.api.authenticate({'environment': {}}, auth_info, auth_context)
@ -86,7 +86,7 @@ class TestAuthPlugin(tests.TestCase):
auth_data['simple-challenge-response'] = {
'response': EXPECTED_RESPONSE}
auth_data = {'identity': auth_data}
auth_info = auth.controllers.AuthInfo(None, auth_data)
auth_info = auth.controllers.AuthInfo.create(None, auth_data)
auth_context = {'extras': {}, 'method_names': []}
self.api.authenticate({'environment': {}}, auth_info, auth_context)
self.assertEqual(auth_context['user_id'], DEMO_USER_ID)
@ -96,7 +96,7 @@ class TestAuthPlugin(tests.TestCase):
auth_data['simple-challenge-response'] = {
'response': uuid.uuid4().hex}
auth_data = {'identity': auth_data}
auth_info = auth.controllers.AuthInfo(None, auth_data)
auth_info = auth.controllers.AuthInfo.create(None, auth_data)
auth_context = {'extras': {}, 'method_names': []}
self.assertRaises(exception.Unauthorized,
self.api.authenticate,

View File

@ -1038,7 +1038,7 @@ class RestfulTestCase(rest.RestfulTestCase):
if not auth_data:
auth_data = self.build_authentication_request()['auth']
no_context = None
auth_info = auth.controllers.AuthInfo(no_context, auth_data)
auth_info = auth.controllers.AuthInfo.create(no_context, auth_data)
auth_context = {'extras': {}, 'method_names': []}
return context, auth_info, auth_context

View File

@ -39,7 +39,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
auth_data = {'identity': {}}
auth_data['identity']['token'] = {'id': uuid.uuid4().hex}
self.assertRaises(exception.ValidationError,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -48,7 +48,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
auth_data['abc'] = {'test': 'test'}
auth_data = {'identity': auth_data}
self.assertRaises(exception.AuthMethodNotSupported,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -56,7 +56,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
auth_data = {'methods': ['password']}
auth_data = {'identity': auth_data}
self.assertRaises(exception.ValidationError,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -66,7 +66,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
password='test',
project_name='abc')['auth']
self.assertRaises(exception.ValidationError,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -77,7 +77,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
project_name='test',
domain_name='test')['auth']
self.assertRaises(exception.ValidationError,
auth.controllers.AuthInfo,
auth.controllers.AuthInfo.create,
None,
auth_data)
@ -86,7 +86,7 @@ class TestAuthInfo(test_v3.RestfulTestCase):
user_id='test',
password='test')['auth']
context = None
auth_info = auth.controllers.AuthInfo(context, auth_data)
auth_info = auth.controllers.AuthInfo.create(context, auth_data)
method_name = uuid.uuid4().hex
self.assertRaises(exception.ValidationError,