Skip to content

Commit ee14633

Browse files
authored
Merge pull request #75 from FriendlyCaptcha/self-hosted-endpoint-guide
Add documentation for setting up a Self-Hosted Endpoint
2 parents 480068c + af48ded commit ee14633

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Self-Hosted Endpoint
2+
3+
:::info
4+
5+
The Self-Hosted Endpoint feature is only available for Friendly Captcha v2.
6+
7+
:::
8+
9+
When a customer website or application loads a Friendly Captcha widget, the widget makes a number of requests to the Friendly Captcha API. The API endpoint is `global.frcapi.com`, or `eu.frcapi.com` for customers who use [the EU Endpoint](./eu-endpoint). Friendly Captcha offers the **Self-Hosted Endpoint** feature for customers who prefer to route all end-user traffic through their own infrastructure.
10+
11+
To use a Self-Hosted Endpoint, customers operate a proxy server that forwards widget requests to the Friendly Captcha API. Because the Friendly Captcha service depends on data sent by the widget, it is important to ensure that a Self-Hosted Endpoint correctly forwards that data.
12+
13+
## Setup
14+
15+
There are 3 steps to setting up a Self-Hosted Endpoint, explained in detail below.
16+
17+
1. [Generate a proxy key in the Friendly Captcha dashboard.](#1-generate-a-proxy-key)
18+
2. [Configure your web server to proxy widget requests to the Friendly Captcha API.](#2-configure-your-web-server)
19+
3. [Configure your widget to use your Self-Hosted Endpoint.](#3-configure-your-site-or-apps-widget)
20+
21+
### 1. Generate a proxy key
22+
23+
To verify that proxied widget requests come from your infrastructure, you must set a header that contains a proxy key. You can generate a key in the [Friendly Captcha dashboard](https://app.friendlycaptcha.eu/dashboard/accounts/-/keys/proxy). Make sure to generate a **Proxy Key**; API keys are not accepted. Store the generated key somewhere safe and retrievable—Friendly Captcha doesn't keep a copy of the key, so you will have to regenerate it if you lose it. You will use this proxy key in the next step.
24+
25+
### 2. Configure your web server
26+
27+
You need to configure your web server to forward the following requests to the Friendly Captcha API:
28+
29+
```
30+
GET /api/v2/captcha/agent
31+
GET /api/v2/captcha/widget
32+
GET /api/v2/captcha/ping
33+
POST /api/v2/captcha/activate
34+
POST /api/v2/captcha/quote
35+
POST /api/v2/captcha/redeem
36+
```
37+
38+
For these requests, your server should forward the request in its entirety, including all headers. Additionally, the forwarded requests should include two ***extra*** headers:
39+
40+
1. `X-Frc-Proxy-Key`: The proxy key you generated in the Friendly Captcha dashboard.
41+
2. `X-Frc-Proxy-Client-IP`: The original (source) IP address of the end user.
42+
43+
Forward the requests to this endpoint (i.e., the upstream server):
44+
45+
```
46+
https://global.proxy.frcapi.com
47+
```
48+
49+
If you have access to [the EU Endpoint](./eu-endpoint.md), you may alternatively forward the requests to this endpoint:
50+
51+
```
52+
https://eu.proxy.frcapi.com
53+
```
54+
55+
[See below](#example-routing-configurations) for examples of how to configure a server to correctly proxy the widget requests.
56+
57+
:::warning Note
58+
59+
If you don't forward the entire request and its headers, the widget may still be functional, but the service will operate in a degraded state. If you forget either of the additional proxy headers (`X-Frc-Proxy-Key` and `X-Frc-Proxy-Client-IP`), the widget will display an error message.
60+
61+
:::
62+
63+
### 3. Configure your site or app's widget
64+
65+
The final step is to configure your server's URL as the API endpoint for your widget. This will ensure that the widget sends its requests to your server (which will then forward them to the upstream Friendly Captcha API). If your server's URL is `https://example.com` and you configure the widget using HTML `data-` attributes, your markup will look something like this:
66+
67+
```html
68+
<div class="frc-captcha" data-sitekey="<my sitekey>" data-api-endpoint="https://example.com"></div>
69+
```
70+
71+
If you configure the widget using the JavaScript SDK, your code will look something like this:
72+
73+
```js
74+
import { FriendlyCaptchaSDK } from "@friendlycaptcha/sdk"
75+
const sdk = new FriendlyCaptchaSDK();
76+
77+
const mount = document.querySelector(".my-widget");
78+
const widget = sdk.createWidget({
79+
element: mount,
80+
sitekey: "<my sitekey>",
81+
apiEndpoint: "https://example.com",
82+
});
83+
```
84+
85+
For more documentation, see the page on [configuring the Friendly Captcha widget](../sdk/configuration.md).
86+
87+
## Example Routing Configurations
88+
89+
Provided below are some example Self-Hosted Endpoints configurations for popular web server technologies. If you copy and paste the configuration, make sure to replace `<% PROXY KEY %>` with your real proxy key, and verify that you're using the correct upstream Friendly Captcha API endpoint.
90+
91+
### Apache
92+
93+
```
94+
LoadModule proxy_module modules/mod_proxy.so
95+
LoadModule proxy_http_module modules/mod_proxy_http.so
96+
LoadModule headers_module modules/mod_headers.so
97+
98+
<LocationMatch "^/api/v2/captcha/(agent|widget|ping|activate|quote|redeem)(/.*)?$">
99+
RequestHeader set X-Frc-Proxy-Key "<% PROXY KEY %>"
100+
RequestHeader set X-Frc-Proxy-Client-IP expr=%{REMOTE_ADDR}
101+
102+
ProxyPass https://global.proxy.frcapi.com
103+
ProxyPassReverse https://global.proxy.frcapi.com
104+
</LocationMatch>
105+
```
106+
107+
### Caddy
108+
109+
```
110+
@captcha_paths path_regexp ^/api/v2/captcha/(agent|widget|ping|activate|quote|redeem)(/.*)?$
111+
112+
reverse_proxy @captcha_paths https://global.proxy.frcapi.com {
113+
header_up X-Frc-Proxy-Key "<% PROXY KEY %>"
114+
header_up X-Frc-Proxy-Client-IP {remote_host}
115+
}
116+
```
117+
118+
### HAProxy
119+
120+
```
121+
frontend http-in
122+
# other frontend configuration...
123+
124+
acl is_captcha_path path_reg ^/api/v2/captcha/(agent|widget|ping|activate|quote|redeem)(/.*)?$
125+
use_backend captcha_paths if is_captcha_path
126+
127+
backend captcha_paths
128+
mode http
129+
130+
http-request set-header X-Frc-Proxy-Key "<% PROXY KEY %>"
131+
http-request set-header X-Frc-Proxy-Client-IP %[src]
132+
133+
# Note: the path to the certificates file may be different for your OS.
134+
server frc_api global.proxy.frcapi.com:443 ssl verify required ca-file /etc/ssl/certs/ca-certificates.crt
135+
```
136+
137+
### NGINX
138+
139+
```
140+
location ~ ^/api/v2/captcha/(agent|widget|ping|activate|quote|redeem)(/.*)?$ {
141+
proxy_set_header X-Frc-Proxy-Key <% PROXY KEY %>;
142+
proxy_set_header X-Frc-Proxy-Client-IP $remote_addr;
143+
144+
proxy_pass https://global.proxy.frcapi.com;
145+
}
146+
```

sidebars.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ const sidebars = {
107107
]
108108
},
109109
"guides/migrating-from-recaptcha",
110-
"guides/migrating-from-hcaptcha"
110+
"guides/migrating-from-hcaptcha",
111+
{
112+
"type": "doc",
113+
"id": "guides/self-hosted-endpoint",
114+
"className": "sidebar-hidden"
115+
}
111116
]
112117
},
113118
{

0 commit comments

Comments
 (0)