diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-08-20 22:00:58 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-08-20 22:00:58 +0000 |
commit | 0986e590501d0909b42d934ae6a01c328ba139b1 (patch) | |
tree | b563acb825e877e112394d24ca171c657570fc5b | |
parent | f99d9a6f918a3180f55cd6864211b961984d3d9c (diff) | |
parent | 6ef23c80d000a721c6a90bbd79420c7a24f74fb8 (diff) | |
download | python-keystoneclient-0986e590501d0909b42d934ae6a01c328ba139b1.tar.gz |
Merge "Allow passing user_id to v2Password plugin"
-rw-r--r-- | keystoneclient/auth/identity/v2.py | 26 | ||||
-rw-r--r-- | keystoneclient/tests/auth/test_identity_v2.py | 31 |
2 files changed, 54 insertions, 3 deletions
diff --git a/keystoneclient/auth/identity/v2.py b/keystoneclient/auth/identity/v2.py index a8e7ac1..174f899 100644 --- a/keystoneclient/auth/identity/v2.py +++ b/keystoneclient/auth/identity/v2.py @@ -97,20 +97,39 @@ class Auth(base.BaseIdentityPlugin): class Password(Auth): - def __init__(self, auth_url, username, password, **kwargs): + @utils.positional(4) + def __init__(self, auth_url, username=None, password=None, user_id=None, + **kwargs): """A plugin for authenticating with a username and password. + A username or user_id must be provided. + :param string auth_url: Identity service endpoint for authorization. :param string username: Username for authentication. :param string password: Password for authentication. + :param string user_id: User ID for authentication. + + :raises TypeError: if a user_id or username is not provided. """ super(Password, self).__init__(auth_url, **kwargs) + + if not (user_id or username): + msg = 'You need to specify either a username or user_id' + raise TypeError(msg) + + self.user_id = user_id self.username = username self.password = password def get_auth_data(self, headers=None): - return {'passwordCredentials': {'username': self.username, - 'password': self.password}} + auth = {'password': self.password} + + if self.username: + auth['username'] = self.username + elif self.user_id: + auth['userId'] = self.user_id + + return {'passwordCredentials': auth} @classmethod def get_options(cls): @@ -121,6 +140,7 @@ class Password(Auth): dest='username', deprecated_name='username', help='Username to login with'), + cfg.StrOpt('user-id', help='User ID to longin with'), cfg.StrOpt('password', secret=True, help='Password to use'), ]) diff --git a/keystoneclient/tests/auth/test_identity_v2.py b/keystoneclient/tests/auth/test_identity_v2.py index 46a505b..52e178b 100644 --- a/keystoneclient/tests/auth/test_identity_v2.py +++ b/keystoneclient/tests/auth/test_identity_v2.py @@ -110,6 +110,20 @@ class V2IdentityPlugin(utils.TestCase): self.assertRequestHeaderEqual('Accept', 'application/json') self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) + def test_authenticate_with_user_id_password(self): + self.stub_auth(json=self.TEST_RESPONSE_DICT) + a = v2.Password(self.TEST_URL, user_id=self.TEST_USER, + password=self.TEST_PASS) + s = session.Session(a) + s.get_token() + + req = {'auth': {'passwordCredentials': {'userId': self.TEST_USER, + 'password': self.TEST_PASS}}} + self.assertRequestBodyIs(json=req) + self.assertRequestHeaderEqual('Content-Type', 'application/json') + self.assertRequestHeaderEqual('Accept', 'application/json') + self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) + def test_authenticate_with_username_password_scoped(self): self.stub_auth(json=self.TEST_RESPONSE_DICT) a = v2.Password(self.TEST_URL, username=self.TEST_USER, @@ -123,6 +137,19 @@ class V2IdentityPlugin(utils.TestCase): self.assertRequestBodyIs(json=req) self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) + def test_authenticate_with_user_id_password_scoped(self): + self.stub_auth(json=self.TEST_RESPONSE_DICT) + a = v2.Password(self.TEST_URL, user_id=self.TEST_USER, + password=self.TEST_PASS, tenant_id=self.TEST_TENANT_ID) + s = session.Session(a) + s.get_token() + + req = {'auth': {'passwordCredentials': {'userId': self.TEST_USER, + 'password': self.TEST_PASS}, + 'tenantId': self.TEST_TENANT_ID}} + self.assertRequestBodyIs(json=req) + self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN) + def test_authenticate_with_token(self): self.stub_auth(json=self.TEST_RESPONSE_DICT) a = v2.Token(self.TEST_URL, 'foo') @@ -247,3 +274,7 @@ class V2IdentityPlugin(utils.TestCase): s = session.Session(auth=a) self.assertEqual(self.TEST_TOKEN, s.get_token()) self.assertNotIn(password, self.logger.output) + + def test_password_with_no_user_id_or_name(self): + self.assertRaises(TypeError, + v2.Password, self.TEST_URL, password=self.TEST_PASS) |