-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-security-theatre
- Loading branch information
Showing
8 changed files
with
125 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { cookies } from 'next/headers'; | ||
|
||
export async function POST(request: Request) { | ||
const { password } = await request.json(); | ||
if (process.env.PEERDB_PASSWORD !== password) { | ||
return new Response(JSON.stringify({ error: 'wrong password' })); | ||
} | ||
cookies().set('auth', password, { | ||
expires: Date.now() + 24 * 60 * 60 * 1000, | ||
secure: process.env.PEERDB_SECURE_COOKIES === 'true', | ||
}); | ||
return new Response('{}'); | ||
} |
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,6 @@ | ||
import { cookies } from 'next/headers'; | ||
|
||
export async function POST(req: Request) { | ||
cookies().delete('auth'); | ||
return new Response(''); | ||
} |
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,61 @@ | ||
'use client'; | ||
import Image from 'next/image'; | ||
import { useRouter, useSearchParams } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
|
||
import { Button } from '@/lib/Button'; | ||
import { Layout, LayoutMain } from '@/lib/Layout'; | ||
import { TextField } from '@/lib/TextField'; | ||
|
||
export default function Login() { | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
const [pass, setPass] = useState(''); | ||
const [error, setError] = useState(() => | ||
searchParams.has('reject') ? 'Authentication failed, please login' : '' | ||
); | ||
const login = () => { | ||
fetch('/api/login', { | ||
method: 'POST', | ||
body: JSON.stringify({ password: pass }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (res.error) setError(res.error); | ||
else router.push('/'); | ||
}); | ||
}; | ||
return ( | ||
<Layout> | ||
<LayoutMain alignSelf='center' justifySelf='center' width='xxLarge'> | ||
<Image src='/images/peerdb-combinedMark.svg' alt='PeerDB' width={512} /> | ||
{error && ( | ||
<div | ||
style={{ | ||
borderRadius: '8px', | ||
fontWeight: 'bold', | ||
color: '#600', | ||
backgroundColor: '#c66', | ||
}} | ||
> | ||
{error} | ||
</div> | ||
)} | ||
<TextField | ||
variant='simple' | ||
placeholder='Password' | ||
value={pass} | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => | ||
setPass(e.target.value) | ||
} | ||
onKeyPress={(e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
login(); | ||
} | ||
}} | ||
/> | ||
<Button onClick={login}>Login</Button> | ||
</LayoutMain> | ||
</Layout> | ||
); | ||
} |
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,16 @@ | ||
'use client'; | ||
import { Button } from '@/lib/Button'; | ||
|
||
export default function Logout() { | ||
return ( | ||
<Button | ||
onClick={() => | ||
fetch('/api/logout', { method: 'POST' }).then((res) => | ||
location.assign('/login') | ||
) | ||
} | ||
> | ||
Logout | ||
</Button> | ||
); | ||
} |
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,23 @@ | ||
import type { NextRequest } from 'next/server'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
export default function middleware(req: NextRequest) { | ||
if ( | ||
req.nextUrl.pathname !== '/login' && | ||
req.nextUrl.pathname !== '/api/login' && | ||
req.nextUrl.pathname !== '/api/logout' && | ||
process.env.PEERDB_PASSWORD && | ||
req.cookies.get('auth')?.value !== process.env.PEERDB_PASSWORD | ||
) { | ||
req.cookies.delete('auth'); | ||
return NextResponse.redirect(new URL('/login?reject', req.url)); | ||
} | ||
return NextResponse.next(); | ||
} | ||
|
||
export const config = { | ||
matcher: [ | ||
// Match everything other than static assets | ||
'/((?!_next/static/|images/|favicon.ico$).*)', | ||
], | ||
}; |