Add independent administrative authentication foundation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import {
|
||||
createAdminSessionCookie,
|
||||
hasTrustedSameOrigin,
|
||||
issueAdminSession,
|
||||
setAdminAuthNoStore,
|
||||
validateAdminPassphrase
|
||||
} from "../../../../lib/admin/adminAuth";
|
||||
import { respondError, respondSuccess } from "../../middleware/apiResponse";
|
||||
|
||||
const forbidden = (res) =>
|
||||
respondError(res, {
|
||||
status: 403,
|
||||
code: "FORBIDDEN",
|
||||
message: "Request not permitted"
|
||||
});
|
||||
|
||||
export default async function handler(req, res) {
|
||||
setAdminAuthNoStore(res);
|
||||
res.setHeader("Allow", "POST");
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return respondError(res, {
|
||||
status: 405,
|
||||
code: "METHOD_NOT_ALLOWED",
|
||||
message: "Method not allowed"
|
||||
});
|
||||
}
|
||||
|
||||
if (!hasTrustedSameOrigin(req)) return forbidden(res);
|
||||
|
||||
const passphrase = req.body?.passphrase;
|
||||
if (!validateAdminPassphrase(passphrase)) return forbidden(res);
|
||||
|
||||
const token = issueAdminSession();
|
||||
if (!token) return forbidden(res);
|
||||
|
||||
res.setHeader("Set-Cookie", createAdminSessionCookie(token));
|
||||
return respondSuccess(res, { authenticated: true });
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
createClearedAdminSessionCookie,
|
||||
hasTrustedSameOrigin,
|
||||
setAdminAuthNoStore
|
||||
} from "../../../../lib/admin/adminAuth";
|
||||
import { respondError, respondSuccess } from "../../middleware/apiResponse";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
setAdminAuthNoStore(res);
|
||||
res.setHeader("Allow", "POST");
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return respondError(res, {
|
||||
status: 405,
|
||||
code: "METHOD_NOT_ALLOWED",
|
||||
message: "Method not allowed"
|
||||
});
|
||||
}
|
||||
|
||||
if (!hasTrustedSameOrigin(req)) {
|
||||
return respondError(res, {
|
||||
status: 403,
|
||||
code: "FORBIDDEN",
|
||||
message: "Request not permitted"
|
||||
});
|
||||
}
|
||||
|
||||
res.setHeader("Set-Cookie", createClearedAdminSessionCookie());
|
||||
return respondSuccess(res, { authenticated: false });
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import {
|
||||
readAdminSessionCookie,
|
||||
setAdminAuthNoStore,
|
||||
validateAdminSession
|
||||
} from "../../../../lib/admin/adminAuth";
|
||||
import { respondError, respondSuccess } from "../../middleware/apiResponse";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
setAdminAuthNoStore(res);
|
||||
res.setHeader("Allow", "GET");
|
||||
|
||||
if (req.method !== "GET") {
|
||||
return respondError(res, {
|
||||
status: 405,
|
||||
code: "METHOD_NOT_ALLOWED",
|
||||
message: "Method not allowed"
|
||||
});
|
||||
}
|
||||
|
||||
const token = readAdminSessionCookie(req);
|
||||
return respondSuccess(res, {
|
||||
authenticated: validateAdminSession(token)
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user