-
Notifications
You must be signed in to change notification settings - Fork 0
feat: conditional authentication via discovery service #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+621
−6
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc8515c
Initial plan
Copilot afffa0d
feat: implement conditional auth loading based on discovery response
Copilot 5505eb2
test: add tests for conditional auth behavior
Copilot 5226f7f
docs: add documentation for conditional authentication feature
Copilot cad19b6
refactor: address code review feedback
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| # Conditional Authentication | ||
|
|
||
| ObjectUI now supports conditional authentication based on server discovery information. This allows the console application to automatically detect whether the backend has authentication enabled and adapt accordingly. | ||
|
|
||
| ## Overview | ||
|
|
||
| When the ObjectStack server discovery endpoint (`/.well-known/objectstack` or `/api`) indicates that authentication is disabled (`auth.enabled === false`), the console application will bypass the authentication flow and operate in "Guest Mode". | ||
|
|
||
| This is useful for: | ||
| - **Development environments** where authentication is not configured | ||
| - **Demo environments** where users should access the system without credentials | ||
| - **Embedded scenarios** where authentication is handled externally | ||
|
|
||
| ## Implementation | ||
|
|
||
| ### 1. Discovery Response Structure | ||
|
|
||
| The server's discovery endpoint should return a response with the following structure: | ||
|
|
||
| ```json | ||
| { | ||
| "name": "ObjectStack API", | ||
| "version": "2.0.0", | ||
| "services": { | ||
| "auth": { | ||
| "enabled": false, | ||
| "status": "unavailable", | ||
| "message": "Authentication service not installed. Install an auth plugin to enable authentication." | ||
| }, | ||
| "data": { | ||
| "enabled": true, | ||
| "status": "available" | ||
| }, | ||
| "metadata": { | ||
| "enabled": true, | ||
| "status": "available" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Client-Side Detection | ||
|
|
||
| The console application uses the `ConditionalAuthWrapper` component which: | ||
|
|
||
| 1. Connects to the server and fetches discovery information | ||
| 2. Checks the `services.auth.enabled` flag | ||
| 3. If `false`, wraps the application with `<AuthProvider enabled={false}>` (Guest Mode) | ||
| 4. If `true` or undefined, wraps with normal `<AuthProvider>` (Standard Auth Mode) | ||
|
|
||
| ### 3. Guest Mode Behavior | ||
|
|
||
| When authentication is disabled: | ||
|
|
||
| - A virtual "Guest User" is automatically authenticated with: | ||
| - ID: `guest` | ||
| - Name: `Guest User` | ||
| - Email: `guest@local` | ||
| - Token: `guest-token` | ||
|
|
||
| - All protected routes become accessible without login | ||
| - Login/Register pages are still accessible but not required | ||
| - The user menu shows the guest user | ||
|
|
||
| ## Usage | ||
|
|
||
| ### For Application Developers | ||
|
|
||
| Simply wrap your application with `ConditionalAuthWrapper`: | ||
|
|
||
| ```tsx | ||
| import { ConditionalAuthWrapper } from './components/ConditionalAuthWrapper'; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <ConditionalAuthWrapper authUrl="/api/auth"> | ||
| <BrowserRouter> | ||
| <Routes> | ||
| {/* Your routes */} | ||
| </Routes> | ||
| </BrowserRouter> | ||
| </ConditionalAuthWrapper> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### For Backend Developers | ||
|
|
||
| Ensure your discovery endpoint returns the correct `services.auth.enabled` flag: | ||
|
|
||
| **With Auth Enabled:** | ||
| ```typescript | ||
| { | ||
| services: { | ||
| auth: { | ||
| enabled: true, | ||
| status: "available" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **With Auth Disabled:** | ||
| ```typescript | ||
| { | ||
| services: { | ||
| auth: { | ||
| enabled: false, | ||
| status: "unavailable", | ||
| message: "Auth service not configured" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Using the useDiscovery Hook | ||
|
|
||
| Components can also directly access discovery information: | ||
|
|
||
| ```tsx | ||
| import { useDiscovery } from '@object-ui/react'; | ||
|
|
||
| function MyComponent() { | ||
| const { discovery, isLoading, isAuthEnabled } = useDiscovery(); | ||
|
|
||
| if (isLoading) return <LoadingSpinner />; | ||
|
|
||
| return ( | ||
| <div> | ||
| {isAuthEnabled | ||
| ? <LoginPrompt /> | ||
| : <GuestWelcome /> | ||
| } | ||
| </div> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| ⚠️ **Important Security Note:** | ||
|
|
||
| - The default behavior (when discovery fails or auth status is unknown) is to **enable authentication** for security. | ||
| - Guest mode should only be used in controlled environments (development, demos). | ||
| - In production, always configure proper authentication. | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### ConditionalAuthWrapper | ||
|
|
||
| **Props:** | ||
| - `authUrl` (string): The authentication endpoint URL | ||
| - `children` (ReactNode): Application content to wrap | ||
|
|
||
| **Behavior:** | ||
| - Fetches discovery on mount | ||
| - Shows loading screen while checking auth status | ||
| - Wraps children with appropriate AuthProvider configuration | ||
|
|
||
| ### AuthProvider | ||
|
|
||
| **New Props:** | ||
| - `enabled` (boolean, default: `true`): Whether authentication is enabled | ||
| - When `false`: Automatically authenticates as guest user | ||
| - When `true`: Normal authentication flow | ||
|
|
||
| ### useDiscovery Hook | ||
|
|
||
| **Returns:** | ||
| - `discovery` (DiscoveryInfo | null): Raw discovery data from server | ||
| - `isLoading` (boolean): Whether discovery is being fetched | ||
| - `error` (Error | null): Any error that occurred during fetch | ||
| - `isAuthEnabled` (boolean): Convenience flag for `discovery?.services?.auth?.enabled ?? true` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1: Development Server Without Auth | ||
|
|
||
| **Discovery Response:** | ||
| ```json | ||
| { | ||
| "name": "Dev Server", | ||
| "services": { | ||
| "auth": { "enabled": false } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Result:** Console loads without requiring login | ||
|
|
||
| ### Example 2: Production Server With Auth | ||
|
|
||
| **Discovery Response:** | ||
| ```json | ||
| { | ||
| "name": "Production Server", | ||
| "services": { | ||
| "auth": { "enabled": true } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Result:** Console requires user authentication | ||
|
|
||
| ### Example 3: Legacy Server (No Discovery) | ||
|
|
||
| **Discovery Response:** 404 or connection error | ||
|
|
||
| **Result:** Defaults to authentication enabled (secure by default) | ||
|
|
||
| ## Testing | ||
|
|
||
| Tests are included in: | ||
| - `packages/auth/src/__tests__/AuthProvider.disabled.test.tsx` | ||
| - Tests verify guest mode behavior | ||
| - Tests verify normal auth mode behavior | ||
| - Tests verify session token creation in guest mode | ||
|
|
||
| Run tests with: | ||
| ```bash | ||
| pnpm test packages/auth/src/__tests__/AuthProvider.disabled.test.tsx | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * ObjectUI Console - Conditional Auth Wrapper | ||
| * | ||
| * This component fetches discovery information from the server and conditionally | ||
| * enables/disables authentication based on the server's auth service status. | ||
| */ | ||
|
|
||
| import { useState, useEffect, ReactNode } from 'react'; | ||
| import { ObjectStackAdapter } from './dataSource'; | ||
| import { AuthProvider } from '@object-ui/auth'; | ||
| import { LoadingScreen } from './components/LoadingScreen'; | ||
| import type { DiscoveryInfo } from '@object-ui/react'; | ||
|
|
||
| interface ConditionalAuthWrapperProps { | ||
| children: ReactNode; | ||
| authUrl: string; | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper component that conditionally enables authentication based on server discovery. | ||
| * | ||
| * This component: | ||
| * 1. Creates a temporary data source connection | ||
| * 2. Fetches discovery information from the server | ||
| * 3. Checks if auth.enabled is true in the discovery response | ||
| * 4. Conditionally wraps children with AuthProvider if auth is enabled | ||
| * 5. Bypasses auth if discovery indicates auth is disabled (development/demo mode) | ||
| */ | ||
| export function ConditionalAuthWrapper({ children, authUrl }: ConditionalAuthWrapperProps) { | ||
| const [authEnabled, setAuthEnabled] = useState<boolean | null>(null); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
|
|
||
| async function checkAuthStatus() { | ||
| try { | ||
| // Create a temporary adapter to fetch discovery | ||
| // Empty baseUrl allows the adapter to use browser-relative paths | ||
| // This works because the console app is served from the same origin as the API | ||
| const adapter = new ObjectStackAdapter({ | ||
| baseUrl: '', | ||
| autoReconnect: false, | ||
| }); | ||
|
|
||
| await adapter.connect(); | ||
| const discovery = await adapter.getDiscovery() as DiscoveryInfo | null; | ||
|
|
||
| if (!cancelled) { | ||
| // Check if auth is enabled in discovery | ||
| // Default to true if discovery doesn't provide this information | ||
| const isAuthEnabled = discovery?.services?.auth?.enabled ?? true; | ||
| setAuthEnabled(isAuthEnabled); | ||
| } | ||
| } catch (error) { | ||
| if (!cancelled) { | ||
| // If discovery fails, default to auth enabled for security | ||
| console.warn('[ConditionalAuthWrapper] Failed to fetch discovery, defaulting to auth enabled:', error); | ||
| setAuthEnabled(true); | ||
| } | ||
| } finally { | ||
| if (!cancelled) { | ||
| setIsLoading(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| checkAuthStatus(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, []); | ||
|
|
||
| if (isLoading) { | ||
| return <LoadingScreen />; | ||
| } | ||
|
|
||
| // If auth is enabled, wrap with AuthProvider | ||
| if (authEnabled) { | ||
| return ( | ||
| <AuthProvider authUrl={authUrl}> | ||
| {children} | ||
| </AuthProvider> | ||
| ); | ||
| } | ||
|
|
||
| // If auth is disabled, wrap with a disabled AuthProvider (guest mode) | ||
| return ( | ||
| <AuthProvider authUrl={authUrl} enabled={false}> | ||
| {children} | ||
| </AuthProvider> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The relative imports are incorrect for a file located in
src/components:./dataSourceand./components/LoadingScreendo not exist from this directory and will break compilation. Import the adapter from../dataSourceandLoadingScreenfrom./LoadingScreen(or the correct local path).