# Check a signed request

Check a request with no signature, sign the same request with your agent's Ed25519 key, check it again, see a changed request fail, and read the decision back.

In this quickstart you check a request the way your server would, then sign the same request as an agent and check it again. Provenance names the signed agent, and refuses the signature once the request changes. You use curl and OpenSSL 3, or Node, or Python, with a published sandbox key, so you don't need an account.

### 1. Set your key and register an agent

The sandbox account key can register agents and check requests. Every key is listed on [Sandbox and keys](https://provenanceauth.com/docs/get-started/sandbox.md). Register the agent that will sign, then publish the public half of its Ed25519 key; the private key stays with you.

**curl**

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5

openssl genpkey -algorithm ed25519 -out agent-key.pem
export AGENT_X=$(openssl pkey -in agent-key.pem -pubout -outform DER | tail -c 32 | openssl base64 -A | tr '+/' '-_' | tr -d '=')

curl -s $PROVENANCE_API/v1/mandate/agents \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "name": "Pantry Pal", "description": "Shops for groceries for a person", "category": "shopping_agent" }' > agent.json
export AGENT_ID=$(jq -r .id agent.json)

curl -s $PROVENANCE_API/v1/identify/agents/$AGENT_ID/signing_keys \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "jwk": { "kty": "OKP", "crv": "Ed25519", "x": "'"$AGENT_X"'" } }' | tee signing-key.json
```

**Node**

```ts
import { Provenance, generateEd25519Jwk } from "@provenanceauth/sdk";

const api = process.env.PROVENANCE_API!;
const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: api });
const agent = await prov.mandate.agents.create({ name: "Pantry Pal", description: "Shops for groceries for a person", category: "shopping_agent" });
const { publicJwk, privateJwk } = await generateEd25519Jwk(); // keep privateJwk in your secret store
const key = await prov.identify.addSigningKey(agent.id, { jwk: publicJwk });
```

**Python**

```python
import os, httpx
from provenanceauth import Provenance
from provenanceauth.web_bot_auth import generate_ed25519_jwk

api = os.environ["PROVENANCE_API"]
prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=api)
agent = httpx.post(f"{api}/v1/mandate/agents", headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"},
                   json={"name": "Pantry Pal", "description": "Shops for groceries for a person", "category": "shopping_agent"}).json()
public_jwk, private_jwk = generate_ed25519_jwk()  # keep private_jwk in your secret store
key = prov.identify.add_signing_key(agent["id"], jwk=public_jwk)
```

```json
{
  "object": "agent_signing_key",
  "status": "active"
}
```

The response's `kid` is the key's thumbprint, the `keyid` every signature names. `directory_url` is the public key directory a site fetches it from, with no key.

```bash
export KEY_ID=$(jq -r .kid signing-key.json)
```

### 2. Check a request with no signature

Send the request as your server received it to [`POST /v1/identify/assessments`](https://provenanceauth.com/docs/api/traffic-checks.md#post-v1-identify-assessments). curl declares itself in its user agent, so the verdict is `declared_bot`.

**curl**

```bash
curl -s $PROVENANCE_API/v1/identify/assessments \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "request": { "method": "GET", "url": "https://quillstack.example/products/linen-a5", "headers": { "user-agent": "curl/8.7.1" } } }'
```

**Node**

```ts
const unsigned = await prov.identify.assess({
  request: { method: "GET", url: "https://quillstack.example/products/linen-a5", headers: { "user-agent": "curl/8.7.1" } },
});
```

**Python**

```python
unsigned = prov.identify.assess(request={
    "method": "GET", "url": "https://quillstack.example/products/linen-a5", "headers": {"user-agent": "curl/8.7.1"},
})
```

```json
{
  "object": "traffic_assessment",
  "class": "declared_bot",
  "agent": null
}
```

### 3. Sign the request and check it again

Sign the same request as Web Bot Auth defines it (RFC 9421 HTTP message signatures): the method, the host, the path and the `Signature-Agent` header that points at your agent's key directory. The bash tab builds the signature base and signs it with OpenSSL, so the signature is fresh when you run it.

**curl**

```bash
export SIGNATURE_AGENT="sig1=\"$PROVENANCE_API/v1/public/agents/$AGENT_ID/directory\";type=jwks_uri"
export CREATED=$(date +%s)
export SIGNATURE_PARAMS="(\"@method\" \"@authority\" \"@path\" \"signature-agent\";key=\"sig1\");created=$CREATED;expires=$((CREATED + 300));keyid=\"$KEY_ID\";alg=\"ed25519\";tag=\"web-bot-auth\""
printf '"@method": GET\n"@authority": quillstack.example\n"@path": /products/linen-a5\n"signature-agent";key="sig1": "%s";type=jwks_uri\n"@signature-params": %s' \
  "$PROVENANCE_API/v1/public/agents/$AGENT_ID/directory" "$SIGNATURE_PARAMS" > signature-base.txt
