: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { organizationLogic } from 'scenes/organizationLogic'
import {
cimdVerificationTokensCreate,
cimdVerificationTokensDestroy,
cimdVerificationTokensList,
cimdVerificationTokensPartialUpdate,
} from '~/generated/core/api'
import { CIMDVerificationTokenApi, CIMDVerificationTokenWithValueApi } from '~/generated/core/api.schemas'
export type CIMDVerificationToken = CIMDVerificationTokenApi
export type CIMDVerificationTokenWithValue = CIMDVerificationTokenWithValueApi
/**
* Cheap client-side check for the two most common mistakes (missing HTTPS, missing path)
* so the user doesn't need a round trip to find out. The server is still the source of
* truth for every other rejection reason (query string, port, etc).
*/
export function validateCimdUrl(url: string): string | null {
if (!url) {
return 'Please enter your CIMD metadata URL'
}
if (!url.startsWith('https://')) {
return 'The metadata URL must start with https://'
}
let parsed: URL
try {
parsed = new URL(url)
} catch {
return 'Please enter a valid URL'
}
if (parsed.pathname === '/' || parsed.pathname === '') {
return 'The metadata URL must include a path, e.g. /.well-known/oauth-client-metadata.json'
}
return null
}
function extractServerError(e: any, fallback: string): string {
return e?.detail ?? e?.cimd_url?.[0] ?? e?.label?.[0] ?? fallback
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cimdVerificationTokensLogicValues {
bindUrl: string
bindingToken: CIMDVerificationToken | null
isBindingToken: boolean
isCreateDialogOpen: boolean
isCreatingToken: boolean
justCreatedToken: CIMDVerificationTokenWithValue | null
newTokenLabel: string
newTokenUrl: string
tokens: CIMDVerificationToken[]
tokensLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cimdVerificationTokensLogicActions {
bindToken: () => {
value: true
}
createToken: () => {
value: true
}
deleteToken: (token: CIMDVerificationToken) => {
token: CIMDVerificationTokenApi
}
hideBindDialog: () => {
value: true
}
hideCreateDialog: () => {
value: true
}
loadTokens: () => any
loadTokensFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadTokensSuccess: (
tokens: CIMDVerificationTokenApi[],
payload?: any
) => {
tokens: CIMDVerificationTokenApi[]
payload?: any
}
setBindUrl: (url: string) => {
url: string
}
setIsBindingToken: (binding: boolean) => {
binding: boolean
}
setIsCreatingToken: (creating: boolean) => {
creating: boolean
}
setJustCreatedToken: (token: CIMDVerificationTokenWithValue | null) => {
token: CIMDVerificationTokenWithValueApi | null
}
setNewTokenLabel: (label: string) => {
label: string
}
setNewTokenUrl: (url: string) => {
url: string
}
showBindDialog: (token: CIMDVerificationToken) => {
token: CIMDVerificationTokenApi
}
showCreateDialog: () => {
value: true
}
}
export type cimdVerificationTokensLogicType = MakeLogicType<
cimdVerificationTokensLogicValues,
cimdVerificationTokensLogicActions
>
export const cimdVerificationTokensLogic = kea<cimdVerificationTokensLogicType>([
path(['scenes', 'settings', 'organization', 'cimdVerificationTokensLogic']),
actions({
showCreateDialog: true,
hideCreateDialog: true,
setNewTokenLabel: (label: string) => ({ label }),
setNewTokenUrl: (url: string) => ({ url }),
setIsCreatingToken: (creating: boolean) => ({ creating }),
setJustCreatedToken: (token: CIMDVerificationTokenWithValue | null) => ({ token }),
createToken: true,
deleteToken: (token: CIMDVerificationToken) => ({ token }),
showBindDialog: (token: CIMDVerificationToken) => ({ token }),
hideBindDialog: true,
setBindUrl: (url: string) => ({ url }),
setIsBindingToken: (binding: boolean) => ({ binding }),
bindToken: true,
}),
reducers({
isCreateDialogOpen: [
false,
{
showCreateDialog: () => true,
hideCreateDialog: () => false,
},
],
isCreatingToken: [
false,
{
setIsCreatingToken: (_, { creating }) => creating,
},
],
newTokenLabel: [
'',
{
setNewTokenLabel: (_, { label }) => label,
hideCreateDialog: () => '',
},
],
newTokenUrl: [
'',
{
setNewTokenUrl: (_, { url }) => url,
hideCreateDialog: () => '',
},
],
justCreatedToken: [
null as CIMDVerificationTokenWithValue | null,
{
setJustCreatedToken: (_, { token }) => token,
},
],
bindingToken: [
null as CIMDVerificationToken | null,
{
showBindDialog: (_, { token }) => token,
hideBindDialog: () => null,
},
],
bindUrl: [
'',
{
setBindUrl: (_, { url }) => url,
showBindDialog: () => '',
hideBindDialog: () => '',
},
],
isBindingToken: [
false,
{
setIsBindingToken: (_, { binding }) => binding,
},
],
}),
loaders({
tokens: [
[] as CIMDVerificationToken[],
{
loadTokens: async () => {
const orgId = organizationLogic.values.currentOrganization?.id
if (!orgId) {
return []
}
const response = await cimdVerificationTokensList(orgId)
return response.results
},
},
],
}),
listeners(({ actions, values }) => ({
createToken: async () => {
const orgId = organizationLogic.values.currentOrganization?.id
if (!orgId) {
return
}
const label = values.newTokenLabel.trim()
if (!label) {
lemonToast.error('Please enter a label for this token.')
return
}
const cimdUrl = values.newTokenUrl.trim()
const urlError = validateCimdUrl(cimdUrl)
if (urlError) {
lemonToast.error(urlError)
return
}
actions.setIsCreatingToken(true)
try {
const created = await cimdVerificationTokensCreate(orgId, { label, cimd_url: cimdUrl })
actions.setJustCreatedToken(created)
actions.hideCreateDialog()
actions.loadTokens()
} catch (e: any) {
lemonToast.error(extractServerError(e, 'Failed to create token'))
} finally {
actions.setIsCreatingToken(false)
}
},
deleteToken: async ({ token }) => {
const orgId = organizationLogic.values.currentOrganization?.id
if (!orgId) {
return
}
try {
await cimdVerificationTokensDestroy(orgId, token.id)
lemonToast.success('Token revoked')
actions.loadTokens()
} catch (e: any) {
lemonToast.error(extractServerError(e, 'Failed to revoke token'))
}
},
bindToken: async () => {
const orgId = organizationLogic.values.currentOrganization?.id
const token = values.bindingToken
if (!orgId || !token) {
return
}
const cimdUrl = values.bindUrl.trim()
const urlError = validateCimdUrl(cimdUrl)
if (urlError) {
lemonToast.error(urlError)
return
}
actions.setIsBindingToken(true)
try {
await cimdVerificationTokensPartialUpdate(orgId, token.id, { cimd_url: cimdUrl })
lemonToast.success('Metadata URL set. The token will verify there now.')
actions.hideBindDialog()
actions.loadTokens()
} catch (e: any) {
lemonToast.error(extractServerError(e, 'Failed to set metadata URL'))
} finally {
actions.setIsBindingToken(false)
}
},
})),
afterMount(({ actions }) => {
actions.loadTokens()
}),
])