Skip to content

flipboxstudio/f-cap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

F-Cap

F-Cap is a simple implementation of Captcha Service. This is the most bare minimum of captcha service, so don't expect too much. We can grow this service even more later.

Quick Run

yarn install # if you don't have yarn, you can just `npm install`
npm run dev  # run a local server with auto reload enabled

Running in Production

yarn install  # if you don't have yarn, you can just `npm install`
npm run build # convert from es6 syntax to node module syntax
npm run serve # serve an application

Basic request

var settings = {
  'url': '{{ YOUR-DOMAIN-HERE }}',
  'method': 'POST',
  'headers': {
    'accept': 'application/json',
    'x-request-identifier': '{{ A-VALID-GUID }}',
  }
}

$.ajax(settings).done(function (response) {
  console.log(response.captcha) // An SVG string, you may insert this response directly to your DOM

  $('#f-cap').append($(response.captcha))
});

Keep in mind, A-VALID-GUID should follows RFC4122. You can generate this GUID using UUID Package. For instance:

const uuid = require('uuid')

let guid = uuid.v1()

NOTE If you don't send headers Accept: application/json, then the server's response should be a blob which is an SVG binary.

Validating User Input

From that response, you may validate user input using this method:

var settings = {
  'url': '{{ YOUR-DOMAIN-HERE }}/validate',
  'method': 'POST',
  'headers': {
    'x-request-identifier': '{{ YOUR-PREVIOUS-GUID-HERE }}',
    'x-challenge': 'southerners',
    'cache-control': 'no-cache'
  }
}

$.ajax(settings).done(function (response) {
  // User input is valid
  // Below is the response
  // {
  //   "valid": true,
  //   "data": {
  //     "text": "southerners",
  //     "ip": "{{ REQUESTED-IP }}", // You may validate sender IP here, currently we're not validating it, maybe next.
  //     "identifier": "{{ YOUR-PREVIOUS-GUID-HERE }}"
  //   }
  // }
}).error(function (response) {
  // User input is invalid
});

NOTE The server using cache to validate user input based on their captcha. The default captcha TTL is 120 seconds. So if after 120 seconds user doesn't validate their input, the response may return an error response. When this happens server will return 426 status code. You can configure this via CACHE_TTL environment variable.

Releases

No releases published

Packages

No packages published