diff --git a/src/frontend/frontend_server.py b/src/frontend/frontend_server.py index 49dbc8773..72557dbc8 100644 --- a/src/frontend/frontend_server.py +++ b/src/frontend/frontend_server.py @@ -28,7 +28,11 @@ @app.get("/config.js", response_class=PlainTextResponse) def get_config(): backend_url = html.escape(os.getenv("BACKEND_API_URL", "http://localhost:8000")) - return f'const BACKEND_API_URL = "{backend_url}";' + auth_enabled = html.escape(os.getenv("AUTH_ENABLED", "True")) + return f''' + const BACKEND_API_URL = "{backend_url}"; + const AUTH_ENABLED = "{auth_enabled}"; + ''' # Redirect root to app.html diff --git a/src/frontend/wwwroot/app.js b/src/frontend/wwwroot/app.js index 32acefbc5..16760c490 100644 --- a/src/frontend/wwwroot/app.js +++ b/src/frontend/wwwroot/app.js @@ -6,6 +6,10 @@ const closeModalButtons = document.querySelectorAll(".modal-close-button"); const myTasksMenu = document.getElementById("myTasksMenu"); const tasksStats = document.getElementById("tasksStats"); + + if(AUTH_ENABLED !== undefined) { + setStoredData('authEnabled', AUTH_ENABLED.toString().toLowerCase()); + } //if (!getStoredData('apiEndpoint'))setStoredData('apiEndpoint', apiEndpoint); // Force rewrite of apiEndpoint @@ -51,8 +55,17 @@ try { const response = await fetch('/.auth/me'); if (!response.ok) { - console.log("No identity provider found. Access to chat will be blocked."); - return null; + if(getStoredData('authEnabled') === 'false'){ + //Authentication is disabled. Will use mock user + return { + name: 'Local User', + authenticated: true + } + } + else{ + console.log("No identity provider found. Access to chat will be blocked."); + return null; + } } const payload = await response.json(); diff --git a/src/frontend/wwwroot/utils.js b/src/frontend/wwwroot/utils.js index ef816dc76..61e5fe561 100644 --- a/src/frontend/wwwroot/utils.js +++ b/src/frontend/wwwroot/utils.js @@ -19,6 +19,14 @@ window.GetAuthDetails = async () => { // Check if the request is successful if (!authResponse.ok) { + if(getStoredData('authEnabled') === 'false') { + //Authentication is disabled. Will use mock user + console.log("Authentication Disabled. Using mock user details."); + + const headers = getMockUserHeaders(); + + return headers; + } console.log("Failed to fetch authentication details. Access to chat will be blocked."); return null; } @@ -47,25 +55,30 @@ window.GetAuthDetails = async () => { // This code runs locally so setup mock headers console.log("Running locally. Skipping authentication details fetch."); + const headers = getMockUserHeaders(); + + return headers; + } + + function getMockUserHeaders() { const mockUserDetails = { client_principal: 'mock-client-principal-id', user_claims: [ - { typ: 'http://schemas.microsoft.com/identity/claims/objectidentifier', val: '12345678-abcd-efgh-ijkl-9876543210ab' }, // Mock Object ID - { typ: 'name', val: 'Local User' }, // Mock Name - { typ: 'email', val: 'localuser@example.com' }, // Mock Email (optional claim) + { typ: 'http://schemas.microsoft.com/identity/claims/objectidentifier', val: '12345678-abcd-efgh-ijkl-9876543210ab' }, // Mock Object ID + { typ: 'name', val: 'Local User' }, // Mock Name + { typ: 'email', val: 'localuser@example.com' }, // Mock Email (optional claim) ], identity_provider: 'mock-identity-provider', // Mock Identity Provider - }; - - const headers = { + }; + + const headers = { 'Content-Type': 'application/json', 'X-Ms-Client-Principal': mockUserDetails.client_principal || '', 'X-Ms-Client-Principal-Id': mockUserDetails.user_claims?.find(claim => claim.typ === 'http://schemas.microsoft.com/identity/claims/objectidentifier')?.val || '', 'X-Ms-Client-Principal-Name': mockUserDetails.user_claims?.find(claim => claim.typ === 'name')?.val || '', 'X-Ms-Client-Principal-Idp': mockUserDetails.identity_provider || '', - }; - - return headers; + }; + return headers; } };