Repository navigation
feat(auth2): Extend EmailAccountsAdmin with new methods needed for email account imports - #3690
Conversation
📝 WalkthroughWalkthroughThis update introduces new administrative methods for managing email authentication accounts, including account lookup, creation, and password setting. It adds a custom exception for missing accounts, refactors utility extensions for byte data conversions, and improves code clarity and test coverage. Minor formatting and documentation updates are also included. Changes
Sequence Diagram(s)sequenceDiagram
participant Admin
participant EmailAccountsAdmin
participant Database
Admin->>EmailAccountsAdmin: findAccount(email)
EmailAccountsAdmin->>Database: Query email account by email
Database-->>EmailAccountsAdmin: Account data or null
EmailAccountsAdmin-->>Admin: Account info or null
Admin->>EmailAccountsAdmin: createEmailAuthentication(authUserId, email, password)
EmailAccountsAdmin->>Database: Insert new email account (with hashed password)
Database-->>EmailAccountsAdmin: New account ID
EmailAccountsAdmin-->>Admin: Account ID
Admin->>EmailAccountsAdmin: setPassword(email, password)
EmailAccountsAdmin->>Database: Update password hash for email account
Database-->>EmailAccountsAdmin: Success or throw EmailAccountNotFoundException
EmailAccountsAdmin-->>Admin: Success or exception
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
⏰ Context from checks skipped due to timeout of 90000ms (53)
🔇 Additional comments (6)
|
7fc34da to
64e539f
Compare
EmailAccountsAdmin with new methods needed for email account importsEmailAccountsAdmin with new methods needed for email account imports
64e539f to
ee81043
Compare
…ccount imports - findAccount: To check whether the model knows about an email address already - createEmailAuthentication: To create an email authentication without going through the verification flow - setPassword: To set an account's password without going through the password reset flow Also fix `unnecessary_non_null_assertion` lint warnings
ee81043 to
08fd344
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (3)
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_admin_test.dart (3)
53-75: Comprehensive test coverage for new admin methods.The tests properly cover the new
findAccountandsetPasswordadmin methods with appropriate scenarios:
findAccountcorrectly returnsnullfor pending accountssetPasswordproperly throwsEmailAccountNotFoundExceptionfor pending accountsThe test structure and assertions are well-designed and align with the security-focused admin functionality.
78-135: Excellent coverage for existing account scenarios.The test group properly validates admin methods against verified accounts:
findAccountreturns valid account datasetPasswordsuccessfully updates passwords and validates through loginThe use of proper test isolation with setup/teardown and the verification through actual login attempts demonstrates thorough testing.
137-192: Comprehensive testing of createEmailAuthentication workflow.The test group validates the new
createEmailAuthenticationmethod effectively:
- Account creation bypasses normal verification flow
- Immediate login capability is properly verified
- Integration with
findAccountmethod works correctlyThis demonstrates the admin import functionality working as intended.
🧹 Nitpick comments (1)
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts_admin.dart (1)
171-176: Consider usingfindFirstRowfor consistency.The method uses
findfollowed bysingleOrNull, whilefindAccountusesfindFirstRow. Both approaches work correctly, but usingfindFirstRowwould be more consistent and slightly more efficient.- var account = (await EmailAccount.db.find( - session, - where: (final t) => t.email.equals(email), - transaction: transaction, - )) - .singleOrNull; + var account = await EmailAccount.db.findFirstRow( + session, + where: (final t) => t.email.equals(email), + transaction: transaction, + );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/serverpod_auth_email_account_server.dart(1 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_account_not_found_exception.dart(1 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_account_secret_hash.dart(2 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart(13 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts_admin.dart(3 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/util/byte_data_extension.dart(1 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/util/uint8list_extension.dart(1 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_account_creation_test.dart(3 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_admin_test.dart(5 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_login_test.dart(1 hunks)modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_password_reset_test.dart(3 hunks)
🧰 Additional context used
🧠 Learnings (1)
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_account_not_found_exception.dart (1)
Learnt from: tp
PR: serverpod/serverpod#3666
File: modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts_admin.dart:157-163
Timestamp: 2025-06-17T11:39:51.491Z
Learning: In `EmailAccountsAdmin` class methods, it's appropriate to throw specific errors like `EmailAccountNotFound` when email accounts don't exist, since this is admin-only functionality marked as `@internal`. However, in non-admin/public-facing contexts, avoid exposing whether email accounts exist to prevent account enumeration attacks - use generic error messages instead.
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: Migration e2e tests (3.32.1)
- GitHub Check: Migration e2e tests (3.24.0)
- GitHub Check: Serverpod E2E web tests (3.24.0)
- GitHub Check: Serverpod E2E web tests (3.32.1)
- GitHub Check: dart analyze downgrade (3.32.1, windows-latest)
🔇 Additional comments (17)
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_login_test.dart (1)
22-22: LGTM! Improved type safety by removing unnecessary null assertion.The removal of the
!operator indicates thatauthUser.idis now guaranteed to be non-null, which eliminates potential runtime exceptions and improves code safety.modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_password_reset_test.dart (1)
52-52: LGTM! Consistent type safety improvements.The removal of unnecessary null assertion operators across multiple test setup blocks improves code safety and follows the same positive pattern seen in other test files.
Also applies to: 138-138, 225-225
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_account_secret_hash.dart (2)
12-13: LGTM! Documentation formatting follows established guidelines.The line wrapping to comply with the 80 character limit improves code consistency as noted in previous reviews.
Also applies to: 20-21
66-71: Excellent performance optimization with clear documentation.The early return for empty hashes is a smart optimization that avoids expensive hash computations when no password is set. The accompanying comment clearly explains the business logic, making the code self-documenting.
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/test/integration/email_accounts_account_creation_test.dart (1)
232-232: LGTM! Consistent type safety improvements across test suite.The systematic removal of unnecessary null assertion operators from
authUser.idimproves code safety and maintains consistency with similar changes in other test files.Also applies to: 293-293, 320-320
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/serverpod_auth_email_account_server.dart (1)
2-2: LGTM! Proper API exposure for new exception class.The export correctly makes
EmailAccountNotFoundExceptionavailable to package consumers, which is essential for handling exceptions thrown by the new admin methods introduced in this PR.modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/util/byte_data_extension.dart (1)
5-10: LGTM! Clean utility extension implementation.The
ByteDataAsUint8Listextension provides a clean wrapper aroundUint8List.sublistView()and properly uses the@internalannotation to restrict usage within the package. This is a good refactoring that centralizes byte data conversion utilities.modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/util/uint8list_extension.dart (1)
5-10: LGTM! Complementary extension implementation.The
Uint8ListAsByteDataExtensionprovides the reverse conversion fromUint8ListtoByteDatausingByteData.sublistView(). This complements theByteDataAsUint8Listextension nicely, providing bidirectional conversion utilities that are properly marked as internal.modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_account_not_found_exception.dart (1)
1-17: Well-designed exception class for admin use.The
EmailAccountNotFoundExceptionis properly implemented with clear documentation emphasizing internal/admin-only usage. This aligns with security best practices to prevent account enumeration attacks in public-facing contexts while allowing specific error handling in administrative operations.modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart (3)
9-10: Good refactoring to use centralized utility extensions.The import of the new utility extensions replaces local byte data conversion logic with centralized, reusable utilities. This improves code organization and maintainability.
24-25: Excellent documentation improvements.The enhanced documentation comments throughout the file provide clearer explanations of:
- Transaction behavior and rollback implications
- Security considerations for preventing information leakage
- Expected exceptions and error conditions
- Method purposes and return values
These improvements significantly enhance code maintainability and developer understanding.
Also applies to: 72-94, 187-201, 323-325, 387-396, 510-511, 528-529, 592-593
57-58: Consistent adoption of new extension methods.The usage of
.asUint8Listand.asByteDataextension methods throughout the file demonstrates consistent refactoring from local byte data conversions to the centralized utility extensions. This maintains the same functionality while improving code organization.Also applies to: 163-166, 244-245, 366-367, 447-448, 472-473
modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts_admin.dart (5)
1-1: LGTM: New imports properly support the extended functionality.The added imports for
dart:typed_data, internal business logic, and utility extensions are appropriately used throughout the new methods.Also applies to: 7-7, 9-9
13-18: LGTM: Documentation improvements enhance clarity.The documentation updates improve consistency and clarity in describing the cleanup methods' behavior with null parameters.
Also applies to: 69-71
88-116: LGTM:findAccountmethod is well-implemented.The method correctly:
- Normalizes email to lowercase for consistent lookups
- Uses
findFirstRowfor efficient single record retrieval- Returns a properly structured record with auth user ID, email account ID, and password status
- Handles the case where no account exists by returning null
- Supports optional transaction context
The use of
passwordHash.lengthInBytes > 0to determine password presence is appropriate.
118-154: LGTM:createEmailAuthenticationmethod handles all scenarios correctly.The method properly:
- Normalizes email input consistently
- Handles optional password by creating empty byte arrays when null
- Uses secure password hashing via
EmailAccountSecretHash.createHash- Converts
Uint8ListtoByteDatausing the extension method- Supports transaction context
- Returns the newly created account ID
The approach of storing empty byte arrays for passwordless accounts is clean and consistent with the password checking logic in
findAccount.
156-194: LGTM:setPasswordmethod implements proper security and error handling.The method correctly:
- Normalizes email input consistently
- Throws
EmailAccountNotFoundExceptionwhen account doesn't exist- Uses secure password hashing
- Updates the account with proper transaction support
- Bypasses password policy validation as documented for admin use
The implementation follows the established patterns and provides appropriate administrative control.
findAccount: To check whether the model knows about an email address alreadycreateEmailAuthentication: To create an email authentication without going through the verification flowsetPassword: To set an account's password without going through the password reset flowAlso, fixes
unnecessary_non_null_assertionlint warnings.Summary by CodeRabbit