# Export and import

> Move every person to another install in one encrypted file, or leave for another product.

From `docs/export.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/migrate/export.

This guide moves every person who signs in to your install to another
install, another store kind, or another product, and brings people in
from Auth0, Clerk, and Supabase. It ends with the reference for the
export file format. The design is in
`design/export.md`.

- To leave, run `tensile-auth-server export`. The file holds addresses,
  password hashes, passkeys, TOTP secrets, recovery codes, external
  identities, and agents.
- To arrive, run `tensile-auth-server import`, with `--from auth0`,
  `--from clerk`, or `--from supabase` for another product's files.

An export isn't a backup. To move or restore an install, back up the
database and the master key.

## Export

Run the command where the server runs, with the same configuration:

```sh
tensile-auth-server export --recipient age1pq1…
```

| Flag | What it does |
|---|---|
| `--out FILE` | Writes to `FILE`. The default is `tensile-auth-export-<UTC time>.jsonl.age` in the working directory. `-` writes to standard output, which must not be a terminal |
| `--recipient KEY` | Encrypts to a post-quantum [age](https://age-encryption.org/v1) public key (`age1pq1…`, from `age-keygen` 1.4 or later). Repeat it for more recipients. A classic X25519 key (`age1…`) is refused |
| `--recipients-file FILE` | Reads post-quantum age recipients from `FILE`, one for each line |
| `--skip-totp` | Leaves TOTP factors out. Those people enroll again |

Without a recipient, on a terminal, the command generates a 128-bit
passphrase and prints it once, to standard error. Store it apart from
the file. `age -d` asks for it.

The command:

- always encrypts the file; there is no plaintext export;
- creates the file with mode `0600`, never overwrites one, and deletes
  it when the export fails;
- records `export_started` and `users_exported` in the audit log, each
  with a high-severity admin alert;
- refuses a database whose schema isn't the binary's: run
  `tensile-auth-server migrate` first.

Delete the file after the move. It holds every credential of every
person.

## Import a tensile-auth export

Import into an empty install whose schema is current:

```sh
tensile-auth-server import tensile-auth-export-20261201T120000Z.jsonl.age --identity key.txt
```

| Flag | What it does |
|---|---|
| `--identity FILE` | Decrypts with the age identity in `FILE` |
| `--passphrase-file FILE` | Decrypts with the passphrase in `FILE`. Without this flag or `--identity`, the command asks for the passphrase on a terminal |
| `--dry-run` | Runs every check, prints the counts, and stores nothing |
| `--merge` | Imports into an install that has sign-in records. A user whose ID, address, passkey, recovery code, or identity exists is skipped and reported, never overwritten or attached to another user |
| `--drop-passkeys` | Imports a file made for another passkey relying party ID, without its passkeys. Those people register a passkey again |

The import stores every record in one transaction, or nothing. It
refuses:

- an install that has sign-in records, unless you pass `--merge`;
- a file whose relying party ID differs from the install's, unless you
  pass `--drop-passkeys`;
- an unknown major version, a missing `end` line, or counts that differ
  from the lines read;
- a password hash that doesn't parse, or a bcrypt hash while
  `passwords.accept_bcrypt` is off;
- two users who share a normalized address.

TOTP secrets are sealed under the install's own keys. People sign in as
the same user ID, with the same password, passkey, TOTP code, recovery
code, and external identity. Sessions and tokens don't move: people
sign in again, and agents are delegated again.

## Move to Auth0

The format is documented, so `jq` converts an export to the JSON file
that Auth0's bulk user import reads:

```sh
age -d -i key.txt tensile-auth-export-20261201T120000Z.jsonl.age > export.jsonl
jq -s '[ .[]
  | select(.type == "user" and .email != null)
  | {user_id: .id, email: .email.address, email_verified: (.email.verified_at != null)}
    + (if .password.hash then {custom_password_hash: {
        algorithm: (if (.password.hash | startswith("$argon2")) then "argon2" else "bcrypt" end),
        hash: {value: .password.hash}}} else {} end)
    + (if .totp and .totp.disabled_at == null then {mfa_factors: [{totp: {secret: .totp.secret}}]} else {} end)
]' export.jsonl > auth0-users.json
rm export.jsonl
```

- Users without an address stay behind, because Auth0's database
  connections sign in by address.
- Auth0 adds `auth0|` to each `user_id`.
- The library hashes the NFC form of a password, and Auth0 hashes the
  bytes as typed. A person who types a password that isn't in NFC, for
  example with a combining accent, resets it after the move.
- Passkeys, recovery codes, and agents don't convert.
- Auth0 limits the size of one import file. Split a large array into
  several files.

Delete `auth0-users.json` after the import. It holds password hashes
and TOTP secrets in the clear.

## Embed it without the server

An application that embeds the library calls the same code:

```go
counts, err := authexport.Export(ctx, store, out, authexport.Options{
	Recipients: recipients, TOTPKeys: keys, RPID: "app.example.com",
})

plan, err := authexport.ReadClerk(file, authexport.SourceOptions{AcceptBcrypt: true})
if err == nil {
	_, err = plan.Import(ctx, store)
}
```

`store` must implement `authexport.Store`, which `memstore`,
`sqlitestore`, and `pgstore` do.
