Skip to content

Commit cfa7e32

Browse files
make it generic
1 parent bbaacac commit cfa7e32

2 files changed

Lines changed: 47 additions & 14 deletions

File tree

readme.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,39 @@ The plugin:
1515
5. Forwards the request to your backend unchanged when the signature is valid.
1616
6. Returns **401 Unauthorized** when the signature is missing or invalid.
1717

18+
## Create a route to receive GitHub webhooks
19+
20+
```shell
21+
$ curl -X POST 'http://otoroshi-api.oto.tools:8080/api/routes' \
22+
-H "Content-type: application/json" \
23+
-u 'admin-api-apikey-id:admin-api-apikey-secret' \
24+
-d '{
25+
"name": "github-webhook-receiver",
26+
"frontend": {
27+
"domains": ["webhooks.oto.tools/github"]
28+
},
29+
"backend": {
30+
"targets": [{
31+
"hostname": "my-backend.example.com",
32+
"port": 443,
33+
"tls": true
34+
}]
35+
},
36+
"plugins": [
37+
{
38+
"enabled": true,
39+
"plugin": "cp:otoroshi_plugins.com.cloud.apim.otoroshi.plugins.webhook.WebhookPayloadValidator",
40+
"config": {
41+
"secret": "your-github-webhook-secret",
42+
"signature_header": "X-Hub-Signature-256",
43+
"algorithm": "HmacSHA256",
44+
"prefix": "sha256="
45+
}
46+
}
47+
]
48+
}'
49+
```
50+
1851
## Create a route to receive YouSign webhooks
1952

2053
```shell
@@ -50,17 +83,17 @@ $ curl -X POST 'http://otoroshi-api.oto.tools:8080/api/routes' \
5083

5184
## Plugin configuration
5285

53-
| Field | Type | Required | Default | Description |
54-
|--------------------|----------|----------|----------------------------|-------------------------------------------------------------------------------------|
55-
| `secret` | `string` | yes | | The HMAC secret shared with the webhook provider. |
56-
| `signature_header` | `string` | no | `X-Yousign-Signature-256` | Name of the HTTP header that carries the signature. |
57-
| `algorithm` | `string` | no | `HmacSHA256` | Java HMAC algorithm name. Supported values: `HmacSHA256`, `HmacSHA512`, `HmacSHA384`, `HmacSHA1`. |
58-
| `prefix` | `string` | no | derived from `algorithm` | String prepended to the hex hash before comparison (e.g. `sha256=`). Defaults are derived automatically from the chosen algorithm. |
86+
| Field | Type | Required | Default | Description |
87+
|--------------------|----------|----------|--------------------------|-------------------------------------------------------------------------------------|
88+
| `secret` | `string` | yes || The HMAC secret shared with the webhook provider. |
89+
| `signature_header` | `string` | no | `X-Hub-Signature-256` | Name of the HTTP header that carries the signature. |
90+
| `algorithm` | `string` | no | `HmacSHA256` | Java HMAC algorithm name. Supported values: `HmacSHA256`, `HmacSHA512`, `HmacSHA384`, `HmacSHA1`. |
91+
| `prefix` | `string` | no | derived from `algorithm` | String prepended to the hex hash before comparison (e.g. `sha256=`). Defaults are derived automatically from the chosen algorithm. |
5992

6093
```json
6194
{
6295
"secret": "your-webhook-secret",
63-
"signature_header": "X-Yousign-Signature-256",
96+
"signature_header": "X-Hub-Signature-256",
6497
"algorithm": "HmacSHA256",
6598
"prefix": "sha256="
6699
}
@@ -77,11 +110,11 @@ $ curl -X POST 'http://otoroshi-api.oto.tools:8080/api/routes' \
77110

78111
## Responses
79112

80-
| Status | Body | Meaning |
81-
|--------|------|---------|
82-
| forwarded to backend || Signature is valid, request is passed through unchanged. |
83-
| `401 Unauthorized` | `{ "error": "missing X-Yousign-Signature-256 header" }` | The header was not present in the incoming request. |
84-
| `401 Unauthorized` | `{ "error": "invalid signature" }` | The computed HMAC does not match the header value. |
113+
| Status | Body | Meaning |
114+
|--------|------------------------------------------------|---------|
115+
| forwarded to backend | | Signature is valid, request is passed through unchanged. |
116+
| `401 Unauthorized` | `{ "error": "missing xxxx header" }` | The header was not present in the incoming request. |
117+
| `401 Unauthorized` | `{ "error": "invalid signature" }` | The computed HMAC does not match the header value. |
85118
| `401 Unauthorized` | `{ "error": "webhook secret not configured" }` | The plugin `secret` field is empty. |
86119

87120

src/main/scala/com/cloud/apim/otoroshi/plugins/webhook/validator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import scala.util.{Failure, Success, Try}
1919

2020
case class WebhookValidatorConfig(
2121
secret: String = "",
22-
signatureHeader: String = "X-Yousign-Signature-256",
22+
signatureHeader: String = "X-Hub-Signature-256",
2323
algorithm: String = "HmacSHA256",
2424
prefix: String = "sha256=",
2525
) extends NgPluginConfig {
@@ -39,7 +39,7 @@ object WebhookValidatorConfig {
3939
val algo = json.select("algorithm").asOpt[String].getOrElse("HmacSHA256")
4040
WebhookValidatorConfig(
4141
secret = json.select("secret").asOpt[String].getOrElse(""),
42-
signatureHeader = json.select("signature_header").asOpt[String].getOrElse("X-Yousign-Signature-256"),
42+
signatureHeader = json.select("signature_header").asOpt[String].getOrElse("X-Hub-Signature-256"),
4343
algorithm = algo,
4444
prefix = json.select("prefix").asOpt[String].getOrElse(WebhookValidatorConfig.defaultPrefix(algo)),
4545
)

0 commit comments

Comments
 (0)