tududi/backend/migrations
Chris c2e9a1aa21
feat: Add OIDC/SSO authentication support (#1008)
* feat: add OIDC/SSO database schema and models (Phase 1)

Add database foundation for OpenID Connect authentication:

Database Migrations:
- Create oidc_identities table (links users to OIDC accounts)
- Create oidc_state_nonces table (OAuth state/nonce for CSRF protection)
- Create auth_audit_log table (security event logging)
- Make password_digest nullable in users table (allow OIDC-only users)

Models:
- OIDCIdentity: Links users to external OIDC providers
- OIDCStateNonce: Temporary OAuth state management
- AuthAuditLog: Authentication event audit trail

Changes:
- Updated User model to allow null password_digest
- Added model associations in models/index.js
- All migrations tested and verified

Related to #977

* feat: add OIDC core services (Phase 2)

- Install openid-client@^6.2.0 for OIDC protocol support
- Implement providerConfig.js for loading providers from .env
  - Support single provider or numbered providers (OIDC_PROVIDER_1_*, etc.)
  - Auto-provision and admin email domain configuration
  - Provider caching for performance
- Implement stateManager.js for OAuth state/nonce management
  - CSRF protection with 10-minute TTL
  - One-time use state consumption
  - Automatic cleanup of expired states
- Implement auditService.js for authentication event logging
  - Track login success/failure, logout, OIDC linking/unlinking
  - Store IP address, user agent, and metadata
  - Support for event queries and retention cleanup
- Add comprehensive unit tests (60 tests, all passing)
  - providerConfig: 36 tests for env parsing and validation
  - stateManager: 12 tests for state lifecycle and security
  - auditService: 12 tests for event logging and queries

Phase 2 completes the backend core services needed for OIDC authentication.

* feat: implement OIDC authentication flow (Phase 3)

Core OIDC Flow (service.js):
- Provider discovery with issuer caching
- Authorization URL generation with state/nonce
- OAuth callback handling and token exchange
- ID token validation using openid-client
- Token refresh functionality

JIT User Provisioning (provisioningService.js):
- Auto-create users from OIDC claims
- Link existing email accounts to OIDC identities
- Admin role assignment based on email domain rules
- Automatic username generation from email
- Transaction-safe identity creation

Identity Management (oidcIdentityService.js):
- List user's linked OIDC identities
- Link additional providers to existing accounts
- Unlink identities with safety checks
- Prevent unlinking last auth method
- Update identity claims on login

HTTP Layer (controller.js + routes.js):
- GET /api/oidc/providers - List configured providers
- GET /api/oidc/auth/:slug - Initiate OIDC flow
- GET /api/oidc/callback/:slug - Handle OAuth callback
- POST /api/oidc/link/:slug - Link provider to current user
- DELETE /api/oidc/unlink/:id - Unlink identity
- GET /api/oidc/identities - Get user's identities

Integration:
- Register OIDC routes in Express app (public + authenticated)
- Update auth service to reject password login for OIDC-only users
- Audit logging for all OIDC operations
- Session creation on successful authentication

Security:
- State/nonce CSRF protection
- One-time use state consumption
- Transaction-safe user provisioning
- Foreign key constraints enforced

* feat: implement OIDC frontend login flow (Phase 4)

- Created OIDCProviderButtons component for SSO login options
- Created OIDCCallback component for OAuth callback handling
- Updated Login page to fetch and display OIDC providers
- Added /auth/callback/:provider route to App.tsx
- Added i18n translations for OIDC UI elements
- Downgraded openid-client to v5.7.0 (CommonJS compatibility)
- Fixed linting issues in backend OIDC modules

Phase 4 completes the frontend login flow for OIDC/SSO authentication.
Users can now see configured SSO providers on the login page.

* feat: implement OIDC account linking UI (Phase 5)

Add Connected Accounts section to Profile Security tab allowing users to:
- View linked OIDC provider accounts
- Link new SSO providers to their account
- Unlink OIDC identities with validation
- Prevent unlinking last authentication method

Backend changes:
- Add has_password virtual field to User model
- Include has_password in profile API response
- Track whether user has password set for validation

Frontend changes:
- Create oidcService for OIDC API operations
- Create ConnectedAccounts component with link/unlink flows
- Add confirmation dialog before unlinking accounts
- Validate that users cannot unlink their last auth method
- Show warning if user has no password set
- Integrate Connected Accounts into SecurityTab

User experience:
- View all linked SSO provider accounts with email and link date
- Link additional providers via "Link Provider" buttons
- Unlink with two-step confirmation to prevent accidents
- Clear error messages when unlinking would leave no auth method
- Warning message suggesting password setup for OIDC-only users

Fixes #977

* feat: complete OIDC documentation and UI improvements (Phase 6)

This commit completes Phase 6 of the OIDC/SSO implementation with comprehensive
documentation, bug fixes, and UI reorganization.

Documentation:
- Add comprehensive user guide at docs/10-oidc-sso.md with:
  - Setup guides for 6 major providers (Google, Okta, Keycloak, Authentik, PocketID, Azure AD)
  - Configuration examples for single and multiple providers
  - User features documentation (login, account linking, management)
  - Advanced topics (auto-provisioning, admin role assignment, hybrid auth)
  - Comprehensive troubleshooting section
  - Security considerations and best practices
- Update README.md with OIDC/SSO section and quick setup examples

Internationalization:
- Add i18n support to OIDCProviderButtons component
- Add translation keys for all OIDC UI text
- Update English translations with "sign_in_with" key

Bug Fixes:
- Fix oidcService.ts to correctly unwrap API responses
  - Backend returns {providers: [...]} and {identities: [...]}
  - Frontend was expecting plain arrays, causing "map is not a function" error
- Fix initiateOIDCLink to properly handle POST response

UI Improvements:
- Move OIDC/SSO to dedicated tab in profile settings
  - Create new OIDCTab component with green LinkIcon
  - Remove ConnectedAccounts from SecurityTab
  - Add OIDC tab between Security and API Keys tabs
  - Update ProfileSettings with new tab configuration
- Security tab now focuses solely on password management

Testing:
- All linting passes
- All tests pass (82 suites, 1223 tests)

Related to #977

* feat: add OIDC/SSO translations for all 24 languages

Add i18n support for OIDC/SSO features across all supported languages:
- "Sign in with {{provider}}" button text
- "OIDC/SSO" tab label in profile settings
- OIDC authentication flow messages

Translations added for: Arabic, Bulgarian, Danish, German, Greek, Spanish,
Finnish, French, Indonesian, Italian, Japanese, Korean, Dutch, Norwegian,
Polish, Portuguese, Romanian, Russian, Slovenian, Swedish, Turkish,
Ukrainian, Vietnamese, and Chinese.

* fix: resolve 13 CodeQL security alerts

This commit addresses critical security vulnerabilities identified by CodeQL scanning:

**Security Configuration (2 fixes)**
- Fix insecure Helmet configuration - enable CSP and HSTS in production
- Fix clear text cookie transmission - enable secure cookies in production

**Path Injection (3 fixes)**
- Add path validation in users/controller.js to prevent arbitrary file deletion
- Add path validation in users/service.js for avatar operations
- Add path sanitization in attachment-utils.js deleteFileFromDisk function

**Cross-Site Scripting (1 fix)**
- Fix XSS vulnerability in GeneralTab.tsx avatar URL handling
- Add URL sanitization to prevent javascript: protocol attacks

**URL Security (2 fixes)**
- Fix double escaping in url/service.js HTML entity decoding
- Fix incomplete URL sanitization for YouTube domain validation

**Denial of Service (1 fix)**
- Add loop bound protection in inboxProcessingService.js (10k char limit)

**Rate Limiting (3 fixes)**
- Add rate limiting to auth routes (register, verify-email)
- Add rate limiting to task attachment upload/delete endpoints
- Add rate limiting to user avatar upload/delete endpoints

**GitHub Actions Security (1 fix)**
- Add explicit read-only permissions to CI workflow

Note: CSRF middleware (#10) requires frontend changes and is tracked separately.

Relates to PR #1008

* fix: allow test files in path validation for tests

* fix: format long condition in attachment-utils for Prettier compliance

Break the path validation condition across multiple lines to meet Prettier formatting requirements and fix CI linting failure.

* fix: resolve CodeQL security alerts

- Add rate limiting to OIDC authentication routes using authLimiter and authenticatedApiLimiter
- Implement CSRF protection middleware using csrf-sync (skips for API tokens and test environment)
- Add CSRF token endpoint at /api/csrf-token
- Fix incomplete URL scheme validation in GeneralTab to block all dangerous schemes (javascript:, data:, vbscript:, file:)

This addresses 5 high-severity CodeQL security vulnerabilities:
- Missing rate limiting on OIDC auth routes
- Missing CSRF middleware protection
- Incomplete URL sanitization in avatar handling

All 1223 tests passing.

* fix: implement CSRF protection with lusca for CodeQL compliance

Add CSRF protection using lusca.csrf (CodeQL's recommended library) to
protect session-based authentication while supporting hybrid auth patterns.

Implementation:
- Pre-check middleware marks exempt requests (test env, Bearer tokens)
- Lusca CSRF middleware applied with exemption flag check
- Session-based requests require valid x-csrf-token header
- Bearer token requests exempt (don't use cookies)
- Test environment exempt for test execution

This addresses CodeQL security alert js/missing-token-validation while
maintaining support for both cookie-based and token-based authentication.

Related: #977 (OIDC/SSO authentication feature)
2026-04-13 12:17:35 +03:00
..
20250116000000-add-first-day-of-week-to-users.js Feat add first day of week (#339) 2025-09-18 12:57:28 +03:00
20250615000001-create-users.js Linting cleanup (#99) 2025-07-01 11:40:09 +03:00
20250618000001-enhance-recurring-tasks.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20250619000001-add-recurring-parent-id.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20250619000002-add-project-image-url.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250620000001-add-task-intelligence-enabled.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250620000002-add-auto-suggest-next-actions-enabled.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250621221841-add-completed-at-to-tasks.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250621223000-create-calendar-tokens.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250622000001-create-task-events.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250622053925-add-pomodoro-enabled-to-users.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250623000001-add-uuid-to-tasks.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250623000003-create-notes-tags-table.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250623000004-add-timestamps-to-notes-tags.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250623000005-add-timestamps-to-projects-tags.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250711000001-add-suggestion-metadata-to-inbox-items.js Lint & format (#159) 2025-07-15 10:44:02 +03:00
20250713072131-add-productivity-assistant-columns.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20250713233301-add-title-to-inbox-items.js Cleanup and add safe utility functions (#154) 2025-07-15 10:44:02 +03:00
20250716085710-add-parent-task-id-to-tasks.js Fix migration for duplicate column, fixes #202 2025-07-31 14:02:42 +03:00
20250722051746-add-today-settings-to-users.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20250722052000-add-task-sorting-columns-to-projects.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20250805000001-standardize-uid-columns.js Feat: habits (#707) 2025-12-13 08:47:52 +02:00
20250810090000-create-roles.js chore(lint): remove unnecessary try/catch and tighten error handling 2025-09-22 15:20:46 +03:00
20250810090100-create-actions.js chore(lint): remove unnecessary try/catch and tighten error handling 2025-09-22 15:20:46 +03:00
20250810090200-create-permissions.js chore(lint): remove unnecessary try/catch and tighten error handling 2025-09-22 15:20:46 +03:00
20250813103351-add-telegram-allowed-users.js Limit users that can send to telegram bot 2025-08-14 18:00:57 +03:00
20250920074825-add-project-state-column.js Feat/add project states (#354) 2025-09-29 16:04:25 +03:00
20250920075905-convert-active-to-states.js Fix upcoming completed issue (#404) 2025-10-11 00:08:13 +03:00
20250920075916-remove-active-column.js Feat/add project states (#354) 2025-09-29 16:04:25 +03:00
20250924000001-add-uid-to-inbox-items.js Fix notes.js & areas.js UID usage and remove IDs. (#355) 2025-09-29 16:03:46 +03:00
20250925000001-add-uid-to-users.js Fix E2E test breakage (#380) 2025-10-05 16:04:46 +03:00
20251011000001-add-surname-to-users.js Improve migrations safe functions usage 2025-10-14 12:58:30 +03:00
20251014000001-create-views.js Universal search (#412) 2025-10-22 22:00:45 +03:00
20251014202005-add-sidebar-settings-to-users.js Universal search (#412) 2025-10-22 22:00:45 +03:00
20251017000000-add-email-verification-to-users.js Scaffold smtp service (#590) 2025-11-24 17:58:09 +02:00
20251019000000-create-settings.js Scaffold smtp service (#590) 2025-11-24 17:58:09 +02:00
20251020000000-backfill-roles.js Add migration to backfill roles for existing users 2025-10-20 14:43:42 +03:00
20251022000001-add-tags-to-views.js Fix universal search (#445) 2025-10-23 21:43:42 +03:00
20251023000001-add-unique-constraint-tags.js Fix tags migration association 2025-10-28 14:44:10 +02:00
20251106000001-add-color-to-notes.js Notes page revamp! (#487) 2025-11-06 17:59:30 +02:00
20251115000001-create-api-tokens.js API Access (#495) 2025-11-07 15:19:30 +02:00
20251117013905-add-order-to-tasks.js Add migration to fix subtasκ  ordering (#554) 2025-11-17 12:09:31 +02:00
20251118000001-add-recurring-to-views.js Feat add recurring search (#579) 2025-11-18 20:35:50 +02:00
20251119000001-add-recurrence-weekdays.js Fix redesign recurring tasks (#582) 2025-11-19 17:03:26 +02:00
20251120000001-add-ui-settings-to-users.js Tc refactor pt1 (#589) 2025-11-23 21:48:49 +02:00
20251124000001-add-defer-until-to-tasks.js Feat add defer until date (#592) 2025-11-24 21:57:30 +02:00
20251124000002-create-notifications.js Feat notifications (#594) 2025-11-25 21:16:21 +02:00
20251125000001-add-notification-features.js Feat notifications (#594) 2025-11-25 21:16:21 +02:00
20251128000001-create-task-attachments.js Cleanup recurring tasks 2025-12-07 14:12:38 +02:00
20251204000001-add-recurring-completions.js Fix recurring structure (#646) 2025-12-04 13:29:37 +02:00
20251204000002-migrate-recurring-instances.js Fix sql issue (#723) 2025-12-18 15:54:18 +02:00
20251208000001-create-backups-table.js Feat backups (#686) 2025-12-09 08:00:46 +02:00
20251209000001-add-telegram-to-notification-preferences.js Feat telegram notifications (#692) 2025-12-09 20:26:53 +02:00
20251213000001-add-habit-fields.js Feat: habits (#707) 2025-12-13 08:47:52 +02:00
20251215000002-upgrade-inbox-content-field.js Feat add inbox flow (#676) 2025-12-08 09:21:35 +02:00
20251224000001-cleanup-unused-task-fields.js Fix SQLite migration failure when removing uuid column from tasks (#787) 2026-01-22 17:18:01 +02:00
20251227000001-remove-today-column.js Fix bug 733 (#735) 2025-12-27 21:00:52 +02:00
20251228000001-update-project-state-enum.js Fix bug 722 (#737) 2025-12-28 07:51:15 +02:00
20251228000002-rename-project-state-to-status.js Fix failing migration 2026-01-08 00:42:20 +02:00
20251229000001-add-task-performance-indexes.js Fix bug 366 (#764) 2026-01-07 18:18:07 +02:00
20260201000001-add-defer-and-extras-to-views.js Reorder elements (#687) 2025-12-09 10:51:51 +02:00
20260201000002-add-keyboard-shortcuts.js Fix safe function def 2026-01-08 12:07:46 +02:00
20260320000001-add-notification-channel-sent-tracking.js Fix Telegram notification spam with channel-level rate limiting (#951) 2026-03-19 20:26:11 +02:00
20260420000001-create-oidc-identities.js feat: Add OIDC/SSO authentication support (#1008) 2026-04-13 12:17:35 +03:00
20260420000002-create-oidc-state-nonces.js feat: Add OIDC/SSO authentication support (#1008) 2026-04-13 12:17:35 +03:00
20260420000003-create-auth-audit-log.js feat: Add OIDC/SSO authentication support (#1008) 2026-04-13 12:17:35 +03:00
20260420000004-make-password-optional.js feat: Add OIDC/SSO authentication support (#1008) 2026-04-13 12:17:35 +03:00