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.

feat(conversations): read identity secret from own endpoint (#119850)

Patricio110cf47969df
diff --git a/products/conversations/frontend/scenes/settings/GeneralSection.tsx b/products/conversations/frontend/scenes/settings/GeneralSection.tsx
index f4fc0af65e3e..f5145ec8f028 100644
--- a/products/conversations/frontend/scenes/settings/GeneralSection.tsx
+++ b/products/conversations/frontend/scenes/settings/GeneralSection.tsx
@@ -12,7 +12,7 @@ import { SceneSection } from '~/layout/scenes/components/SceneSection'
import { AuthorizedDomains } from './AuthorizedDomains'
import { ChannelsSection } from './ChannelsSection'
import { DraftModeSection } from './DraftModeSection'
-import { SecretApiKeySection } from './SecretApiKeySection'
+import { IdentityVerificationSecretSection } from './IdentityVerificationSecretSection'
import { supportSettingsLogic } from './supportSettingsLogic'
export function GeneralSection(): JSX.Element {
@@ -141,8 +141,9 @@ export function GeneralSection(): JSX.Element {
className="my-8"
description={
<>
- For logged-in users, sign their distinct_id on your backend with the secret API key so
- tickets persist across browsers and devices without email recovery.{' '}
+ For logged-in users, sign their distinct_id on your backend with the identity
+ verification secret so tickets persist across browsers and devices without email
+ recovery.{' '}
<Link
to="https://posthog.com/docs/support/javascript-api#user-identification"
target="_blank"
@@ -155,8 +156,9 @@ export function GeneralSection(): JSX.Element {
>
<LemonCard hoverEffect={false} className="max-w-[800px] px-4 py-3">
<p className="mb-2">
- Compute an HMAC-SHA256 of the user's <code>distinct_id</code> using the secret API key
- below (server-side) and pass both values to <code>posthog.init()</code>:
+ Compute an HMAC-SHA256 of the user's <code>distinct_id</code> using the identity
+ verification secret below (server-side) and pass both values to{' '}
+ <code>posthog.init()</code>:
</p>
<pre className="bg-surface-secondary rounded p-3 text-xs overflow-x-auto mb-2">
{`posthog.init('<ph_project_api_key>', {
@@ -170,7 +172,7 @@ export function GeneralSection(): JSX.Element {
</p>
</LemonCard>
</SceneSection>
- <SecretApiKeySection />
+ <IdentityVerificationSecretSection />
{/* The send menu confirms every send, so the draft-mode default has nothing left to set. */}
{!featureFlags[FEATURE_FLAGS.PRODUCT_SUPPORT_SIMPLIFIED_REPLIES] && <DraftModeSection />}
</>
diff --git a/products/conversations/frontend/scenes/settings/IdentityVerificationSecretSection.tsx b/products/conversations/frontend/scenes/settings/IdentityVerificationSecretSection.tsx
new file mode 100644
index 000000000000..aae04847035a
--- /dev/null
+++ b/products/conversations/frontend/scenes/settings/IdentityVerificationSecretSection.tsx
@@ -0,0 +1,95 @@
+import { useActions, useValues } from 'kea'
+
+import { IconRefresh } from '@posthog/icons'
+import { LemonBanner, LemonCard, LemonSkeleton } from '@posthog/lemon-ui'
+
+import { CodeSnippet } from 'lib/components/CodeSnippet'
+import { LemonButton } from 'lib/lemon-ui/LemonButton'
+import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
+import { teamLogic } from 'scenes/teamLogic'
+
+import { SceneSection } from '~/layout/scenes/components/SceneSection'
+
+import { signingSecretLogic } from './signingSecretLogic'
+
+export function IdentityVerificationSecretSection(): JSX.Element {
+ const { signingSecret, signingSecretLoading, signingSecretLoadFailed } = useValues(signingSecretLogic)
+ const { rotateSigningSecret, loadSigningSecret } = useActions(signingSecretLogic)
+ const { isTeamTokenResetAvailable } = useValues(teamLogic)
+
+ const secret = signingSecret?.secret ?? null
+
+ const openRotateDialog = (): void => {
+ const verb = secret ? 'Rotate' : 'Generate'
+ const description = secret
+ ? 'This generates a new secret right away. Identity hashes signed with the current secret stop verifying immediately, so update your server-side code with the new secret as soon as you rotate.'
+ : 'This generates a secret for verifying the identity of users in the support widget.'
+
+ LemonDialog.open({
+ title: `${verb} identity verification secret?`,
+ description,
+ primaryButton: {
+ children: verb,
+ onClick: rotateSigningSecret,
+ },
+ secondaryButton: { children: 'Cancel' },
+ })
+ }
+
+ return (
+ <SceneSection
+ title="Identity verification secret"
+ titleSize="sm"
+ description="Signs the identity hashes that verify who a user is in the support widget. Only use it server-side and keep it out of client code."
+ >
+ <LemonCard hoverEffect={false} className="flex flex-col gap-y-2 max-w-[800px] px-4 py-3">
+ {signingSecretLoading && !signingSecret ? (
+ <LemonSkeleton className="h-10" />
+ ) : signingSecretLoadFailed ? (
+ <LemonBanner type="error" action={{ children: 'Retry', onClick: () => loadSigningSecret() }}>
+ Couldn't load the identity verification secret. Retry, and if it keeps happening contact
+ support.
+ </LemonBanner>
+ ) : (
+ <>
+ <CodeSnippet
+ actions={
+ <LemonButton
+ icon={<IconRefresh />}
+ size="xsmall"
+ data-attr="conversations-rotate-signing-secret"
+ onClick={openRotateDialog}
+ loading={signingSecretLoading}
+ disabledReason={
+ !isTeamTokenResetAvailable
+ ? 'You do not have permission to rotate this secret'
+ : undefined
+ }
+ tooltip={secret ? 'Rotate secret' : 'Generate secret'}
+ />
+ }
+ className={secret ? '' : 'text-muted'}
+ thing="Identity verification secret"
+ >
+ {secret ||
+ 'No secret yet. Generate one with the button on the right to enable identity verification.'}
+ </CodeSnippet>
+
+ {secret ? (
+ <LemonBanner type="warning" className="my-2">
+ Rotating this secret invalidates identity hashes signed with the current one
+ immediately. Rotate if it has been compromised or as part of your regular rotation
+ policy, and update your server-side code right after.
+ </LemonBanner>
+ ) : (
+ <LemonBanner type="info" className="my-2">
+ This project doesn't have an identity verification secret yet. Identity verification in
+ the support widget stays off until you generate one.
+ </LemonBanner>
+ )}
+ </>
+ )}
+ </LemonCard>
+ </SceneSection>
+ )
+}
diff --git a/products/conversations/frontend/scenes/settings/SecretApiKeySection.tsx b/products/conversations/frontend/scenes/settings/SecretApiKeySection.tsx
deleted file mode 100644
index fd0ae9dccd39..000000000000
--- a/products/conversations/frontend/scenes/settings/SecretApiKeySection.tsx
+++ /dev/null
@@ -1,106 +0,0 @@
-import { useActions, useValues } from 'kea'
-
-import { IconRefresh, IconTrash } from '@posthog/icons'
-import { LemonBanner, LemonButton, LemonCard } from '@posthog/lemon-ui'
-
-import { CodeSnippet } from 'lib/components/CodeSnippet'
-import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
-import { teamLogic } from 'scenes/teamLogic'
-
-import { SceneSection } from '~/layout/scenes/components/SceneSection'
-
-export function SecretApiKeySection(): JSX.Element {
- const { currentTeam, isTeamTokenResetAvailable } = useValues(teamLogic)
- const { rotateSecretToken, deleteSecretTokenBackup } = useActions(teamLogic)
-
- const openRotateDialog = (): void => {
- const verb = currentTeam?.secret_api_token ? 'Rotate' : 'Generate'
- const description = currentTeam?.secret_api_token
- ? 'This will generate a new secret API key and move the existing one to backup. The old key will remain active until you delete it.'
- : 'This will generate a new secret API key for authenticating external API requests.'
-
- LemonDialog.open({
- title: `${verb} secret API key?`,
- description,
- primaryButton: {
- children: verb,
- onClick: rotateSecretToken,
- },
- secondaryButton: { children: 'Cancel' },
- })
- }
-
- return (
- <SceneSection
- title="Secret API key"
- titleSize="sm"
- description="Authenticates workflow actions that read or update tickets, and signs identity hashes for identity verification. Project secret API keys can't be used for either."
- >
- <LemonCard hoverEffect={false} className="flex flex-col gap-y-2 max-w-[800px] px-4 py-3">
- <div>
- <h3 className="text-sm font-semibold mb-1">
- Primary key{' '}
- {currentTeam?.secret_api_token && <span className="text-green-700 text-xs ml-2">(Active)</span>}
- </h3>
- <CodeSnippet
- actions={
- <LemonButton
- icon={<IconRefresh />}
- size="xsmall"
- onClick={openRotateDialog}
- disabledReason={
- !isTeamTokenResetAvailable
- ? 'You do not have permission to rotate this key'
- : undefined
- }
- tooltip={currentTeam?.secret_api_token ? 'Rotate key' : 'Generate key'}
- />
- }
- className={currentTeam?.secret_api_token ? '' : 'text-muted'}
- thing="Secret API key"
- >
- {currentTeam?.secret_api_token ||
- 'No key yet — generate one with the button on the right to start using ticket workflow actions.'}
- </CodeSnippet>
- </div>
-
- {currentTeam?.secret_api_token ? (
- <LemonBanner type="warning" className="my-2">
- Rotating this key will require updating it everywhere it's used. Rotate if it has been
- compromised or as part of your regular key rotation policy.
- </LemonBanner>
- ) : (
- <LemonBanner type="warning" className="my-2">
- This project doesn't have a secret API key yet. Workflow steps that get or update tickets will
- fail until you generate one.
- </LemonBanner>
- )}
-
- {currentTeam?.secret_api_token_backup ? (
- <div>
- <h3 className="text-sm font-semibold mb-1">
- Backup key <span className="text-orange-600 text-xs ml-2">(Pending deletion)</span>
- </h3>
- <CodeSnippet
- actions={
- <LemonButton
- icon={<IconTrash />}
- size="xsmall"
- onClick={() => deleteSecretTokenBackup()}
- tooltip="Delete backup key"
- />
- }
- thing="Backup secret API key"
- >
- {currentTeam.secret_api_token_backup}
- </CodeSnippet>
- <p className="text-xs text-muted mt-1">
- This key is still active to support services using the previous key. Delete it once you've
- fully migrated.
- </p>
- </div>
- ) : null}
- </LemonCard>
- </SceneSection>
- )
-}
diff --git a/products/conversations/frontend/scenes/settings/signingSecretLogic.ts b/products/conversations/frontend/scenes/settings/signingSecretLogic.ts
new file mode 100644
index 000000000000..adfaf1b808f6
--- /dev/null
+++ b/products/conversations/frontend/scenes/settings/signingSecretLogic.ts
@@ -0,0 +1,81 @@
+import { MakeLogicType, afterMount, connect, kea, path, reducers } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import { teamLogic } from 'scenes/teamLogic'
+
+import { conversationsSigningSecretRetrieve, conversationsSigningSecretRotateCreate } from '../../generated/api'
+import type { SigningSecretApi } from '../../generated/api.schemas'
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface signingSecretLogicValues {
+ currentTeamId: number | null // teamLogic
+ signingSecret: SigningSecretApi | null
+ signingSecretLoadFailed: boolean
+ signingSecretLoading: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface signingSecretLogicActions {
+ loadSigningSecret: () => any
+ loadSigningSecretFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadSigningSecretSuccess: (
+ signingSecret: SigningSecretApi,
+ payload?: any
+ ) => {
+ signingSecret: SigningSecretApi
+ payload?: any
+ }
+ rotateSigningSecret: () => any
+ rotateSigningSecretFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ rotateSigningSecretSuccess: (
+ signingSecret: SigningSecretApi,
+ payload?: any
+ ) => {
+ signingSecret: SigningSecretApi
+ payload?: any
+ }
+}
+
+export type signingSecretLogicType = MakeLogicType<signingSecretLogicValues, signingSecretLogicActions>
+
+export const signingSecretLogic = kea<signingSecretLogicType>([
+ path(['products', 'conversations', 'frontend', 'scenes', 'settings', 'signingSecretLogic']),
+ connect(() => ({ values: [teamLogic, ['currentTeamId']] })),
+ loaders(({ values }) => ({
+ signingSecret: [
+ null as SigningSecretApi | null,
+ {
+ loadSigningSecret: async () => await conversationsSigningSecretRetrieve(String(values.currentTeamId)),
+ rotateSigningSecret: async () =>
+ await conversationsSigningSecretRotateCreate(String(values.currentTeamId)),
+ },
+ ],
+ })),
+ reducers({
+ // A failed fetch must not render as "no secret yet": generating from that state
+ // would rotate an existing secret without the cutoff warning.
+ signingSecretLoadFailed: [
+ false,
+ {
+ loadSigningSecret: () => false,
+ loadSigningSecretSuccess: () => false,
+ loadSigningSecretFailure: () => true,
+ },
+ ],
+ }),
+ afterMount(({ actions }) => {
+ actions.loadSigningSecret()
+ }),
+])