Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | """Oauth2client tests |
| 19 | |
| 20 | Unit tests for oauth2client. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
| 25 | import httplib2 |
| 26 | import os |
| 27 | import sys |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 28 | import tempfile |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 29 | import time |
| 30 | import unittest |
| 31 | import urlparse |
| 32 | |
| 33 | try: |
| 34 | from urlparse import parse_qs |
| 35 | except ImportError: |
| 36 | from cgi import parse_qs |
| 37 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 38 | from apiclient.http import HttpMockSequence |
| 39 | from oauth2client import crypt |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 40 | from oauth2client.anyjson import simplejson |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 41 | from oauth2client.client import Credentials |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 42 | from oauth2client.client import SignedJwtAssertionCredentials |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 43 | from oauth2client.client import VerifyJwtTokenError |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 44 | from oauth2client.client import verify_id_token |
Joe Gregorio | cff6b4d | 2013-02-12 13:13:04 -0500 | [diff] [blame] | 45 | from oauth2client.client import HAS_OPENSSL |
| 46 | from oauth2client.client import HAS_CRYPTO |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 47 | from oauth2client.file import Storage |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def datafile(filename): |
| 51 | f = open(os.path.join(os.path.dirname(__file__), 'data', filename), 'r') |
| 52 | data = f.read() |
| 53 | f.close() |
| 54 | return data |
| 55 | |
| 56 | |
| 57 | class CryptTests(unittest.TestCase): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 58 | def setUp(self): |
| 59 | self.format = 'p12' |
| 60 | self.signer = crypt.OpenSSLSigner |
| 61 | self.verifier = crypt.OpenSSLVerifier |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 62 | |
| 63 | def test_sign_and_verify(self): |
Joe Gregorio | 89d832a | 2013-10-29 16:02:32 -0400 | [diff] [blame^] | 64 | self._check_sign_and_verify('privatekey.%s' % self.format) |
| 65 | |
| 66 | def test_sign_and_verify_from_converted_pkcs12(self): |
| 67 | """Tests that following instructions to convert from PKCS12 to PEM works.""" |
| 68 | if self.format == 'pem': |
| 69 | self._check_sign_and_verify('pem_from_pkcs12.pem') |
| 70 | |
| 71 | def _check_sign_and_verify(self, private_key_file): |
| 72 | private_key = datafile(private_key_file) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 73 | public_key = datafile('publickey.pem') |
| 74 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 75 | signer = self.signer.from_string(private_key) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 76 | signature = signer.sign('foo') |
| 77 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 78 | verifier = self.verifier.from_string(public_key, True) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 79 | |
| 80 | self.assertTrue(verifier.verify('foo', signature)) |
| 81 | |
| 82 | self.assertFalse(verifier.verify('bar', signature)) |
| 83 | self.assertFalse(verifier.verify('foo', 'bad signagure')) |
| 84 | |
| 85 | def _check_jwt_failure(self, jwt, expected_error): |
| 86 | try: |
| 87 | public_key = datafile('publickey.pem') |
| 88 | certs = {'foo': public_key} |
| 89 | audience = 'https://www.googleapis.com/auth/id?client_id=' + \ |
| 90 | 'external_public_key@testing.gserviceaccount.com' |
| 91 | contents = crypt.verify_signed_jwt_with_certs(jwt, certs, audience) |
| 92 | self.fail('Should have thrown for %s' % jwt) |
| 93 | except: |
| 94 | e = sys.exc_info()[1] |
| 95 | msg = e.args[0] |
| 96 | self.assertTrue(expected_error in msg) |
| 97 | |
| 98 | def _create_signed_jwt(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 99 | private_key = datafile('privatekey.%s' % self.format) |
| 100 | signer = self.signer.from_string(private_key) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 101 | audience = 'some_audience_address@testing.gserviceaccount.com' |
| 102 | now = long(time.time()) |
| 103 | |
| 104 | return crypt.make_signed_jwt( |
| 105 | signer, |
| 106 | { |
| 107 | 'aud': audience, |
| 108 | 'iat': now, |
| 109 | 'exp': now + 300, |
| 110 | 'user': 'billy bob', |
| 111 | 'metadata': {'meta': 'data'}, |
| 112 | }) |
| 113 | |
| 114 | def test_verify_id_token(self): |
| 115 | jwt = self._create_signed_jwt() |
| 116 | public_key = datafile('publickey.pem') |
| 117 | certs = {'foo': public_key } |
| 118 | audience = 'some_audience_address@testing.gserviceaccount.com' |
| 119 | contents = crypt.verify_signed_jwt_with_certs(jwt, certs, audience) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 120 | self.assertEqual('billy bob', contents['user']) |
| 121 | self.assertEqual('data', contents['metadata']['meta']) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 122 | |
| 123 | def test_verify_id_token_with_certs_uri(self): |
| 124 | jwt = self._create_signed_jwt() |
| 125 | |
| 126 | http = HttpMockSequence([ |
| 127 | ({'status': '200'}, datafile('certs.json')), |
| 128 | ]) |
| 129 | |
| 130 | contents = verify_id_token(jwt, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 131 | 'some_audience_address@testing.gserviceaccount.com', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 132 | self.assertEqual('billy bob', contents['user']) |
| 133 | self.assertEqual('data', contents['metadata']['meta']) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 134 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 135 | def test_verify_id_token_with_certs_uri_fails(self): |
| 136 | jwt = self._create_signed_jwt() |
| 137 | |
| 138 | http = HttpMockSequence([ |
| 139 | ({'status': '404'}, datafile('certs.json')), |
| 140 | ]) |
| 141 | |
| 142 | self.assertRaises(VerifyJwtTokenError, verify_id_token, jwt, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 143 | 'some_audience_address@testing.gserviceaccount.com', http=http) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 144 | |
| 145 | def test_verify_id_token_bad_tokens(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 146 | private_key = datafile('privatekey.%s' % self.format) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 147 | |
| 148 | # Wrong number of segments |
| 149 | self._check_jwt_failure('foo', 'Wrong number of segments') |
| 150 | |
| 151 | # Not json |
| 152 | self._check_jwt_failure('foo.bar.baz', |
| 153 | 'Can\'t parse token') |
| 154 | |
| 155 | # Bad signature |
| 156 | jwt = 'foo.%s.baz' % crypt._urlsafe_b64encode('{"a":"b"}') |
| 157 | self._check_jwt_failure(jwt, 'Invalid token signature') |
| 158 | |
| 159 | # No expiration |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 160 | signer = self.signer.from_string(private_key) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 161 | audience = 'https:#www.googleapis.com/auth/id?client_id=' + \ |
| 162 | 'external_public_key@testing.gserviceaccount.com' |
| 163 | jwt = crypt.make_signed_jwt(signer, { |
| 164 | 'aud': 'audience', |
| 165 | 'iat': time.time(), |
| 166 | } |
| 167 | ) |
| 168 | self._check_jwt_failure(jwt, 'No exp field in token') |
| 169 | |
| 170 | # No issued at |
| 171 | jwt = crypt.make_signed_jwt(signer, { |
| 172 | 'aud': 'audience', |
| 173 | 'exp': time.time() + 400, |
| 174 | } |
| 175 | ) |
| 176 | self._check_jwt_failure(jwt, 'No iat field in token') |
| 177 | |
| 178 | # Too early |
| 179 | jwt = crypt.make_signed_jwt(signer, { |
| 180 | 'aud': 'audience', |
| 181 | 'iat': time.time() + 301, |
| 182 | 'exp': time.time() + 400, |
| 183 | }) |
| 184 | self._check_jwt_failure(jwt, 'Token used too early') |
| 185 | |
| 186 | # Too late |
| 187 | jwt = crypt.make_signed_jwt(signer, { |
| 188 | 'aud': 'audience', |
| 189 | 'iat': time.time() - 500, |
| 190 | 'exp': time.time() - 301, |
| 191 | }) |
| 192 | self._check_jwt_failure(jwt, 'Token used too late') |
| 193 | |
| 194 | # Wrong target |
| 195 | jwt = crypt.make_signed_jwt(signer, { |
| 196 | 'aud': 'somebody else', |
| 197 | 'iat': time.time(), |
| 198 | 'exp': time.time() + 300, |
| 199 | }) |
| 200 | self._check_jwt_failure(jwt, 'Wrong recipient') |
| 201 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 202 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 203 | class PEMCryptTestsPyCrypto(CryptTests): |
| 204 | def setUp(self): |
| 205 | self.format = 'pem' |
| 206 | self.signer = crypt.PyCryptoSigner |
| 207 | self.verifier = crypt.OpenSSLVerifier |
| 208 | |
| 209 | |
| 210 | class PEMCryptTestsOpenSSL(CryptTests): |
| 211 | def setUp(self): |
| 212 | self.format = 'pem' |
| 213 | self.signer = crypt.OpenSSLSigner |
| 214 | self.verifier = crypt.OpenSSLVerifier |
| 215 | |
| 216 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 217 | class SignedJwtAssertionCredentialsTests(unittest.TestCase): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 218 | def setUp(self): |
| 219 | self.format = 'p12' |
| 220 | crypt.Signer = crypt.OpenSSLSigner |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 221 | |
| 222 | def test_credentials_good(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 223 | private_key = datafile('privatekey.%s' % self.format) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 224 | credentials = SignedJwtAssertionCredentials( |
| 225 | 'some_account@example.com', |
| 226 | private_key, |
| 227 | scope='read+write', |
Daniel Hermes | 482ab76 | 2013-03-13 13:37:27 -0700 | [diff] [blame] | 228 | sub='joe@example.org') |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 229 | http = HttpMockSequence([ |
| 230 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 231 | ({'status': '200'}, 'echo_request_headers'), |
| 232 | ]) |
| 233 | http = credentials.authorize(http) |
| 234 | resp, content = http.request('http://example.org') |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 235 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 236 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 237 | def test_credentials_to_from_json(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 238 | private_key = datafile('privatekey.%s' % self.format) |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 239 | credentials = SignedJwtAssertionCredentials( |
| 240 | 'some_account@example.com', |
| 241 | private_key, |
| 242 | scope='read+write', |
Daniel Hermes | 482ab76 | 2013-03-13 13:37:27 -0700 | [diff] [blame] | 243 | sub='joe@example.org') |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 244 | json = credentials.to_json() |
| 245 | restored = Credentials.new_from_json(json) |
| 246 | self.assertEqual(credentials.private_key, restored.private_key) |
| 247 | self.assertEqual(credentials.private_key_password, |
| 248 | restored.private_key_password) |
| 249 | self.assertEqual(credentials.kwargs, restored.kwargs) |
| 250 | |
| 251 | def _credentials_refresh(self, credentials): |
| 252 | http = HttpMockSequence([ |
| 253 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 254 | ({'status': '401'}, ''), |
| 255 | ({'status': '200'}, '{"access_token":"3/3w","expires_in":3600}'), |
| 256 | ({'status': '200'}, 'echo_request_headers'), |
| 257 | ]) |
| 258 | http = credentials.authorize(http) |
| 259 | resp, content = http.request('http://example.org') |
| 260 | return content |
| 261 | |
| 262 | def test_credentials_refresh_without_storage(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 263 | private_key = datafile('privatekey.%s' % self.format) |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 264 | credentials = SignedJwtAssertionCredentials( |
| 265 | 'some_account@example.com', |
| 266 | private_key, |
| 267 | scope='read+write', |
Daniel Hermes | 482ab76 | 2013-03-13 13:37:27 -0700 | [diff] [blame] | 268 | sub='joe@example.org') |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 269 | |
| 270 | content = self._credentials_refresh(credentials) |
| 271 | |
| 272 | self.assertEqual('Bearer 3/3w', content['Authorization']) |
| 273 | |
| 274 | def test_credentials_refresh_with_storage(self): |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 275 | private_key = datafile('privatekey.%s' % self.format) |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 276 | credentials = SignedJwtAssertionCredentials( |
| 277 | 'some_account@example.com', |
| 278 | private_key, |
| 279 | scope='read+write', |
Daniel Hermes | 482ab76 | 2013-03-13 13:37:27 -0700 | [diff] [blame] | 280 | sub='joe@example.org') |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 281 | |
| 282 | (filehandle, filename) = tempfile.mkstemp() |
| 283 | os.close(filehandle) |
| 284 | store = Storage(filename) |
| 285 | store.put(credentials) |
| 286 | credentials.set_store(store) |
| 287 | |
| 288 | content = self._credentials_refresh(credentials) |
| 289 | |
| 290 | self.assertEqual('Bearer 3/3w', content['Authorization']) |
| 291 | os.unlink(filename) |
| 292 | |
| 293 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 294 | class PEMSignedJwtAssertionCredentialsOpenSSLTests( |
| 295 | SignedJwtAssertionCredentialsTests): |
| 296 | def setUp(self): |
| 297 | self.format = 'pem' |
| 298 | crypt.Signer = crypt.OpenSSLSigner |
| 299 | |
| 300 | |
| 301 | class PEMSignedJwtAssertionCredentialsPyCryptoTests( |
| 302 | SignedJwtAssertionCredentialsTests): |
| 303 | def setUp(self): |
| 304 | self.format = 'pem' |
| 305 | crypt.Signer = crypt.PyCryptoSigner |
| 306 | |
| 307 | |
| 308 | class PKCSSignedJwtAssertionCredentialsPyCryptoTests(unittest.TestCase): |
| 309 | def test_for_failure(self): |
| 310 | crypt.Signer = crypt.PyCryptoSigner |
| 311 | private_key = datafile('privatekey.p12') |
| 312 | credentials = SignedJwtAssertionCredentials( |
| 313 | 'some_account@example.com', |
| 314 | private_key, |
| 315 | scope='read+write', |
Daniel Hermes | 482ab76 | 2013-03-13 13:37:27 -0700 | [diff] [blame] | 316 | sub='joe@example.org') |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 317 | try: |
| 318 | credentials._generate_assertion() |
| 319 | self.fail() |
| 320 | except NotImplementedError: |
| 321 | pass |
| 322 | |
Joe Gregorio | cff6b4d | 2013-02-12 13:13:04 -0500 | [diff] [blame] | 323 | class TestHasOpenSSLFlag(unittest.TestCase): |
| 324 | def test_true(self): |
| 325 | self.assertEqual(True, HAS_OPENSSL) |
| 326 | self.assertEqual(True, HAS_CRYPTO) |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 327 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 328 | if __name__ == '__main__': |
| 329 | unittest.main() |