mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 07:21:16 -07:00
Fix: use strict bool check for auth
This commit is contained in:
+3
-1
@@ -1,7 +1,9 @@
|
|||||||
import { getToken } from "next-auth/jwt";
|
import { getToken } from "next-auth/jwt";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
const authEnabled = Boolean(process.env.HOMEPAGE_AUTH_ENABLED);
|
import { isAuthEnabled } from "utils/env";
|
||||||
|
|
||||||
|
const authEnabled = isAuthEnabled();
|
||||||
const authSecret = process.env.NEXTAUTH_SECRET || process.env.HOMEPAGE_AUTH_SECRET;
|
const authSecret = process.env.NEXTAUTH_SECRET || process.env.HOMEPAGE_AUTH_SECRET;
|
||||||
|
|
||||||
export async function middleware(req) {
|
export async function middleware(req) {
|
||||||
|
|||||||
@@ -100,6 +100,16 @@ describe("middleware", () => {
|
|||||||
expect(res).toEqual({ type: "next" });
|
expect(res).toEqual({ type: "next" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each(["false", "0", "no", "off", ""])("treats HOMEPAGE_AUTH_ENABLED=%j as disabled", async (value) => {
|
||||||
|
process.env.HOMEPAGE_AUTH_ENABLED = value;
|
||||||
|
|
||||||
|
const middleware = await loadMiddleware();
|
||||||
|
const res = await middleware(createReq("localhost:3000", "http://localhost:3000/some"));
|
||||||
|
|
||||||
|
expect(getToken).not.toHaveBeenCalled();
|
||||||
|
expect(res).toEqual({ type: "next" });
|
||||||
|
});
|
||||||
|
|
||||||
it("redirects to signin when auth is enabled and no token is present", async () => {
|
it("redirects to signin when auth is enabled and no token is present", async () => {
|
||||||
process.env.HOMEPAGE_AUTH_ENABLED = "true";
|
process.env.HOMEPAGE_AUTH_ENABLED = "true";
|
||||||
process.env.HOMEPAGE_AUTH_SECRET = "secret";
|
process.env.HOMEPAGE_AUTH_SECRET = "secret";
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import { createHash, timingSafeEqual } from "node:crypto";
|
|||||||
import NextAuth from "next-auth";
|
import NextAuth from "next-auth";
|
||||||
import CredentialsProvider from "next-auth/providers/credentials";
|
import CredentialsProvider from "next-auth/providers/credentials";
|
||||||
|
|
||||||
|
import { isAuthEnabled } from "utils/env";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
const authEnabled = Boolean(process.env.HOMEPAGE_AUTH_ENABLED);
|
const authEnabled = isAuthEnabled();
|
||||||
const issuer = process.env.HOMEPAGE_OIDC_ISSUER;
|
const issuer = process.env.HOMEPAGE_OIDC_ISSUER;
|
||||||
const clientId = process.env.HOMEPAGE_OIDC_CLIENT_ID;
|
const clientId = process.env.HOMEPAGE_OIDC_CLIENT_ID;
|
||||||
const clientSecret = process.env.HOMEPAGE_OIDC_CLIENT_SECRET;
|
const clientSecret = process.env.HOMEPAGE_OIDC_CLIENT_SECRET;
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth/next";
|
||||||
|
|
||||||
import { authOptions } from "pages/api/auth/[...nextauth]";
|
import { authOptions } from "pages/api/auth/[...nextauth]";
|
||||||
|
import { isAuthEnabled } from "utils/env";
|
||||||
import { handleMcpRequest, mcpEnabled, mcpTokenAuthorized } from "utils/mcp/homepage-mcp";
|
import { handleMcpRequest, mcpEnabled, mcpTokenAuthorized } from "utils/mcp/homepage-mcp";
|
||||||
|
|
||||||
async function hasHomepageSession(req, res) {
|
async function hasHomepageSession(req, res) {
|
||||||
if (!process.env.HOMEPAGE_AUTH_ENABLED) return false;
|
if (!isAuthEnabled()) return false;
|
||||||
return Boolean(await getServerSession(req, res, authOptions));
|
return Boolean(await getServerSession(req, res, authOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user