ADR 0002: Authentication and Authorization Strategy¶
- Status: Accepted; six-role hierarchy, role-based policy examples, and obsolete migration status superseded by ADR 0009
- Date: 2025-11-27
ADR 0009 supersedes this ADR's six-role authorization hierarchy, role-based policy examples, and obsolete migration-status section. The Rodauth authentication, deny-by-default Pundit framework, and base PaperTrail audit decisions remain accepted.
Context¶
MedTracker requires robust authentication and authorization to protect sensitive medication data. The application serves multiple user roles (administrators, doctors, nurses, carers, parents) with different access levels. UK healthcare compliance (GDPR, DTAC) mandates strong identity verification and audit trails.
Key requirements:
- Role-based access control for 6 user roles
- Support for email/password and OAuth (Google) authentication
- Email verification for account security
- Audit logging of authentication events
- Future support for two-factor authentication (2FA)
Decision¶
Authentication: Rodauth¶
We adopt Rodauth (rodauth-rails) as the primary authentication framework, replacing the initial has_secure_password implementation.
Rationale:
- Feature-complete: Built-in support for email verification, password reset, remember me, OAuth, and 2FA
- Security-focused: Designed with security as the primary concern, not bolted on
- PostgreSQL-optimized: Works excellently with our PostgreSQL-only strategy
- Extensible: Easy to customize flows without monkey-patching
- Maintained: Active development with strong security track record
Implementation:
Accountmodel for authentication (separate fromPersonfor demographics)rodauth-omniauthfor Google OAuth integration- Environment-specific email verification (strict in production, 7-day grace period in development)
- Legacy
Usermodel retained during transition period
Authorization: Pundit¶
We adopt Pundit for authorization with deny-by-default policies.
Rationale:
- Simple and explicit: Plain Ruby objects, easy to test and understand
- Rails conventions: Follows Rails patterns, integrates cleanly
- Flexible: Supports complex authorization logic without framework constraints
- Testable: Policies are easily unit-tested with RSpec
Implementation:
- Policy classes for each resource (
UserPolicy,PersonPolicy,PrescriptionPolicy, etc.) ApplicationPolicywith deny-by-default approachpolicy_scopefor data filtering based on user role- Comprehensive policy tests using
pundit-matchers
Role Hierarchy¶
| Role | Access Level |
|---|---|
| Administrator | Full system access, user management |
| Doctor | All patients, prescriptions, clinical data |
| Nurse | All patients, medication recording |
| Carer | Assigned patients only |
| Parent | Own children only |
| Minor | Own data only (limited) |
Audit Trail: PaperTrail¶
We use PaperTrail for audit logging of all authentication and data changes.
Rationale:
- Battle-tested: Widely used in production healthcare applications
- Compliance: Meets UK healthcare audit requirements
- Integration: Works seamlessly with Pundit and Rodauth
Consequences¶
Positive¶
- Strong security foundation for healthcare compliance
- Clear separation of authentication (Rodauth) and authorization (Pundit)
- Comprehensive audit trail for regulatory requirements
- Testable, maintainable authorization logic
- Future-ready for 2FA and advanced security features
Negative¶
- Dual authentication systems during migration (legacy + Rodauth)
- Learning curve for Rodauth's Roda-based DSL
- Additional complexity in Account/Person/User relationships
Migration Path¶
- Phase 1 (Complete): Pundit authorization framework
- Phase 2 (In Progress): Rodauth foundation installed, login working
- Phase 3 (Pending): Rodauth signup with Person creation
- Phase 4 (Pending): Google OAuth integration
- Phase 5 (Pending): Legacy auth removal, user migration
Related Documents¶
docs/plans/USER_MANAGEMENT_PLAN.md- Overall user management strategydocs/plans/USER_SIGNUP_PLAN.md- Rodauth signup implementation statusdocs/plans/RODAUTH_SIGNUP_IMPLEMENTATION.md- Detailed implementation plandocs/plans/USER_SIGNUP_AND_2FA_PLAN.md- 2FA implementation plandocs/plans/AUTHORIZATION_COMPLETION_PLAN.md- Pundit implementation details