Skip to content

feat: Manage tokens in dashboard #5444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions coderd/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) {
}

keys, err := api.Database.GetAPIKeysByLoginType(ctx, database.LoginTypeToken)
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusOK, []codersdk.APIKey{})
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching API keys.",
Expand All @@ -201,7 +197,7 @@ func (api *API) tokens(rw http.ResponseWriter, r *http.Request) {
return
}

var apiKeys []codersdk.APIKey
apiKeys := []codersdk.APIKey{}
for _, key := range keys {
apiKeys = append(apiKeys, convertAPIKey(key))
}
Expand Down
4 changes: 4 additions & 0 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const SecurityPage = lazy(
const SSHKeysPage = lazy(
() => import("./pages/UserSettingsPage/SSHKeysPage/SSHKeysPage"),
)
const TokensPage = lazy(
() => import("./pages/UserSettingsPage/TokensPage/TokensPage"),
)
const CreateUserPage = lazy(
() => import("./pages/UsersPage/CreateUserPage/CreateUserPage"),
)
Expand Down Expand Up @@ -219,6 +222,7 @@ export const AppRouter: FC = () => {
<Route path="account" element={<AccountPage />} />
<Route path="security" element={<SecurityPage />} />
<Route path="ssh-keys" element={<SSHKeysPage />} />
<Route path="tokens" element={<TokensPage />} />
</Route>

<Route path="/@:username">
Expand Down
12 changes: 12 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ export const getApiKey = async (): Promise<TypesGen.GenerateAPIKeyResponse> => {
return response.data
}

export const getTokens = async (): Promise<TypesGen.APIKey[]> => {
const response = await axios.get<TypesGen.APIKey[]>(
"/api/v2/users/me/keys/tokens",
)
return response.data
}

export const deleteAPIKey = async (keyId: string): Promise<void> => {
const response = await axios.delete("/api/v2/users/me/keys/" + keyId)
return response.data
}

export const getUsers = async (
options: TypesGen.UsersRequest,
): Promise<TypesGen.GetUsersResponse> => {
Expand Down
9 changes: 8 additions & 1 deletion site/src/components/SettingsLayout/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeStyles } from "@material-ui/core/styles"
import VpnKeyOutlined from "@material-ui/icons/VpnKeyOutlined"
import FingerprintOutlinedIcon from "@material-ui/icons/FingerprintOutlined"
import { User } from "api/typesGenerated"
import { Stack } from "components/Stack/Stack"
import { UserAvatar } from "components/UserAvatar/UserAvatar"
Expand Down Expand Up @@ -65,10 +66,16 @@ export const Sidebar: React.FC<{ user: User }> = ({ user }) => {
</SidebarNavItem>
<SidebarNavItem
href="ssh-keys"
icon={<SidebarNavItemIcon icon={VpnKeyOutlined} />}
icon={<SidebarNavItemIcon icon={FingerprintOutlinedIcon} />}
>
SSH Keys
</SidebarNavItem>
<SidebarNavItem
href="tokens"
icon={<SidebarNavItemIcon icon={VpnKeyOutlined} />}
>
Tokens
</SidebarNavItem>
</nav>
)
}
Expand Down
80 changes: 80 additions & 0 deletions site/src/pages/UserSettingsPage/TokensPage/TokensPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { FC, PropsWithChildren } from "react"
import { Section } from "../../../components/Section/Section"
import { TokensPageView } from "./TokensPageView"
import { tokensMachine } from "xServices/tokens/tokensXService"
import { useMachine } from "@xstate/react"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import { Typography } from "components/Typography/Typography"
import makeStyles from "@material-ui/core/styles/makeStyles"

export const Language = {
title: "Tokens",
descriptionPrefix:
"Tokens are used to authenticate with the Coder API. You can create a token with the Coder CLI using the ",
deleteTitle: "Delete Token",
deleteDescription: "Are you sure you want to delete this token?",
}

export const TokensPage: FC<PropsWithChildren<unknown>> = () => {
const [tokensState, tokensSend] = useMachine(tokensMachine)
const isLoading = tokensState.matches("gettingTokens")
const hasLoaded = tokensState.matches("loaded")
const { getTokensError, tokens, deleteTokenId } = tokensState.context
const styles = useStyles()
const description = (
<p>
{Language.descriptionPrefix}{" "}
<code className={styles.code}>coder tokens create</code> command.
</p>
)

const content = (
<Typography>
{Language.deleteDescription}
<br />
<br />
{deleteTokenId}
</Typography>
)

return (
<>
<Section title={Language.title} description={description} layout="fluid">
<TokensPageView
tokens={tokens}
isLoading={isLoading}
hasLoaded={hasLoaded}
getTokensError={getTokensError}
onDelete={(id) => {
tokensSend({ type: "DELETE_TOKEN", id })
}}
/>
</Section>

<ConfirmDialog
title={Language.deleteTitle}
description={content}
open={tokensState.matches("confirmTokenDelete")}
confirmLoading={tokensState.matches("deletingToken")}
onConfirm={() => {
tokensSend("CONFIRM_DELETE_TOKEN")
}}
onClose={() => {
tokensSend("CANCEL_DELETE_TOKEN")
}}
/>
</>
)
}

const useStyles = makeStyles((theme) => ({
code: {
background: theme.palette.divider,
fontSize: 12,
padding: "2px 4px",
color: theme.palette.text.primary,
borderRadius: 2,
},
}))

export default TokensPage
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Story } from "@storybook/react"
import { makeMockApiError, MockTokens } from "testHelpers/entities"
import { TokensPageView, TokensPageViewProps } from "./TokensPageView"

