Registry / auth-security / devicecheck

devicecheck

JSON →
library1.3.3pypypi✓ verified 24d ago

The `devicecheck` Python library provides a wrapper for the Apple DeviceCheck API, allowing developers to manage device state and assert app integrity to reduce fraudulent use of services. It supports both synchronous and asynchronous operations. The library is actively maintained, with regular releases, currently at version 1.3.3.

pip install devicecheck
INSTALL
IMPORT
SIG · DEVICECHECK
D
devicecheck
auth-securitypythonv1.3.3
Install
3.2s avg
Import
409ms
Disk
37MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.430s · 38.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.2s · import 0.388s · 39MB
37MB installed
● package 37MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DeviceCheck
from devicecheck import DeviceCheck
AsyncioDeviceCheck
from devicecheck.asyncio import AsyncioDeviceCheck
from devicecheck import AsyncioDeviceCheck
Asyncio specific classes are in a dedicated 'asyncio' submodule since v1.3.0.
validate_device
from devicecheck.decorators import validate_device
DCSupportedFrameworks
from devicecheck.decorators import DCSupportedFrameworks
async_validate_device
from devicecheck.decorators import async_validate_device
DCSupportedAsyncFrameworks
from devicecheck.decorators import DCSupportedAsyncFrameworks

This quickstart demonstrates how to initialize the `DeviceCheck` client and validate a device token. It uses environment variables for sensitive Apple credentials, which is a recommended security practice. Remember to obtain `team_id`, `bundle_id`, `key_id`, and the `private_key` path from your Apple Developer account, and set `dev_environment` appropriately for your testing or production environment. The `device_token` must be generated by your iOS application on a physical device.

import os from devicecheck import DeviceCheck # Configure DeviceCheck client using environment variables for security dc_client = DeviceCheck( team_id=os.environ.get("APPLE_TEAM_ID", ""), bundle_id=os.environ.get("APPLE_BUNDLE_ID", ""), key_id=os.environ.get("APPLE_KEY_ID", ""), private_key=os.environ.get("APPLE_PRIVATE_KEY_PATH", ""), dev_environment=bool(os.environ.get("DC_DEV_ENVIRONMENT", "False").lower() == "true"), # Set raise_on_error=True to get Python exceptions on API errors raise_on_error=True ) # IMPORTANT: Replace with an actual device token generated by your iOS app # For testing, you might use a mocked token if the library supports it via env vars. device_token = os.environ.get("TEST_DEVICE_TOKEN", "YOUR_ACTUAL_DEVICE_TOKEN_HERE") if not device_token: print("Error: TEST_DEVICE_TOKEN environment variable not set, or placeholder not replaced.") print("Please provide a valid device token from your iOS app for real testing.") else: try: # Validate the device token validation_result = dc_client.validate_device_token(device_token) if validation_result.is_ok: print('Device is valid!') # Example: Query bits data if needed # bits_data = dc_client.query_two_bits(device_token) # print(f'Bit 0: {bits_data.bit0}, Bit 1: {bits_data.bit1}') else: print(f'Device validation failed: {validation_result.description}') except Exception as e: print(f"An error occurred during DeviceCheck operation: {e}")
Debug
Known issues
breakingDecorators for web frameworks (`@validate_device`, etc.) were reworked in version 1.2.0. If you were using these decorators prior to this version, review the changes and update your implementation accordingly.
fix
Refer to the library's GitHub README or documentation for updated decorator usage patterns.
affects: >=1.2.0
gotchaThe `dev_environment` parameter in `DeviceCheck` initialization is crucial. It defaults to `True` (development) for new instances. Ensure it is explicitly set to `False` in production to interact with Apple's production DeviceCheck environment. Incorrect setting will lead to validation failures or incorrect bit states.
fix
Always set `dev_environment=False` in production deployments and `True` (or omit if default desired) for development/sandbox environments. Use environment variables for configuration.
affects: All
gotchaApple's DeviceCheck API, and by extension this library, only works on physical iOS devices. It will not function correctly on simulators.
fix
Test DeviceCheck functionality on actual iOS hardware. Use mock environment variables (e.g., `MOCK_DEVICE_CHECK_DECORATOR_TOKEN`) for local development if real device testing is not feasible for every iteration.
affects: All
gotchaVersion 1.3.0 introduced dedicated asyncio support. When working with asynchronous code, ensure you import `AsyncioDeviceCheck` from `devicecheck.asyncio` instead of the top-level `DeviceCheck` class to leverage `async/await` functionality.
fix
For asyncio, use `from devicecheck.asyncio import AsyncioDeviceCheck`. The rest of the asynchronous network methods will need to be `await`ed.
affects: >=1.3.0
gotchaThe private key (`.p8` file) used for authentication with Apple's API is highly sensitive. It should never be hardcoded or committed directly into source control. Always load it securely, for example, from an environment variable or a secure vault at runtime.
fix
Store the private key path or content in an environment variable (`APPLE_PRIVATE_KEY_PATH` or `APPLE_PRIVATE_KEY_CONTENT`) and retrieve it at runtime. Ensure proper access controls are on the `.p8` file.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'devicecheck'
The 'devicecheck' library is not installed in your current Python environment or there is a typo in the import statement.
fix
Install the library using pip: `pip install devicecheck` or ensure the correct import statement: `from devicecheck import DeviceCheck` or `from devicecheck.asyncio import AsyncioDeviceCheck`.
ValueError: Invalid token
This error typically occurs when the Apple DeviceCheck API rejects the token due to incorrect parameters provided during the DeviceCheck object initialization (e.g., `team_id`, `bundle_id`, `key_id`, or `private_key`) or an expired/invalid token itself.
fix
Verify that your `team_id`, `bundle_id`, `key_id`, and `private_key` are correct and match the values from your Apple Developer account. Ensure the private key is in the correct format and has not been revoked or expired.
HTTPError: 400 Bad Request (or similar API response with 400 status)
The request sent to the Apple DeviceCheck API via the Python library was malformed, missing required data, or contained invalid parameters, leading to a 'Bad Request' response from the API server.
fix
Check the parameters you are passing to the `devicecheck` library methods, particularly when generating or validating tokens. Ensure all required fields are present and correctly formatted according to Apple's DeviceCheck API specifications. Refer to the library's documentation for parameter details.
AttributeError: 'NoneType' object has no attribute '_load_key'
This error suggests that the internal key loading mechanism failed, likely because the `private_key` provided to the `DeviceCheck` constructor was invalid, inaccessible, or could not be parsed, resulting in a `None` object where a key object was expected.
fix
Ensure the `private_key` argument is a valid string representation of your .p8 key file content and that it is correctly passed to the `DeviceCheck` constructor. Double-check for any leading/trailing whitespace or corrupted characters.
Upgrade
Version history
1.3.3latest on PyPI · released May 3, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
30 hits · last 30 days
node
28
OpenAI (training)
1
Resources
devicecheck — pip install devicecheck · libregistry