request_validator.py 30 KB

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