Skip to content

Instantly share code, notes, and snippets.

@brissmyr
Last active June 21, 2021 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brissmyr/0967ce31fe95c3743a8b5f0d9dfadbc4 to your computer and use it in GitHub Desktop.
Save brissmyr/0967ce31fe95c3743a8b5f0d9dfadbc4 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'json'
api_secret = {you_api_secret}
token = request.params['castle_request_token']
sanitized_headers = {}
headers.each_with_object({}) do |(name, value), acc|
sanitized_headers[name] = if %w[Cookie Authorization].include?(name.to_s)
true
else
value
end
end
uri = URI.parse("https://api.castle.io/v1/risk") # change to /v1/log for $login $failed
castle = Net::HTTP::Post.new(uri)
castle.basic_auth("", api_secret)
castle.content_type = "application/json"
castle.body = JSON.dump({
event: "$login",
status: "$succeeded", # "$failed" for /v1/log
request_token: token, # remove for /v1/log
user: {
id: current_user.id, # optional (if there's a matching account) for /v1/log
email: current_user.email
},
context: {
ip: request.ip, # or a IP header from your CDN, e.g. CF-Connecting-IP
headers: sanitized_headers
}
})
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(castle)
end
if response.code.to_i >= 300
puts 'Error!'
end
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment