fix #12: http basic auth
This commit is contained in:
1 parent
79ea2fba2c
commit
e2b469c39d
3 files changed
+34
-1
No files matched your search
@@ -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' } }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user