Skip to main content

Castle protects your users from account compromise

Project description

Build Status

Castle analyzes user behavior in web and mobile apps to stop fraud before it happens.

Installation

pip install castle

Configuration

Import and configure the library with your Castle API secret.

from castle.configuration import configuration, DEFAULT_ALLOWLIST, TRUSTED_PROXIES

configuration.api_secret = ':YOUR-API-SECRET'

# For risk/filter methods you can set failover strategies: allow(default), deny, challenge, throw
configuration.failover_strategy = 'deny'

# RequestError is raised when timing out in milliseconds (default: 1000 milliseconds)
configuration.request_timeout = 1500

# Base Castle API url
# configuration.base_url = "https://api.castle.io/v1"

# Logger (need to respond to info method) - logs Castle API requests and responses
# configuration.logger = logging.getLogger()

# Allowlisted and Denylisted headers are case insensitive
# and allow to use _ and - as a separator, http prefixes are removed
# By default all headers are passed, but some are automatically scrubbed.
# If you need to apply an allowlist, we recommend using the minimum set of
# standard headers that we've exposed in the `DEFAULT_ALLOWLIST` constant.
# Allowlisted headers
configuration.allowlisted = DEFAULT_ALLOWLIST + ['X_HEADER']

# Denylisted headers take advantage over allowlisted elements. Note that
# some headers are always scrubbed, for security reasons.
configuration.denylisted = ['HTTP-X-header']

# Castle needs the original IP of the client, not the IP of your proxy or load balancer.
# The SDK will only trust the proxy chain as defined in the configuration.
# We try to fetch the client IP based on X-Forwarded-For or Remote-Addr headers in that order,
# but sometimes the client IP may be stored in a different header or order.
# The SDK can be configured to look for the client IP address in headers that you specify.

# Sometimes, Cloud providers do not use consistent IP addresses to proxy requests.
# In this case, the client IP is usually preserved in a custom header. Example:
# Cloudflare preserves the client request in the 'Cf-Connecting-Ip' header.
# It would be used like so: configuration.ip_headers=['Cf-Connecting-Ip']
configuration.ip_headers = []

# If the specified header or X-Forwarded-For default contains a proxy chain with public IP addresses,
# then you must choose only one of the following (but not both):
# 1. The trusted_proxies value must match the known proxy IPs. This option is preferable if the IP is static.
# 2. The trusted_proxy_depth value must be set to the number of known trusted proxies in the chain (see below).
# This option is preferable if the IPs are ephemeral, but the depth is consistent.

# Additionally to make X-Forwarded-For and other headers work better discovering client ip address,
# and not the address of a reverse proxy server, you can define trusted proxies
# which will help to fetch proper ip from those headers

# In order to extract the client IP of the X-Forwarded-For header
# and not the address of a reverse proxy server, you must define all trusted public proxies
# you can achieve this by listing all the proxies ip defined by string or regular expressions
# in the trusted_proxies setting
configuration.trusted_proxies = []
# or by providing number of trusted proxies used in the chain
configuration.trusted_proxy_depth = 0
# note that you must pick one approach over the other.

# If there is no possibility to define options above and there is no other header that holds the client IP,
# then you may set trust_proxy_chain = true to trust all of the proxy IPs in X-Forwarded-For
configuration.trust_proxy_chain = false
# *Warning*: this mode is highly promiscuous and could lead to wrongly trusting a spoofed IP if the request passes through a malicious proxy

# *Note: the default list of proxies that are always marked as "trusted" can be found in TRUSTED_PROXIES

Usage

See documentation for how to use this SDK with the Castle APIs

Multi-environment configuration

It is also possible to define multiple configs within one application.

from castle.configuration import Configuration

# Initialize a separate Configuration instance
config = Configuration()
config.api_secret = ':YOUR-API-SECRET'

After a successful setup, you can pass the config to a client and call any API as follows:

from castle.client import Client

client = Client({'context': {}})
client.risk({
    'request_token': '<token>',
    'event': '$login',
    'status': '$succeeded',
    'user': {'id': '1234'}
})

Signature

from castle.secure_mode import signature

signature(user_id)

will create a signed user_id.

Exceptions

CastleError will be thrown if the Castle API returns a 400 or a 500 level HTTP response. You can also choose to catch a more finegrained error.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

castle-7.0.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

castle-7.0.0-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file castle-7.0.0.tar.gz.

File metadata

  • Download URL: castle-7.0.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for castle-7.0.0.tar.gz
Algorithm Hash digest
SHA256 12c375690946a55523b5bfe3d36773784d1c3ac1d4fc90b5955c207d06c312a1
MD5 a20256a7d623210859c30b54f96ec73e
BLAKE2b-256 345f41f616d68b2e4ee2dc36370f74cb6f96068bebcb898c46810f0bd1035da8

See more details on using hashes here.

File details

Details for the file castle-7.0.0-py3-none-any.whl.

File metadata

  • Download URL: castle-7.0.0-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for castle-7.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f25834e67649c0103d1f1c439da1225dd0862ae47fb10a8901f0a93494975cbb
MD5 d516f4e33a72c3db576eeb004a0bea21
BLAKE2b-256 58b34d877c4118fcf3edd1d32f808c235e661deb1009ae0180cfa24d8c6ff704

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page