signature.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.oauth1.rfc5849.signature
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This module represents a direct implementation of `section 3.4`_ of the spec.
  6. Terminology:
  7. * Client: software interfacing with an OAuth API
  8. * Server: the API provider
  9. * Resource Owner: the user who is granting authorization to the client
  10. Steps for signing a request:
  11. 1. Collect parameters from the uri query, auth header, & body
  12. 2. Normalize those parameters
  13. 3. Normalize the uri
  14. 4. Pass the normalized uri, normalized parameters, and http method to
  15. construct the base string
  16. 5. Pass the base string and any keys needed to a signing function
  17. .. _`section 3.4`: http://tools.ietf.org/html/rfc5849#section-3.4
  18. """
  19. from __future__ import absolute_import, unicode_literals
  20. import binascii
  21. import hashlib
  22. import hmac
  23. try:
  24. import urlparse
  25. except ImportError:
  26. import urllib.parse as urlparse
  27. from . import utils
  28. from oauthlib.common import urldecode, extract_params, safe_string_equals
  29. from oauthlib.common import bytes_type, unicode_type
  30. def construct_base_string(http_method, base_string_uri,
  31. normalized_encoded_request_parameters):
  32. """**String Construction**
  33. Per `section 3.4.1.1`_ of the spec.
  34. For example, the HTTP request::
  35. POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
  36. Host: example.com
  37. Content-Type: application/x-www-form-urlencoded
  38. Authorization: OAuth realm="Example",
  39. oauth_consumer_key="9djdj82h48djs9d2",
  40. oauth_token="kkk9d7dh3k39sjv7",
  41. oauth_signature_method="HMAC-SHA1",
  42. oauth_timestamp="137131201",
  43. oauth_nonce="7d8f3e4a",
  44. oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"
  45. c2&a3=2+q
  46. is represented by the following signature base string (line breaks
  47. are for display purposes only)::
  48. POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q
  49. %26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_
  50. key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m
  51. ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk
  52. 9d7dh3k39sjv7
  53. .. _`section 3.4.1.1`: http://tools.ietf.org/html/rfc5849#section-3.4.1.1
  54. """
  55. # The signature base string is constructed by concatenating together,
  56. # in order, the following HTTP request elements:
  57. # 1. The HTTP request method in uppercase. For example: "HEAD",
  58. # "GET", "POST", etc. If the request uses a custom HTTP method, it
  59. # MUST be encoded (`Section 3.6`_).
  60. #
  61. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  62. base_string = utils.escape(http_method.upper())
  63. # 2. An "&" character (ASCII code 38).
  64. base_string += '&'
  65. # 3. The base string URI from `Section 3.4.1.2`_, after being encoded
  66. # (`Section 3.6`_).
  67. #
  68. # .. _`Section 3.4.1.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.2
  69. # .. _`Section 3.4.6`: http://tools.ietf.org/html/rfc5849#section-3.4.6
  70. base_string += utils.escape(base_string_uri)
  71. # 4. An "&" character (ASCII code 38).
  72. base_string += '&'
  73. # 5. The request parameters as normalized in `Section 3.4.1.3.2`_, after
  74. # being encoded (`Section 3.6`).
  75. #
  76. # .. _`Section 3.4.1.3.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.2
  77. # .. _`Section 3.4.6`: http://tools.ietf.org/html/rfc5849#section-3.4.6
  78. base_string += utils.escape(normalized_encoded_request_parameters)
  79. return base_string
  80. def normalize_base_string_uri(uri, host=None):
  81. """**Base String URI**
  82. Per `section 3.4.1.2`_ of the spec.
  83. For example, the HTTP request::
  84. GET /r%20v/X?id=123 HTTP/1.1
  85. Host: EXAMPLE.COM:80
  86. is represented by the base string URI: "http://example.com/r%20v/X".
  87. In another example, the HTTPS request::
  88. GET /?q=1 HTTP/1.1
  89. Host: www.example.net:8080
  90. is represented by the base string URI: "https://www.example.net:8080/".
  91. .. _`section 3.4.1.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.2
  92. The host argument overrides the netloc part of the uri argument.
  93. """
  94. if not isinstance(uri, unicode_type):
  95. raise ValueError('uri must be a unicode object.')
  96. # FIXME: urlparse does not support unicode
  97. scheme, netloc, path, params, query, fragment = urlparse.urlparse(uri)
  98. # The scheme, authority, and path of the request resource URI `RFC3986`
  99. # are included by constructing an "http" or "https" URI representing
  100. # the request resource (without the query or fragment) as follows:
  101. #
  102. # .. _`RFC3986`: http://tools.ietf.org/html/rfc3986
  103. if not scheme or not netloc:
  104. raise ValueError('uri must include a scheme and netloc')
  105. # Per `RFC 2616 section 5.1.2`_:
  106. #
  107. # Note that the absolute path cannot be empty; if none is present in
  108. # the original URI, it MUST be given as "/" (the server root).
  109. #
  110. # .. _`RFC 2616 section 5.1.2`: http://tools.ietf.org/html/rfc2616#section-5.1.2
  111. if not path:
  112. path = '/'
  113. # 1. The scheme and host MUST be in lowercase.
  114. scheme = scheme.lower()
  115. netloc = netloc.lower()
  116. # 2. The host and port values MUST match the content of the HTTP
  117. # request "Host" header field.
  118. if host is not None:
  119. netloc = host.lower()
  120. # 3. The port MUST be included if it is not the default port for the
  121. # scheme, and MUST be excluded if it is the default. Specifically,
  122. # the port MUST be excluded when making an HTTP request `RFC2616`_
  123. # to port 80 or when making an HTTPS request `RFC2818`_ to port 443.
  124. # All other non-default port numbers MUST be included.
  125. #
  126. # .. _`RFC2616`: http://tools.ietf.org/html/rfc2616
  127. # .. _`RFC2818`: http://tools.ietf.org/html/rfc2818
  128. default_ports = (
  129. ('http', '80'),
  130. ('https', '443'),
  131. )
  132. if ':' in netloc:
  133. host, port = netloc.split(':', 1)
  134. if (scheme, port) in default_ports:
  135. netloc = host
  136. return urlparse.urlunparse((scheme, netloc, path, params, '', ''))
  137. # ** Request Parameters **
  138. #
  139. # Per `section 3.4.1.3`_ of the spec.
  140. #
  141. # In order to guarantee a consistent and reproducible representation of
  142. # the request parameters, the parameters are collected and decoded to
  143. # their original decoded form. They are then sorted and encoded in a
  144. # particular manner that is often different from their original
  145. # encoding scheme, and concatenated into a single string.
  146. #
  147. # .. _`section 3.4.1.3`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3
  148. def collect_parameters(uri_query='', body=[], headers=None,
  149. exclude_oauth_signature=True, with_realm=False):
  150. """**Parameter Sources**
  151. Parameters starting with `oauth_` will be unescaped.
  152. Body parameters must be supplied as a dict, a list of 2-tuples, or a
  153. formencoded query string.
  154. Headers must be supplied as a dict.
  155. Per `section 3.4.1.3.1`_ of the spec.
  156. For example, the HTTP request::
  157. POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
  158. Host: example.com
  159. Content-Type: application/x-www-form-urlencoded
  160. Authorization: OAuth realm="Example",
  161. oauth_consumer_key="9djdj82h48djs9d2",
  162. oauth_token="kkk9d7dh3k39sjv7",
  163. oauth_signature_method="HMAC-SHA1",
  164. oauth_timestamp="137131201",
  165. oauth_nonce="7d8f3e4a",
  166. oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D"
  167. c2&a3=2+q
  168. contains the following (fully decoded) parameters used in the
  169. signature base sting::
  170. +------------------------+------------------+
  171. | Name | Value |
  172. +------------------------+------------------+
  173. | b5 | =%3D |
  174. | a3 | a |
  175. | c@ | |
  176. | a2 | r b |
  177. | oauth_consumer_key | 9djdj82h48djs9d2 |
  178. | oauth_token | kkk9d7dh3k39sjv7 |
  179. | oauth_signature_method | HMAC-SHA1 |
  180. | oauth_timestamp | 137131201 |
  181. | oauth_nonce | 7d8f3e4a |
  182. | c2 | |
  183. | a3 | 2 q |
  184. +------------------------+------------------+
  185. Note that the value of "b5" is "=%3D" and not "==". Both "c@" and
  186. "c2" have empty values. While the encoding rules specified in this
  187. specification for the purpose of constructing the signature base
  188. string exclude the use of a "+" character (ASCII code 43) to
  189. represent an encoded space character (ASCII code 32), this practice
  190. is widely used in "application/x-www-form-urlencoded" encoded values,
  191. and MUST be properly decoded, as demonstrated by one of the "a3"
  192. parameter instances (the "a3" parameter is used twice in this
  193. request).
  194. .. _`section 3.4.1.3.1`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
  195. """
  196. headers = headers or {}
  197. params = []
  198. # The parameters from the following sources are collected into a single
  199. # list of name/value pairs:
  200. # * The query component of the HTTP request URI as defined by
  201. # `RFC3986, Section 3.4`_. The query component is parsed into a list
  202. # of name/value pairs by treating it as an
  203. # "application/x-www-form-urlencoded" string, separating the names
  204. # and values and decoding them as defined by
  205. # `W3C.REC-html40-19980424`_, Section 17.13.4.
  206. #
  207. # .. _`RFC3986, Section 3.4`: http://tools.ietf.org/html/rfc3986#section-3.4
  208. # .. _`W3C.REC-html40-19980424`: http://tools.ietf.org/html/rfc5849#ref-W3C.REC-html40-19980424
  209. if uri_query:
  210. params.extend(urldecode(uri_query))
  211. # * The OAuth HTTP "Authorization" header field (`Section 3.5.1`_) if
  212. # present. The header's content is parsed into a list of name/value
  213. # pairs excluding the "realm" parameter if present. The parameter
  214. # values are decoded as defined by `Section 3.5.1`_.
  215. #
  216. # .. _`Section 3.5.1`: http://tools.ietf.org/html/rfc5849#section-3.5.1
  217. if headers:
  218. headers_lower = dict((k.lower(), v) for k, v in headers.items())
  219. authorization_header = headers_lower.get('authorization')
  220. if authorization_header is not None:
  221. params.extend([i for i in utils.parse_authorization_header(
  222. authorization_header) if with_realm or i[0] != 'realm'])
  223. # * The HTTP request entity-body, but only if all of the following
  224. # conditions are met:
  225. # * The entity-body is single-part.
  226. #
  227. # * The entity-body follows the encoding requirements of the
  228. # "application/x-www-form-urlencoded" content-type as defined by
  229. # `W3C.REC-html40-19980424`_.
  230. # * The HTTP request entity-header includes the "Content-Type"
  231. # header field set to "application/x-www-form-urlencoded".
  232. #
  233. # .._`W3C.REC-html40-19980424`: http://tools.ietf.org/html/rfc5849#ref-W3C.REC-html40-19980424
  234. # TODO: enforce header param inclusion conditions
  235. bodyparams = extract_params(body) or []
  236. params.extend(bodyparams)
  237. # ensure all oauth params are unescaped
  238. unescaped_params = []
  239. for k, v in params:
  240. if k.startswith('oauth_'):
  241. v = utils.unescape(v)
  242. unescaped_params.append((k, v))
  243. # The "oauth_signature" parameter MUST be excluded from the signature
  244. # base string if present.
  245. if exclude_oauth_signature:
  246. unescaped_params = list(filter(lambda i: i[0] != 'oauth_signature',
  247. unescaped_params))
  248. return unescaped_params
  249. def normalize_parameters(params):
  250. """**Parameters Normalization**
  251. Per `section 3.4.1.3.2`_ of the spec.
  252. For example, the list of parameters from the previous section would
  253. be normalized as follows:
  254. Encoded::
  255. +------------------------+------------------+
  256. | Name | Value |
  257. +------------------------+------------------+
  258. | b5 | %3D%253D |
  259. | a3 | a |
  260. | c%40 | |
  261. | a2 | r%20b |
  262. | oauth_consumer_key | 9djdj82h48djs9d2 |
  263. | oauth_token | kkk9d7dh3k39sjv7 |
  264. | oauth_signature_method | HMAC-SHA1 |
  265. | oauth_timestamp | 137131201 |
  266. | oauth_nonce | 7d8f3e4a |
  267. | c2 | |
  268. | a3 | 2%20q |
  269. +------------------------+------------------+
  270. Sorted::
  271. +------------------------+------------------+
  272. | Name | Value |
  273. +------------------------+------------------+
  274. | a2 | r%20b |
  275. | a3 | 2%20q |
  276. | a3 | a |
  277. | b5 | %3D%253D |
  278. | c%40 | |
  279. | c2 | |
  280. | oauth_consumer_key | 9djdj82h48djs9d2 |
  281. | oauth_nonce | 7d8f3e4a |
  282. | oauth_signature_method | HMAC-SHA1 |
  283. | oauth_timestamp | 137131201 |
  284. | oauth_token | kkk9d7dh3k39sjv7 |
  285. +------------------------+------------------+
  286. Concatenated Pairs::
  287. +-------------------------------------+
  288. | Name=Value |
  289. +-------------------------------------+
  290. | a2=r%20b |
  291. | a3=2%20q |
  292. | a3=a |
  293. | b5=%3D%253D |
  294. | c%40= |
  295. | c2= |
  296. | oauth_consumer_key=9djdj82h48djs9d2 |
  297. | oauth_nonce=7d8f3e4a |
  298. | oauth_signature_method=HMAC-SHA1 |
  299. | oauth_timestamp=137131201 |
  300. | oauth_token=kkk9d7dh3k39sjv7 |
  301. +-------------------------------------+
  302. and concatenated together into a single string (line breaks are for
  303. display purposes only)::
  304. a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj
  305. dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1
  306. &oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7
  307. .. _`section 3.4.1.3.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.2
  308. """
  309. # The parameters collected in `Section 3.4.1.3`_ are normalized into a
  310. # single string as follows:
  311. #
  312. # .. _`Section 3.4.1.3`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3
  313. # 1. First, the name and value of each parameter are encoded
  314. # (`Section 3.6`_).
  315. #
  316. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  317. key_values = [(utils.escape(k), utils.escape(v)) for k, v in params]
  318. # 2. The parameters are sorted by name, using ascending byte value
  319. # ordering. If two or more parameters share the same name, they
  320. # are sorted by their value.
  321. key_values.sort()
  322. # 3. The name of each parameter is concatenated to its corresponding
  323. # value using an "=" character (ASCII code 61) as a separator, even
  324. # if the value is empty.
  325. parameter_parts = ['{0}={1}'.format(k, v) for k, v in key_values]
  326. # 4. The sorted name/value pairs are concatenated together into a
  327. # single string by using an "&" character (ASCII code 38) as
  328. # separator.
  329. return '&'.join(parameter_parts)
  330. def sign_hmac_sha1_with_client(base_string, client):
  331. return sign_hmac_sha1(base_string,
  332. client.client_secret,
  333. client.resource_owner_secret
  334. )
  335. def sign_hmac_sha1(base_string, client_secret, resource_owner_secret):
  336. """**HMAC-SHA1**
  337. The "HMAC-SHA1" signature method uses the HMAC-SHA1 signature
  338. algorithm as defined in `RFC2104`_::
  339. digest = HMAC-SHA1 (key, text)
  340. Per `section 3.4.2`_ of the spec.
  341. .. _`RFC2104`: http://tools.ietf.org/html/rfc2104
  342. .. _`section 3.4.2`: http://tools.ietf.org/html/rfc5849#section-3.4.2
  343. """
  344. # The HMAC-SHA1 function variables are used in following way:
  345. # text is set to the value of the signature base string from
  346. # `Section 3.4.1.1`_.
  347. #
  348. # .. _`Section 3.4.1.1`: http://tools.ietf.org/html/rfc5849#section-3.4.1.1
  349. text = base_string
  350. # key is set to the concatenated values of:
  351. # 1. The client shared-secret, after being encoded (`Section 3.6`_).
  352. #
  353. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  354. key = utils.escape(client_secret or '')
  355. # 2. An "&" character (ASCII code 38), which MUST be included
  356. # even when either secret is empty.
  357. key += '&'
  358. # 3. The token shared-secret, after being encoded (`Section 3.6`_).
  359. #
  360. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  361. key += utils.escape(resource_owner_secret or '')
  362. # FIXME: HMAC does not support unicode!
  363. key_utf8 = key.encode('utf-8')
  364. text_utf8 = text.encode('utf-8')
  365. signature = hmac.new(key_utf8, text_utf8, hashlib.sha1)
  366. # digest is used to set the value of the "oauth_signature" protocol
  367. # parameter, after the result octet string is base64-encoded
  368. # per `RFC2045, Section 6.8`.
  369. #
  370. # .. _`RFC2045, Section 6.8`: http://tools.ietf.org/html/rfc2045#section-6.8
  371. return binascii.b2a_base64(signature.digest())[:-1].decode('utf-8')
  372. _jwtrs1 = None
  373. #jwt has some nice pycrypto/cryptography abstractions
  374. def _jwt_rs1_signing_algorithm():
  375. global _jwtrs1
  376. if _jwtrs1 is None:
  377. import jwt.algorithms as jwtalgo
  378. _jwtrs1 = jwtalgo.RSAAlgorithm(jwtalgo.hashes.SHA1)
  379. return _jwtrs1
  380. def sign_rsa_sha1(base_string, rsa_private_key):
  381. """**RSA-SHA1**
  382. Per `section 3.4.3`_ of the spec.
  383. The "RSA-SHA1" signature method uses the RSASSA-PKCS1-v1_5 signature
  384. algorithm as defined in `RFC3447, Section 8.2`_ (also known as
  385. PKCS#1), using SHA-1 as the hash function for EMSA-PKCS1-v1_5. To
  386. use this method, the client MUST have established client credentials
  387. with the server that included its RSA public key (in a manner that is
  388. beyond the scope of this specification).
  389. .. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3
  390. .. _`RFC3447, Section 8.2`: http://tools.ietf.org/html/rfc3447#section-8.2
  391. """
  392. if isinstance(base_string, unicode_type):
  393. base_string = base_string.encode('utf-8')
  394. # TODO: finish RSA documentation
  395. alg = _jwt_rs1_signing_algorithm()
  396. key = _prepare_key_plus(alg, rsa_private_key)
  397. s=alg.sign(base_string, key)
  398. return binascii.b2a_base64(s)[:-1].decode('utf-8')
  399. def sign_rsa_sha1_with_client(base_string, client):
  400. if not client.rsa_key:
  401. raise ValueError('rsa_key is required when using RSA signature method.')
  402. return sign_rsa_sha1(base_string, client.rsa_key)
  403. def sign_plaintext(client_secret, resource_owner_secret):
  404. """Sign a request using plaintext.
  405. Per `section 3.4.4`_ of the spec.
  406. The "PLAINTEXT" method does not employ a signature algorithm. It
  407. MUST be used with a transport-layer mechanism such as TLS or SSL (or
  408. sent over a secure channel with equivalent protections). It does not
  409. utilize the signature base string or the "oauth_timestamp" and
  410. "oauth_nonce" parameters.
  411. .. _`section 3.4.4`: http://tools.ietf.org/html/rfc5849#section-3.4.4
  412. """
  413. # The "oauth_signature" protocol parameter is set to the concatenated
  414. # value of:
  415. # 1. The client shared-secret, after being encoded (`Section 3.6`_).
  416. #
  417. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  418. signature = utils.escape(client_secret or '')
  419. # 2. An "&" character (ASCII code 38), which MUST be included even
  420. # when either secret is empty.
  421. signature += '&'
  422. # 3. The token shared-secret, after being encoded (`Section 3.6`_).
  423. #
  424. # .. _`Section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
  425. signature += utils.escape(resource_owner_secret or '')
  426. return signature
  427. def sign_plaintext_with_client(base_string, client):
  428. return sign_plaintext(client.client_secret, client.resource_owner_secret)
  429. def verify_hmac_sha1(request, client_secret=None,
  430. resource_owner_secret=None):
  431. """Verify a HMAC-SHA1 signature.
  432. Per `section 3.4`_ of the spec.
  433. .. _`section 3.4`: http://tools.ietf.org/html/rfc5849#section-3.4
  434. To satisfy `RFC2616 section 5.2`_ item 1, the request argument's uri
  435. attribute MUST be an absolute URI whose netloc part identifies the
  436. origin server or gateway on which the resource resides. Any Host
  437. item of the request argument's headers dict attribute will be
  438. ignored.
  439. .. _`RFC2616 section 5.2`: http://tools.ietf.org/html/rfc2616#section-5.2
  440. """
  441. norm_params = normalize_parameters(request.params)
  442. uri = normalize_base_string_uri(request.uri)
  443. base_string = construct_base_string(request.http_method, uri, norm_params)
  444. signature = sign_hmac_sha1(base_string, client_secret,
  445. resource_owner_secret)
  446. return safe_string_equals(signature, request.signature)
  447. def _prepare_key_plus(alg, keystr):
  448. if isinstance(keystr, bytes_type):
  449. keystr = keystr.decode('utf-8')
  450. return alg.prepare_key(keystr)
  451. def verify_rsa_sha1(request, rsa_public_key):
  452. """Verify a RSASSA-PKCS #1 v1.5 base64 encoded signature.
  453. Per `section 3.4.3`_ of the spec.
  454. Note this method requires the jwt and cryptography libraries.
  455. .. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3
  456. To satisfy `RFC2616 section 5.2`_ item 1, the request argument's uri
  457. attribute MUST be an absolute URI whose netloc part identifies the
  458. origin server or gateway on which the resource resides. Any Host
  459. item of the request argument's headers dict attribute will be
  460. ignored.
  461. .. _`RFC2616 section 5.2`: http://tools.ietf.org/html/rfc2616#section-5.2
  462. """
  463. norm_params = normalize_parameters(request.params)
  464. uri = normalize_base_string_uri(request.uri)
  465. message = construct_base_string(request.http_method, uri, norm_params).encode('utf-8')
  466. sig = binascii.a2b_base64(request.signature.encode('utf-8'))
  467. alg = _jwt_rs1_signing_algorithm()
  468. key = _prepare_key_plus(alg, rsa_public_key)
  469. return alg.verify(message, key, sig)
  470. def verify_plaintext(request, client_secret=None, resource_owner_secret=None):
  471. """Verify a PLAINTEXT signature.
  472. Per `section 3.4`_ of the spec.
  473. .. _`section 3.4`: http://tools.ietf.org/html/rfc5849#section-3.4
  474. """
  475. signature = sign_plaintext(client_secret, resource_owner_secret)
  476. return safe_string_equals(signature, request.signature)