export default {
title: "components/TokensPageView",
component: TokensPageView,
argTypes: {
onRegenerateClick: { action: "Submit" },
},
}

const Template: Story<TokensPageViewProps> = (args: TokensPageViewProps) => (
<TokensPageView {...args} />
)

export const Example = Template.bind({})
Example.args = {
isLoading: false,
hasLoaded: true,
tokens: MockTokens,
onDelete: () => {
return Promise.resolve()
},
}

export const Loading = Template.bind({})
Loading.args = {
...Example.args,
isLoading: true,
hasLoaded: false,
}

export const Empty = Template.bind({})
Empty.args = {
...Example.args,
tokens: [],
}

export const WithGetTokensError = Template.bind({})
WithGetTokensError.args = {
...Example.args,
hasLoaded: false,
getTokensError: makeMockApiError({
message: "Failed to get tokens.",
}),
}

export const WithDeleteTokenError = Template.bind({})
WithDeleteTokenError.args = {
...Example.args,
hasLoaded: false,
deleteTokenError: makeMockApiError({
message: "Failed to delete token.",
}),
}
132 changes: 132 additions & 0 deletions site/src/pages/UserSettingsPage/TokensPage/TokensPageView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { useTheme } from "@material-ui/core/styles"
import Table from "@material-ui/core/Table"
import TableBody from "@material-ui/core/TableBody"
import TableCell from "@material-ui/core/TableCell"
import TableContainer from "@material-ui/core/TableContainer"
import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import { APIKey } from "api/typesGenerated"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
import { Stack } from "components/Stack/Stack"
import { TableEmpty } from "components/TableEmpty/TableEmpty"
import { TableLoader } from "components/TableLoader/TableLoader"
import DeleteOutlineIcon from "@material-ui/icons/DeleteOutline"
import dayjs from "dayjs"
import { FC } from "react"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import IconButton from "@material-ui/core/IconButton/IconButton"

