<commit_before>/*-
 * #%L
 * FWAPP Framework
 * %%
 * Copyright (C) 2016 - 2017 Open Design Flow
 * %%
 * This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
package org.odfi.wsb.fwapp.lib.security.provider.google

import org.odfi.indesign.core.module.IndesignModule
import com.idyria.osi.wsb.webapp.http.message.HTTPRequest
import com.idyria.osi.wsb.core.network.connectors.tcp.TCPNetworkContext
import com.idyria.osi.ooxoo.core.buffers.structural.ElementBuffer
import com.idyria.osi.ooxoo.lib.json.JSonUtilTrait
import com.idyria.osi.ooxoo.core.buffers.datatypes.BooleanBuffer
import com.idyria.osi.ooxoo.core.buffers.structural.xelement
import com.idyria.osi.ooxoo.core.buffers.structural.XList
import com.idyria.osi.ooxoo.core.buffers.datatypes.XSDStringBuffer
import java.net.URL
import java.net.HttpURLConnection
import com.idyria.osi.tea.io.TeaIOUtils
import com.google.api.client.json.jackson2.JacksonParser
import com.google.api.client.json.jackson2.JacksonParser
import com.google.api.client.json.jackson2.JacksonFactory

object GoogleRecaptchaModule extends IndesignModule {

  class GoogleRecaptchaResponse extends ElementBuffer with JSonUtilTrait {
    
    @xelement(name = "success")
    var success : BooleanBuffer = null
    
    
    @xelement(name = "error-codes")
    var errorCodes = new XList[XSDStringBuffer](_.value)
    
  }
  
  
  def verify(req: HTTPRequest, resp: String) = GoogleRecaptchaModule.config.get.getString("serverSiteKey") match {
    case Some(serverKey) =>

      var userIp = req.networkContext.get.asInstanceOf[TCPNetworkContext].getRemoteHostIP

      println("Verifying Key for: " + userIp)
      var url = s"""https://www.google.com/recaptcha/api/siteverify?secret=${serverKey}&response=$resp&remoteip=$userIp"""
      
      //-- Call
      var urlConn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
      urlConn.getResponseCode match {
        case 200 => 
          
          var is = urlConn.getInputStream
          var jsonResp = new String(TeaIOUtils.swallowStream(is))
          println("Resp: "+jsonResp)
          """"success"\s*:\s*true""".r.findFirstIn(jsonResp).isDefined match {<commit_msg>Fixed Google Recaptcha interface for new XList<commit_after>/*-
 * #%L
 * FWAPP Framework
 * %%
 * Copyright (C) 2016 - 2017 Open Design Flow
 * %%
 * This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
package org.odfi.wsb.fwapp.lib.security.provider.google

import org.odfi.indesign.core.module.IndesignModule
import com.idyria.osi.wsb.webapp.http.message.HTTPRequest
import com.idyria.osi.wsb.core.network.connectors.tcp.TCPNetworkContext
import com.idyria.osi.ooxoo.core.buffers.structural.ElementBuffer
import com.idyria.osi.ooxoo.lib.json.JSonUtilTrait
import com.idyria.osi.ooxoo.core.buffers.datatypes.BooleanBuffer
import com.idyria.osi.ooxoo.core.buffers.structural.xelement
import com.idyria.osi.ooxoo.core.buffers.structural.XList
import com.idyria.osi.ooxoo.core.buffers.datatypes.XSDStringBuffer
import java.net.URL
import java.net.HttpURLConnection
import com.idyria.osi.tea.io.TeaIOUtils
import com.google.api.client.json.jackson2.JacksonParser
import com.google.api.client.json.jackson2.JacksonParser
import com.google.api.client.json.jackson2.JacksonFactory
import com.idyria.osi.ooxoo.core.buffers.structural.DataUnit

object GoogleRecaptchaModule extends IndesignModule {

  class GoogleRecaptchaResponse extends ElementBuffer with JSonUtilTrait {
    
    @xelement(name = "success")
    var success : BooleanBuffer = null
    
    
    @xelement(name = "error-codes")
    var errorCodes = XList[XSDStringBuffer]({ du : DataUnit => XSDStringBuffer(du.value)})
    
  }
  
  
  def verify(req: HTTPRequest, resp: String) = GoogleRecaptchaModule.config.get.getString("serverSiteKey") match {
    case Some(serverKey) =>

      var userIp = req.networkContext.get.asInstanceOf[TCPNetworkContext].getRemoteHostIP

      println("Verifying Key for: " + userIp)
      var url = s"""https://www.google.com/recaptcha/api/siteverify?secret=${serverKey}&response=$resp&remoteip=$userIp"""
      
      //-- Call
      var urlConn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
      urlConn.getResponseCode match {
        case 200 => 
          
          var is = urlConn.getInputStream
          var jsonResp = new String(TeaIOUtils.swallowStream(is))
          println("Resp: "+jsonResp)
          """"success"\s*:\s*true""".r.findFirstIn(jsonResp).isDefined match {
{'fraction_non_alphanumeric': 0.10003547357218871, 'fraction_numerical': 0.005321035828307911, 'mean_word_length': 4.02584670231729, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6893887', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1706, 'n_words': 422}