Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
518ea31
commit d47552e
Showing
10 changed files
with
271 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import asyncio | ||
import json | ||
import aiohttp | ||
|
||
from . import receipt | ||
from . import exceptions | ||
from .environment import default as default_env | ||
|
||
|
||
class AiohttpVerify: | ||
|
||
@asyncio.coroutine | ||
def aioverify_from(self, url, timeout): | ||
body = json.dumps(self.request_content).encode() | ||
with aiohttp.ClientSession() as session: | ||
try: | ||
http_response = yield from session.post(url, data=body, timeout=timeout) | ||
except asyncio.TimeoutError as e: | ||
raise exceptions.ItunesServerNotReachable(exc=e) | ||
if http_response.status != 200: | ||
response_text = yield from http_response.text() | ||
raise exceptions.ItunesServerNotAvailable(http_response.status, response_text) | ||
response_body = yield from http_response.text() | ||
response = receipt.Response(json.loads(response_body)) | ||
if response.status != 0: | ||
raise exceptions.InvalidReceipt(response.status, response=response) | ||
return response | ||
|
||
@asyncio.coroutine | ||
def aioverify(self, **options): | ||
"""Try to verify the given receipt with current environment. | ||
See also: | ||
- Receipt_Validation_Programming_Guide_. | ||
.. _Receipt_Validation_Programming_Guide: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html | ||
:param itunesiap.environment.Environment env: Override the environment. | ||
:param float timeout: The value is connection timeout of the verifying | ||
request. The default value is 30.0 when no `env` is given. | ||
:param bool use_production: The value is weather verifying in | ||
production server or not. The default value is :class:`bool` True | ||
when no `env` is given. | ||
:param bool use_sandbox: The value is weather verifying in | ||
sandbox server or not. The default value is :class:`bool` False | ||
when no `env` is given. | ||
:param bool verify_ssl: The value will be ignored. | ||
:return: :class:`itunesiap.receipt.Receipt` object if succeed. | ||
:raises: Otherwise raise a request exception. | ||
""" | ||
env = options.get('env', default_env) | ||
use_production = options.get('use_production', env.use_production) | ||
use_sandbox = options.get('use_sandbox', env.use_sandbox) | ||
verify_ssl = options.get('verify_ssl', env.verify_ssl) # noqa | ||
timeout = options.get('timeout', env.timeout) | ||
|
||
response = None | ||
if use_production: | ||
try: | ||
response = yield from self.aioverify_from(self.PRODUCTION_VALIDATION_URL, timeout=timeout) | ||
except exceptions.InvalidReceipt as e: | ||
if not use_sandbox or e.status != self.STATUS_SANDBOX_RECEIPT_ERROR: | ||
raise | ||
if not response and use_sandbox: | ||
try: | ||
response = yield from self.aioverify_from(self.SANDBOX_VALIDATION_URL, timeout=timeout) | ||
except exceptions.InvalidReceipt as e: | ||
raise | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
import json | ||
import functools | ||
import requests | ||
|
||
from . import receipt | ||
from . import exceptions | ||
from .environment import Environment | ||
|
||
|
||
class RequestsVerify(object): | ||
def verify_from(self, url, timeout=None, verify_ssl=True): | ||
"""The actual implemention of verification request. | ||
:func:`verify` calls this method to try to verifying for each servers. | ||
:param str url: iTunes verification API URL. | ||
:param float timeout: The value is connection timeout of the verifying | ||
request. The default value is 30.0 when no `env` is given. | ||
:param bool verify_ssl: SSL verification. | ||
:return: :class:`itunesiap.receipt.Receipt` object if succeed. | ||
:raises: Otherwise raise a request exception. | ||
""" | ||
post_body = json.dumps(self.request_content) | ||
requests_post = requests.post | ||
if self.proxy_url: | ||
protocol = self.proxy_url.split('://')[0] | ||
requests_post = functools.partial(requests_post, proxies={protocol: self.proxy_url}) | ||
if timeout is not None: | ||
requests_post = functools.partial(requests_post, timeout=timeout) | ||
try: | ||
http_response = requests_post(url, post_body, verify=verify_ssl) | ||
except requests.exceptions.RequestException as e: | ||
raise exceptions.ItunesServerNotReachable(exc=e) | ||
|
||
if http_response.status_code != 200: | ||
raise exceptions.ItunesServerNotAvailable(http_response.status_code, http_response.content) | ||
|
||
response = receipt.Response(json.loads(http_response.content.decode('utf-8'))) | ||
if response.status != 0: | ||
raise exceptions.InvalidReceipt(response.status, response=response) | ||
return response | ||
|
||
def verify(self, **options): | ||
"""Try verification with current environment. | ||
See also: | ||
- Receipt_Validation_Programming_Guide_. | ||
.. _Receipt_Validation_Programming_Guide: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html | ||
:param itunesiap.environment.Environment env: Override the environment. | ||
:param float timeout: The value is connection timeout of the verifying | ||
request. The default value is 30.0 when no `env` is given. | ||
:param bool use_production: The value is weather verifying in | ||
production server or not. The default value is :class:`bool` True | ||
when no `env` is given. | ||
:param bool use_sandbox: The value is weather verifying in | ||
sandbox server or not. The default value is :class:`bool` False | ||
when no `env` is given. | ||
:param bool verify_ssl: The value is weather enabling SSL verification | ||
or not. WARNING: DO NOT TURN IT OFF WITHOUT A PROPER REASON. IF YOU | ||
DON'T UNDERSTAND WHAT IT MEANS, NEVER SET IT YOURSELF. | ||
:return: :class:`itunesiap.receipt.Receipt` object if succeed. | ||
:raises: Otherwise raise a request exception. | ||
""" | ||
env = options.get('env') | ||
if not env: # backward compitibility | ||
env = Environment._stack[-1] | ||
use_production = options.get('use_production', env.use_production) | ||
use_sandbox = options.get('use_sandbox', env.use_sandbox) | ||
verify_ssl = options.get('verify_ssl', env.verify_ssl) | ||
timeout = options.get('timeout', env.timeout) | ||
assert(env.use_production or env.use_sandbox) | ||
|
||
response = None | ||
if use_production: | ||
try: | ||
response = self.verify_from(self.PRODUCTION_VALIDATION_URL, timeout=timeout, verify_ssl=verify_ssl) | ||
except exceptions.InvalidReceipt as e: | ||
if not use_sandbox or e.status != self.STATUS_SANDBOX_RECEIPT_ERROR: | ||
raise | ||
|
||
if not response and use_sandbox: | ||
try: | ||
response = self.verify_from(self.SANDBOX_VALIDATION_URL, timeout=timeout, verify_ssl=verify_ssl) | ||
except exceptions.InvalidReceipt: | ||
raise | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.