#1 2018-01-08 10:11:11

esmondb
Member
From: London
Registered: 2010-07-20
Posts: 299

TLS and recaptcha

Thank you for adding TLS support. I've tried google's recaptcha and gmail and it seems to work fine. Only thing I've noticed is that when sending emails only port 465 works and not 587.

In case it's useful to anyone below is some code that processes a simple html feedback form with a recaptcha:

Type
  Trecaptcha = packed record
    success: boolean;
    challenge_ts: RawUTF8;
    hostname: RawUTF8;
  end;

procedure TMyServer.sendFeedback(cTxt: TSQLRestServerURIContext);
var
  feedbackEmail, feedbackMessage, gRecaptchaResponse: RawUTF8;
  recaptchaSocket: THttpClientSocket;
  P: PUTF8Char;
  response: Trecaptcha;
  Server: TSMTPConnection;
begin
  P := PUTF8Char(cTxt.Call.InBody);
  if UrlDecodeNeedParameters(P, 'EMAIL,MESSAGE,G-RECAPTCHA-RESPONSE') then begin
    while P<>nil do begin
      UrlDecodeValue(P,'EMAIL=', feedbackEmail);
      UrlDecodeValue(P,'MESSAGE=', feedbackMessage);
      UrlDecodeValue(P,'G-RECAPTCHA-RESPONSE=', gRecaptchaResponse, @P);
    end;
    if (feedbackEmail = '') or (feedbackMessage = '') or (gRecaptchaResponse = '') then
      Ctxt.Error('Missing Parameter')
    else begin
      recaptchaSocket:= THttpClientSocket.Open(
           'www.google.com','443',cslTCP, 10000, true);
      try
        recaptchaSocket.Post('recaptcha/api/siteverify',
          'secret=XXXXXX&response='+
          gRecaptchaResponse + '&remoteip=' + Ctxt.RemoteIP, 'application/x-www-form-urlencoded');
        RecordLoadJSON(response, pointer(recaptchaSocket.Content), TypeInfo(Trecaptcha));
        if response.success then begin
          if SendEmail('smtp.gmail.com', 'me@me.net',
            'me@me.net',
            'Feedback Form From: ' + feedbackEmail, feedbackMessage, '',
            'me@me.net','xxx', '465', '', true)
          then
            Ctxt.Success
          else
            Ctxt.Error('Sorry, message sending failure');
        end else
          Ctxt.Error('recaptcha error');
      finally
        recaptchaSocket.Free;
      end;
    end;
  end else
    Ctxt.Error('Missing Parameter');
end;

const
  __recaptcha = 'success: boolean; challenge_ts: RawUTF8; hostname: RawUTF8;';

Initialization
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(Trecaptcha),__recaptcha);

Last edited by esmondb (2018-01-08 11:35:41)

Offline

#2 2018-01-08 17:02:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TLS and recaptcha

Nice!

Offline

Board footer

Powered by FluxBB