Skip to content

Commit 6f30b3f

Browse files
refactor: enforcing authentication based on environment variable "ENFORCE_AUTH" (#1634)
1 parent 21b57da commit 6f30b3f

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

code/backend/batch/utilities/helpers/env_helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ def __load_config(self, **kwargs) -> None:
362362
self.SEMENTIC_KERNEL_SYSTEM_PROMPT = os.getenv(
363363
"SEMENTIC_KERNEL_SYSTEM_PROMPT", ""
364364
)
365+
366+
self.ENFORCE_AUTH = self.get_env_var_bool("ENFORCE_AUTH", "True")
367+
365368
logger.info("Initializing EnvHelper completed")
366369

367370
def is_chat_model(self):

code/create_app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,10 @@ def assistanttype():
541541
result = ConfigHelper.get_active_config_or_default()
542542
return jsonify({"ai_assistant_type": result.prompts.ai_assistant_type})
543543

544+
@app.route("/api/checkauth", methods=["GET"])
545+
async def check_auth_enforced():
546+
"""Check if the authentiction is enforced."""
547+
return jsonify({"is_auth_enforced": env_helper.ENFORCE_AUTH})
548+
544549
app.register_blueprint(bp_chat_history_response, url_prefix="/api")
545550
return app

code/frontend/src/api/api.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ export async function getUserInfo(): Promise<UserInfo[]> {
5454
}
5555
}
5656

57+
export async function checkAuthEnforced(): Promise<boolean> {
58+
try {
59+
const response = await fetch("/api/checkauth", {
60+
method: "GET",
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
});
65+
if (!response.ok) {
66+
throw new Error("Network response was not ok");
67+
}
68+
const config = await response.json(); // Parse JSON response
69+
return config.is_auth_enforced;
70+
} catch (error) {
71+
console.error("Failed to fetch configuration:", error);
72+
return true; // Return true because we need to enforce auth by default
73+
}
74+
}
75+
5776
export async function getAssistantTypeApi() {
5877
try {
5978
const response = await fetch("/api/assistanttype", {

code/frontend/src/pages/layout/Layout.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import Layout from "./Layout";
99

1010
import { BrowserRouter } from "react-router-dom";
11-
import { getUserInfo } from "../../api/api";
11+
import { getUserInfo, checkAuthEnforced } from "../../api/api";
1212
import { before } from "lodash";
1313
import { hostname } from "os";
1414

@@ -29,7 +29,7 @@ const DefaultLayoutPropsloderfalse = {
2929
};
3030

3131
jest.mock('../../api/api', () => ({
32-
getUserInfo: jest.fn()
32+
getUserInfo: jest.fn(), checkAuthEnforced: jest.fn()
3333
}));
3434

3535

@@ -72,6 +72,7 @@ describe("Layout Component", () => {
7272
},
7373
});
7474
;(getUserInfo as jest.Mock).mockResolvedValue(mocklist)
75+
;(checkAuthEnforced as jest.Mock).mockResolvedValue(true)
7576
await act(async () => {
7677
render(
7778
<BrowserRouter>

code/frontend/src/pages/layout/Layout.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { Dialog, Stack, TextField } from "@fluentui/react";
1010
import { ReactNode, useEffect, useRef, useState } from "react";
1111
import { HistoryButton } from "../../components/HistoryButton/HistoryButton";
12-
import { getUserInfo } from "../../api";
12+
import { getUserInfo, checkAuthEnforced } from "../../api";
1313
import SpinnerComponent from '../../components/Spinner/Spinner';
1414

1515

@@ -52,6 +52,12 @@ const Layout = ({ children,toggleSpinner, ...props }: LayoutProps) => {
5252
const firstRender = useRef(true);
5353

5454
const getUserInfoList = async () => {
55+
const isAuthEnforced = await checkAuthEnforced(); // Check if auth is enforced
56+
if(!isAuthEnforced) {
57+
setShowAuthMessage(false);
58+
return;
59+
}
60+
5561
const userInfoList = await getUserInfo();
5662
if (
5763
userInfoList.length === 0 &&

0 commit comments

Comments
 (0)