fix #12: http basic auth

pull/61/head
lyc8503 2024-08-21 14:48:21 +08:00
parent 79ea2fba2c
commit e2b469c39d
3 changed files with 34 additions and 1 deletions

View File

@ -18,6 +18,8 @@ A more advanced, serverless, and free uptime monitoring & status page solution,
- Responsive UI that adapts to your system theme
- Customizable status page
- Use your own domain with CNAME
- Optional password authentication (private status page)
- JSON API for fetching realtime status data
## 👀Demo
@ -44,4 +46,6 @@ Please refer to [Wiki](https://github.com/lyc8503/UptimeFlare/wiki)
- [ ] SSL certificate checks
- [ ] Self-host Dockerfile
- [ ] Incident timeline
- [ ] Improve `checkLocationWorkerRoute` and fix possible `proxy failed`
- [ ] Groups
- [x] Remove old incidents

27
middleware.ts Normal file
View File

@ -0,0 +1,27 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { workerConfig } from './uptime.config'
export async function middleware(request: NextRequest) {
// @ts-ignore
const passwordProtection = workerConfig.passwordProtection
if (passwordProtection) {
const authHeader = request.headers.get('Authorization')
let authenticated = false
const expected = 'Basic ' + btoa(passwordProtection)
if (authHeader && authHeader.length === expected.length) {
// a simple timing-safe compare
authenticated = true;
for (let i = 0; i < authHeader.length; i++) {
if (authHeader[i] !== expected[i]) authenticated = false;
}
}
if (!authenticated) {
return NextResponse.json(
{ code: 401, message: "Not authenticated" }, { status: 401, headers: { 'WWW-Authenticate': 'Basic' } }
)
}
}
}

View File

@ -10,8 +10,10 @@ const pageConfig = {
}
const workerConfig = {
// Write KV at most every 3 minutes unless the status changed.
// Write KV at most every 3 minutes unless the status changed
kvWriteCooldownMinutes: 3,
// Enable HTTP Basic auth for status page & API by uncommenting the line below, format `<USERNAME>:<PASSWORD>`
// passwordProtection: 'username:password',
// Define all your monitors here
monitors: [
// Example HTTP Monitor