本文整理汇总了Python中core.model.ConfigurationSetting.for_externalintegration方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigurationSetting.for_externalintegration方法的具体用法?Python ConfigurationSetting.for_externalintegration怎么用?Python ConfigurationSetting.for_externalintegration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类core.model.ConfigurationSetting
的用法示例。
在下文中一共展示了ConfigurationSetting.for_externalintegration方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_client_authorization
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def check_client_authorization(self, collection, client):
"""Verify that an IntegrationClient is whitelisted for access to the collection."""
external_library_urls = ConfigurationSetting.for_externalintegration(
BaseSharedCollectionAPI.EXTERNAL_LIBRARY_URLS, collection.external_integration
).json_value
if client.url not in [IntegrationClient.normalize_url(url) for url in external_library_urls]:
raise AuthorizationFailedException()
示例2: __init__
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def __init__(self, integration, library=None):
_db = Session.object_session(integration)
if not library:
raise CannotLoadConfiguration("Google Analytics can't be configured without a library.")
url_setting = ConfigurationSetting.for_externalintegration(ExternalIntegration.URL, integration)
self.url = url_setting.value or self.DEFAULT_URL
self.tracking_id = ConfigurationSetting.for_library_and_externalintegration(
_db, self.TRACKING_ID, library, integration,
).value
if not self.tracking_id:
raise CannotLoadConfiguration("Missing tracking id for library %s" % library.short_name)
示例3: setup
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def setup(self):
super(TestSharedCollectionAPI, self).setup()
self.collection = self._collection(protocol="Mock")
self.shared_collection = SharedCollectionAPI(
self._db, api_map = {
"Mock" : MockAPI
}
)
self.api = self.shared_collection.api(self.collection)
ConfigurationSetting.for_externalintegration(
BaseSharedCollectionAPI.EXTERNAL_LIBRARY_URLS, self.collection.external_integration
).value = json.dumps(["http://library.org"])
self.client, ignore = IntegrationClient.register(self._db, "http://library.org")
edition, self.pool = self._edition(
with_license_pool=True, collection=self.collection
)
[self.delivery_mechanism] = self.pool.delivery_mechanisms
示例4: test_push
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def test_push(self):
"""Test the other methods orchestrated by the push() method.
"""
class Mock(Registration):
def _extract_catalog_information(self, response):
self.initial_catalog_response = response
return "register_url", "vendor_id"
def _create_registration_payload(self, url_for, stage):
self.payload_ingredients = (url_for, stage)
return dict(payload="this is it")
def _create_registration_headers(self):
self._create_registration_headers_called = True
return dict(Header="Value")
def _send_registration_request(
self, register_url, headers, payload, do_post
):
self._send_registration_request_called_with = (
register_url, headers, payload, do_post
)
return MockRequestsResponse(
200, content=json.dumps("you did it!")
)
def _process_registration_result(self, catalog, encryptor, stage):
self._process_registration_result_called_with = (
catalog, encryptor, stage
)
return "all done!"
def mock_do_get(self, url):
self.do_get_called_with = url
return "A fake catalog"
# If there is no preexisting key pair set up for the library,
# registration fails. (This normally won't happen because the
# key pair is set up when the LibraryAuthenticator is
# initialized.
library = self._default_library
registration = Mock(self.registry, library)
stage = Registration.TESTING_STAGE
url_for = object()
catalog_url = "http://catalog/"
do_post = object()
def push():
return registration.push(
stage, url_for, catalog_url, registration.mock_do_get, do_post
)
result = push()
expect = "Library %s has no key pair set." % library.short_name
eq_(expect, result.detail)
# When a key pair is present, registration is kicked off, and
# in this case it succeeds.
key_pair_setting = ConfigurationSetting.for_library(
Configuration.KEY_PAIR, library
)
public_key, private_key = Configuration.key_pair(key_pair_setting)
result = registration.push(
stage, url_for, catalog_url, registration.mock_do_get, do_post
)
eq_("all done!", result)
# But there were many steps towards this result.
# First, do_get was called on the catalog URL.
eq_(catalog_url, registration.do_get_called_with)
# Then, the catalog was passed into _extract_catalog_information.
eq_("A fake catalog", registration.initial_catalog_response)
# _extract_catalog_information returned a registration URL and
# a vendor ID. The registration URL was used later on...
#
# The vendor ID was set as a ConfigurationSetting on
# the ExternalIntegration associated with this registry.
eq_(
"vendor_id",
ConfigurationSetting.for_externalintegration(
AuthdataUtility.VENDOR_ID_KEY, self.integration
).value
)
# _create_registration_payload was called to create the body
# of the registration request.
eq_((url_for, stage), registration.payload_ingredients)
# _create_registration_headers was called to create the headers
# sent along with the request.
eq_(True, registration._create_registration_headers_called)
# Then _send_registration_request was called, POSTing the
# payload to "register_url", the registration URL we got earlier.
results = registration._send_registration_request_called_with
eq_(
#.........这里部分代码省略.........
示例5: test_register
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def test_register(self):
# An auth document URL is required to register.
assert_raises(InvalidInputException, self.shared_collection.register,
self.collection, None)
# If the url doesn't return a valid auth document, there's an exception.
auth_response = "not json"
def do_get(*args, **kwargs):
return MockRequestsResponse(200, content=auth_response)
assert_raises(RemoteInitiatedServerError, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
# The auth document also must have a link to the library's catalog.
auth_response = json.dumps({"links": []})
assert_raises(RemoteInitiatedServerError, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
# If no external library URLs are configured, no one can register.
auth_response = json.dumps({"links": [{"href": "http://library.org", "rel": "start"}]})
ConfigurationSetting.for_externalintegration(
BaseSharedCollectionAPI.EXTERNAL_LIBRARY_URLS, self.collection.external_integration
).value = None
assert_raises(AuthorizationFailedException, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
# If the library's URL isn't in the configuration, it can't register.
auth_response = json.dumps({"links": [{"href": "http://differentlibrary.org", "rel": "start"}]})
ConfigurationSetting.for_externalintegration(
BaseSharedCollectionAPI.EXTERNAL_LIBRARY_URLS, self.collection.external_integration
).value = json.dumps(["http://library.org"])
assert_raises(AuthorizationFailedException, self.shared_collection.register,
self.collection, "http://differentlibrary.org/auth", do_get=do_get)
# Or if the public key is missing from the auth document.
auth_response = json.dumps({"links": [{"href": "http://library.org", "rel": "start"}]})
assert_raises(RemoteInitiatedServerError, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
auth_response = json.dumps({"public_key": { "type": "not RSA", "value": "123" },
"links": [{"href": "http://library.org", "rel": "start"}]})
assert_raises(RemoteInitiatedServerError, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
auth_response = json.dumps({"public_key": { "type": "RSA" },
"links": [{"href": "http://library.org", "rel": "start"}]})
assert_raises(RemoteInitiatedServerError, self.shared_collection.register,
self.collection, "http://library.org/auth", do_get=do_get)
# Here's an auth document with a valid key.
key = RSA.generate(2048)
public_key = key.publickey().exportKey()
encryptor = PKCS1_OAEP.new(key)
auth_response = json.dumps({"public_key": { "type": "RSA", "value": public_key },
"links": [{"href": "http://library.org", "rel": "start"}]})
response = self.shared_collection.register(self.collection, "http://library.org/auth", do_get=do_get)
# An IntegrationClient has been created.
client = get_one(self._db, IntegrationClient, url=IntegrationClient.normalize_url("http://library.org/"))
decrypted_secret = encryptor.decrypt(base64.b64decode(response.get("metadata", {}).get("shared_secret")))
eq_(client.shared_secret, decrypted_secret)
示例6: RemoteInitiatedServerError
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
links = auth_document.get("links")
start_url = None
for link in links:
if link.get("rel") == "start":
start_url = link.get("href")
break
if not start_url:
raise RemoteInitiatedServerError(
_("Authentication document at %(auth_document_url)s did not contain a start link.",
auth_document_url=auth_document_url),
_("Remote authentication document"))
external_library_urls = ConfigurationSetting.for_externalintegration(
BaseSharedCollectionAPI.EXTERNAL_LIBRARY_URLS, collection.external_integration
).json_value
if not external_library_urls or start_url not in external_library_urls:
raise AuthorizationFailedException(
_("Your library's URL is not one of the allowed URLs for this collection. Ask the collection administrator to add %(library_url)s to the list of allowed URLs.",
library_url=start_url))
public_key = auth_document.get("public_key")
if not public_key or not public_key.get("type") == "RSA" or not public_key.get("value"):
raise RemoteInitiatedServerError(
_("Authentication document at %(auth_document_url)s did not contain an RSA public key.",
auth_document_url=auth_document_url),
_("Remote authentication document"))
public_key = public_key.get("value")
示例7: push
# 需要导入模块: from core.model import ConfigurationSetting [as 别名]
# 或者: from core.model.ConfigurationSetting import for_externalintegration [as 别名]
def push(self, stage, url_for, catalog_url=None, do_get=HTTP.debuggable_get,
do_post=HTTP.debuggable_post):
"""Attempt to register a library with a RemoteRegistry.
NOTE: This method is designed to be used in a
controller. Other callers may use this method, but they must be
able to render a ProblemDetail when there's a failure.
NOTE: The application server must be running when this method
is called, because part of the OPDS Directory Registration
Protocol is the remote server retrieving the library's
Authentication For OPDS document.
:param stage: Either TESTING_STAGE or PRODUCTION_STAGE
:param url_for: Flask url_for() or equivalent, used to generate URLs
for the application server.
:param do_get: Mockable method to make a GET request.
:param do_post: Mockable method to make a POST request.
:return: A ProblemDetail if there was a problem; otherwise True.
"""
# Assume that the registration will fail.
#
# TODO: If a registration has previously succeeded, failure to
# re-register probably means a maintenance of the status quo,
# not a change of success to failure. But we don't have any way
# of being sure.
self.status_field.value = self.FAILURE_STATUS
if stage not in self.VALID_REGISTRATION_STAGES:
return INVALID_INPUT.detailed(
_("%r is not a valid registration stage") % stage
)
# Verify that a public/private key pair exists for this library.
# This key pair is created during initialization of the
# LibraryAuthenticator, so this should always be present.
#
# We can't just create the key pair here because the process
# of pushing a registration involves the other site making a
# request to the circulation manager. This means the key pair
# needs to be committed to the database _before_ the push
# attempt starts.
key_pair = ConfigurationSetting.for_library(
Configuration.KEY_PAIR, self.library).json_value
if not key_pair:
# TODO: We could create the key pair _here_. The database
# session will be committed at the end of this request,
# so the push attempt would succeed if repeated.
return SHARED_SECRET_DECRYPTION_ERROR.detailed(
_("Library %(library)s has no key pair set.",
library=self.library.short_name)
)
public_key, private_key = key_pair
cipher = Configuration.cipher(private_key)
# Before we can start the registration protocol, we must fetch
# the remote catalog's URL and extract the link to the
# registration resource that kicks off the protocol.
catalog_url = catalog_url or self.integration.url
response = do_get(catalog_url)
if isinstance(response, ProblemDetail):
return response
result = self._extract_catalog_information(response)
if isinstance(result, ProblemDetail):
return result
register_url, vendor_id = result
# Store the vendor id as a ConfigurationSetting on the integration
# -- it'll be the same value for all libraries.
if vendor_id:
ConfigurationSetting.for_externalintegration(
AuthdataUtility.VENDOR_ID_KEY, self.integration
).value = vendor_id
# Build the document we'll be sending to the registration URL.
payload = self._create_registration_payload(url_for, stage)
if isinstance(payload, ProblemDetail):
return payload
headers = self._create_registration_headers()
if isinstance(headers, ProblemDetail):
return headers
# Send the document.
response = self._send_registration_request(
register_url, headers, payload, do_post
)
if isinstance(response, ProblemDetail):
return response
catalog = json.loads(response.content)
# Process the result.
return self._process_registration_result(catalog, cipher, stage)