|
| 1 | +--- |
| 2 | +title: "Server Sessions" |
| 3 | +description: "Delegate a PKP to an ephemeral session key and hand that scoped capability to backend services that mint fresh session signatures on demand." |
| 4 | +--- |
| 5 | + |
| 6 | +# Overview |
| 7 | + |
| 8 | +Some integrations need a backend or serverless worker to execute Lit actions long after a user has left the client. Instead of caching `pkpSessionSigs` (which expire quickly and can become invalid when nodes join or leave the network), you can delegate the PKP to a session key and send the **session keypair plus delegation auth signature** to the server. The server then recreates the auth context and generates short-lived session signatures immediately before each request. |
| 9 | + |
| 10 | +This pattern keeps the delegation scoped (resources and expiration are enforced by the delegation) while avoiding the flakiness that comes from reusing stale session signatures. |
| 11 | + |
| 12 | +# Workflow |
| 13 | + |
| 14 | +1. **Client generates a session keypair** with `generateSessionKeyPair()`. |
| 15 | +2. **Client mints a delegation auth signature** with `authManager.generatePkpDelegationAuthSig`, scoping the allowed abilities and expiration. |
| 16 | +3. **Client sends the bundle** `{ sessionKeyPair, delegationAuthSig, pkpPublicKey }` to the server over a secure channel. |
| 17 | +4. **Server restores an auth context** using `authManager.createPkpAuthContextFromPreGenerated`. |
| 18 | +5. **Server issues fresh session signatures on demand** (e.g., `authManager.createPkpSessionSigs`) immediately before calling SDK helpers such as the wrapped-keys API or `pkpSign`. |
| 19 | + |
| 20 | +# Client hand-off example |
| 21 | + |
| 22 | +```ts |
| 23 | +import { |
| 24 | + createAuthManager, |
| 25 | + generateSessionKeyPair, |
| 26 | +} from '@lit-protocol/auth'; |
| 27 | + |
| 28 | +const sessionKeyPair = generateSessionKeyPair(); |
| 29 | + |
| 30 | +const delegationAuthSig = await authManager.generatePkpDelegationAuthSig({ |
| 31 | + pkpPublicKey, |
| 32 | + authData, |
| 33 | + sessionKeyPair, |
| 34 | + authConfig: { |
| 35 | + resources: [ |
| 36 | + ['pkp-signing', '*'], |
| 37 | + ['lit-action-execution', '*'], |
| 38 | + ['access-control-condition-decryption', '*'], |
| 39 | + ], |
| 40 | + expiration: new Date(Date.now() + 15 * 60 * 1000).toISOString(), |
| 41 | + }, |
| 42 | + litClient: litClient, |
| 43 | +}); |
| 44 | + |
| 45 | +const envelope = JSON.stringify({ |
| 46 | + pkpPublicKey, |
| 47 | + payload: Buffer.from( |
| 48 | + JSON.stringify({ sessionKeyPair, delegationAuthSig }), |
| 49 | + 'utf8' |
| 50 | + ).toString('base64url'), |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +# Server restore example |
| 55 | + |
| 56 | +```ts |
| 57 | +import { |
| 58 | + createAuthManager, |
| 59 | + storagePlugins, |
| 60 | + validateDelegationAuthSig, |
| 61 | +} from '@lit-protocol/auth'; |
| 62 | +import { createLitClient } from '@lit-protocol/lit-client'; |
| 63 | + |
| 64 | +const parsedEnvelope = JSON.parse(envelope) as { |
| 65 | + pkpPublicKey: string; |
| 66 | + payload: string; |
| 67 | +}; |
| 68 | + |
| 69 | +const decodedPayload = JSON.parse( |
| 70 | + Buffer.from(parsedEnvelope.payload, 'base64url').toString('utf8') |
| 71 | +) as { |
| 72 | + sessionKeyPair: typeof sessionKeyPair; |
| 73 | + delegationAuthSig: typeof delegationAuthSig; |
| 74 | +}; |
| 75 | + |
| 76 | +validateDelegationAuthSig({ |
| 77 | + delegationAuthSig: decodedPayload.delegationAuthSig, |
| 78 | + sessionKeyUri: decodedPayload.sessionKeyPair.publicKey, |
| 79 | +}); |
| 80 | + |
| 81 | +const serverLitClient = await createLitClient({ network: resolvedNetwork }); |
| 82 | +const serverAuthManager = createAuthManager({ |
| 83 | + storage: storagePlugins.localStorageNode({ |
| 84 | + appName: 'server-session-demo', |
| 85 | + networkName: resolvedNetwork.name, |
| 86 | + storagePath: './.server-session-cache', |
| 87 | + }), |
| 88 | +}); |
| 89 | + |
| 90 | +const authContext = |
| 91 | + await serverAuthManager.createPkpAuthContextFromPreGenerated({ |
| 92 | + pkpPublicKey: parsedEnvelope.pkpPublicKey, |
| 93 | + sessionKeyPair: decodedPayload.sessionKeyPair, |
| 94 | + delegationAuthSig: decodedPayload.delegationAuthSig, |
| 95 | + }); |
| 96 | + |
| 97 | +// ONly call this when the downstream API explicitly requires session sigs |
| 98 | +const pkpSessionSigs = await serverAuthManager.createPkpSessionSigs({ |
| 99 | + sessionKeyPair: decodedPayload.sessionKeyPair, |
| 100 | + pkpPublicKey: parsedEnvelope.pkpPublicKey, |
| 101 | + delegationAuthSig: decodedPayload.delegationAuthSig, |
| 102 | + litClient: serverLitClient, |
| 103 | +}); |
| 104 | + |
| 105 | +await serverLitClient.chain.ethereum.pkpSign({ |
| 106 | + authContext, |
| 107 | + pubKey: parsedEnvelope.pkpPublicKey, |
| 108 | + toSign: 'hello from server reuse', |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +<Note> |
| 113 | + Only call `createPkpSessionSigs` when the downstream API explicitly requires the session sigs (for example, the wrapped-keys service). |
| 114 | +</Note> |
| 115 | + |
| 116 | +# Security considerations |
| 117 | + |
| 118 | +- **Treat the session keypair like a secret.** Whoever holds the private key can mint new session signatures until the delegation expires. |
| 119 | +- **Scope the delegation.** Restrict resources to the minimal Lit resources needed and set conservative expirations. |
| 120 | +- **Rotate on failure.** If a node joins or leaves the network the server can simply regenerate session signatures with the current handshake; if that fails, request a fresh delegation from the client. |
| 121 | + |
| 122 | +# When to use this pattern |
| 123 | + |
| 124 | +- Long-running workflows where session signatures might expire before all steps finish (e.g., Bitcoin transactions that need multiple confirmations). |
| 125 | +- Server-driven orchestration that must run without a browser tab staying open. |
| 126 | +- Integrations that want to avoid caching `pkpSessionSigs`, but still need durable delegated access. |
0 commit comments