-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
302 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {scrypt, randomBytes, createCipheriv, createDecipheriv} from 'crypto' | ||
|
||
export const getCryptoSuite = async () => { | ||
const algorithm = 'aes-192-cbc' | ||
const key = await new Promise<Buffer>((resolve, reject) => { | ||
scrypt( | ||
process.env.PASSWORD_KEY!, | ||
process.env.PASSWORD_SALT!, | ||
24, | ||
(error, derivedKey) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
|
||
resolve(derivedKey) | ||
} | ||
) | ||
}) | ||
|
||
const encrypt = (text: string) => { | ||
console.dir(randomBytes(8).toString('hex')) | ||
|
||
const cipher = createCipheriv( | ||
algorithm, | ||
key, | ||
Buffer.from(process.env.PASSWORD_IV!, 'utf8') | ||
) | ||
|
||
return cipher.update(text, 'utf8', 'hex') + cipher.final('hex') | ||
} | ||
|
||
const decrypt = (hash: string) => { | ||
const decipher = createDecipheriv( | ||
algorithm, | ||
key, | ||
Buffer.from(process.env.PASSWORD_IV!, 'utf8') | ||
) | ||
return decipher.update(hash, 'hex', 'utf8') + decipher.final('utf8') | ||
} | ||
|
||
return {encrypt, decrypt} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {type LoaderFunctionArgs, json} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
import {useState} from 'react' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {AButton} from '~/lib/components/button' | ||
import {buildMDXBundle} from '~/lib/mdx.server' | ||
import {MDXComponent} from '~/lib/mdx' | ||
|
||
export const loader = async ({request, params}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'password:view', { | ||
passwordId: params.password | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const password = await prisma.password.findFirstOrThrow({ | ||
select: {id: true, title: true, username: true, notes: true}, | ||
where: {id: params.password} | ||
}) | ||
|
||
const code = await buildMDXBundle(password.notes) | ||
|
||
return json({user, password, code}) | ||
} | ||
|
||
const AssetManagerAsset = () => { | ||
const {password, code} = useLoaderData<typeof loader>() | ||
const [passwordOpen, setPasswordOpen] = useState(false) | ||
|
||
return ( | ||
<div> | ||
<h4 className="text-xl">{password.title}</h4> | ||
<AButton href={`/app/passwords/${password.id}/edit`} className="bg-info"> | ||
Edit | ||
</AButton> | ||
<p> | ||
<b>Username</b> | ||
<br /> | ||
{password.username} | ||
</p> | ||
<p> | ||
<b>Password</b> | ||
<br /> | ||
{passwordOpen ? ( | ||
'OPEN' | ||
) : ( | ||
<button | ||
onClick={() => { | ||
setPasswordOpen(true) | ||
}} | ||
> | ||
🔐 | ||
</button> | ||
)} | ||
</p> | ||
<MDXComponent code={code} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default AssetManagerAsset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {type LoaderFunctionArgs, json} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {AButton} from '~/lib/components/button' | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'password:list', {}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const passwords = await prisma.password.findMany({orderBy: {title: 'asc'}}) | ||
|
||
return json({user, passwords}) | ||
} | ||
|
||
const DocumentsList = () => { | ||
const {passwords} = useLoaderData<typeof loader>() | ||
|
||
return ( | ||
<div> | ||
<AButton className="bg-success" href="/app/passwords/add"> | ||
Add Password | ||
</AButton> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Password</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{passwords.map(({id, title}) => { | ||
return ( | ||
<tr key={id}> | ||
<td> | ||
<a href={`/app/passwords/${id}`}>{title}</a> | ||
</td> | ||
</tr> | ||
) | ||
})} | ||
</tbody> | ||
</table> | ||
</div> | ||
) | ||
} | ||
|
||
export default DocumentsList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
type ActionFunctionArgs, | ||
json, | ||
redirect | ||
} from '@remix-run/node' | ||
import {invariant} from '@arcath/utils' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {Button} from '~/lib/components/button' | ||
import {Label, Input, HelperText, TextArea} from '~/lib/components/input' | ||
|
||
import {getCryptoSuite} from '~/lib/crypto.server' | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'password:add', {}) | ||
|
||
return json({user}) | ||
} | ||
|
||
export const action = async ({request}: ActionFunctionArgs) => { | ||
await ensureUser(request, 'password:add', {}) | ||
|
||
const formData = await request.formData() | ||
|
||
const prisma = getPrisma() | ||
const {encrypt} = await getCryptoSuite() | ||
|
||
const title = formData.get('title') as string | undefined | ||
const username = formData.get('username') as string | undefined | ||
const password = formData.get('password') as string | undefined | ||
const notes = formData.get('notes') as string | undefined | ||
|
||
invariant(title) | ||
invariant(password) | ||
|
||
const newPassword = await prisma.password.create({ | ||
data: { | ||
title, | ||
username: username ? username : '', | ||
password: encrypt(password), | ||
notes: notes ? notes : '' | ||
} | ||
}) | ||
|
||
return redirect(`/app/passwords/${newPassword.id}`) | ||
} | ||
|
||
const PasswordAdd = () => { | ||
return ( | ||
<div> | ||
<h2>Add Password</h2> | ||
<form method="POST"> | ||
<Label> | ||
Title | ||
<Input name="title" /> | ||
<HelperText>The title of the Password.</HelperText> | ||
</Label> | ||
<Label> | ||
Username | ||
<Input name="username" /> | ||
<HelperText>The username (can be blank).</HelperText> | ||
</Label> | ||
<Label> | ||
Password | ||
<Input name="password" /> | ||
<HelperText>The password.</HelperText> | ||
</Label> | ||
<Label> | ||
Notes | ||
<TextArea name="notes" className="min-h-[25vh]" /> | ||
<HelperText>Any notes for the password.</HelperText> | ||
</Label> | ||
<Button className="bg-success">Add Password</Button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
export default PasswordAdd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Outlet} from '@remix-run/react' | ||
|
||
import {Header} from '~/lib/components/header' | ||
|
||
const Passwords = () => { | ||
return ( | ||
<div> | ||
<Header title="Passwords" /> | ||
<Outlet /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Passwords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
prisma/migrations/20240303172355_add_passwords/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- CreateTable | ||
CREATE TABLE "Password" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"title" TEXT NOT NULL, | ||
"username" TEXT NOT NULL, | ||
"password" TEXT NOT NULL, | ||
"notes" TEXT NOT NULL, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "PasswordHistory" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"previousTitle" TEXT NOT NULL, | ||
"previousBody" TEXT NOT NULL, | ||
"editedById" TEXT NOT NULL, | ||
"passwordId" TEXT NOT NULL, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL, | ||
CONSTRAINT "PasswordHistory_editedById_fkey" FOREIGN KEY ("editedById") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE, | ||
CONSTRAINT "PasswordHistory_passwordId_fkey" FOREIGN KEY ("passwordId") REFERENCES "Password" ("id") ON DELETE CASCADE ON UPDATE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters