Commit c074e3623902edb2c44af47d0dcabd4a528f1d70

Authored by Evandro Junior
1 parent c062dee3

Serpro Captcha code

Showing 1 changed file with 18 additions and 1 deletions   Show diff stats
lib/noosfero/api/helpers.rb
@@ -209,6 +209,7 @@ @@ -209,6 +209,7 @@
209 ########################################## 209 ##########################################
210 210
211 def test_captcha(remote_ip, params, _environment = nil) 211 def test_captcha(remote_ip, params, _environment = nil)
  212 + binding.pry
212 environment ||= _environment 213 environment ||= _environment
213 d = environment.api_captcha_settings 214 d = environment.api_captcha_settings
214 return true unless d[:enabled] == true 215 return true unless d[:enabled] == true
@@ -226,8 +227,10 @@ @@ -226,8 +227,10 @@
226 end 227 end
227 228
228 if d[:provider] == 'serpro' 229 if d[:provider] == 'serpro'
229 - #TODO ADD SERPRO's CAPTCHA 230 + return verify_serpro_captcha(d[:serpro_client_id], params[:txtToken_captcha_serpro_gov_br], params[:captcha_text])
230 end 231 end
  232 +
  233 + raise ArgumentError, "Environment api_captcha_settings provider not defined"
231 end 234 end
232 235
233 def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field) 236 def verify_recaptcha_v1(remote_ip, private_key, api_recaptcha_verify_uri, recaptcha_challenge_field, recaptcha_response_field)
@@ -270,6 +273,20 @@ @@ -270,6 +273,20 @@
270 captcha_result["success"] ? true : captcha_result 273 captcha_result["success"] ? true : captcha_result
271 end 274 end
272 275
  276 + def verify_serpro_captcha(client_id, token, captcha_text)
  277 + verify_uri = 'http://homcaptcha.servicoscorporativos.serpro.gov.br/captchavalidar/1.0.0/validar'
  278 + if token == nil || captcha_text == nil
  279 + return _('Missing captcha data')
  280 + end
  281 + uri = URI(verify_uri)
  282 + https = Net::HTTP.new(uri.host, uri.port)
  283 + request = Net::HTTP::Post.new(uri.path)
  284 + verify_string = "#{client_id}&#{token}&#{captcha_text}"
  285 + request.body = verify_string
  286 + body = https.request(request).body
  287 + body == '1' ? true : body
  288 + end
  289 +
273 end 290 end
274 end 291 end
275 end 292 end