This page is for admins who make Buttons and for the developers of the Target.

An Interface button in [Direct iframe](/docs/buttons/interface-button/sections/direct-iframe) mode adds an encrypted
token to the Target URL. The token tells the Target which Pipedrive user pressed the Button, and it carries the
headers of the Button. In this guide, you decrypt the token on a Node.js server.

You need:

- An Interface button in Direct iframe mode.
- The key of the Button.
- The origin of the Target URL, for example `https://your-app.example`.
- Node.js 20 or later on your server.

## Decrypt the token with the crypto module

This function uses only the built-in `crypto` module. It returns the claims of a valid token, or `null`. The code
uses `import` and `export`, so set `"type": "module"` in `package.json` or use a `.mjs` file.

```js
import { createDecipheriv } from 'node:crypto';

export function decryptOneClickToken(token, key, targetOrigin) {
	const parts = token.split('.');
	if (parts.length !== 5) return null;
	const [protectedHeader, encryptedKey, iv, ciphertext, tag] = parts;
	if (encryptedKey !== '') return null;

	let header;
	try {
		header = JSON.parse(Buffer.from(protectedHeader, 'base64url').toString('utf8'));
	} catch {
		return null;
	}
	if (header.alg !== 'dir' || header.enc !== 'A256GCM') return null;

	const rawKey = Buffer.from(key, 'base64url');
	if (rawKey.length !== 32) return null;

	let claims;
	try {
		const decipher = createDecipheriv('aes-256-gcm', rawKey, Buffer.from(iv, 'base64url'));
		decipher.setAAD(Buffer.from(protectedHeader, 'ascii'));
		decipher.setAuthTag(Buffer.from(tag, 'base64url'));
		const plaintext = Buffer.concat([decipher.update(Buffer.from(ciphertext, 'base64url')), decipher.final()]);
		claims = JSON.parse(plaintext.toString('utf8'));
	} catch {
		return null;
	}

	if (typeof claims.exp !== 'number' || claims.exp <= Date.now() / 1000) return null;
	if (claims.aud !== targetOrigin) return null;
	return claims;
}
```

The function does the steps in
[Decrypt the token on the Target](/docs/buttons/interface-button/sections/direct-iframe#decrypt-the-token-on-the-target).
The token has five parts. The second part is empty, because the key of the Button encrypts the content directly. The
first part is the additional authenticated data of AES-GCM, so a changed header makes the decryption fail.

## Decrypt the token with the jose library

With the [jose](https://github.com/panva/jose) library, give the decoded bytes of the key. `jwtDecrypt` also checks
`exp`, and the `audience` option checks `aud`:

```js
import { jwtDecrypt } from 'jose';

export async function decryptOneClickToken(token, key, targetOrigin) {
	try {
		const { payload } = await jwtDecrypt(token, Buffer.from(key, 'base64url'), {
			keyManagementAlgorithms: ['dir'],
			contentEncryptionAlgorithms: ['A256GCM'],
			audience: targetOrigin,
		});
		return payload;
	} catch {
		return null;
	}
}
```

## Use the claims

The claims tell the Pipedrive company, the Pipedrive user and the headers of the Button. For the list of the claims,
see [Decrypt the token on the Target](/docs/buttons/interface-button/sections/direct-iframe#decrypt-the-token-on-the-target).

Do not use `resource` and `resource_id` for access control. OneClick does not check them.
