> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Seal Claude Code OAuth Tokens

> Let each user sign in to Claude Code with their own account while Formal encrypts the OAuth tokens on the device

## Overview

Anthropic does not support third-party OAuth, so Claude can't use Formal [linked accounts](https://docs.formal.ai/docs/guides/core-concepts/resources/oauth.md). You can still keep each user on their own Claude account without leaving usable tokens on their laptop.

Each user signs in to Claude Code with their own claude.ai account. Formal then seals the OAuth tokens Anthropic returns:

1. An **encrypt** [policy](https://docs.formal.ai/docs/glossary/index.md#policy) encrypts `access_token` and `refresh_token` in the response from `platform.claude.com`.
2. Claude Code stores `formalsealed:...` values instead of plaintext tokens.
3. A **decrypt** policy decrypts the `Authorization` header before the request reaches Anthropic. It also decrypts the refresh token when Claude Code refreshes or revokes it.

A sealed token that is copied off the device can't be used against Anthropic directly.

This guide uses the Formal Endpoint [Transparent Mode](https://docs.formal.ai/docs/guides/client-apps/desktop-app.md#transparent-mode) on macOS, which encrypts with the device Secure Enclave.

> [!NOTE]
>
> Endpoint `encrypt` is macOS-only. On other platforms, forward this traffic to a [Connector](https://docs.formal.ai/docs/glossary/index.md#connector) with a [token encryption key](https://docs.formal.ai/docs/guides/core-concepts/connectors/token_encryption.md). See [Encrypt on the Connector](https://docs.formal.ai/docs/guides/example-use-cases/encrypt-mcp-oauth-tokens.md#encrypt-on-the-connector).

## Intercept Anthropic traffic

Claude Code signs in through `platform.claude.com` and calls `api.anthropic.com` and `mcp-proxy.anthropic.com`. A [network rule](https://docs.formal.ai/docs/guides/network-rules.md) sends all three through the Endpoint.

**1. Enable Transparent Mode**

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal transparent-proxy install
formal transparent-proxy enable
formal transparent-proxy status
```

**Verify:** the status command reports that Transparent Mode is running.

**2. Create the network rule**

> [tab] Control Plane
>
> 1. Navigate to [Network Rules](https://app.formal.ai/network-rules)
> 2. Create a rule named `claude-oauth`
> 3. Paste the CEL below
> 4. Leave **Forward to Connector** unset
> 5. Save the rule and set it to **Active**
>
> ```cel theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> {
>   "condition": {
>     "pre_tls": hostname == "platform.claude.com" || hostname == "api.anthropic.com" || hostname == "mcp-proxy.anthropic.com",
>     "post_tls": true
>   },
>   "outputs": {}
> }
> ```

> [tab] Terraform
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_network_rule" "claude_oauth" {
>   name        = "claude-oauth"
>   description = "Intercept Claude Code sign-in and API traffic for token sealing"
>   status      = "active"
>
>   cel_expression = <<-EOT
>     {
>       "condition": {
>         "pre_tls": hostname == "platform.claude.com" || hostname == "api.anthropic.com" || hostname == "mcp-proxy.anthropic.com",
>         "post_tls": true
>       },
>       "outputs": {}
>     }
>   EOT
> }
> ```

**Verify:** run `claude -p "test"` and confirm the request appears in [Logs](https://app.formal.ai/logs).

## Encrypt the OAuth tokens

This policy encrypts the tokens in every OAuth response from `platform.claude.com`.

> [tab] Control Plane
>
> 1. Navigate to [Policies](https://app.formal.ai/policies)
> 2. Click **Create Policy**
> 3. Set name to `claude-encrypt-oauth` and add a description
> 4. Paste the Rego below into the editor
> 5. Click **Create Policy** to save, then set the policy to **Active**
>
> ```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> package formal.v2
>
> import future.keywords.if
>
> response := {
>   "action": "encrypt",
>   "targets": [
>     {"kind": "json", "name": "access_token"},
>     {"kind": "json", "name": "refresh_token"}
>   ],
>   "reason": "Seal Claude Code OAuth tokens"
> } if {
>   input.resource.hostname == "platform.claude.com"
>   input.http.method == "POST"
>   startswith(input.http.path, "/v1/oauth/")
> }
> ```

> [tab] Terraform
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_policy" "claude_encrypt_oauth" {
>   name        = "claude-encrypt-oauth"
>   description = "Seal Claude Code OAuth tokens on the token response"
>   status      = "active"
>
>   module = <<-EOT
>     package formal.v2
>
>     import future.keywords.if
>
>     response := {
>       "action": "encrypt",
>       "targets": [
>         {"kind": "json", "name": "access_token"},
>         {"kind": "json", "name": "refresh_token"}
>       ],
>       "reason": "Seal Claude Code OAuth tokens"
>     } if {
>       input.resource.hostname == "platform.claude.com"
>       input.http.method == "POST"
>       startswith(input.http.path, "/v1/oauth/")
>     }
>   EOT
> }
> ```

## Decrypt the tokens for Anthropic

This policy has two rules. The first decrypts `Authorization` on API and MCP proxy requests. The second decrypts the refresh token when Claude Code refreshes or revokes it.

> [tab] Control Plane
>
> 1. Navigate to [Policies](https://app.formal.ai/policies)
> 2. Click **Create Policy**
> 3. Set name to `claude-decrypt-oauth` and add a description
> 4. Paste the Rego below into the editor
> 5. Click **Create Policy** to save, then set the policy to **Active**
>
> ```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> package formal.v2
>
> import future.keywords.if
> import future.keywords.in
>
> request := {
>   "action": "decrypt",
>   "targets": [{"kind": "header", "name": "Authorization"}],
>   "reason": "Open Claude token for Anthropic"
> } if {
>   input.resource.hostname in {"api.anthropic.com", "mcp-proxy.anthropic.com"}
> }
>
> request := {
>   "action": "decrypt",
>   "targets": [
>     {"kind": "json", "name": "refresh_token"},
>     {"kind": "json", "name": "token"}
>   ],
>   "reason": "Open Claude refresh or revoke token"
> } if {
>   input.resource.hostname == "platform.claude.com"
>   startswith(input.http.path, "/v1/oauth/")
> }
> ```

> [tab] Terraform
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_policy" "claude_decrypt_oauth" {
>   name        = "claude-decrypt-oauth"
>   description = "Decrypt sealed Claude Code tokens before they reach Anthropic"
>   status      = "active"
>
>   module = <<-EOT
>     package formal.v2
>
>     import future.keywords.if
>     import future.keywords.in
>
>     request := {
>       "action": "decrypt",
>       "targets": [{"kind": "header", "name": "Authorization"}],
>       "reason": "Open Claude token for Anthropic"
>     } if {
>       input.resource.hostname in {"api.anthropic.com", "mcp-proxy.anthropic.com"}
>     }
>
>     request := {
>       "action": "decrypt",
>       "targets": [
>         {"kind": "json", "name": "refresh_token"},
>         {"kind": "json", "name": "token"}
>       ],
>       "reason": "Open Claude refresh or revoke token"
>     } if {
>       input.resource.hostname == "platform.claude.com"
>       startswith(input.http.path, "/v1/oauth/")
>     }
>   EOT
> }
> ```

> [!WARNING]
>
> Encrypt is fail-closed. If encryption fails, Formal blocks the token response. If decryption fails, Formal forwards the request unchanged.

## Verify end to end

Tokens issued before the policies were active are still plaintext. Sign out and sign in again with Transparent Mode running:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
claude auth logout
claude auth login
```

Confirm Claude Code stored sealed tokens:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
security find-generic-password -s "Claude Code-credentials" -w \
  | jq '.claudeAiOauth | {a: .accessToken[0:16], r: .refreshToken[0:16], expiresAt}'
# Expected: both "a" and "r" start with formalsealed:
```

Confirm Anthropic accepts the decrypted token:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
claude -p "test"
# Expected: a normal response from Claude
```

> [!TIP]
>
> Pair this guide with [Restrict Anthropic Sign-Ins](https://docs.formal.ai/docs/guides/example-use-cases/restrict-anthropic-sign-ins.md). Then users can sign in only with accounts in your Anthropic organization.

## Troubleshooting

> [accordion] Claude Code stores plaintext sk-ant tokens
>
> **Possible causes:**
>
> * Transparent Mode or the network rule is inactive
> * The device is not macOS, so the Endpoint can't encrypt
> * You signed in before the encrypt policy was active
>
> **Fix:**
>
> 1. Run `formal transparent-proxy status`
> 2. Set the network rule and both policies to **Active**
> 3. On Linux or Windows, forward the traffic to a Connector with a [token encryption key](https://docs.formal.ai/docs/guides/core-concepts/connectors/token_encryption.md)
> 4. Run `claude auth logout` and `claude auth login` again

> [accordion] Claude Code returns 401 Unauthorized
>
> **Possible causes:**
>
> * Encrypt ran, but the decrypt policy is inactive or doesn't match the hostname
> * The sealed token came from another device or Formal user
>
> **Fix:**
>
> 1. Confirm `claude-decrypt-oauth` is **Active** and matches `api.anthropic.com` and `mcp-proxy.anthropic.com`
> 2. Sign out and sign in again on this device

## Next Steps

> [card] [Restrict Anthropic Sign-Ins](https://docs.formal.ai/docs/guides/example-use-cases/restrict-anthropic-sign-ins.md)
>
> Allow only your Anthropic organization

> [card] [Encrypt MCP OAuth Tokens](https://docs.formal.ai/docs/guides/example-use-cases/encrypt-mcp-oauth-tokens.md)
>
> Apply the same pattern to MCP servers

> [card] [Encrypt & Decrypt](https://docs.formal.ai/docs/guides/policies/enforcement.md#encrypt--decrypt-actions)
>
> Full action reference

> [card] [Token Encryption](https://docs.formal.ai/docs/guides/core-concepts/connectors/token_encryption.md)
>
> Encrypt on the Connector with a KMS key


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.