reCAPTCHA - pustki w POST - [ solved ]

Próbuję uruchomić: https://github.com/ambethia/recaptcha

Mam klucze, config itp.

W widoku:

<%= simple_form_for('pay_by_link', ... ) do |f| %>
  <%= f.input 'invoice', input_html: {value: @invoice.id, class: 'hidden'} %>
      ...
  <%= recaptcha_tags %>
  <%= f.submit %>
 <% end %>

No i problem polega na tym, że w POST nie leci zupełnie nic z recaptcha.
Captacha sama w sobie się wyświetla, mogę zaznaczać, odznaczać - czasem mi wyskakuje dodatkowa walidacje, ale co z tego skoro nie wysyła mi później żadnej informacji.

Ktoś przerabiał coś podobnego?

1 Like

Wszystkie pozostałe parametry oprócz g-recaptcha-response wysyłają się poprawnie?

Tak - tak wygląda zrzut params.inspect w metodzie create, gdzie wysyłany jest formularz:

<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"K9womX4bAobc61Kd4wvhB04zzhuIUlEj4cWpaT7V9O+u4T5FoavoZNpAqFgNhl7Jtt0BgZu1q+kRKW92FYpTGQ==", "pay_by_link"=>{"invoice"=>"4558", "to_pay"=>"61.0", "signature"=>"0ea6613f86236953910474947cd37faf"}, "commit"=>"Potwierdź", "controller"=>"pay_by_link", "action"=>"create"} permitted: false>

HTML wygląda tak (próbowałem też z opcją noscript: false - nic to nie zmienia,

<form class="simple_form " role="form" novalidate="novalidate" action="/pay_by_link" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" />
<input type="hidden" name="authenticity_token" value="9H6Z2PDwzwNHIEEIEgAtldU5HyCLx3pCpDU7TbsK0ptxQ48EL0Al4UGLu838jZJbLdfQupgggIhU2f1SkFV1bQ==" />
<input value="4558" class="string optional hidden" type="text" name="pay_by_link[invoice]" id="pay_by_link_invoice" />
<input value="61.0" class="string optional hidden" type="text" name="pay_by_link[to_pay]" id="pay_by_link_to_pay" />
<input value="0ea6613f86236953910474947cd37faf" class="string optional hidden" type="text" name="pay_by_link[signature]" id="pay_by_link_signature" />

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha text-center" data-sitekey="6Ley2TgUAAAAACaZHYvGv6ynrmIoagjyTlAcWTHw"></div>
<noscript>
  <div>
    <div style="width: 302px; height: 422px; position: relative;">
      <div style="width: 302px; height: 422px; position: absolute;">
        <iframe
          src="https://www.google.com/recaptcha/api/fallback?k=6Ley2TgUAAAAACaZHYvGv6ynrmIoagjyTlAcWTHw"
          frameborder="0" scrolling="no"
          style="width: 302px; height:422px; border-style: none;">
        </iframe>
      </div>
    </div>
    <div style="width: 300px; height: 60px; border-style: none;
      bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
      background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
      <textarea id="g-recaptcha-response" name="g-recaptcha-response"
        class="g-recaptcha-response"
        style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
        margin: 10px 25px; padding: 0px; resize: none;" value="">
      </textarea>
    </div>
  </div>
</noscript>
<input type="submit" name="commit" value="Potwierdź" class="payu-button btn text-center" data-disable-with="Potwierdź" />
</form>

Co mogę dodać - jest to na serwerze deweloperskim - odpalony jako rails s, który jest wystawiony przez nginx reverse proxy:

  server_name devxx.tdl;
    location / {

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass_header Server;
    proxy_set_header Host $host;
    proxy_pass http://IP_RAILS:9292;

    }

Dodatkowo: posnifowałem tcpdumpem i na port 9292 w post nie leci nic z captcha. Sniff z nginx - sniff na nginx - też cisza.

I jeszcze - zainstalowałem web sniff do chroma - nie widzę w POST nic z recaptcha :confused:

robisz jakieś czary mary hokus pokus a w chromie wystarczy F12 -> network, wyczyść, zaznacz preserve log, zrób request i wszystko zobaczysz (wiem, że to w ogóle Ci nie pomaga rozwiązać w tej chwili problemu, tak se napisałem :upside_down:)

Trochę obejście problemu, na razie testuję

<%= recaptcha_tags, noscript: false, script: true, id: 'captchaid', callback: 'verifyCallback' %>

W momencie pozytywnej weryfikacji wywołuje funkcję javascript:

var verifyCallback = function() {
}

Jest to jakieś rozwiązanie nad którym mogę dalej popracować.

Ok, problem rozwiązałem. Chyba ten gem nie działa tak jak powinien.

Zatem co zrobiłem:

W formularzu:

<%= recaptcha_tags class: "text-center", noscript: false, script: true, id: 'captchaid', callback: 'verifyCallback' %>
<input value="" class="hidden" type="text" name="g-recaptcha-response" id="g-recaptcha-response-id" />

Javascript

<script type="text/javascript">
    var verifyCallback = function(response) {
      console.log(response);
      $('#captcha-submit').removeAttr('disabled');
      $('#g-recaptcha-response-id').val(response);
  };
</script>

Zrobiłem verify_recaptcha_service.rb

class VerifyRecaptchaService

attr_accessor :secret, :response, :url

def initialize(response)
  @secret = Recaptcha.configuration.secret_key
  @response = response
  @url = 'https://www.google.com/recaptcha/api/siteverify'
end

def call
  begin
    params = {
      secret: @secret,
      response: @response
    }
    ret = Net::HTTP.post_form(URI.parse(@url), params)
    JSON.parse(ret.body)['success']
  rescue StandardError => _e
    false
  end
end  

end

W kontrolerze wywołuję:

VerifyRecaptchaService.new(params['g-recaptcha-response']).call