Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.7 KB
Raw Blame
import jwt from 'jsonwebtoken'

export enum PosthogJwtAudience {
    RECORDING_API = 'posthog:recording_api',
    // Must match PosthogJwtAudience.REPLAY_BLOCKS in posthog/jwt.py exactly.
    REPLAY_BLOCKS = 'posthog:replay:blocks',
    SUBSCRIPTION_PREFERENCES = 'posthog:messaging:subscription_preferences',
    WORKFLOWS_RESCHEDULE_PARKED = 'posthog:workflows:reschedule_parked',
    WORKFLOWS_CANCEL_INVOCATIONS = 'posthog:workflows:cancel_invocations',
    WORKFLOWS_CANCEL_BATCH = 'posthog:workflows:cancel_batch',
    WORKFLOWS_STEP_RESUME = 'posthog:workflows:step_resume',
    WORKFLOWS_SEND_TRANSACTIONAL_EMAIL = 'posthog:workflows:send_transactional_email',
    WORKFLOWS_RESUME_BATCH = 'posthog:workflows:resume_batch',
    CUSTOMER_TASKS_CREATE = 'posthog:customer-tasks:create',
    TASKS_CREATE = 'posthog:tasks:create',
    WORKFLOW_SCOUT_RUN = 'posthog:workflows:scout_run',
    WORKFLOW_CLASSIFY = 'posthog:workflows:classify',
    // Must match PosthogJwtAudience.WORKFLOW_VISION_REQUEST in posthog/jwt.py exactly.
    WORKFLOW_VISION_REQUEST = 'posthog:workflows:vision_request',
    // Must match PosthogJwtAudience.WORKFLOW_ADD_TO_STATIC_COHORT in posthog/jwt.py exactly.
    WORKFLOW_ADD_TO_STATIC_COHORT = 'posthog:workflows:add_to_static_cohort',
    // Must match PosthogJwtAudience.WORKFLOW_MESSAGE_OPT_OUT in posthog/jwt.py exactly.
    WORKFLOW_MESSAGE_OPT_OUT = 'posthog:workflows:message_opt_out',
    // Must match PosthogJwtAudience.CONVERSATIONS_TICKETS in posthog/jwt.py exactly.
    CONVERSATIONS_TICKETS = 'posthog:conversations:tickets',
    // Must match PosthogJwtAudience.CUSTOMER_ANALYTICS_ACCOUNTS in posthog/jwt.py exactly.
    CUSTOMER_ANALYTICS_ACCOUNTS = 'posthog:customer_analytics:accounts',
}

export class JWT {
    private secrets: string[] = []

    constructor(commaSeparatedSaltKeys: string) {
        const saltKeys = parseJwtKeys(commaSeparatedSaltKeys)
        if (!saltKeys.length) {
            throw new Error('Encryption keys are not set')
        }
        this.secrets = saltKeys
    }

    sign(payload: object, audience: PosthogJwtAudience, options?: jwt.SignOptions): string {
        return jwt.sign(payload, this.secrets[0], { ...options, audience: audience })
    }

    verify(
        token: string,
        audience: PosthogJwtAudience,
        options?: jwt.VerifyOptions & { ignoreVerificationErrors?: boolean }
    ): string | jwt.Jwt | jwt.JwtPayload | undefined {
        let error: Error | undefined
        for (const secret of this.secrets) {
            try {
                const payload = jwt.verify(token, secret, { ...options, audience: audience })
                return payload
            } catch (e) {
                error = e
            }
        }
        if (options?.ignoreVerificationErrors) {
            return undefined
        }
        throw error
    }
}

/** Split a comma-separated key string into usable keys (newest first), trimming each and dropping
 * empty segments. Trimming keeps this in lockstep with the Python minter's recording_api_signing_keys. */
export function parseJwtKeys(commaSeparatedSaltKeys: string): string[] {
    return commaSeparatedSaltKeys
        .split(',')
        .map((key) => key.trim())
        .filter((key) => key)
}

/** Whether a comma-separated key string yields at least one usable key. */
export function hasJwtKeys(commaSeparatedSaltKeys: string): boolean {
    return parseJwtKeys(commaSeparatedSaltKeys).length > 0
}

/** Build a verifier, or null when no usable key is configured, so a malformed-but-truthy value like
 * ',' disables JWT instead of throwing at construction. */
export function makeOptionalJwt(commaSeparatedSaltKeys: string): JWT | null {
    return hasJwtKeys(commaSeparatedSaltKeys) ? new JWT(commaSeparatedSaltKeys) : null
}