|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { createColumnHelper, getCoreRowModel, useReactTable } from '@tanstack/react-table' |
| 10 | +import { useCallback, useMemo, useState } from 'react' |
| 11 | +import { type LoaderFunctionArgs } from 'react-router' |
| 12 | + |
| 13 | +import { AccessToken24Icon } from '@oxide/design-system/icons/react' |
| 14 | +import { Badge } from '@oxide/design-system/ui' |
| 15 | + |
| 16 | +import { |
| 17 | + apiQueryClient, |
| 18 | + useApiMutation, |
| 19 | + usePrefetchedApiQuery, |
| 20 | + type ScimClientBearerToken, |
| 21 | +} from '~/api' |
| 22 | +import { getSiloSelector, useSiloSelector } from '~/hooks/use-params' |
| 23 | +import { confirmDelete } from '~/stores/confirm-delete' |
| 24 | +import { addToast } from '~/stores/toast' |
| 25 | +import { useColsWithActions, type MenuAction } from '~/table/columns/action-col' |
| 26 | +import { Columns } from '~/table/columns/common' |
| 27 | +import { Table } from '~/table/Table' |
| 28 | +import { CardBlock } from '~/ui/lib/CardBlock' |
| 29 | +import { CopyToClipboard } from '~/ui/lib/CopyToClipboard' |
| 30 | +import { CreateButton } from '~/ui/lib/CreateButton' |
| 31 | +import { DateTime } from '~/ui/lib/DateTime' |
| 32 | +import { EmptyMessage } from '~/ui/lib/EmptyMessage' |
| 33 | +import { Message } from '~/ui/lib/Message' |
| 34 | +import { Modal } from '~/ui/lib/Modal' |
| 35 | +import { TableEmptyBox } from '~/ui/lib/Table' |
| 36 | +import { Truncate } from '~/ui/lib/Truncate' |
| 37 | + |
| 38 | +const colHelper = createColumnHelper<ScimClientBearerToken>() |
| 39 | + |
| 40 | +const EmptyState = () => ( |
| 41 | + <TableEmptyBox border={false}> |
| 42 | + <EmptyMessage |
| 43 | + icon={<AccessToken24Icon />} |
| 44 | + title="No SCIM tokens" |
| 45 | + body="Create a token to see it here" |
| 46 | + /> |
| 47 | + </TableEmptyBox> |
| 48 | +) |
| 49 | + |
| 50 | +export async function clientLoader({ params }: LoaderFunctionArgs) { |
| 51 | + const { silo } = getSiloSelector(params) |
| 52 | + await apiQueryClient.prefetchQuery('scimTokenList', { query: { silo } }) |
| 53 | + return null |
| 54 | +} |
| 55 | + |
| 56 | +export default function SiloScimTab() { |
| 57 | + const siloSelector = useSiloSelector() |
| 58 | + const { data } = usePrefetchedApiQuery('scimTokenList', { |
| 59 | + query: { silo: siloSelector.silo }, |
| 60 | + }) |
| 61 | + |
| 62 | + // Order tokens by creation date, oldest first |
| 63 | + const tokens = useMemo( |
| 64 | + () => [...data].sort((a, b) => a.timeCreated.getTime() - b.timeCreated.getTime()), |
| 65 | + [data] |
| 66 | + ) |
| 67 | + |
| 68 | + const [showCreateModal, setShowCreateModal] = useState(false) |
| 69 | + const [createdToken, setCreatedToken] = useState<{ |
| 70 | + id: string |
| 71 | + bearerToken: string |
| 72 | + timeCreated: Date |
| 73 | + timeExpires?: Date | null |
| 74 | + } | null>(null) |
| 75 | + |
| 76 | + const deleteToken = useApiMutation('scimTokenDelete', { |
| 77 | + onSuccess() { |
| 78 | + apiQueryClient.invalidateQueries('scimTokenList') |
| 79 | + }, |
| 80 | + }) |
| 81 | + |
| 82 | + const makeActions = useCallback( |
| 83 | + (token: ScimClientBearerToken): MenuAction[] => [ |
| 84 | + { |
| 85 | + label: 'Delete', |
| 86 | + onActivate: confirmDelete({ |
| 87 | + doDelete: () => |
| 88 | + deleteToken.mutateAsync({ |
| 89 | + path: { tokenId: token.id }, |
| 90 | + query: { silo: siloSelector.silo }, |
| 91 | + }), |
| 92 | + label: token.id, |
| 93 | + }), |
| 94 | + }, |
| 95 | + ], |
| 96 | + [deleteToken, siloSelector.silo] |
| 97 | + ) |
| 98 | + |
| 99 | + const staticColumns = useMemo( |
| 100 | + () => [ |
| 101 | + colHelper.accessor('id', { |
| 102 | + header: 'ID', |
| 103 | + cell: (info) => ( |
| 104 | + <Truncate text={info.getValue()} position="middle" maxLength={18} /> |
| 105 | + ), |
| 106 | + }), |
| 107 | + colHelper.accessor('timeCreated', Columns.timeCreated), |
| 108 | + colHelper.accessor('timeExpires', { |
| 109 | + header: 'Expires', |
| 110 | + cell: (info) => { |
| 111 | + const expires = info.getValue() |
| 112 | + return expires ? ( |
| 113 | + <DateTime date={expires} /> |
| 114 | + ) : ( |
| 115 | + <Badge color="neutral">Never</Badge> |
| 116 | + ) |
| 117 | + }, |
| 118 | + meta: { thClassName: 'lg:w-1/4' }, |
| 119 | + }), |
| 120 | + ], |
| 121 | + [] |
| 122 | + ) |
| 123 | + |
| 124 | + const columns = useColsWithActions(staticColumns, makeActions, 'Copy token ID') |
| 125 | + |
| 126 | + const table = useReactTable({ |
| 127 | + data: tokens, |
| 128 | + columns, |
| 129 | + getCoreRowModel: getCoreRowModel(), |
| 130 | + }) |
| 131 | + // const { href, linkText } = docLinks.scim |
| 132 | + return ( |
| 133 | + <> |
| 134 | + <CardBlock> |
| 135 | + <CardBlock.Header |
| 136 | + title="SCIM Tokens" |
| 137 | + titleId="scim-tokens-label" |
| 138 | + description="Tokens for authenticating requests to SCIM endpoints" |
| 139 | + > |
| 140 | + <CreateButton onClick={() => setShowCreateModal(true)}>Create token</CreateButton> |
| 141 | + </CardBlock.Header> |
| 142 | + <CardBlock.Body> |
| 143 | + {tokens.length === 0 ? ( |
| 144 | + <EmptyState /> |
| 145 | + ) : ( |
| 146 | + <Table |
| 147 | + aria-labelledby="scim-tokens-label" |
| 148 | + table={table} |
| 149 | + className="table-inline" |
| 150 | + /> |
| 151 | + )} |
| 152 | + </CardBlock.Body> |
| 153 | + {/* TODO: put this back! |
| 154 | + <CardBlock.Footer> |
| 155 | + <LearnMore href={links.scimDocs} text="SCIM" /> |
| 156 | + </CardBlock.Footer> */} |
| 157 | + </CardBlock> |
| 158 | + |
| 159 | + {showCreateModal && ( |
| 160 | + <CreateTokenModal |
| 161 | + siloSelector={siloSelector} |
| 162 | + onDismiss={() => setShowCreateModal(false)} |
| 163 | + onSuccess={(token) => { |
| 164 | + setShowCreateModal(false) |
| 165 | + setCreatedToken(token) |
| 166 | + }} |
| 167 | + /> |
| 168 | + )} |
| 169 | + |
| 170 | + {createdToken && ( |
| 171 | + <TokenCreatedModal token={createdToken} onDismiss={() => setCreatedToken(null)} /> |
| 172 | + )} |
| 173 | + </> |
| 174 | + ) |
| 175 | +} |
| 176 | + |
| 177 | +function CreateTokenModal({ |
| 178 | + siloSelector, |
| 179 | + onDismiss, |
| 180 | + onSuccess, |
| 181 | +}: { |
| 182 | + siloSelector: { silo: string } |
| 183 | + onDismiss: () => void |
| 184 | + onSuccess: (token: { |
| 185 | + id: string |
| 186 | + bearerToken: string |
| 187 | + timeCreated: Date |
| 188 | + timeExpires?: Date | null |
| 189 | + }) => void |
| 190 | +}) { |
| 191 | + const createToken = useApiMutation('scimTokenCreate', { |
| 192 | + onSuccess(token) { |
| 193 | + apiQueryClient.invalidateQueries('scimTokenList') |
| 194 | + onSuccess(token) |
| 195 | + }, |
| 196 | + onError(err) { |
| 197 | + addToast({ variant: 'error', title: 'Failed to create token', content: err.message }) |
| 198 | + }, |
| 199 | + }) |
| 200 | + |
| 201 | + return ( |
| 202 | + <Modal isOpen onDismiss={onDismiss} title="Create SCIM token"> |
| 203 | + <Modal.Section> |
| 204 | + Anyone with this token can manage users and groups in this silo via SCIM. Since |
| 205 | + group membership grants roles, this token can be used to give a user admin |
| 206 | + privileges. Store it securely and never share it publicly. |
| 207 | + </Modal.Section> |
| 208 | + |
| 209 | + <Modal.Footer |
| 210 | + onDismiss={onDismiss} |
| 211 | + onAction={() => { |
| 212 | + createToken.mutate({ query: { silo: siloSelector.silo } }) |
| 213 | + }} |
| 214 | + actionText="Create" |
| 215 | + actionLoading={createToken.isPending} |
| 216 | + /> |
| 217 | + </Modal> |
| 218 | + ) |
| 219 | +} |
| 220 | + |
| 221 | +function TokenCreatedModal({ |
| 222 | + token, |
| 223 | + onDismiss, |
| 224 | +}: { |
| 225 | + token: { |
| 226 | + id: string |
| 227 | + bearerToken: string |
| 228 | + timeCreated: Date |
| 229 | + timeExpires?: Date | null |
| 230 | + } |
| 231 | + onDismiss: () => void |
| 232 | +}) { |
| 233 | + return ( |
| 234 | + <Modal isOpen onDismiss={onDismiss} title="SCIM token created"> |
| 235 | + <Modal.Section> |
| 236 | + <Message |
| 237 | + variant="notice" |
| 238 | + content=<> |
| 239 | + This is the only time you’ll see this token. Copy it now and store it securely. |
| 240 | + </> |
| 241 | + /> |
| 242 | + |
| 243 | + <div className="mt-4"> |
| 244 | + <div className="text-sans-md text-raise mb-2">Bearer Token</div> |
| 245 | + <div className="text-sans-md text-raise bg-default border-default flex items-stretch rounded border"> |
| 246 | + <div className="flex-1 overflow-hidden px-3 py-2.75 text-ellipsis"> |
| 247 | + {token.bearerToken} |
| 248 | + </div> |
| 249 | + <div className="border-default flex w-8 items-center justify-center border-l"> |
| 250 | + <CopyToClipboard text={token.bearerToken} /> |
| 251 | + </div> |
| 252 | + </div> |
| 253 | + </div> |
| 254 | + </Modal.Section> |
| 255 | + |
| 256 | + <Modal.Footer |
| 257 | + onDismiss={onDismiss} |
| 258 | + actionText="Done" |
| 259 | + onAction={onDismiss} |
| 260 | + showCancel={false} |
| 261 | + /> |
| 262 | + </Modal> |
| 263 | + ) |
| 264 | +} |
0 commit comments