test_signatures.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. # -*- coding: utf-8 -*-
  2. from oauthlib.oauth1.rfc5849.signature import (
  3. base_string_uri, collect_parameters, normalize_parameters,
  4. sign_hmac_sha1_with_client, sign_hmac_sha256_with_client,
  5. sign_hmac_sha512_with_client, sign_plaintext_with_client,
  6. sign_rsa_sha1_with_client, sign_rsa_sha256_with_client,
  7. sign_rsa_sha512_with_client, signature_base_string, verify_hmac_sha1,
  8. verify_hmac_sha256, verify_hmac_sha512, verify_plaintext, verify_rsa_sha1,
  9. verify_rsa_sha256, verify_rsa_sha512,
  10. )
  11. from tests.unittest import TestCase
  12. # ################################################################
  13. class MockRequest:
  14. """
  15. Mock of a request used by the verify_* functions.
  16. """
  17. def __init__(self,
  18. method: str,
  19. uri_str: str,
  20. params: list,
  21. signature: str):
  22. """
  23. The params is a list of (name, value) tuples. It is not a dictionary,
  24. because there can be multiple parameters with the same name.
  25. """
  26. self.uri = uri_str
  27. self.http_method = method
  28. self.params = params
  29. self.signature = signature
  30. # ################################################################
  31. class MockClient:
  32. """
  33. Mock of client credentials used by the sign_*_with_client functions.
  34. For HMAC, set the client_secret and resource_owner_secret.
  35. For RSA, set the rsa_key to either a PEM formatted PKCS #1 public key or
  36. PEM formatted PKCS #1 private key.
  37. """
  38. def __init__(self,
  39. client_secret: str = None,
  40. resource_owner_secret: str = None,
  41. rsa_key: str = None):
  42. self.client_secret = client_secret
  43. self.resource_owner_secret = resource_owner_secret
  44. self.rsa_key = rsa_key # used for private or public key: a poor design!
  45. # ################################################################
  46. class SignatureTests(TestCase):
  47. """
  48. Unit tests for the oauthlib/oauth1/rfc5849/signature.py module.
  49. The tests in this class are organised into sections, to test the
  50. functions relating to:
  51. - Signature base string calculation
  52. - HMAC-based signature methods
  53. - RSA-based signature methods
  54. - PLAINTEXT signature method
  55. Each section is separated by a comment beginning with "====".
  56. Those comments have been formatted to remain visible when the code is
  57. collapsed using PyCharm's code folding feature. That is, those section
  58. heading comments do not have any other comment lines around it, so they
  59. don't get collapsed when the contents of the class is collapsed. While
  60. there is a "Sequential comments" option in the code folding configuration,
  61. by default they are folded.
  62. They all use some/all of the example test vector, defined in the first
  63. section below.
  64. """
  65. # ==== Example test vector =======================================
  66. eg_signature_base_string =\
  67. 'POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q' \
  68. '%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_' \
  69. 'key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m' \
  70. 'ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk' \
  71. '9d7dh3k39sjv7'
  72. # The _signature base string_ above is copied from the end of
  73. # RFC 5849 section 3.4.1.1.
  74. #
  75. # It corresponds to the three values below.
  76. #
  77. # The _normalized parameters_ below is copied from the end of
  78. # RFC 5849 section 3.4.1.3.2.
  79. eg_http_method = 'POST'
  80. eg_base_string_uri = 'http://example.com/request'
  81. eg_normalized_parameters =\
  82. 'a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj' \
  83. 'dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1' \
  84. '&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7'
  85. # The above _normalized parameters_ corresponds to the parameters below.
  86. #
  87. # The parameters below is copied from the table at the end of
  88. # RFC 5849 section 3.4.1.3.1.
  89. eg_params = [
  90. ('b5', '=%3D'),
  91. ('a3', 'a'),
  92. ('c@', ''),
  93. ('a2', 'r b'),
  94. ('oauth_consumer_key', '9djdj82h48djs9d2'),
  95. ('oauth_token', 'kkk9d7dh3k39sjv7'),
  96. ('oauth_signature_method', 'HMAC-SHA1'),
  97. ('oauth_timestamp', '137131201'),
  98. ('oauth_nonce', '7d8f3e4a'),
  99. ('c2', ''),
  100. ('a3', '2 q'),
  101. ]
  102. # The above parameters correspond to parameters from the three values below.
  103. #
  104. # These come from RFC 5849 section 3.4.1.3.1.
  105. eg_uri_query = 'b5=%3D%253D&a3=a&c%40=&a2=r%20b'
  106. eg_body = 'c2&a3=2+q'
  107. eg_authorization_header =\
  108. 'OAuth realm="Example", oauth_consumer_key="9djdj82h48djs9d2",' \
  109. ' oauth_token="kkk9d7dh3k39sjv7", oauth_signature_method="HMAC-SHA1",' \
  110. ' oauth_timestamp="137131201", oauth_nonce="7d8f3e4a",' \
  111. ' oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D"'
  112. # ==== Signature base string calculating function tests ==========
  113. def test_signature_base_string(self):
  114. """
  115. Test the ``signature_base_string`` function.
  116. """
  117. # Example from RFC 5849
  118. self.assertEqual(
  119. self.eg_signature_base_string,
  120. signature_base_string(
  121. self.eg_http_method,
  122. self.eg_base_string_uri,
  123. self.eg_normalized_parameters))
  124. # Test method is always uppercase in the signature base string
  125. for test_method in ['POST', 'Post', 'pOST', 'poST', 'posT', 'post']:
  126. self.assertEqual(
  127. self.eg_signature_base_string,
  128. signature_base_string(
  129. test_method,
  130. self.eg_base_string_uri,
  131. self.eg_normalized_parameters))
  132. def test_base_string_uri(self):
  133. """
  134. Test the ``base_string_uri`` function.
  135. """
  136. # ----------------
  137. # Examples from the OAuth 1.0a specification: RFC 5849.
  138. # First example from RFC 5849 section 3.4.1.2.
  139. #
  140. # GET /r%20v/X?id=123 HTTP/1.1
  141. # Host: EXAMPLE.COM:80
  142. #
  143. # Note: there is a space between "r" and "v"
  144. self.assertEqual(
  145. 'http://example.com/r%20v/X',
  146. base_string_uri('http://EXAMPLE.COM:80/r v/X?id=123'))
  147. # Second example from RFC 5849 section 3.4.1.2.
  148. #
  149. # GET /?q=1 HTTP/1.1
  150. # Host: www.example.net:8080
  151. self.assertEqual(
  152. 'https://www.example.net:8080/',
  153. base_string_uri('https://www.example.net:8080/?q=1'))
  154. # ----------------
  155. # Scheme: will always be in lowercase
  156. for uri in [
  157. 'foobar://www.example.com',
  158. 'FOOBAR://www.example.com',
  159. 'Foobar://www.example.com',
  160. 'FooBar://www.example.com',
  161. 'fOObAR://www.example.com',
  162. ]:
  163. self.assertEqual('foobar://www.example.com/', base_string_uri(uri))
  164. # ----------------
  165. # Host: will always be in lowercase
  166. for uri in [
  167. 'http://www.example.com',
  168. 'http://WWW.EXAMPLE.COM',
  169. 'http://www.EXAMPLE.com',
  170. 'http://wWW.eXAMPLE.cOM',
  171. ]:
  172. self.assertEqual('http://www.example.com/', base_string_uri(uri))
  173. # base_string_uri has an optional host parameter that can be used to
  174. # override the URI's netloc (or used as the host if there is no netloc)
  175. # The "netloc" refers to the "hostname[:port]" part of the URI.
  176. self.assertEqual(
  177. 'http://actual.example.com/',
  178. base_string_uri('http://IGNORE.example.com', 'ACTUAL.example.com'))
  179. self.assertEqual(
  180. 'http://override.example.com/path',
  181. base_string_uri('http:///path', 'OVERRIDE.example.com'))
  182. # ----------------
  183. # Host: valid host allows for IPv4 and IPv6
  184. self.assertEqual(
  185. 'https://192.168.0.1/',
  186. base_string_uri('https://192.168.0.1')
  187. )
  188. self.assertEqual(
  189. 'https://192.168.0.1:13000/',
  190. base_string_uri('https://192.168.0.1:13000')
  191. )
  192. self.assertEqual(
  193. 'https://[123:db8:fd00:1000::5]:13000/',
  194. base_string_uri('https://[123:db8:fd00:1000::5]:13000')
  195. )
  196. self.assertEqual(
  197. 'https://[123:db8:fd00:1000::5]/',
  198. base_string_uri('https://[123:db8:fd00:1000::5]')
  199. )
  200. # ----------------
  201. # Port: default ports always excluded; non-default ports always included
  202. self.assertEqual(
  203. "http://www.example.com/",
  204. base_string_uri("http://www.example.com:80/")) # default port
  205. self.assertEqual(
  206. "https://www.example.com/",
  207. base_string_uri("https://www.example.com:443/")) # default port
  208. self.assertEqual(
  209. "https://www.example.com:999/",
  210. base_string_uri("https://www.example.com:999/")) # non-default port
  211. self.assertEqual(
  212. "http://www.example.com:443/",
  213. base_string_uri("HTTP://www.example.com:443/")) # non-default port
  214. self.assertEqual(
  215. "https://www.example.com:80/",
  216. base_string_uri("HTTPS://www.example.com:80/")) # non-default port
  217. self.assertEqual(
  218. "http://www.example.com/",
  219. base_string_uri("http://www.example.com:/")) # colon but no number
  220. # ----------------
  221. # Paths
  222. self.assertEqual(
  223. 'http://www.example.com/',
  224. base_string_uri('http://www.example.com')) # no slash
  225. self.assertEqual(
  226. 'http://www.example.com/',
  227. base_string_uri('http://www.example.com/')) # with slash
  228. self.assertEqual(
  229. 'http://www.example.com:8080/',
  230. base_string_uri('http://www.example.com:8080')) # no slash
  231. self.assertEqual(
  232. 'http://www.example.com:8080/',
  233. base_string_uri('http://www.example.com:8080/')) # with slash
  234. self.assertEqual(
  235. 'http://www.example.com/foo/bar',
  236. base_string_uri('http://www.example.com/foo/bar')) # no slash
  237. self.assertEqual(
  238. 'http://www.example.com/foo/bar/',
  239. base_string_uri('http://www.example.com/foo/bar/')) # with slash
  240. # ----------------
  241. # Query parameters & fragment IDs do not appear in the base string URI
  242. self.assertEqual(
  243. 'https://www.example.com/path',
  244. base_string_uri('https://www.example.com/path?foo=bar'))
  245. self.assertEqual(
  246. 'https://www.example.com/path',
  247. base_string_uri('https://www.example.com/path#fragment'))
  248. # ----------------
  249. # Percent encoding
  250. #
  251. # RFC 5849 does not specify what characters are percent encoded, but in
  252. # one of its examples it shows spaces being percent encoded.
  253. # So it is assumed that spaces must be encoded, but we don't know what
  254. # other characters are encoded or not.
  255. self.assertEqual(
  256. 'https://www.example.com/hello%20world',
  257. base_string_uri('https://www.example.com/hello world'))
  258. self.assertEqual(
  259. 'https://www.hello%20world.com/',
  260. base_string_uri('https://www.hello world.com/'))
  261. # ----------------
  262. # Errors detected
  263. # base_string_uri expects a string
  264. self.assertRaises(ValueError, base_string_uri, None)
  265. self.assertRaises(ValueError, base_string_uri, 42)
  266. self.assertRaises(ValueError, base_string_uri, b'http://example.com')
  267. # Missing scheme is an error
  268. self.assertRaises(ValueError, base_string_uri, '')
  269. self.assertRaises(ValueError, base_string_uri, ' ') # single space
  270. self.assertRaises(ValueError, base_string_uri, 'http')
  271. self.assertRaises(ValueError, base_string_uri, 'example.com')
  272. # Missing host is an error
  273. self.assertRaises(ValueError, base_string_uri, 'http:')
  274. self.assertRaises(ValueError, base_string_uri, 'http://')
  275. self.assertRaises(ValueError, base_string_uri, 'http://:8080')
  276. # Port is not a valid TCP/IP port number
  277. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:0')
  278. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:-1')
  279. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:65536')
  280. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:3.14')
  281. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:BAD')
  282. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:NaN')
  283. self.assertRaises(ValueError, base_string_uri, 'http://eg.com: ')
  284. self.assertRaises(ValueError, base_string_uri, 'http://eg.com:42:42')
  285. def test_collect_parameters(self):
  286. """
  287. Test the ``collect_parameters`` function.
  288. """
  289. # ----------------
  290. # Examples from the OAuth 1.0a specification: RFC 5849.
  291. params = collect_parameters(
  292. self.eg_uri_query,
  293. self.eg_body,
  294. {'Authorization': self.eg_authorization_header})
  295. # Check params contains the same pairs as control_params, ignoring order
  296. self.assertEqual(sorted(self.eg_params), sorted(params))
  297. # ----------------
  298. # Examples with no parameters
  299. self.assertEqual([], collect_parameters('', '', {}))
  300. self.assertEqual([], collect_parameters(None, None, None))
  301. self.assertEqual([], collect_parameters())
  302. self.assertEqual([], collect_parameters(headers={'foo': 'bar'}))
  303. # ----------------
  304. # Test effect of exclude_oauth_signature"
  305. no_sig = collect_parameters(
  306. headers={'authorization': self.eg_authorization_header})
  307. with_sig = collect_parameters(
  308. headers={'authorization': self.eg_authorization_header},
  309. exclude_oauth_signature=False)
  310. self.assertEqual(sorted(no_sig + [('oauth_signature',
  311. 'djosJKDKJSD8743243/jdk33klY=')]),
  312. sorted(with_sig))
  313. # ----------------
  314. # Test effect of "with_realm" as well as header name case insensitivity
  315. no_realm = collect_parameters(
  316. headers={'authorization': self.eg_authorization_header},
  317. with_realm=False)
  318. with_realm = collect_parameters(
  319. headers={'AUTHORIZATION': self.eg_authorization_header},
  320. with_realm=True)
  321. self.assertEqual(sorted(no_realm + [('realm', 'Example')]),
  322. sorted(with_realm))
  323. def test_normalize_parameters(self):
  324. """
  325. Test the ``normalize_parameters`` function.
  326. """
  327. # headers = {'Authorization': self.authorization_header}
  328. # parameters = collect_parameters(
  329. # uri_query=self.uri_query, body=self.body, headers=headers)
  330. # normalized = normalize_parameters(parameters)
  331. #
  332. # # Unicode everywhere and always
  333. # self.assertIsInstance(normalized, str)
  334. #
  335. # # Lets see if things are in order
  336. # # check to see that querystring keys come in alphanumeric order:
  337. # querystring_keys = ['a2', 'a3', 'b5', 'oauth_consumer_key',
  338. # 'oauth_nonce', 'oauth_signature_method',
  339. # 'oauth_timestamp', 'oauth_token']
  340. # index = -1 # start at -1 because the 'a2' key starts at index 0
  341. # for key in querystring_keys:
  342. # self.assertGreater(normalized.index(key), index)
  343. # index = normalized.index(key)
  344. # ----------------
  345. # Example from the OAuth 1.0a specification: RFC 5849.
  346. # Params from end of section 3.4.1.3.1. and the expected
  347. # normalized parameters from the end of section 3.4.1.3.2.
  348. self.assertEqual(self.eg_normalized_parameters,
  349. normalize_parameters(self.eg_params))
  350. # ==== HMAC-based signature method tests =========================
  351. hmac_client = MockClient(
  352. client_secret='ECrDNoq1VYzzzzzzzzzyAK7TwZNtPnkqatqZZZZ',
  353. resource_owner_secret='just-a-string asdasd')
  354. # The following expected signatures were calculated by putting the value of
  355. # the eg_signature_base_string in a file ("base-str.txt") and running:
  356. #
  357. # echo -n `cat base-str.txt` | openssl dgst -hmac KEY -sha1 -binary| base64
  358. #
  359. # Where the KEY is the concatenation of the client_secret, an ampersand and
  360. # the resource_owner_secret. But those values need to be encoded properly,
  361. # so the spaces in the resource_owner_secret must be represented as '%20'.
  362. #
  363. # Note: the "echo -n" is needed to remove the last newline character, which
  364. # most text editors will add.
  365. expected_signature_hmac_sha1 = \
  366. 'wsdNmjGB7lvis0UJuPAmjvX/PXw='
  367. expected_signature_hmac_sha256 = \
  368. 'wdfdHUKXHbOnOGZP8WFAWMSAmWzN3EVBWWgXGlC/Eo4='
  369. expected_signature_hmac_sha512 = \
  370. 'u/vlyZFDxOWOZ9UUXwRBJHvq8/T4jCA74ocRmn2ECnjUBTAeJiZIRU8hDTjS88Tz' \
  371. '1fGONffMpdZxUkUTW3k1kg=='
  372. def test_sign_hmac_sha1_with_client(self):
  373. """
  374. Test sign and verify with HMAC-SHA1.
  375. """
  376. self.assertEqual(
  377. self.expected_signature_hmac_sha1,
  378. sign_hmac_sha1_with_client(self.eg_signature_base_string,
  379. self.hmac_client))
  380. self.assertTrue(verify_hmac_sha1(
  381. MockRequest('POST',
  382. 'http://example.com/request',
  383. self.eg_params,
  384. self.expected_signature_hmac_sha1),
  385. self.hmac_client.client_secret,
  386. self.hmac_client.resource_owner_secret))
  387. def test_sign_hmac_sha256_with_client(self):
  388. """
  389. Test sign and verify with HMAC-SHA256.
  390. """
  391. self.assertEqual(
  392. self.expected_signature_hmac_sha256,
  393. sign_hmac_sha256_with_client(self.eg_signature_base_string,
  394. self.hmac_client))
  395. self.assertTrue(verify_hmac_sha256(
  396. MockRequest('POST',
  397. 'http://example.com/request',
  398. self.eg_params,
  399. self.expected_signature_hmac_sha256),
  400. self.hmac_client.client_secret,
  401. self.hmac_client.resource_owner_secret))
  402. def test_sign_hmac_sha512_with_client(self):
  403. """
  404. Test sign and verify with HMAC-SHA512.
  405. """
  406. self.assertEqual(
  407. self.expected_signature_hmac_sha512,
  408. sign_hmac_sha512_with_client(self.eg_signature_base_string,
  409. self.hmac_client))
  410. self.assertTrue(verify_hmac_sha512(
  411. MockRequest('POST',
  412. 'http://example.com/request',
  413. self.eg_params,
  414. self.expected_signature_hmac_sha512),
  415. self.hmac_client.client_secret,
  416. self.hmac_client.resource_owner_secret))
  417. def test_hmac_false_positives(self):
  418. """
  419. Test verify_hmac-* functions will correctly detect invalid signatures.
  420. """
  421. _ros = self.hmac_client.resource_owner_secret
  422. for functions in [
  423. (sign_hmac_sha1_with_client, verify_hmac_sha1),
  424. (sign_hmac_sha256_with_client, verify_hmac_sha256),
  425. (sign_hmac_sha512_with_client, verify_hmac_sha512),
  426. ]:
  427. signing_function = functions[0]
  428. verify_function = functions[1]
  429. good_signature = \
  430. signing_function(
  431. self.eg_signature_base_string,
  432. self.hmac_client)
  433. bad_signature_on_different_value = \
  434. signing_function(
  435. 'not the signature base string',
  436. self.hmac_client)
  437. bad_signature_produced_by_different_client_secret = \
  438. signing_function(
  439. self.eg_signature_base_string,
  440. MockClient(client_secret='wrong-secret',
  441. resource_owner_secret=_ros))
  442. bad_signature_produced_by_different_resource_owner_secret = \
  443. signing_function(
  444. self.eg_signature_base_string,
  445. MockClient(client_secret=self.hmac_client.client_secret,
  446. resource_owner_secret='wrong-secret'))
  447. bad_signature_produced_with_no_resource_owner_secret = \
  448. signing_function(
  449. self.eg_signature_base_string,
  450. MockClient(client_secret=self.hmac_client.client_secret))
  451. bad_signature_produced_with_no_client_secret = \
  452. signing_function(
  453. self.eg_signature_base_string,
  454. MockClient(resource_owner_secret=_ros))
  455. self.assertTrue(verify_function(
  456. MockRequest('POST',
  457. 'http://example.com/request',
  458. self.eg_params,
  459. good_signature),
  460. self.hmac_client.client_secret,
  461. self.hmac_client.resource_owner_secret))
  462. for bad_signature in [
  463. '',
  464. 'ZG9uJ3QgdHJ1c3QgbWUK', # random base64 encoded value
  465. 'altérer', # value with a non-ASCII character in it
  466. bad_signature_on_different_value,
  467. bad_signature_produced_by_different_client_secret,
  468. bad_signature_produced_by_different_resource_owner_secret,
  469. bad_signature_produced_with_no_resource_owner_secret,
  470. bad_signature_produced_with_no_client_secret,
  471. ]:
  472. self.assertFalse(verify_function(
  473. MockRequest('POST',
  474. 'http://example.com/request',
  475. self.eg_params,
  476. bad_signature),
  477. self.hmac_client.client_secret,
  478. self.hmac_client.resource_owner_secret))
  479. # ==== RSA-based signature methods tests =========================
  480. rsa_private_client = MockClient(rsa_key='''
  481. -----BEGIN RSA PRIVATE KEY-----
  482. MIICXgIBAAKBgQDk1/bxyS8Q8jiheHeYYp/4rEKJopeQRRKKpZI4s5i+UPwVpupG
  483. AlwXWfzXwSMaKPAoKJNdu7tqKRniqst5uoHXw98gj0x7zamu0Ck1LtQ4c7pFMVah
  484. 5IYGhBi2E9ycNS329W27nJPWNCbESTu7snVlG8V8mfvGGg3xNjTMO7IdrwIDAQAB
  485. AoGBAOQ2KuH8S5+OrsL4K+wfjoCi6MfxCUyqVU9GxocdM1m30WyWRFMEz2nKJ8fR
  486. p3vTD4w8yplTOhcoXdQZl0kRoaDzrcYkm2VvJtQRrX7dKFT8dR8D/Tr7dNQLOXfC
  487. DY6xveQczE7qt7Vk7lp4FqmxBsaaEuokt78pOOjywZoInjZhAkEA9wz3zoZNT0/i
  488. rf6qv2qTIeieUB035N3dyw6f1BGSWYaXSuerDCD/J1qZbAPKKhyHZbVawFt3UMhe
  489. 542UftBaxQJBAO0iJy1I8GQjGnS7B3yvyH3CcLYGy296+XO/2xKp/d/ty1OIeovx
  490. C60pLNwuFNF3z9d2GVQAdoQ89hUkOtjZLeMCQQD0JO6oPHUeUjYT+T7ImAv7UKVT
  491. Suy30sKjLzqoGw1kR+wv7C5PeDRvscs4wa4CW9s6mjSrMDkDrmCLuJDtmf55AkEA
  492. kmaMg2PNrjUR51F0zOEFycaaqXbGcFwe1/xx9zLmHzMDXd4bsnwt9kk+fe0hQzVS
  493. JzatanQit3+feev1PN3QewJAWv4RZeavEUhKv+kLe95Yd0su7lTLVduVgh4v5yLT
  494. Ga6FHdjGPcfajt+nrpB1n8UQBEH9ZxniokR/IPvdMlxqXA==
  495. -----END RSA PRIVATE KEY-----
  496. ''')
  497. rsa_public_client = MockClient(rsa_key='''
  498. -----BEGIN RSA PUBLIC KEY-----
  499. MIGJAoGBAOTX9vHJLxDyOKF4d5hin/isQomil5BFEoqlkjizmL5Q/BWm6kYCXBdZ
  500. /NfBIxoo8Cgok127u2opGeKqy3m6gdfD3yCPTHvNqa7QKTUu1DhzukUxVqHkhgaE
  501. GLYT3Jw1Lfb1bbuck9Y0JsRJO7uydWUbxXyZ+8YaDfE2NMw7sh2vAgMBAAE=
  502. -----END RSA PUBLIC KEY-----
  503. ''')
  504. # The above private key was generated using:
  505. # $ openssl genrsa -out example.pvt 1024
  506. # $ chmod 600 example.pvt
  507. # Public key was extract from it using:
  508. # $ ssh-keygen -e -m pem -f example.pvt
  509. # PEM encoding requires the key to be concatenated with linebreaks.
  510. # The following expected signatures were calculated by putting the private
  511. # key in a file (test.pvt) and the value of sig_base_str_rsa in another file
  512. # ("base-str.txt") and running:
  513. #
  514. # echo -n `cat base-str.txt` | openssl dgst -sha1 -sign test.pvt| base64
  515. #
  516. # Note: the "echo -n" is needed to remove the last newline character, which
  517. # most text editors will add.
  518. expected_signature_rsa_sha1 = \
  519. 'mFY2KOEnlYWsTvUA+5kxuBIcvBYXu+ljw9ttVJQxKduMueGSVPCB1tK1PlqVLK738' \
  520. 'HK0t19ecBJfb6rMxUwrriw+MlBO+jpojkZIWccw1J4cAb4qu4M81DbpUAq4j/1w/Q' \
  521. 'yTR4TWCODlEfN7Zfgy8+pf+TjiXfIwRC1jEWbuL1E='
  522. expected_signature_rsa_sha256 = \
  523. 'jqKl6m0WS69tiVJV8ZQ6aQEfJqISoZkiPBXRv6Al2+iFSaDpfeXjYm+Hbx6m1azR' \
  524. 'drZ/35PM3cvuid3LwW/siAkzb0xQcGnTyAPH8YcGWzmnKGY7LsB7fkqThchNxvRK' \
  525. '/N7s9M1WMnfZZ+1dQbbwtTs1TG1+iexUcV7r3M7Heec='
  526. expected_signature_rsa_sha512 = \
  527. 'jL1CnjlsNd25qoZVHZ2oJft47IRYTjpF5CvCUjL3LY0NTnbEeVhE4amWXUFBe9GL' \
  528. 'DWdUh/79ZWNOrCirBFIP26cHLApjYdt4ZG7EVK0/GubS2v8wT1QPRsog8zyiMZkm' \
  529. 'g4JXdWCGXG8YRvRJTg+QKhXuXwS6TcMNakrgzgFIVhA='
  530. def test_sign_rsa_sha1_with_client(self):
  531. """
  532. Test sign and verify with RSA-SHA1.
  533. """
  534. self.assertEqual(
  535. self.expected_signature_rsa_sha1,
  536. sign_rsa_sha1_with_client(self.eg_signature_base_string,
  537. self.rsa_private_client))
  538. self.assertTrue(verify_rsa_sha1(
  539. MockRequest('POST',
  540. 'http://example.com/request',
  541. self.eg_params,
  542. self.expected_signature_rsa_sha1),
  543. self.rsa_public_client.rsa_key))
  544. def test_sign_rsa_sha256_with_client(self):
  545. """
  546. Test sign and verify with RSA-SHA256.
  547. """
  548. self.assertEqual(
  549. self.expected_signature_rsa_sha256,
  550. sign_rsa_sha256_with_client(self.eg_signature_base_string,
  551. self.rsa_private_client))
  552. self.assertTrue(verify_rsa_sha256(
  553. MockRequest('POST',
  554. 'http://example.com/request',
  555. self.eg_params,
  556. self.expected_signature_rsa_sha256),
  557. self.rsa_public_client.rsa_key))
  558. def test_sign_rsa_sha512_with_client(self):
  559. """
  560. Test sign and verify with RSA-SHA512.
  561. """
  562. self.assertEqual(
  563. self.expected_signature_rsa_sha512,
  564. sign_rsa_sha512_with_client(self.eg_signature_base_string,
  565. self.rsa_private_client))
  566. self.assertTrue(verify_rsa_sha512(
  567. MockRequest('POST',
  568. 'http://example.com/request',
  569. self.eg_params,
  570. self.expected_signature_rsa_sha512),
  571. self.rsa_public_client.rsa_key))
  572. def test_rsa_false_positives(self):
  573. """
  574. Test verify_rsa-* functions will correctly detect invalid signatures.
  575. """
  576. another_client = MockClient(rsa_key='''
  577. -----BEGIN RSA PRIVATE KEY-----
  578. MIICXQIBAAKBgQDZcD/1OZNJJ6Y3QZM16Z+O7fkD9kTIQuT2BfpAOUvDfxzYhVC9
  579. TNmSDHCQhr+ClutyolBk5jTE1/FXFUuHoPsTrkI7KQFXPP834D4gnSY9jrAiUJHe
  580. DVF6wXNuS7H4Ueh16YPjUxgLLRh/nn/JSEj98gsw+7DP01OWMfWS99S7eQIDAQAB
  581. AoGBALsQZRXVyK7BG7CiC8HwEcNnXDpaXmZjlpNKJTenk1THQMvONd4GBZAuf5D3
  582. PD9fE4R1u/ByVKecmBaxTV+L0TRQfD8K/nbQe0SKRQIkLI2ymLJKC/eyw5iTKT0E
  583. +BS6wYpVd+mfcqgvpHOYpUmz9X8k/eOa7uslFmvt+sDb5ZcBAkEA+++SRqqUxFEG
  584. s/ZWAKw9p5YgkeVUOYVUwyAeZ97heySrjVzg1nZ6v6kv7iOPi9KOEpaIGPW7x1K/
  585. uQuSt4YEqQJBANzyNqZTTPpv7b/R8ABFy0YMwPVNt3b1GOU1Xxl6iuhH2WcHuueo
  586. UB13JHoZCMZ7hsEqieEz6uteUjdRzRPKclECQFNhVK4iop3emzNQYeJTHwyp+RmQ
  587. JrHq2MTDioyiDUouNsDQbnFMQQ/RtNVB265Q/0hTnbN1ELLFRkK9+87VghECQQC9
  588. hacLFPk6+TffCp3sHfI3rEj4Iin1iFhKhHWGzW7JwJfjoOXaQK44GDLZ6Q918g+t
  589. MmgDHR2tt8KeYTSgfU+BAkBcaVF91EQ7VXhvyABNYjeYP7lU7orOgdWMa/zbLXSU
  590. 4vLsK1WOmwPY9zsXpPkilqszqcru4gzlG462cSbEdAW9
  591. -----END RSA PRIVATE KEY-----
  592. ''')
  593. for functions in [
  594. (sign_rsa_sha1_with_client, verify_rsa_sha1),
  595. (sign_rsa_sha256_with_client, verify_rsa_sha256),
  596. (sign_rsa_sha512_with_client, verify_rsa_sha512),
  597. ]:
  598. signing_function = functions[0]
  599. verify_function = functions[1]
  600. good_signature = \
  601. signing_function(self.eg_signature_base_string,
  602. self.rsa_private_client)
  603. bad_signature_on_different_value = \
  604. signing_function('wrong value signed', self.rsa_private_client)
  605. bad_signature_produced_by_different_private_key = \
  606. signing_function(self.eg_signature_base_string, another_client)
  607. self.assertTrue(verify_function(
  608. MockRequest('POST',
  609. 'http://example.com/request',
  610. self.eg_params,
  611. good_signature),
  612. self.rsa_public_client.rsa_key))
  613. for bad_signature in [
  614. '',
  615. 'ZG9uJ3QgdHJ1c3QgbWUK', # random base64 encoded value
  616. 'altérer', # value with a non-ASCII character in it
  617. bad_signature_on_different_value,
  618. bad_signature_produced_by_different_private_key,
  619. ]:
  620. self.assertFalse(verify_function(
  621. MockRequest('POST',
  622. 'http://example.com/request',
  623. self.eg_params,
  624. bad_signature),
  625. self.rsa_public_client.rsa_key))
  626. def test_rsa_bad_keys(self):
  627. """
  628. Testing RSA sign and verify with bad key values produces errors.
  629. This test is useful for coverage tests, since it runs the code branches
  630. that deal with error situations.
  631. """
  632. # Signing needs a private key
  633. for bad_value in [None, '', 'foobar']:
  634. self.assertRaises(ValueError,
  635. sign_rsa_sha1_with_client,
  636. self.eg_signature_base_string,
  637. MockClient(rsa_key=bad_value))
  638. self.assertRaises(AttributeError,
  639. sign_rsa_sha1_with_client,
  640. self.eg_signature_base_string,
  641. self.rsa_public_client) # public key doesn't sign
  642. # Verify needs a public key
  643. for bad_value in [None, '', 'foobar', self.rsa_private_client.rsa_key]:
  644. self.assertRaises(TypeError,
  645. verify_rsa_sha1,
  646. MockRequest('POST',
  647. 'http://example.com/request',
  648. self.eg_params,
  649. self.expected_signature_rsa_sha1),
  650. MockClient(rsa_key=bad_value))
  651. # For completeness, this text could repeat the above for RSA-SHA256 and
  652. # RSA-SHA512 signing and verification functions.
  653. def test_rsa_jwt_algorithm_cache(self):
  654. # Tests cache of RSAAlgorithm objects is implemented correctly.
  655. # This is difficult to test, since the cache is internal.
  656. #
  657. # Running this test with coverage will show the cache-hit branch of code
  658. # being executed by two signing operations with the same hash algorithm.
  659. self.test_sign_rsa_sha1_with_client() # creates cache entry
  660. self.test_sign_rsa_sha1_with_client() # reuses cache entry
  661. # Some possible bugs will be detected if multiple signing operations
  662. # with different hash algorithms produce the wrong results (e.g. if the
  663. # cache incorrectly returned the previously used algorithm, instead
  664. # of the one that is needed).
  665. self.test_sign_rsa_sha256_with_client()
  666. self.test_sign_rsa_sha256_with_client()
  667. self.test_sign_rsa_sha1_with_client()
  668. self.test_sign_rsa_sha256_with_client()
  669. self.test_sign_rsa_sha512_with_client()
  670. # ==== PLAINTEXT signature method tests ==========================
  671. plaintext_client = hmac_client # for convenience, use the same HMAC secrets
  672. expected_signature_plaintext = (
  673. 'ECrDNoq1VYzzzzzzzzzyAK7TwZNtPnkqatqZZZZ'
  674. '&'
  675. 'just-a-string%20%20%20%20asdasd')
  676. def test_sign_plaintext_with_client(self):
  677. # With PLAINTEXT, the "signature" is always the same: regardless of the
  678. # contents of the request. It is the concatenation of the encoded
  679. # client_secret, an ampersand, and the encoded resource_owner_secret.
  680. #
  681. # That is why the spaces in the resource owner secret are "%20".
  682. self.assertEqual(self.expected_signature_plaintext,
  683. sign_plaintext_with_client(None, # request is ignored
  684. self.plaintext_client))
  685. self.assertTrue(verify_plaintext(
  686. MockRequest('PUT',
  687. 'http://example.com/some-other-path',
  688. [('description', 'request is ignored in PLAINTEXT')],
  689. self.expected_signature_plaintext),
  690. self.plaintext_client.client_secret,
  691. self.plaintext_client.resource_owner_secret))
  692. def test_plaintext_false_positives(self):
  693. """
  694. Test verify_plaintext function will correctly detect invalid signatures.
  695. """
  696. _ros = self.plaintext_client.resource_owner_secret
  697. good_signature = \
  698. sign_plaintext_with_client(
  699. self.eg_signature_base_string,
  700. self.plaintext_client)
  701. bad_signature_produced_by_different_client_secret = \
  702. sign_plaintext_with_client(
  703. self.eg_signature_base_string,
  704. MockClient(client_secret='wrong-secret',
  705. resource_owner_secret=_ros))
  706. bad_signature_produced_by_different_resource_owner_secret = \
  707. sign_plaintext_with_client(
  708. self.eg_signature_base_string,
  709. MockClient(client_secret=self.plaintext_client.client_secret,
  710. resource_owner_secret='wrong-secret'))
  711. bad_signature_produced_with_no_resource_owner_secret = \
  712. sign_plaintext_with_client(
  713. self.eg_signature_base_string,
  714. MockClient(client_secret=self.plaintext_client.client_secret))
  715. bad_signature_produced_with_no_client_secret = \
  716. sign_plaintext_with_client(
  717. self.eg_signature_base_string,
  718. MockClient(resource_owner_secret=_ros))
  719. self.assertTrue(verify_plaintext(
  720. MockRequest('POST',
  721. 'http://example.com/request',
  722. self.eg_params,
  723. good_signature),
  724. self.plaintext_client.client_secret,
  725. self.plaintext_client.resource_owner_secret))
  726. for bad_signature in [
  727. '',
  728. 'ZG9uJ3QgdHJ1c3QgbWUK', # random base64 encoded value
  729. 'altérer', # value with a non-ASCII character in it
  730. bad_signature_produced_by_different_client_secret,
  731. bad_signature_produced_by_different_resource_owner_secret,
  732. bad_signature_produced_with_no_resource_owner_secret,
  733. bad_signature_produced_with_no_client_secret,
  734. ]:
  735. self.assertFalse(verify_plaintext(
  736. MockRequest('POST',
  737. 'http://example.com/request',
  738. self.eg_params,
  739. bad_signature),
  740. self.plaintext_client.client_secret,
  741. self.plaintext_client.resource_owner_secret))