request_validator.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. """
  2. oauthlib.oauth1.rfc5849
  3. ~~~~~~~~~~~~~~
  4. This module is an implementation of various logic needed
  5. for signing and checking OAuth 1.0 RFC 5849 requests.
  6. """
  7. from . import SIGNATURE_METHODS, utils
  8. class RequestValidator:
  9. """A validator/datastore interaction base class for OAuth 1 providers.
  10. OAuth providers should inherit from RequestValidator and implement the
  11. methods and properties outlined below. Further details are provided in the
  12. documentation for each method and property.
  13. Methods used to check the format of input parameters. Common tests include
  14. length, character set, membership, range or pattern. These tests are
  15. referred to as `whitelisting or blacklisting`_. Whitelisting is better
  16. but blacklisting can be useful to spot malicious activity.
  17. The following have methods a default implementation:
  18. - check_client_key
  19. - check_request_token
  20. - check_access_token
  21. - check_nonce
  22. - check_verifier
  23. - check_realms
  24. The methods above default to whitelist input parameters, checking that they
  25. are alphanumerical and between a minimum and maximum length. Rather than
  26. overloading the methods a few properties can be used to configure these
  27. methods.
  28. * @safe_characters -> (character set)
  29. * @client_key_length -> (min, max)
  30. * @request_token_length -> (min, max)
  31. * @access_token_length -> (min, max)
  32. * @nonce_length -> (min, max)
  33. * @verifier_length -> (min, max)
  34. * @realms -> [list, of, realms]
  35. Methods used to validate/invalidate input parameters. These checks usually
  36. hit either persistent or temporary storage such as databases or the
  37. filesystem. See each methods documentation for detailed usage.
  38. The following methods must be implemented:
  39. - validate_client_key
  40. - validate_request_token
  41. - validate_access_token
  42. - validate_timestamp_and_nonce
  43. - validate_redirect_uri
  44. - validate_requested_realms
  45. - validate_realms
  46. - validate_verifier
  47. - invalidate_request_token
  48. Methods used to retrieve sensitive information from storage.
  49. The following methods must be implemented:
  50. - get_client_secret
  51. - get_request_token_secret
  52. - get_access_token_secret
  53. - get_rsa_key
  54. - get_realms
  55. - get_default_realms
  56. - get_redirect_uri
  57. Methods used to save credentials.
  58. The following methods must be implemented:
  59. - save_request_token
  60. - save_verifier
  61. - save_access_token
  62. Methods used to verify input parameters. This methods are used during
  63. authorizing request token by user (AuthorizationEndpoint), to check if
  64. parameters are valid. During token authorization request is not signed,
  65. thus 'validation' methods can not be used. The following methods must be
  66. implemented:
  67. - verify_realms
  68. - verify_request_token
  69. To prevent timing attacks it is necessary to not exit early even if the
  70. client key or resource owner key is invalid. Instead dummy values should
  71. be used during the remaining verification process. It is very important
  72. that the dummy client and token are valid input parameters to the methods
  73. get_client_secret, get_rsa_key and get_(access/request)_token_secret and
  74. that the running time of those methods when given a dummy value remain
  75. equivalent to the running time when given a valid client/resource owner.
  76. The following properties must be implemented:
  77. * @dummy_client
  78. * @dummy_request_token
  79. * @dummy_access_token
  80. Example implementations have been provided, note that the database used is
  81. a simple dictionary and serves only an illustrative purpose. Use whichever
  82. database suits your project and how to access it is entirely up to you.
  83. The methods are introduced in an order which should make understanding
  84. their use more straightforward and as such it could be worth reading what
  85. follows in chronological order.
  86. .. _`whitelisting or blacklisting`: https://www.schneier.com/blog/archives/2011/01/whitelisting_vs.html
  87. """
  88. def __init__(self):
  89. pass
  90. @property
  91. def allowed_signature_methods(self):
  92. return SIGNATURE_METHODS
  93. @property
  94. def safe_characters(self):
  95. return set(utils.UNICODE_ASCII_CHARACTER_SET)
  96. @property
  97. def client_key_length(self):
  98. return 20, 30
  99. @property
  100. def request_token_length(self):
  101. return 20, 30
  102. @property
  103. def access_token_length(self):
  104. return 20, 30
  105. @property
  106. def timestamp_lifetime(self):
  107. return 600
  108. @property
  109. def nonce_length(self):
  110. return 20, 30
  111. @property
  112. def verifier_length(self):
  113. return 20, 30
  114. @property
  115. def realms(self):
  116. return []
  117. @property
  118. def enforce_ssl(self):
  119. return True
  120. def check_client_key(self, client_key):
  121. """Check that the client key only contains safe characters
  122. and is no shorter than lower and no longer than upper.
  123. """
  124. lower, upper = self.client_key_length
  125. return (set(client_key) <= self.safe_characters and
  126. lower <= len(client_key) <= upper)
  127. def check_request_token(self, request_token):
  128. """Checks that the request token contains only safe characters
  129. and is no shorter than lower and no longer than upper.
  130. """
  131. lower, upper = self.request_token_length
  132. return (set(request_token) <= self.safe_characters and
  133. lower <= len(request_token) <= upper)
  134. def check_access_token(self, request_token):
  135. """Checks that the token contains only safe characters
  136. and is no shorter than lower and no longer than upper.
  137. """
  138. lower, upper = self.access_token_length
  139. return (set(request_token) <= self.safe_characters and
  140. lower <= len(request_token) <= upper)
  141. def check_nonce(self, nonce):
  142. """Checks that the nonce only contains only safe characters
  143. and is no shorter than lower and no longer than upper.
  144. """
  145. lower, upper = self.nonce_length
  146. return (set(nonce) <= self.safe_characters and
  147. lower <= len(nonce) <= upper)
  148. def check_verifier(self, verifier):
  149. """Checks that the verifier contains only safe characters
  150. and is no shorter than lower and no longer than upper.
  151. """
  152. lower, upper = self.verifier_length
  153. return (set(verifier) <= self.safe_characters and
  154. lower <= len(verifier) <= upper)
  155. def check_realms(self, realms):
  156. """Check that the realm is one of a set allowed realms."""
  157. return all(r in self.realms for r in realms)
  158. def _subclass_must_implement(self, fn):
  159. """
  160. Returns a NotImplementedError for a function that should be implemented.
  161. :param fn: name of the function
  162. """
  163. m = "Missing function implementation in {}: {}".format(type(self), fn)
  164. return NotImplementedError(m)
  165. @property
  166. def dummy_client(self):
  167. """Dummy client used when an invalid client key is supplied.
  168. :returns: The dummy client key string.
  169. The dummy client should be associated with either a client secret,
  170. a rsa key or both depending on which signature methods are supported.
  171. Providers should make sure that
  172. get_client_secret(dummy_client)
  173. get_rsa_key(dummy_client)
  174. return a valid secret or key for the dummy client.
  175. This method is used by
  176. * AccessTokenEndpoint
  177. * RequestTokenEndpoint
  178. * ResourceEndpoint
  179. * SignatureOnlyEndpoint
  180. """
  181. raise self._subclass_must_implement("dummy_client")
  182. @property
  183. def dummy_request_token(self):
  184. """Dummy request token used when an invalid token was supplied.
  185. :returns: The dummy request token string.
  186. The dummy request token should be associated with a request token
  187. secret such that get_request_token_secret(.., dummy_request_token)
  188. returns a valid secret.
  189. This method is used by
  190. * AccessTokenEndpoint
  191. """
  192. raise self._subclass_must_implement("dummy_request_token")
  193. @property
  194. def dummy_access_token(self):
  195. """Dummy access token used when an invalid token was supplied.
  196. :returns: The dummy access token string.
  197. The dummy access token should be associated with an access token
  198. secret such that get_access_token_secret(.., dummy_access_token)
  199. returns a valid secret.
  200. This method is used by
  201. * ResourceEndpoint
  202. """
  203. raise self._subclass_must_implement("dummy_access_token")
  204. def get_client_secret(self, client_key, request):
  205. """Retrieves the client secret associated with the client key.
  206. :param client_key: The client/consumer key.
  207. :param request: OAuthlib request.
  208. :type request: oauthlib.common.Request
  209. :returns: The client secret as a string.
  210. This method must allow the use of a dummy client_key value.
  211. Fetching the secret using the dummy key must take the same amount of
  212. time as fetching a secret for a valid client::
  213. # Unlikely to be near constant time as it uses two database
  214. # lookups for a valid client, and only one for an invalid.
  215. from your_datastore import ClientSecret
  216. if ClientSecret.has(client_key):
  217. return ClientSecret.get(client_key)
  218. else:
  219. return 'dummy'
  220. # Aim to mimic number of latency inducing operations no matter
  221. # whether the client is valid or not.
  222. from your_datastore import ClientSecret
  223. return ClientSecret.get(client_key, 'dummy')
  224. Note that the returned key must be in plaintext.
  225. This method is used by
  226. * AccessTokenEndpoint
  227. * RequestTokenEndpoint
  228. * ResourceEndpoint
  229. * SignatureOnlyEndpoint
  230. """
  231. raise self._subclass_must_implement('get_client_secret')
  232. def get_request_token_secret(self, client_key, token, request):
  233. """Retrieves the shared secret associated with the request token.
  234. :param client_key: The client/consumer key.
  235. :param token: The request token string.
  236. :param request: OAuthlib request.
  237. :type request: oauthlib.common.Request
  238. :returns: The token secret as a string.
  239. This method must allow the use of a dummy values and the running time
  240. must be roughly equivalent to that of the running time of valid values::
  241. # Unlikely to be near constant time as it uses two database
  242. # lookups for a valid client, and only one for an invalid.
  243. from your_datastore import RequestTokenSecret
  244. if RequestTokenSecret.has(client_key):
  245. return RequestTokenSecret.get((client_key, request_token))
  246. else:
  247. return 'dummy'
  248. # Aim to mimic number of latency inducing operations no matter
  249. # whether the client is valid or not.
  250. from your_datastore import RequestTokenSecret
  251. return ClientSecret.get((client_key, request_token), 'dummy')
  252. Note that the returned key must be in plaintext.
  253. This method is used by
  254. * AccessTokenEndpoint
  255. """
  256. raise self._subclass_must_implement('get_request_token_secret')
  257. def get_access_token_secret(self, client_key, token, request):
  258. """Retrieves the shared secret associated with the access token.
  259. :param client_key: The client/consumer key.
  260. :param token: The access token string.
  261. :param request: OAuthlib request.
  262. :type request: oauthlib.common.Request
  263. :returns: The token secret as a string.
  264. This method must allow the use of a dummy values and the running time
  265. must be roughly equivalent to that of the running time of valid values::
  266. # Unlikely to be near constant time as it uses two database
  267. # lookups for a valid client, and only one for an invalid.
  268. from your_datastore import AccessTokenSecret
  269. if AccessTokenSecret.has(client_key):
  270. return AccessTokenSecret.get((client_key, request_token))
  271. else:
  272. return 'dummy'
  273. # Aim to mimic number of latency inducing operations no matter
  274. # whether the client is valid or not.
  275. from your_datastore import AccessTokenSecret
  276. return ClientSecret.get((client_key, request_token), 'dummy')
  277. Note that the returned key must be in plaintext.
  278. This method is used by
  279. * ResourceEndpoint
  280. """
  281. raise self._subclass_must_implement("get_access_token_secret")
  282. def get_default_realms(self, client_key, request):
  283. """Get the default realms for a client.
  284. :param client_key: The client/consumer key.
  285. :param request: OAuthlib request.
  286. :type request: oauthlib.common.Request
  287. :returns: The list of default realms associated with the client.
  288. The list of default realms will be set during client registration and
  289. is outside the scope of OAuthLib.
  290. This method is used by
  291. * RequestTokenEndpoint
  292. """
  293. raise self._subclass_must_implement("get_default_realms")
  294. def get_realms(self, token, request):
  295. """Get realms associated with a request token.
  296. :param token: The request token string.
  297. :param request: OAuthlib request.
  298. :type request: oauthlib.common.Request
  299. :returns: The list of realms associated with the request token.
  300. This method is used by
  301. * AuthorizationEndpoint
  302. * AccessTokenEndpoint
  303. """
  304. raise self._subclass_must_implement("get_realms")
  305. def get_redirect_uri(self, token, request):
  306. """Get the redirect URI associated with a request token.
  307. :param token: The request token string.
  308. :param request: OAuthlib request.
  309. :type request: oauthlib.common.Request
  310. :returns: The redirect URI associated with the request token.
  311. It may be desirable to return a custom URI if the redirect is set to "oob".
  312. In this case, the user will be redirected to the returned URI and at that
  313. endpoint the verifier can be displayed.
  314. This method is used by
  315. * AuthorizationEndpoint
  316. """
  317. raise self._subclass_must_implement("get_redirect_uri")
  318. def get_rsa_key(self, client_key, request):
  319. """Retrieves a previously stored client provided RSA key.
  320. :param client_key: The client/consumer key.
  321. :param request: OAuthlib request.
  322. :type request: oauthlib.common.Request
  323. :returns: The rsa public key as a string.
  324. This method must allow the use of a dummy client_key value. Fetching
  325. the rsa key using the dummy key must take the same amount of time
  326. as fetching a key for a valid client. The dummy key must also be of
  327. the same bit length as client keys.
  328. Note that the key must be returned in plaintext.
  329. This method is used by
  330. * AccessTokenEndpoint
  331. * RequestTokenEndpoint
  332. * ResourceEndpoint
  333. * SignatureOnlyEndpoint
  334. """
  335. raise self._subclass_must_implement("get_rsa_key")
  336. def invalidate_request_token(self, client_key, request_token, request):
  337. """Invalidates a used request token.
  338. :param client_key: The client/consumer key.
  339. :param request_token: The request token string.
  340. :param request: OAuthlib request.
  341. :type request: oauthlib.common.Request
  342. :returns: None
  343. Per `Section 2.3`_ of the spec:
  344. "The server MUST (...) ensure that the temporary
  345. credentials have not expired or been used before."
  346. .. _`Section 2.3`: https://tools.ietf.org/html/rfc5849#section-2.3
  347. This method should ensure that provided token won't validate anymore.
  348. It can be simply removing RequestToken from storage or setting
  349. specific flag that makes it invalid (note that such flag should be
  350. also validated during request token validation).
  351. This method is used by
  352. * AccessTokenEndpoint
  353. """
  354. raise self._subclass_must_implement("invalidate_request_token")
  355. def validate_client_key(self, client_key, request):
  356. """Validates that supplied client key is a registered and valid client.
  357. :param client_key: The client/consumer key.
  358. :param request: OAuthlib request.
  359. :type request: oauthlib.common.Request
  360. :returns: True or False
  361. Note that if the dummy client is supplied it should validate in same
  362. or nearly the same amount of time as a valid one.
  363. Ensure latency inducing tasks are mimiced even for dummy clients.
  364. For example, use::
  365. from your_datastore import Client
  366. try:
  367. return Client.exists(client_key, access_token)
  368. except DoesNotExist:
  369. return False
  370. Rather than::
  371. from your_datastore import Client
  372. if access_token == self.dummy_access_token:
  373. return False
  374. else:
  375. return Client.exists(client_key, access_token)
  376. This method is used by
  377. * AccessTokenEndpoint
  378. * RequestTokenEndpoint
  379. * ResourceEndpoint
  380. * SignatureOnlyEndpoint
  381. """
  382. raise self._subclass_must_implement("validate_client_key")
  383. def validate_request_token(self, client_key, token, request):
  384. """Validates that supplied request token is registered and valid.
  385. :param client_key: The client/consumer key.
  386. :param token: The request token string.
  387. :param request: OAuthlib request.
  388. :type request: oauthlib.common.Request
  389. :returns: True or False
  390. Note that if the dummy request_token is supplied it should validate in
  391. the same nearly the same amount of time as a valid one.
  392. Ensure latency inducing tasks are mimiced even for dummy clients.
  393. For example, use::
  394. from your_datastore import RequestToken
  395. try:
  396. return RequestToken.exists(client_key, access_token)
  397. except DoesNotExist:
  398. return False
  399. Rather than::
  400. from your_datastore import RequestToken
  401. if access_token == self.dummy_access_token:
  402. return False
  403. else:
  404. return RequestToken.exists(client_key, access_token)
  405. This method is used by
  406. * AccessTokenEndpoint
  407. """
  408. raise self._subclass_must_implement("validate_request_token")
  409. def validate_access_token(self, client_key, token, request):
  410. """Validates that supplied access token is registered and valid.
  411. :param client_key: The client/consumer key.
  412. :param token: The access token string.
  413. :param request: OAuthlib request.
  414. :type request: oauthlib.common.Request
  415. :returns: True or False
  416. Note that if the dummy access token is supplied it should validate in
  417. the same or nearly the same amount of time as a valid one.
  418. Ensure latency inducing tasks are mimiced even for dummy clients.
  419. For example, use::
  420. from your_datastore import AccessToken
  421. try:
  422. return AccessToken.exists(client_key, access_token)
  423. except DoesNotExist:
  424. return False
  425. Rather than::
  426. from your_datastore import AccessToken
  427. if access_token == self.dummy_access_token:
  428. return False
  429. else:
  430. return AccessToken.exists(client_key, access_token)
  431. This method is used by
  432. * ResourceEndpoint
  433. """
  434. raise self._subclass_must_implement("validate_access_token")
  435. def validate_timestamp_and_nonce(self, client_key, timestamp, nonce,
  436. request, request_token=None, access_token=None):
  437. """Validates that the nonce has not been used before.
  438. :param client_key: The client/consumer key.
  439. :param timestamp: The ``oauth_timestamp`` parameter.
  440. :param nonce: The ``oauth_nonce`` parameter.
  441. :param request_token: Request token string, if any.
  442. :param access_token: Access token string, if any.
  443. :param request: OAuthlib request.
  444. :type request: oauthlib.common.Request
  445. :returns: True or False
  446. Per `Section 3.3`_ of the spec.
  447. "A nonce is a random string, uniquely generated by the client to allow
  448. the server to verify that a request has never been made before and
  449. helps prevent replay attacks when requests are made over a non-secure
  450. channel. The nonce value MUST be unique across all requests with the
  451. same timestamp, client credentials, and token combinations."
  452. .. _`Section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
  453. One of the first validation checks that will be made is for the validity
  454. of the nonce and timestamp, which are associated with a client key and
  455. possibly a token. If invalid then immediately fail the request
  456. by returning False. If the nonce/timestamp pair has been used before and
  457. you may just have detected a replay attack. Therefore it is an essential
  458. part of OAuth security that you not allow nonce/timestamp reuse.
  459. Note that this validation check is done before checking the validity of
  460. the client and token.::
  461. nonces_and_timestamps_database = [
  462. (u'foo', 1234567890, u'rannoMstrInghere', u'bar')
  463. ]
  464. def validate_timestamp_and_nonce(self, client_key, timestamp, nonce,
  465. request_token=None, access_token=None):
  466. return ((client_key, timestamp, nonce, request_token or access_token)
  467. not in self.nonces_and_timestamps_database)
  468. This method is used by
  469. * AccessTokenEndpoint
  470. * RequestTokenEndpoint
  471. * ResourceEndpoint
  472. * SignatureOnlyEndpoint
  473. """
  474. raise self._subclass_must_implement("validate_timestamp_and_nonce")
  475. def validate_redirect_uri(self, client_key, redirect_uri, request):
  476. """Validates the client supplied redirection URI.
  477. :param client_key: The client/consumer key.
  478. :param redirect_uri: The URI the client which to redirect back to after
  479. authorization is successful.
  480. :param request: OAuthlib request.
  481. :type request: oauthlib.common.Request
  482. :returns: True or False
  483. It is highly recommended that OAuth providers require their clients
  484. to register all redirection URIs prior to using them in requests and
  485. register them as absolute URIs. See `CWE-601`_ for more information
  486. about open redirection attacks.
  487. By requiring registration of all redirection URIs it should be
  488. straightforward for the provider to verify whether the supplied
  489. redirect_uri is valid or not.
  490. Alternatively per `Section 2.1`_ of the spec:
  491. "If the client is unable to receive callbacks or a callback URI has
  492. been established via other means, the parameter value MUST be set to
  493. "oob" (case sensitive), to indicate an out-of-band configuration."
  494. .. _`CWE-601`: http://cwe.mitre.org/top25/index.html#CWE-601
  495. .. _`Section 2.1`: https://tools.ietf.org/html/rfc5849#section-2.1
  496. This method is used by
  497. * RequestTokenEndpoint
  498. """
  499. raise self._subclass_must_implement("validate_redirect_uri")
  500. def validate_requested_realms(self, client_key, realms, request):
  501. """Validates that the client may request access to the realm.
  502. :param client_key: The client/consumer key.
  503. :param realms: The list of realms that client is requesting access to.
  504. :param request: OAuthlib request.
  505. :type request: oauthlib.common.Request
  506. :returns: True or False
  507. This method is invoked when obtaining a request token and should
  508. tie a realm to the request token and after user authorization
  509. this realm restriction should transfer to the access token.
  510. This method is used by
  511. * RequestTokenEndpoint
  512. """
  513. raise self._subclass_must_implement("validate_requested_realms")
  514. def validate_realms(self, client_key, token, request, uri=None,
  515. realms=None):
  516. """Validates access to the request realm.
  517. :param client_key: The client/consumer key.
  518. :param token: A request token string.
  519. :param request: OAuthlib request.
  520. :type request: oauthlib.common.Request
  521. :param uri: The URI the realms is protecting.
  522. :param realms: A list of realms that must have been granted to
  523. the access token.
  524. :returns: True or False
  525. How providers choose to use the realm parameter is outside the OAuth
  526. specification but it is commonly used to restrict access to a subset
  527. of protected resources such as "photos".
  528. realms is a convenience parameter which can be used to provide
  529. a per view method pre-defined list of allowed realms.
  530. Can be as simple as::
  531. from your_datastore import RequestToken
  532. request_token = RequestToken.get(token, None)
  533. if not request_token:
  534. return False
  535. return set(request_token.realms).issuperset(set(realms))
  536. This method is used by
  537. * ResourceEndpoint
  538. """
  539. raise self._subclass_must_implement("validate_realms")
  540. def validate_verifier(self, client_key, token, verifier, request):
  541. """Validates a verification code.
  542. :param client_key: The client/consumer key.
  543. :param token: A request token string.
  544. :param verifier: The authorization verifier string.
  545. :param request: OAuthlib request.
  546. :type request: oauthlib.common.Request
  547. :returns: True or False
  548. OAuth providers issue a verification code to clients after the
  549. resource owner authorizes access. This code is used by the client to
  550. obtain token credentials and the provider must verify that the
  551. verifier is valid and associated with the client as well as the
  552. resource owner.
  553. Verifier validation should be done in near constant time
  554. (to avoid verifier enumeration). To achieve this we need a
  555. constant time string comparison which is provided by OAuthLib
  556. in ``oauthlib.common.safe_string_equals``::
  557. from your_datastore import Verifier
  558. correct_verifier = Verifier.get(client_key, request_token)
  559. from oauthlib.common import safe_string_equals
  560. return safe_string_equals(verifier, correct_verifier)
  561. This method is used by
  562. * AccessTokenEndpoint
  563. """
  564. raise self._subclass_must_implement("validate_verifier")
  565. def verify_request_token(self, token, request):
  566. """Verify that the given OAuth1 request token is valid.
  567. :param token: A request token string.
  568. :param request: OAuthlib request.
  569. :type request: oauthlib.common.Request
  570. :returns: True or False
  571. This method is used only in AuthorizationEndpoint to check whether the
  572. oauth_token given in the authorization URL is valid or not.
  573. This request is not signed and thus similar ``validate_request_token``
  574. method can not be used.
  575. This method is used by
  576. * AuthorizationEndpoint
  577. """
  578. raise self._subclass_must_implement("verify_request_token")
  579. def verify_realms(self, token, realms, request):
  580. """Verify authorized realms to see if they match those given to token.
  581. :param token: An access token string.
  582. :param realms: A list of realms the client attempts to access.
  583. :param request: OAuthlib request.
  584. :type request: oauthlib.common.Request
  585. :returns: True or False
  586. This prevents the list of authorized realms sent by the client during
  587. the authorization step to be altered to include realms outside what
  588. was bound with the request token.
  589. Can be as simple as::
  590. valid_realms = self.get_realms(token)
  591. return all((r in valid_realms for r in realms))
  592. This method is used by
  593. * AuthorizationEndpoint
  594. """
  595. raise self._subclass_must_implement("verify_realms")
  596. def save_access_token(self, token, request):
  597. """Save an OAuth1 access token.
  598. :param token: A dict with token credentials.
  599. :param request: OAuthlib request.
  600. :type request: oauthlib.common.Request
  601. The token dictionary will at minimum include
  602. * ``oauth_token`` the access token string.
  603. * ``oauth_token_secret`` the token specific secret used in signing.
  604. * ``oauth_authorized_realms`` a space separated list of realms.
  605. Client key can be obtained from ``request.client_key``.
  606. The list of realms (not joined string) can be obtained from
  607. ``request.realm``.
  608. This method is used by
  609. * AccessTokenEndpoint
  610. """
  611. raise self._subclass_must_implement("save_access_token")
  612. def save_request_token(self, token, request):
  613. """Save an OAuth1 request token.
  614. :param token: A dict with token credentials.
  615. :param request: OAuthlib request.
  616. :type request: oauthlib.common.Request
  617. The token dictionary will at minimum include
  618. * ``oauth_token`` the request token string.
  619. * ``oauth_token_secret`` the token specific secret used in signing.
  620. * ``oauth_callback_confirmed`` the string ``true``.
  621. Client key can be obtained from ``request.client_key``.
  622. This method is used by
  623. * RequestTokenEndpoint
  624. """
  625. raise self._subclass_must_implement("save_request_token")
  626. def save_verifier(self, token, verifier, request):
  627. """Associate an authorization verifier with a request token.
  628. :param token: A request token string.
  629. :param verifier: A dictionary containing the oauth_verifier and
  630. oauth_token
  631. :param request: OAuthlib request.
  632. :type request: oauthlib.common.Request
  633. We need to associate verifiers with tokens for validation during the
  634. access token request.
  635. Note that unlike save_x_token token here is the ``oauth_token`` token
  636. string from the request token saved previously.
  637. This method is used by
  638. * AuthorizationEndpoint
  639. """
  640. raise self._subclass_must_implement("save_verifier")