Skip to content

Commit

Permalink
Include copy of ratelimit, refill, identityId and roles
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroasano committed Sep 5, 2024
1 parent 58e0e48 commit 16513bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CopyButton } from "@/components/dashboard/copy-button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Code } from "@/components/ui/code";
import { getTenantId } from "@/lib/auth";
import { and, db, eq, isNull, schema } from "@/lib/db";
import { and, db, eq, isNull, Key, schema } from "@/lib/db";
import { ArrowLeft } from "lucide-react";
import Link from "next/link";
import { notFound } from "next/navigation";
Expand Down Expand Up @@ -34,6 +34,8 @@ export default async function SettingsPage(props: Props) {
workspace: true,
encrypted: true,
identity: true,
roles: true,
permissions: true,
},
});
if (!key || key.workspace.tenantId !== tenantId) {
Expand Down Expand Up @@ -70,7 +72,7 @@ export default async function SettingsPage(props: Props) {
</Code>
</CardContent>
</Card>
<RerollKey apiId={props.params.apiId} apiKey={key} />
<RerollKey apiId={props.params.apiId} apiKey={key as unknown as Key & { roles : []}} />
<DeleteKey apiKey={key} keyAuthId={key.keyAuthId} />
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import { z } from "zod";

type Props = {
apiId: string;
apiKey: Key;
apiKey: Key & {
roles: []
};
};

const EXPIRATION_OPTIONS = [
Expand Down Expand Up @@ -67,6 +69,9 @@ export const RerollKey: React.FC<Props> = ({ apiKey, apiId }: Props) => {
});

const createKey = trpc.key.create.useMutation({
onMutate() {
toast.success("Rerolling Key");
},
onSuccess({ keyId }) {
setNewKeyId(keyId);
},
Expand All @@ -77,9 +82,17 @@ export const RerollKey: React.FC<Props> = ({ apiKey, apiId }: Props) => {
},
});

const updateNewKey = trpc.rbac.connectRoleToKey.useMutation({
onError(err) {
console.error(err);
const message = parseTrpcError(err);
toast.error(message);
},
});

const updateDeletedAt = trpc.key.update.deletedAt.useMutation({
onSuccess() {
toast.success("Rerolling completed.");
toast.success("Key Rerolled.");
router.push(`/apis/${apiId}/keys/${apiKey.keyAuthId}/${newKeyId}/settings`);
},
onError(err) {
Expand All @@ -90,16 +103,32 @@ export const RerollKey: React.FC<Props> = ({ apiKey, apiId }: Props) => {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
toast.success("Rerolling in progress.");
const ratelimit = apiKey.ratelimitLimit ? {
async: apiKey.ratelimitAsync ?? false,
duration: apiKey.ratelimitDuration ?? 0,
limit: apiKey.ratelimitLimit ?? 0,
} : undefined;

await createKey.mutateAsync({
const refill = apiKey.refillInterval ? {
interval: apiKey.refillInterval ?? "daily",
amount: apiKey.refillAmount ?? 0,
} : undefined;

const newKey = await createKey.mutateAsync({
...apiKey,
keyAuthId: apiKey.keyAuthId,
name: apiKey.name || undefined,
environment: apiKey.environment || undefined,
meta: apiKey.meta ? JSON.parse(apiKey.meta) : undefined,
expires: apiKey.expires?.getTime() ?? undefined,
remaining: apiKey.remaining ?? undefined,
identityId: apiKey.identityId ?? undefined,
ratelimit,
refill,
});

apiKey.roles.forEach(async (role: { roleId : string }) => {
await updateNewKey.mutateAsync({ roleId: role.roleId, keyId: newKey.keyId})
});

const miliseconds = ms(values.expiresIn);
Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/lib/trpc/routers/key/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const createKey = t.procedure
.optional(),
enabled: z.boolean().default(true),
environment: z.string().optional(),
identityId: z.string().optional(),
}),
)
.mutation(async ({ input, ctx }) => {
Expand Down Expand Up @@ -92,6 +93,7 @@ export const createKey = t.procedure
deletedAt: null,
enabled: input.enabled,
environment: input.environment,
identityId: input.identityId,
})
.catch((_err) => {
throw new TRPCError({
Expand Down

0 comments on commit 16513bc

Please sign in to comment.