# Sidebar apps

Show your own tools beside each conversation: the customer's account in your admin, their orders, their plan. Owners and admins add them in Settings → Integrations → Sidebar apps. Everyone who can see a conversation sees its apps.

## Add an app

- **Name:** the button's or panel's title.
- **Address:** an `https://` URL. Fill-ins put the conversation into it:
  - `{{contact.id}}`: the contact's ID in Opskeet.
  - `{{contact.email}}`, `{{contact.name}}`.
  - `{{contact.<field>}}`: any contact field by its key, like `{{contact.plan}}`.
  - `{{conversation.id}}`.
- **Shows as:**
  - **Panel:** your page in a frame above the contact's details, which the team can collapse.
  - **Link:** a button that opens your page in a new tab.
- **Signed:** adds `opskeet_token` to the address, so your server knows the request came from your team. You see the app's secret once; copy it. **New secret** replaces it at once.

Example: `https://admin.example.com/users?email={{contact.email}}`

Each value is URL-encoded, so a value can't change the path, query or fragment it's in. Fill-ins can't be in the site's address itself: the site an app opens is fixed when you save it. A value that's missing is left empty. Visitors can type their name and an unproven email, so treat fill-ins as hints. Trust only the signed token.

## The panel's frame

The panel loads your page in a sandboxed frame: `allow-scripts allow-forms allow-popups`, without `allow-same-origin`. So:

- Your page runs its scripts and forms and can open new tabs.
- It has an opaque origin: no cookies, `localStorage` or IndexedDB, and its requests send `Origin: null`. Sign the user in from the token instead of a cookie.
- It can't reach the Opskeet page around it.
- It's loaded without a referrer.

Your page must allow being framed by Opskeet: don't send `X-Frame-Options: DENY`, and if you send `Content-Security-Policy: frame-ancestors`, include `https://opskeet.com` (and your agency domain, if you use one).

A new panel shows for teammates after they reload Opskeet: the app page lets frames load only the panels' sites.

## The token

`opskeet_token` is a JWT signed with HS256, with the app's secret as the key. It's made when the panel opens or the link is clicked, and works for 5 minutes.

```json
{
  "iss": "opskeet",
  "aud": "<the app's ID>",
  "iat": 1795528800,
  "exp": 1795529100,
  "project_id": "…",
  "conversation_id": "…",
  "teammate_id": "…",
  "contact_id": "…",
  "contact_email": "ada@globex.com",
  "contact_user_id": "user-1"
}
```

- `teammate_id`: who opened it.
- `contact_email`: only when the email is proven (in your site's signed token, or confirmed from a link we sent).
- `contact_user_id`: your site's user ID, only once your site's signed token named them.

Check the signature, `exp` and `aud` before you show anything. Then start your own session, since the frame has no cookies.

### Check it in Node.js

With [jose](https://github.com/panva/jose):

```js
import { jwtVerify } from "jose"

const secret = new TextEncoder().encode(process.env.OPSKEET_APP_SECRET)

export async function opskeetClaims(token) {
  const { payload } = await jwtVerify(token, secret, {
    algorithms: ["HS256"],
    issuer: "opskeet",
    audience: process.env.OPSKEET_APP_ID,
  })
  return payload
}
```

Without a library:

```js
import crypto from "node:crypto"

export function opskeetClaims(token, secret, appId) {
  const [header, payload, signature] = token.split(".")
  const expected = crypto
    .createHmac("sha256", secret)
    .update(`${header}.${payload}`)
    .digest("base64url")
  const given = Buffer.from(signature ?? "")
  if (
    given.length !== expected.length ||
    !crypto.timingSafeEqual(given, Buffer.from(expected))
  )
    throw new Error("Bad signature")
  if (JSON.parse(Buffer.from(header, "base64url")).alg !== "HS256")
    throw new Error("Wrong algorithm")
  const claims = JSON.parse(Buffer.from(payload, "base64url"))
  if (claims.aud !== appId || claims.exp < Date.now() / 1000)
    throw new Error("Expired or not for this app")
  return claims
}
```

A token is reusable for its 5 minutes. If that matters, keep the `iat` and `teammate_id` you've seen and refuse repeats.
