|
| 1 | +# Add Hanko SSO Authentication |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Integrates Hanko SSO authentication as an alternative to legacy OSM OAuth, enabling single sign-on across the HOT ecosystem via login.hotosm.org. |
| 6 | + |
| 7 | +**Key changes:** |
| 8 | +- New `AUTH_PROVIDER` setting to switch between `legacy` (current) and `hanko` modes |
| 9 | +- Hanko auth uses JWT cookies instead of access-token headers |
| 10 | +- User onboarding flow to link existing accounts or create new ones |
| 11 | +- Shared auth-libs web component for login UI |
| 12 | + |
| 13 | +**This PR is functionality only.** Deploy configuration (Dockerfiles, workflows, nginx) comes in a separate PR. |
| 14 | + |
| 15 | +## For Deployment: Required Secrets & Variables |
| 16 | + |
| 17 | +### Backend Environment Variables |
| 18 | + |
| 19 | +When deploying with `AUTH_PROVIDER=hanko`: |
| 20 | + |
| 21 | +| Variable | Required | Example | Description | |
| 22 | +|----------|----------|---------|-------------| |
| 23 | +| `AUTH_PROVIDER` | Yes | `hanko` | Set to `hanko` to enable SSO | |
| 24 | +| `HANKO_API_URL` | Yes | `https://login.hotosm.org` | Hanko service URL | |
| 25 | +| `COOKIE_SECRET` | Yes | `<shared-secret>` | **Must match login service** - for cookie encryption | |
| 26 | +| `COOKIE_DOMAIN` | Yes | `.hotosm.org` | Domain for auth cookies | |
| 27 | +| `LOGIN_URL` | No | `https://login.hotosm.org` | Login service URL for redirects | |
| 28 | +| `FRONTEND_URL` | Yes | `https://fair.hotosm.org` | Frontend URL for redirects | |
| 29 | + |
| 30 | +### Frontend Environment Variables |
| 31 | + |
| 32 | +| Variable | Required | Example | Description | |
| 33 | +|----------|----------|---------|-------------| |
| 34 | +| `VITE_AUTH_PROVIDER` | Yes | `hanko` | Must match backend | |
| 35 | +| `VITE_HANKO_URL` | Yes | `https://login.hotosm.org` | Hanko service URL | |
| 36 | + |
| 37 | +### Important Notes |
| 38 | + |
| 39 | +1. **`COOKIE_SECRET` must be shared** with the login service (login.hotosm.org) - coordinate with login team |
| 40 | +2. **`COOKIE_DOMAIN`** should be `.hotosm.org` for production so cookies work across subdomains |
| 41 | +3. **Default is `legacy` mode** - existing deployments continue working without changes |
| 42 | + |
| 43 | +## New Dependencies |
| 44 | + |
| 45 | +| Package | Location | Notes | |
| 46 | +|---------|----------|-------| |
| 47 | +| `hotosm-auth[django]==0.2.10` | Backend (PyPI) | Hanko auth middleware & helpers | |
| 48 | +| `@hotosm/hanko-auth` | Frontend (npm) | Login web component | |
| 49 | + |
| 50 | +## New API Endpoints |
| 51 | + |
| 52 | +| Endpoint | Method | Description | |
| 53 | +|----------|--------|-------------| |
| 54 | +| `/api/v1/auth/onboarding/` | GET | Callback from login service after onboarding | |
| 55 | +| `/api/v1/auth/status/` | GET | Check authentication status | |
| 56 | + |
| 57 | +## How it Works |
| 58 | + |
| 59 | +### Legacy Mode (default) |
| 60 | +``` |
| 61 | +AUTH_PROVIDER=legacy |
| 62 | +``` |
| 63 | +No changes - continues using OSM OAuth with access-token header. |
| 64 | + |
| 65 | +### Hanko Mode |
| 66 | +``` |
| 67 | +AUTH_PROVIDER=hanko |
| 68 | +``` |
| 69 | +1. User clicks login → redirected to login.hotosm.org |
| 70 | +2. Hanko sets JWT cookie after authentication |
| 71 | +3. Backend middleware validates JWT cookie |
| 72 | +4. If user mapping exists → authenticated |
| 73 | +5. If no mapping → user goes through onboarding |
| 74 | + |
| 75 | +### Onboarding Flow |
| 76 | +New Hanko users choose: |
| 77 | +- **"I had an account"** → Connect OSM to recover existing fAIr data |
| 78 | +- **"I'm new"** → Create fresh account with synthetic ID |
| 79 | + |
| 80 | +## Test Plan |
| 81 | + |
| 82 | +- [ ] Legacy auth continues working (`AUTH_PROVIDER=legacy`) |
| 83 | +- [ ] Hanko login/logout flow works |
| 84 | +- [ ] New user onboarding creates account |
| 85 | +- [ ] Existing user onboarding recovers data |
| 86 | +- [ ] Navbar shows correct user state |
| 87 | +- [ ] Protected routes redirect to login correctly |
| 88 | +- [ ] `?mine=true` filter works for both auth types |
| 89 | + |
| 90 | +## Backward Compatibility |
| 91 | + |
| 92 | +- **Default is `legacy`** - no action needed for existing deployments |
| 93 | +- Existing users continue working with OSM OAuth |
| 94 | +- Can switch to `hanko` when ready by setting environment variables |
0 commit comments