Update code2, and change secret protocol for code 1
This commit is contained in:
@@ -65,69 +65,43 @@ the notification instead while offline). The result page displays
|
||||
step when ready.
|
||||
|
||||
The robot submits code 1 with `POST /event/buzz` and a JSON body containing
|
||||
`delta_ms` (a signed integer) and `signature` (hex-encoded HMAC-SHA256). The
|
||||
signature is the HMAC of the UTF-8 decimal representation of `delta_ms`, using
|
||||
`CODE1_HMAC_SECRET` as the key. Set `CODE1_HMAC_SECRET` to a unique random
|
||||
secret of at least 32 bytes in `.env`; for example:
|
||||
`delta_ms` (a signed integer) and `secret` (the value of `CODE1_SECRET`).
|
||||
Set `CODE1_SECRET` to a unique random secret of at least 32 bytes in
|
||||
`.env`. For example, with the server running locally:
|
||||
|
||||
```json
|
||||
{"delta_ms":-42,"signature":"<64 hex characters>"}
|
||||
```sh
|
||||
curl.exe -X POST http://127.0.0.1:3000/event/buzz -H "Content-Type: application/json" -d '{"delta_ms":-42,"secret":"<your CODE1_SECRET value>"}'
|
||||
```
|
||||
|
||||
The integer `delta_ms` value is passed through to the congratulations message;
|
||||
a valid request returns HTTP `202`.
|
||||
|
||||
### Test code1 with Postman
|
||||
|
||||
Create a `POST` request to `http://127.0.0.1:3000/event/buzz`, select **Body >
|
||||
raw > JSON**, and use this body:
|
||||
|
||||
raw > JSON**, and send:
|
||||
```json
|
||||
{
|
||||
"delta_ms": {{delta_ms}},
|
||||
"signature": "{{buzz_signature}}"
|
||||
"delta_ms": -42,
|
||||
"secret": "<your CODE1_SECRET value>"
|
||||
}
|
||||
```
|
||||
|
||||
Add a Postman pre-request script to calculate the signature:
|
||||
|
||||
```javascript
|
||||
const deltaMs = pm.environment.get("delta_ms") || "-42";
|
||||
const secret = pm.environment.get("CODE1_HMAC_SECRET");
|
||||
|
||||
if (!secret) {
|
||||
throw new Error("Set CODE1_HMAC_SECRET in the active Postman environment");
|
||||
}
|
||||
|
||||
pm.environment.set("delta_ms", deltaMs);
|
||||
pm.environment.set(
|
||||
"buzz_signature",
|
||||
CryptoJS.HmacSHA256(String(deltaMs), secret).toString(CryptoJS.enc.Hex)
|
||||
);
|
||||
```
|
||||
|
||||
Set `CODE1_HMAC_SECRET` in the active Postman environment to the same value as
|
||||
in `.env`. Set `delta_ms` to a signed integer (for example `-42`) to change the
|
||||
test event. A successful request returns HTTP `202`.
|
||||
|
||||
### Test code1 with Node.js
|
||||
|
||||
With the server running and `CODE1_HMAC_SECRET` set in your shell, this sends a
|
||||
With the server running and `CODE1_SECRET` set in your shell, this sends a
|
||||
single test event:
|
||||
|
||||
```javascript
|
||||
const { createHmac } = require("node:crypto");
|
||||
|
||||
async function main() {
|
||||
const deltaMs = -42;
|
||||
const secret = process.env.CODE1_HMAC_SECRET;
|
||||
if (!secret) throw new Error("CODE1_HMAC_SECRET is not set");
|
||||
|
||||
const signature = createHmac("sha256", secret)
|
||||
.update(String(deltaMs), "utf8")
|
||||
.digest("hex");
|
||||
const secret = process.env.CODE1_SECRET;
|
||||
if (!secret) throw new Error("CODE1_SECRET is not set");
|
||||
|
||||
const response = await fetch("http://127.0.0.1:3000/event/buzz", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ delta_ms: deltaMs, signature }),
|
||||
body: JSON.stringify({ delta_ms: deltaMs, secret }),
|
||||
});
|
||||
|
||||
console.log(response.status, await response.text());
|
||||
|
||||
Reference in New Issue
Block a user