Skip to content
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

chore: add internal package called @unkey/vault #2279

Merged
merged 3 commits into from
Oct 28, 2024
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
1 change: 1 addition & 0 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@unkey/rbac": "workspace:^",
"@unkey/resend": "workspace:^",
"@unkey/schema": "workspace:^",
"@unkey/vault": "workspace:^",
"@unkey/vercel": "workspace:^",
"@upstash/ratelimit": "^2.0.1",
"@upstash/redis": "^1.31.3",
Expand Down
4 changes: 2 additions & 2 deletions apps/www/app/glossary/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CTA } from "@/components/cta";
import { Frame } from "@/components/frame";

import { FilterableCommand } from "@/components/glossary/search";
import TermsRolodexDesktop from "@/components/glossary/terms-rolodex-desktop";
import TermsStepperMobile from "@/components/glossary/terms-stepper-mobile";
import { MDX } from "@/components/mdx-content";
import { TopLeftShiningLight, TopRightShiningLight } from "@/components/svg/background-shiny";
Expand All @@ -14,8 +16,6 @@ import Link from "next/link";
import { notFound } from "next/navigation";
import { FAQ } from "./faq";
import Takeaways from "./takeaways";
import TermsRolodexDesktop from "@/components/glossary/terms-rolodex-desktop";
import { FilterableCommand } from "@/components/glossary/search";

export const generateStaticParams = async () =>
allGlossaries.map((term) => ({
Expand Down
4 changes: 2 additions & 2 deletions apps/www/app/glossary/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { CTA } from "@/components/cta";
import { ChangelogLight } from "@/components/svg/changelog";

import { type Glossary, allGlossaries } from "@/.content-collections/generated";
import { PrimaryButton } from "@/components/button";
import { Container } from "@/components/container";
import { FilterableCommand } from "@/components/glossary/search";
import { MeteorLinesAngular } from "@/components/ui/meteorLines";
import { LogIn } from "lucide-react";
import Link from "next/link";
import { allGlossaries, type Glossary } from "@/.content-collections/generated";
import { Zap } from "lucide-react";
import Link from "next/link";

export function GlossaryClient() {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
Expand Down
6 changes: 3 additions & 3 deletions apps/www/components/glossary/search.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import * as React from "react";
import { Command as CommandPrimitive } from "cmdk";
import type { Glossary } from "@/.content-collections/generated";
import {
Command,
CommandEmpty,
Expand All @@ -10,8 +9,9 @@ import {
CommandList,
} from "@/components/ui/command";
import { cn } from "@/lib/utils";
import { Command as CommandPrimitive } from "cmdk";
import { useRouter } from "next/navigation";
import type { Glossary } from "@/.content-collections/generated";
import * as React from "react";

export function FilterableCommand(props: {
placeholder: string;
Expand Down
8 changes: 4 additions & 4 deletions apps/www/components/glossary/terms-rolodex-desktop.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";

import { useState } from "react";
import Link from "next/link";
import type { Glossary } from "@/.content-collections/generated";
import { Button } from "@/components/ui/button";
import { ChevronUpIcon, ChevronDownIcon } from "lucide-react";
import { cn } from "@/lib/utils";
import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";
import Link from "next/link";
import { useParams } from "next/navigation";
import type { Glossary } from "@/.content-collections/generated";
import { useState } from "react";

export default function TermsRolodexDesktop({
className,
Expand Down
14 changes: 14 additions & 0 deletions internal/vault/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@unkey/vault",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"types": "src/index.ts",
"keywords": [],
"author": "",
"devDependencies": {
"@types/node": "^20.14.9",
"@unkey/tsconfig": "workspace:*",
"typescript": "^5.5.3"
}
}
101 changes: 101 additions & 0 deletions internal/vault/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
export type EncryptRequest = {
keyring: string;
data: string;
};

export type EncryptResponse = {
encrypted: string;
keyId: string;
};

export type EncryptBulkRequest = {
keyring: string;
data: string[];
};

export type EncryptBulkResponse = {
encrypted: EncryptResponse[];
};

export type DecryptRequest = {
keyring: string;
encrypted: string;
};

export type DecryptResponse = {
plaintext: string;
};

export type RequestContext = {
requestId: string;
};

export class Vault {
private readonly baseUrl: string;
private readonly token: string;

constructor(baseUrl: string, token: string) {
this.baseUrl = baseUrl;
this.token = token;
}

public async encrypt(c: RequestContext, req: EncryptRequest): Promise<EncryptResponse> {
const url = `${this.baseUrl}/vault.v1.VaultService/Encrypt`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
"Unkey-Request-Id": c.requestId,
},
body: JSON.stringify(req),
});

if (!res.ok) {
throw new Error(`Unable to encrypt, fetch error: ${await res.text()}`);
}
chronark marked this conversation as resolved.
Show resolved Hide resolved

return res.json();
}
chronark marked this conversation as resolved.
Show resolved Hide resolved

public async encryptBulk(
c: RequestContext,
req: EncryptBulkRequest,
): Promise<EncryptBulkResponse> {
const url = `${this.baseUrl}/vault.v1.VaultService/EncryptBulk`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
"Unkey-Request-Id": c.requestId,
},
body: JSON.stringify(req),
});

if (!res.ok) {
throw new Error(`Unable to encryptBulk, fetch error: ${await res.text()}`);
}

return res.json();
}

public async decrypt(c: RequestContext, req: DecryptRequest): Promise<DecryptResponse> {
const url = `${this.baseUrl}/vault.v1.VaultService/Decrypt`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.token}`,
"Unkey-Request-Id": c.requestId,
},
body: JSON.stringify(req),
});

if (!res.ok) {
throw new Error(`Unable to decrypt, fetch error: ${await res.text()}`);
}

return res.json();
}
}
chronark marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading