# Entitlement tokens

Already run your own update or download server? Let it trust SellMyCode licences without calling us: activate and verify answers carry an `entitlement` — a signed token, valid for 24 hours, that says which site holds which licence and whether its update period is running.

> **Note:** The token is a standard JWT signed with EdDSA (Ed25519) by the same key as every licence answer. Any JWT library that supports EdDSA can check it offline.

## How it fits together
1. The installation calls [activate](https://sellmycode.net/api-docs/licence-activation.md), or verify with its `activation_token`, and keeps `entitlement` from the answer.
2. When it asks YOUR server for an update, it sends the token along, e.g. `Authorization: Bearer <entitlement>`.
3. Your server checks the signature with the public key, then the claims: `iss`, `exp`, `item_id`, `domain`, `updates.active`.
4. To get a fresh token, the installation calls verify again — at the latest every 24 hours.
Want to cut access before a token runs out? Subscribe to the [licence webhooks](https://sellmycode.net/api-docs/webhooks.md#licence-events) and refuse tokens of a licence or site you were told about.

## Claims

```json
{
  "iss": "https://sellmycode.net",
  "sub": "4b1f0c…e93a",
  "iat": 1793073762,
  "exp": 1793160162,
  "item_id": 12345678,
  "domain": "customer-site.com",
  "license_type": "Regular",
  "updates": { "active": true, "until": "2027-04-27T00:00:00+00:00", "lifetime": false }
}
```

| Claim | Type | Description |
|---|---|---|
| `iss` | string | always `https://sellmycode.net` — reject anything else |
| `sub` | string | SHA-256 (hex) of the purchase code. Match it against `hash('sha256', $code)` of the codes in your [sale webhooks](https://sellmycode.net/api-docs/webhooks.md); the code itself is not in the token |
| `iat` / `exp` | integer | issued at / expires at, Unix seconds; `exp` is 24 hours after `iat` |
| `item_id` | integer | the item — check it is YOURS |
| `domain` | string | the activated domain: lower case, no scheme, no `www.` |
| `license_type` | string | "Regular" \| "Extended" |
| `updates.active` | boolean | the buyer may download updates now |
| `updates.until` | string \| null | end of the update period, ISO 8601 UTC; null when lifetime |
| `updates.lifetime` | boolean | updates never end |

The header is `{"alg":"EdDSA","typ":"JWT","kid":"k1"}`. Pick the key by `kid`.

## Keys

- Raw key and PEM: [Signatures & fields](https://sellmycode.net/api-docs/licence-signatures.md#public-key).
- As a JWK set, for JWT libraries: `GET /api/license/jwks`

```json
{ "keys": [ { "kty": "OKP", "crv": "Ed25519", "x": "…", "kid": "k1", "alg": "EdDSA", "use": "sig" } ] }
```

## Verify on your server
```php
// with the SDK (SellMyCodeLicense.php from /api-docs/licence-sdk)
$claims = SellMyCodeLicense::verifyEntitlement($token, [
    'item_id' => 12345678,
    'domain' => $requestingDomain,
]);
if (!$claims || !$claims['updates']['active']) {
    http_response_code(403);
    exit;
}
```

```node
// npm install jose
import { createRemoteJWKSet, jwtVerify } from 'jose';

const JWKS = createRemoteJWKSet(new URL('https://sellmycode.net/api/license/jwks'));

const { payload } = await jwtVerify(token, JWKS, {
  issuer: 'https://sellmycode.net',
  algorithms: ['EdDSA'],
});
if (payload.item_id !== 12345678 || !payload.updates.active) throw new Error('Not entitled');
```

```text
firebase/php-jwt:
$claims = (array) Firebase\JWT\JWT::decode($token, new Firebase\JWT\Key('5Uk3p1+J0PXzJkwS4wVFvTN9GcHpJzX5ClRI8zlI0yQ=', 'EdDSA'));
then check iss, item_id, domain and updates yourself.
```
> **Warning:** A valid signature only proves the token came from SellMyCode. Always check `iss`, `item_id` and `exp` too, and `domain` when the request says which site it comes from — otherwise a token for another product or site would be accepted.

## When there is no token

The token opens your downloads, so a purchase code and a domain alone are not enough to get one — the same rule as [licence updates](https://sellmycode.net/api-docs/licence-updates.md):

| Answer | `entitlement` |
|---|---|
| activate → `activated`, `reactivated` | always (a new `activation_token` is issued with it) |
| activate → `already_activated`, verify → `valid` | only when the request includes this site's `activation_token`; otherwise `null` |
| any error | none — paused, revoked or blocked sites, blocked or refunded licences and removed items get no token |

A wrong `activation_token` does not fail the call, but counts towards the failed-lookup limit.

---
Source: https://sellmycode.net/api-docs/licence-entitlement