export const Language = {
idLabel: "ID",
createdAtLabel: "Created At",
lastUsedLabel: "Last Used",
expiresAtLabel: "Expires At",
emptyMessage: "No tokens found",
ariaDeleteLabel: "Delete Token",
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an alternative to Language now, react-i18n, but if it's a hassle, don't worry about it, we're going to revisit whether it's worth the time


const lastUsedOrNever = (lastUsed: string) => {
const t = dayjs(lastUsed)
const now = dayjs()
return now.isBefore(t.add(100, "year")) ? t.fromNow() : "Never"
}

export interface TokensPageViewProps {
tokens?: APIKey[]
getTokensError?: Error | unknown
isLoading: boolean
hasLoaded: boolean
onDelete: (id: string) => void
deleteTokenError?: Error | unknown
}

export const TokensPageView: FC<
React.PropsWithChildren<TokensPageViewProps>
> = ({
tokens,
getTokensError,
isLoading,
hasLoaded,
onDelete,
deleteTokenError,
}) => {
const theme = useTheme()

return (
<Stack>
{Boolean(getTokensError) && (
<AlertBanner severity="error" error={getTokensError} />
)}
{Boolean(deleteTokenError) && (
<AlertBanner severity="error" error={deleteTokenError} />
)}
<TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell width="25%">{Language.idLabel}</TableCell>
<TableCell width="25%">{Language.createdAtLabel}</TableCell>
<TableCell width="25%">{Language.lastUsedLabel}</TableCell>
<TableCell width="25%">{Language.expiresAtLabel}</TableCell>
<TableCell width="0%"></TableCell>
</TableRow>
</TableHead>
<TableBody>
<ChooseOne>
<Cond condition={isLoading}>
<TableLoader />
</Cond>
<Cond condition={hasLoaded && tokens?.length === 0}>
<TableEmpty message={Language.emptyMessage} />
</Cond>
<Cond>
{tokens?.map((token) => {
return (
<TableRow
key={token.id}
data-testid={`token-${token.id}`}
tabIndex={0}
>
<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
{token.id}
</span>
</TableCell>

<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
{dayjs(token.created_at).fromNow()}
</span>
</TableCell>

<TableCell>{lastUsedOrNever(token.last_used)}</TableCell>

<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
{dayjs(token.expires_at).fromNow()}
</span>
</TableCell>
<TableCell>
<span style={{ color: theme.palette.text.secondary }}>
<IconButton
onClick={() => {
onDelete(token.id)
}}
size="medium"
aria-label={Language.ariaDeleteLabel}
>
<DeleteOutlineIcon />
</IconButton>
</span>
</TableCell>
</TableRow>
)
})}
</Cond>
</ChooseOne>
</TableBody>
</Table>
</TableContainer>
</Stack>
)
}
25 changes: 25 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ export const MockAPIKey: TypesGen.GenerateAPIKeyResponse = {
key: "my-api-key",
}

export const MockTokens: TypesGen.APIKey[] = [
{
id: "tBoVE3dqLl",
user_id: "f9ee61d8-1d84-4410-ab6e-c1ec1a641e0b",
last_used: "0001-01-01T00:00:00Z",
expires_at: "2023-01-15T20:10:45.637438Z",
created_at: "2022-12-16T20:10:45.637452Z",
updated_at: "2022-12-16T20:10:45.637452Z",
login_type: "token",
scope: "all",
lifetime_seconds: 2592000,
},
{
id: "tBoVE3dqLl",
user_id: "f9ee61d8-1d84-4410-ab6e-c1ec1a641e0b",
last_used: "0001-01-01T00:00:00Z",
expires_at: "2023-01-15T20:10:45.637438Z",
created_at: "2022-12-16T20:10:45.637452Z",
updated_at: "2022-12-16T20:10:45.637452Z",
login_type: "token",
scope: "all",
lifetime_seconds: 2592000,
},
]

export const MockBuildInfo: TypesGen.BuildInfoResponse = {
external_url: "file:///mock-url",
version: "v99.999.9999+c9cdf14",
Expand Down
Loading