GithubHelp home page GithubHelp logo

castle / castle-ruby Goto Github PK

View Code? Open in Web Editor NEW
34.0 21.0 17.0 799 KB

Ruby gem for Castle

Home Page: https://castle.io

License: MIT License

Ruby 99.95% Shell 0.05%
castle ruby sdk fraud-detection fraud-prevention

castle-ruby's Introduction

Ruby SDK for Castle

Build Status Coverage Status Gem Version

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

Installation

Add the castle-rb gem to your Gemfile

gem 'castle-rb'

Configuration

Framework configuration

Load and configure the library with your Castle API secret in an initializer or similar.

Castle.api_secret = 'YOUR_API_SECRET'

A Castle client instance will be made available as castle in your

  • Rails controllers when you add require 'castle/support/rails'

  • Padrino controllers when you add require 'castle/support/padrino'

  • Sinatra app when you add require 'castle/support/sinatra' (and additionally explicitly add register Sinatra::Castle to your Sinatra::Base class if you have a modular application)

require 'castle/support/sinatra'

class ApplicationController < Sinatra::Base
  register Sinatra::Castle
end
  • Hanami when you add require 'castle/support/hanami' and include Castle::Hanami to your Hanami application
require 'castle/support/hanami'

module Web
  class Application < Hanami::Application
    include Castle::Hanami
  end
end

Client configuration

Castle.configure do |config|
  # Same as setting it through Castle.api_secret
  config.api_secret = 'secret'

  # For authenticate method you can set failover strategies: allow(default), deny, challenge, throw
  config.failover_strategy = :deny

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

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

  # Logger (need to respond to info method) - logs Castle API requests and responses
  # config.logger = Logger.new(STDOUT)

  # Allowlisted and Denylisted headers are case insensitive and allow to use _ and - as a separator, http prefixes are removed
  # Allowlisted headers
  # By default, the SDK sends all HTTP headers, except for Cookie and Authorization.
  # If you decide to use a allowlist, the SDK will:
  # - always send the User-Agent header
  # - send scrubbed values of non-allowlisted headers
  # - send proper values of allowlisted headers.
  # @example
  #   config.allowlisted = ['X_HEADER']
  #   # will send { 'User-Agent' => 'Chrome', 'X_HEADER' => 'proper value', 'Any-Other-Header' => true }
  #
  # We highly suggest using denylist instead of allowlist, so that Castle can use as many data points
  # as possible to secure your users. If you want to use the allowlist, this is the minimal
  # amount of headers we recommend:
  config.allowlisted = Castle::Configuration::DEFAULT_ALLOWLIST

  # Denylisted headers take precedence over allowlisted elements
  # We always denylist Cookie and Authentication headers. If you use any other headers that
  # might contain sensitive information, you should denylist them.
  config.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: config.ip_headers=['Cf-Connecting-Ip']
  config.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
  config.trusted_proxies = []

  # or by providing number of trusted proxies used in the chain
  config.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
  config.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: Castle::Configuration::TRUSTED_PROXIES
end

Multi-environment configuration

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

# Initialize new instance of Castle::Configuration
config =
  Castle::Configuration.new.tap do |c|
    # and set any attribute
    c.api_secret = 'YOUR_API_SECRET'
  end

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

::Castle::API::GetDevice.call(device_token: device_token, config: config)

Usage

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

Exceptions

Castle::Error 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.

castle-ruby's People

Contributors

afterdesign avatar baloran avatar bartes avatar brissmyr avatar cconcannon avatar dawlib avatar dependabot-support avatar dependabot[bot] avatar dja avatar eik3 avatar filiptepper avatar h3xar0n avatar joladev avatar keithpitt avatar lluft avatar madejejej avatar marcqualie avatar marysieek avatar mensfeld avatar nijikon avatar owyongsk avatar psyho avatar stigkj avatar tilljoel avatar wallin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

castle-ruby's Issues

Handle symbol header

Clearance sends a symbol header (:clearance) instead of string, which crashes the parsing.

[2] pry(#<Castle::HeaderFormatter>)>

From: /Users/brissmyr/.rvm/gems/ruby-2.2.4/gems/castle-rb-3.0.0/lib/castle/header_formatter.rb @ line 7 Castle::HeaderFormatter#call:

    5: def call(header)
    6:   binding.pry
 => 7:   header.gsub(/^HTTP(?:_|-)/i, '').split(/_|-/).map(&:capitalize).join('-')
    8: end

[1] pry(#<Castle::HeaderFormatter>)> header
=> "action_dispatch.request.content_type"
[2] pry(#<Castle::HeaderFormatter>)>

From: /Users/brissmyr/.rvm/gems/ruby-2.2.4/gems/castle-rb-3.0.0/lib/castle/header_formatter.rb @ line 7 Castle::HeaderFormatter#call:

    5: def call(header)
    6:   binding.pry
 => 7:   header.gsub(/^HTTP(?:_|-)/i, '').split(/_|-/).map(&:capitalize).join('-')
    8: end

[1] pry(#<Castle::HeaderFormatter>)> header
=> :clearance
[2] pry(#<Castle::HeaderFormatter>)>
Completed 500 Internal Server Error in 179877ms

NoMethodError (undefined method `gsub' for :clearance:Symbol):
  app/controllers/sessions_controller.rb:11:in `create'

Too many dependencies

Woha, this was a worm can of dependencies! I don't want to pollute my project with activesupport etc. Would prefer if you minimized or didn't use any external dependencies, just use Net::HTTP, even if it can a be a pain, but it'll be a lot less pain for your customers. Writing my own custom integration now..

Add support for /approve and /report APIs

Our new APIs allow Admins to directly approve and report a device with a device token, not need to collect the request context. However, these APIs, included the Device fetching API, are not available via our SDK.

Can we add methods to the SDK that can handle these three APIs:
GET https://api.castle.io/v1/users/{user-id}/devices
PUT https://api.castle.io/v1/devices/{device_token}/approve
PUT https://api.castle.io/v1/devices/{device_token}/report

See more on each API here: https://castle.io/docs/device_management_tool

Rails 5 Rc1 Support

I have held off any attempts to upgrade to Rails 5 until I saw a release candidate. Now that the time has come, I am getting dependency conflicts mainly caused by the Her gem which this gem depends on:

-> bundle update rails

In Gemfile:
    devise_castle was resolved to 1.3.5, which depends on
      castle-rb (~> 1.2.13) was resolved to 1.2.13, which depends on
        her (~> 0.7) was resolved to 0.8.1, which depends on
          activemodel (<= 4.3.0, >= 3.0.0)

The Her gem has not made any progress on supporting Rails 5:

What are your thoughts? I am using the Devise_Castle gem and would imagine that other users will eventually want to upgrade to Rails 5 as well.

GDPR/Privacy API for data access/purge requests

Hello, we're looking into some better integration with the privacy APIs, i.e. data access and delete requests, as described in https://docs.castle.io/docs/gdpr-compliance.

I could not find anything about those APIs in the library / source code on a brief search. Is there any support planned in the library for those APIs as well?

For the time being we might just trigger the requests to the specified endpoints ourselves, but it'd be nice to have out of the box support within the library. Thanks.

Feature Request: Add support for multiple API environments

Consider the following:
Company A has a monolithic code base that serves more than one type of users that operate on the same data layer.
E.g.

  • Admins
  • Researchers
  • Customers
    They want to implement account takeover prevention, but they want to serve each different type of user a different flow.

It would be great if castle could support multiple API environments. So that Admin events could go to one environment and Researchers could be sent to another.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.