export SIGNATURE="sig1=:$(openssl pkeyutl -sign -rawin -inkey agent-key.pem -in signature-base.txt | openssl base64 -A):"

curl -s $PROVENANCE_API/v1/identify/assessments \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "request": { "method": "GET", "url": "https://quillstack.example/products/linen-a5", "headers": {
    "user-agent": "PantryPal/1.0",
    "signature-agent": "'"${SIGNATURE_AGENT//\"/\\\"}"'",
    "signature-input": "sig1='"${SIGNATURE_PARAMS//\"/\\\"}"'",
    "signature": "'"$SIGNATURE"'" } } }'
```

**Node**

```ts
import { signRequest } from "@provenanceauth/sdk";

const url = "https://quillstack.example/products/linen-a5";
const headers = await signRequest(
  { method: "GET", url },
  { key: privateJwk, signatureAgent: { url: `${api}/v1/public/agents/${agent.id}/directory`, type: "jwks_uri" }, expiresIn: 300 },
);
const signed = await prov.identify.assess({ request: { method: "GET", url, headers: { "user-agent": "PantryPal/1.0", ...headers } } });
console.log(signed.class, signed.agent?.name); // signed_agent Pantry Pal
```

**Python**

```python
from provenanceauth.web_bot_auth import sign_request

url = "https://quillstack.example/products/linen-a5"
headers = sign_request("GET", url, private_jwk=private_jwk,
                       signature_agent_url=f"{api}/v1/public/agents/{agent['id']}/directory",
                       signature_agent_type="jwks_uri", expires_in=300)
signed = prov.identify.assess(request={"method": "GET", "url": url, "headers": {"user-agent": "PantryPal/1.0", **headers}})
print(signed["class"], signed["agent"]["name"])  # signed_agent Pantry Pal
```

```json
{
  "object": "traffic_assessment",
  "class": "signed_agent",
  "agent": {
    "name": "Pantry Pal",
    "category": "shopping_agent",
    "verified": true
  }
}
```

The verdict names the agent from its registration, so your traffic policy can treat Pantry Pal differently from an unknown bot.

### 4. Change the request after signing

Send the same headers with a different path. The signature covers `@path`, so it fails to verify and the request is a `suspected_bot`.

**curl**

```bash
curl -s $PROVENANCE_API/v1/identify/assessments \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "request": { "method": "GET", "url": "https://quillstack.example/checkout", "headers": {
    "user-agent": "PantryPal/1.0",
    "signature-agent": "'"${SIGNATURE_AGENT//\"/\\\"}"'",
    "signature-input": "sig1='"${SIGNATURE_PARAMS//\"/\\\"}"'",
    "signature": "'"$SIGNATURE"'" } } }'
```

**Node**

```ts
const changed = await prov.identify.assess({
  request: { method: "GET", url: "https://quillstack.example/checkout", headers: { "user-agent": "PantryPal/1.0", ...headers } },
});
```

**Python**

```python
changed = prov.identify.assess(request={
    "method": "GET", "url": "https://quillstack.example/checkout", "headers": {"user-agent": "PantryPal/1.0", **headers},
})
```

```json
{
  "object": "traffic_assessment",
  "class": "suspected_bot",
  "signals": [{ "code": "signature_invalid" }]
}
```

### 5. Read the decision back

Every check is kept as a traffic decision for 30 days. [`GET /v1/identify/decisions`](https://provenanceauth.com/docs/api/traffic-decisions.md#get-v1-identify-decisions) lists them, newest first, with the class, the decision your policy took and the path.

**curl**

```bash
curl -s "$PROVENANCE_API/v1/identify/decisions?limit=1" \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const { data } = await prov.identify.listDecisions({ limit: 1 });
```

**Python**

```python
decisions = prov.identify.list_decisions(limit=1)
```

```json
{
  "object": "list",
  "data": [
    { "object": "traffic_decision", "class": "suspected_bot", "decision": "allow", "path": "/checkout" }
  ],
  "has_more": true
}
```

The decision is `allow` because the sandbox account has no traffic policy yet. [Write a traffic policy](https://provenanceauth.com/docs/identify/guides/traffic-policy.md) to block or rate-limit suspected bots, or to require a person.

## Try it

The [Identify explorer](https://provenanceauth.com/identify#endpoints) runs the check in your browser against the sandbox. To see the RFC 9421 signature base for any request, print `signature-base.txt` from step 3.

## Next steps

- [Verify signed agents](https://provenanceauth.com/docs/identify/guides/verify-signed-agents.md): put the middleware in front of your site.
- [Sign your agent's requests](https://provenanceauth.com/docs/identify/guides/sign-agent-requests.md): the agent side, key rotation and suspension.
- [Write a traffic policy](https://provenanceauth.com/docs/identify/guides/traffic-policy.md): block, rate-limit or require a person.
- [Example apps](https://provenanceauth.com/docs/get-started/example-apps.md): small working apps that use this product end to end.
