|
1 | 1 | # Authentication |
2 | 2 |
|
3 | | -This page is under construction. |
| 3 | +SmooSense supports optional authentication using Auth0. When configured, all pages require users to log in before accessing the application. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Authentication is **optional** by default. If no Auth0 credentials are configured, SmooSense runs without authentication and all pages are publicly accessible. This is suitable for local development or private deployments. |
| 8 | + |
| 9 | +When Auth0 is configured, users must authenticate before accessing any page. Unauthenticated users are redirected to Auth0's login page. |
| 10 | + |
| 11 | +## Setting Up Auth0 |
| 12 | + |
| 13 | +### 1. Create an Auth0 Application |
| 14 | + |
| 15 | +1. Go to [Auth0 Dashboard](https://manage.auth0.com/) |
| 16 | +2. Navigate to **Applications > Applications** |
| 17 | +3. Click **Create Application** |
| 18 | +4. Choose **Regular Web Application** |
| 19 | +5. Note your **Domain**, **Client ID**, and **Client Secret** |
| 20 | + |
| 21 | +### 2. Configure Callback URLs |
| 22 | + |
| 23 | +In your Auth0 application settings, configure: |
| 24 | + |
| 25 | +- **Allowed Callback URLs**: `http://localhost:8000/auth/callback` |
| 26 | +- **Allowed Logout URLs**: `http://localhost:8000` |
| 27 | + |
| 28 | +For production, replace `localhost:8000` with your actual domain. |
| 29 | + |
| 30 | +### 3. Set Environment Variables |
| 31 | + |
| 32 | +Set the following environment variables before starting SmooSense: |
| 33 | + |
| 34 | +```bash |
| 35 | +export AUTH0_DOMAIN="your-tenant.auth0.com" |
| 36 | +export AUTH0_CLIENT_ID="your-client-id" |
| 37 | +export AUTH0_CLIENT_SECRET="your-client-secret" |
| 38 | +export APP_SECRET_KEY="your-random-secret-key" # Optional, auto-generated if not set |
| 39 | +``` |
| 40 | + |
| 41 | +You can also create a `.env` file in your project directory with these values. |
| 42 | + |
| 43 | +### 4. Start SmooSense |
| 44 | + |
| 45 | +```bash |
| 46 | +sense |
| 47 | +``` |
| 48 | + |
| 49 | +When Auth0 is properly configured, you'll see a log message: |
| 50 | +``` |
| 51 | +Auth0 authentication enabled |
| 52 | +``` |
| 53 | + |
| 54 | +## Authentication Flow |
| 55 | + |
| 56 | +1. User visits any protected page (e.g., `/`, `/FolderBrowser`, `/Table`) |
| 57 | +2. If not authenticated, user is redirected to `/auth/login` |
| 58 | +3. Auth0 handles the login (username/password, SSO, etc.) |
| 59 | +4. After successful login, user is redirected back to the application |
| 60 | +5. User session is stored and maintained until logout |
| 61 | + |
| 62 | +## API Endpoints |
| 63 | + |
| 64 | +SmooSense provides the following authentication endpoints: |
| 65 | + |
| 66 | +| Endpoint | Description | |
| 67 | +|----------|-------------| |
| 68 | +| `/auth/login` | Initiates Auth0 login flow | |
| 69 | +| `/auth/logout` | Clears session and logs out from Auth0 | |
| 70 | +| `/auth/callback` | Handles OAuth callback from Auth0 | |
| 71 | +| `/auth/me` | Returns current user info as JSON | |
| 72 | + |
| 73 | +### Check Authentication Status |
| 74 | + |
| 75 | +```bash |
| 76 | +curl http://localhost:8000/auth/me |
| 77 | +``` |
| 78 | + |
| 79 | +Returns: |
| 80 | +```json |
| 81 | +{ |
| 82 | + "authenticated": true, |
| 83 | + |
| 84 | + "name": "John Doe", |
| 85 | + "picture": "https://..." |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Or if not authenticated: |
| 90 | +```json |
| 91 | +{ |
| 92 | + "authenticated": false |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Restricting Access by Email Domain |
| 97 | + |
| 98 | +You can restrict access to users from specific email domains using Auth0 Actions. In your Auth0 Dashboard: |
| 99 | + |
| 100 | +1. Go to **Actions > Flows > Login** |
| 101 | +2. Create a new Action with this code: |
| 102 | + |
| 103 | +```javascript |
| 104 | +exports.onExecutePostLogin = async (event, api) => { |
| 105 | + if (!event.user.email) { |
| 106 | + api.access.deny('access_denied', 'Email required'); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const allowList = ['mycorp.com', 'partner.com']; // Your allowed domains |
| 111 | + |
| 112 | + const parts = event.user.email.split('@'); |
| 113 | + const domain = parts[parts.length - 1].toLowerCase(); |
| 114 | + |
| 115 | + if (!allowList.includes(domain)) { |
| 116 | + api.access.deny( |
| 117 | + 'access_denied', |
| 118 | + 'Only specific email domains are allowed to access this app.' |
| 119 | + ); |
| 120 | + } |
| 121 | +}; |
| 122 | +``` |
| 123 | + |
| 124 | +3. Deploy the Action and add it to your Login flow |
| 125 | + |
| 126 | +## Troubleshooting |
| 127 | + |
| 128 | +### "Auth0 not configured, running without authentication" |
| 129 | + |
| 130 | +This message appears when environment variables are not set. Verify: |
| 131 | +- `AUTH0_DOMAIN` is set |
| 132 | +- `AUTH0_CLIENT_ID` is set |
| 133 | +- `AUTH0_CLIENT_SECRET` is set |
| 134 | + |
| 135 | +### Callback URL Mismatch |
| 136 | + |
| 137 | +Ensure your Auth0 application's "Allowed Callback URLs" exactly matches `http://your-host:port/auth/callback`. |
| 138 | + |
| 139 | +### Session Not Persisting |
| 140 | + |
| 141 | +If sessions aren't persisting across requests, ensure: |
| 142 | +1. `APP_SECRET_KEY` is set consistently (not auto-generated each restart) |
| 143 | +2. Cookies are enabled in the browser |
0 commit comments