-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
191 additions
and
9 deletions.
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
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
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
File renamed without changes.
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,39 @@ | ||
import { verifyPassword } from '@/lib/crypto'; | ||
import { prisma } from '@/lib/db'; | ||
import { log } from '@/lib/logger'; | ||
import { combine } from '@/lib/middleware/combine'; | ||
import { method } from '@/lib/middleware/method'; | ||
import { NextApiReq, NextApiRes } from '@/lib/response'; | ||
|
||
export type ApiUserUrlsIdPasswordResponse = { | ||
success: boolean; | ||
}; | ||
|
||
type Body = { | ||
password: string; | ||
}; | ||
|
||
const logger = log('api').c('user').c('urls').c('$id').c('password'); | ||
|
||
export async function handler(req: NextApiReq<Body>, res: NextApiRes<ApiUserUrlsIdPasswordResponse>) { | ||
const url = await prisma.url.findFirst({ | ||
where: { | ||
OR: [{ id: req.query.id }, { code: req.query.id }, { vanity: req.query.id }], | ||
}, | ||
select: { | ||
password: true, | ||
id: true, | ||
}, | ||
}); | ||
if (!url) return res.notFound(); | ||
if (!url.password) return res.notFound(); | ||
|
||
const verified = await verifyPassword(req.body.password, url.password); | ||
if (!verified) return res.forbidden('Incorrect password'); | ||
|
||
logger.info(`url ${url.id} was accessed with the correct password`, { ua: req.headers['user-agent'] }); | ||
|
||
return res.ok({ success: true }); | ||
} | ||
|
||
export default combine([method(['POST'])], handler); |
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,114 @@ | ||
import { verifyPassword } from '@/lib/crypto'; | ||
import { prisma } from '@/lib/db'; | ||
import { fetchApi } from '@/lib/fetchApi'; | ||
import { Button, Modal, PasswordInput, Title } from '@mantine/core'; | ||
import { GetServerSideProps, InferGetServerSidePropsType } from 'next'; | ||
import { useRouter } from 'next/router'; | ||
import { useState } from 'react'; | ||
|
||
export default function ViewUrl({ url, password }: InferGetServerSidePropsType<typeof getServerSideProps>) { | ||
const router = useRouter(); | ||
|
||
const [passwordValue, setPassword] = useState<string>(''); | ||
const [passwordError, setPasswordError] = useState<string>(''); | ||
|
||
const verifyPassword = async () => { | ||
const { error } = await fetchApi(`/api/user/urls/${url.id}/password`, 'POST', { | ||
password: passwordValue.trim(), | ||
}); | ||
|
||
if (error) { | ||
setPasswordError('Invalid password'); | ||
} else { | ||
setPasswordError(''); | ||
router.replace(`/view/url/${url.id}?pw=${encodeURI(passwordValue.trim())}`); | ||
} | ||
}; | ||
|
||
return password ? ( | ||
<Modal | ||
onClose={() => {}} | ||
opened={true} | ||
withCloseButton={false} | ||
centered | ||
title={<Title>Password required</Title>} | ||
> | ||
<PasswordInput | ||
description='This link is password protected, enter password to view it' | ||
required | ||
mb='sm' | ||
value={passwordValue} | ||
onChange={(event) => setPassword(event.currentTarget.value)} | ||
error={passwordError} | ||
/> | ||
|
||
<Button | ||
fullWidth | ||
variant='outline' | ||
my='sm' | ||
onClick={() => verifyPassword()} | ||
disabled={passwordValue.trim().length === 0} | ||
> | ||
Verify | ||
</Button> | ||
</Modal> | ||
) : null; | ||
} | ||
|
||
export const getServerSideProps: GetServerSideProps<{ | ||
url: { id: string }; | ||
password?: boolean; | ||
}> = async (context) => { | ||
const { id, pw } = context.query as { id: string; pw: string }; | ||
if (!id) return { notFound: true }; | ||
|
||
const url = await prisma.url.findFirst({ | ||
where: { | ||
OR: [{ vanity: id }, { code: id }, { id }], | ||
}, | ||
select: { | ||
id: true, | ||
password: true, | ||
destination: true, | ||
}, | ||
}); | ||
if (!url) return { notFound: true }; | ||
|
||
if (pw) { | ||
const verified = await verifyPassword(pw, url.password!); | ||
// @ts-ignore | ||
delete url.password; | ||
|
||
if (!verified) { | ||
// @ts-ignore | ||
delete url.destination; | ||
|
||
return { | ||
props: { | ||
url, | ||
password: true, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
redirect: { | ||
destination: url.destination, | ||
permanent: true, | ||
}, | ||
}; | ||
} | ||
|
||
const password = url.password ? true : false; | ||
// @ts-ignore | ||
delete url.password; | ||
// @ts-ignore | ||
delete url.destination; | ||
|
||
return { | ||
props: { | ||
url, | ||
password, | ||
}, | ||
}; | ||
}; |
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