Class: Rack::Picatcha
- Inherits:
-
Object
- Object
- Rack::Picatcha
- Defined in:
- lib/rack/picatcha.rb,
lib/rack/picatcha/helpers.rb
Defined Under Namespace
Modules: Helpers Classes: PicatchaError
Constant Summary collapse
- API_URL =
'http://api.picatcha.com'
- API_SECURE_URL =
'https://api.picatcha.com'
- VERIFY_URL =
'http://api.picatcha.com/v'
- CHALLENGE_FIELD =
'picatcha'
- SKIP_VERIFY_ENV =
['test', 'cucumber']
Class Attribute Summary collapse
-
.private_key ⇒ Object
Returns the value of attribute private_key.
-
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
.proxy_password ⇒ Object
Returns the value of attribute proxy_password.
-
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
.proxy_user ⇒ Object
Returns the value of attribute proxy_user.
-
.public_key ⇒ Object
Returns the value of attribute public_key.
-
.test_mode ⇒ Object
Returns the value of attribute test_mode.
Class Method Summary collapse
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Picatcha
constructor
Initialize the Rack Middleware.
- #verify(options = {}) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Picatcha
Initialize the Rack Middleware. Some of the available options are:
:public_key -- your Picatcha API public key *(required)*
:private_key -- your Picatcha API private key *(required)*
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rack/picatcha.rb', line 26 def initialize(app, = {}) @app = app @paths = [:paths] && [[:paths]].flatten.compact self.class.private_key = [:private_key] self.class.public_key = [:public_key] self.class.proxy_host = [:proxy_host] self.class.proxy_port = [:proxy_port] self.class.proxy_user = [:proxy_user] self.class.proxy_password = [:proxy_password] end |
Class Attribute Details
.private_key ⇒ Object
Returns the value of attribute private_key.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def private_key @private_key end |
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def proxy_host @proxy_host end |
.proxy_password ⇒ Object
Returns the value of attribute proxy_password.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def proxy_password @proxy_password end |
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def proxy_port @proxy_port end |
.proxy_user ⇒ Object
Returns the value of attribute proxy_user.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def proxy_user @proxy_user end |
.public_key ⇒ Object
Returns the value of attribute public_key.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def public_key @public_key end |
.test_mode ⇒ Object
Returns the value of attribute test_mode.
14 15 16 |
# File 'lib/rack/picatcha.rb', line 14 def test_mode @test_mode end |
Class Method Details
.test_mode!(options = {}) ⇒ Object
16 17 18 19 |
# File 'lib/rack/picatcha.rb', line 16 def test_mode!( = {}) value = [:return] self.test_mode = value.nil? ? true : [:return] end |
.verify_picatcha(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rack/picatcha.rb', line 75 def self.verify_picatcha( = {}) if !.is_a? Hash = {:model => } end ipaddr = [:ipaddr] proxy_host = [:proxy_host] proxy_port = [:proxy_port] proxy_user = [:proxy_user] proxy_password = [:proxy_password] private_key = [:private_key] picatchadata = [:picatcha] data = { "k" => private_key, "ip" => ipaddr, "ua"=> "Rack-Picatcha Ruby Gem", "s" => picatchadata[:stages], #the number of stages "t" => picatchadata[:token], #the challenge token "r" => picatchadata[:r] #the array of images } payload = data.to_json # LOGGER.debug "payload = #{payload}" uri = URI.parse(VERIFY_URL) http = Net::HTTP.start(uri.host, uri.port) if proxy_host && proxy_port http = Net::HTTP.Proxy(proxy_host, proxy_port, proxy_user, proxy_password).start(uri.host, uri.port) end request = Net::HTTP::Post.new(uri.path) request.body = payload response = http.request(request) # debugging info # LOGGER.debug "response.body = #{response.body}" if response.body !=nil parsed_json = JSON(response.body) else return false, 'No reponse captured' end # LOGGER.error "error? = #{parsed_json["e"]}" error = parsed_json["e"] # so far just a simple if.. else to check if the picatcha was # solved correctly. will revisit later and make it more # verbose if parsed_json["s"]==true return true, "" else = "Sorry, you incorrectly filled out Picatcha. Please try again." = I18n.translate(:'picatcha.errors.verification_failed', :default => ) if defined?(I18n) return false, end end |
Instance Method Details
#_call(env) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rack/picatcha.rb', line 42 def _call(env) request = Request.new(env) # LOGGER.debug "def _call env=#{env} params=#{request.params}" if request.params[CHALLENGE_FIELD] value, msg = verify({ :request => request, :ip => request.ip, :challenge => request.params[CHALLENGE_FIELD] }) env.merge!('picatcha.valid' => value == 'true', 'picatcha.msg' => msg) end @app.call(env) end |
#call(env) ⇒ Object
37 38 39 40 |
# File 'lib/rack/picatcha.rb', line 37 def call(env) # LOGGER.debug "def call" dup._call(env) end |
#verify(options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rack/picatcha.rb', line 56 def verify( = {}) if !.is_a? Hash = {:model => } end model = [:model] request = [:request] return Rack::Picatcha.verify_picatcha({ :private_key => Rack::Picatcha.private_key, :ipaddr => request.ip, :proxy_host => self.class.proxy_host, :proxy_port => self.class.proxy_port, :proxy_user => self.class.proxy_user, :proxy_password => self.class.proxy_password, :picatcha => request.params["picatcha"] }) end |