Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): implement legacy password migration support #6949

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fre2d0m
Copy link

@fre2d0m fre2d0m commented Jan 17, 2025

Add support for legacy password migration with the following changes:

  • core: Add legacy password migration implementation and API endpoints
  • schemas: Add database schema changes for legacy password encryption
  • phrases: Add error messages for legacy password scenarios in all supported languages

This change enables seamless migration of user passwords from legacy systems while maintaining security and providing appropriate error handling.

Summary

This PR implements support for legacy password migration, allowing seamless integration of existing user accounts from legacy systems into Logto. The implementation includes:

  1. Core Package Changes:

    • Added legacy password validation and migration logic in packages/core/src/utils/password.ts
    • Implemented new API endpoints in packages/core/src/routes/admin-user/basics.ts
    • Enhanced user library to handle legacy password scenarios
  2. Database Schema Changes:

    • Added new columns in users table for legacy password support
    • Created database migration script for schema updates
    • Ensures backward compatibility with existing password storage
    • Legacy password stored format: ["sha256",["salt123", "@"],"hashed2345"]
  3. Internationalization:

    • Added new error messages for legacy password scenarios:
      • invalid_legacy_password_format
      • unsupported_legacy_hash_algorithm
    • Implemented translations for all supported languages (19 locales)

Testing

The changes have been tested through:

  1. Unit Tests:

    • Added comprehensive test suite in packages/core/src/utils/password.test.ts
    • Tests cover various legacy password formats and migration scenarios
    • Includes both success and failure cases for password validation
  2. Manual Testing:

    • Verified legacy password migration flow
    • Tested error handling for invalid password formats
    • Confirmed proper internationalization of error messages

Checklist

  • .changeset - Added changeset for version bump
  • unit tests - Added password migration unit tests
  • integration tests - Not applicable for this change
  • necessary TSDoc comments - Added documentation for new functions and types

@github-actions github-actions bot added the feature Cool stuff label Jan 17, 2025
Copy link

github-actions bot commented Jan 17, 2025

COMPARE TO master

Total Size Diff ⚠️ 📈 +11.07 KB

Diff by File
Name Diff
packages/core/src/libraries/user.test.ts 📈 +51 Bytes
packages/core/src/libraries/user.ts 📈 +303 Bytes
packages/core/src/routes/admin-user/basics.ts 📈 +189 Bytes
packages/core/src/utils/password.test.ts 📈 +3.91 KB
packages/core/src/utils/password.ts 📈 +2.68 KB
packages/phrases/src/locales/ar/errors/password.ts 📈 +225 Bytes
packages/phrases/src/locales/de/errors/password.ts 📈 +173 Bytes
packages/phrases/src/locales/en/errors/password.ts 📈 +159 Bytes
packages/phrases/src/locales/es/errors/password.ts 📈 +175 Bytes
packages/phrases/src/locales/fr/errors/password.ts 📈 +190 Bytes
packages/phrases/src/locales/it/errors/password.ts 📈 +166 Bytes
packages/phrases/src/locales/ja/errors/password.ts 📈 +203 Bytes
packages/phrases/src/locales/ko/errors/password.ts 📈 +193 Bytes
packages/phrases/src/locales/pl-pl/errors/password.ts 📈 +173 Bytes
packages/phrases/src/locales/pt-br/errors/password.ts 📈 +168 Bytes
packages/phrases/src/locales/pt-pt/errors/password.ts 📈 +176 Bytes
packages/phrases/src/locales/ru/errors/password.ts 📈 +258 Bytes
packages/phrases/src/locales/tr-tr/errors/password.ts 📈 +163 Bytes
packages/phrases/src/locales/zh-cn/errors/password.ts 📈 +152 Bytes
packages/phrases/src/locales/zh-hk/errors/password.ts 📈 +155 Bytes
packages/phrases/src/locales/zh-tw/errors/password.ts 📈 +155 Bytes
packages/schemas/alterations/next-1736492439-add-legacy-password-encryption.ts 📈 +1.11 KB
packages/schemas/tables/users.sql 📈 +10 Bytes

@fre2d0m
Copy link
Author

fre2d0m commented Jan 17, 2025

The failed test doesn't seem to come from my commit.

@wangsijie
Copy link
Contributor

@fre2d0m I'll take a look

@wangsijie
Copy link
Contributor

Hi, @fre2d0m thanks for you contribution, you'll need to resolve the failed test:

 SyntaxError: The requested module '#src/utils/password.js' does not provide an export named 'legacyVerify'

@fre2d0m
Copy link
Author

fre2d0m commented Jan 22, 2025

Hi, @fre2d0m thanks for you contribution, you'll need to resolve the failed test:

 SyntaxError: The requested module '#src/utils/password.js' does not provide an export named 'legacyVerify'

Function is

export const legacyVerify = async (
  storedPassword: string,
  inputPassword: string
): Promise<boolean> => {
  try {
    const parsed = parseLegacyPassword(storedPassword);
    const calculatedHash = await executeLegacyHash(parsed, inputPassword);
    return calculatedHash === parsed.encryptedPassword;
  } catch {
    return false;
  }
};

I actually executed ci locally as well as tested the function and if it was not exported then I wouldn't even log in successfully

case UsersPasswordEncryptionMethod.Legacy: {
        const isValid = await legacyVerify(passwordEncrypted, password);
        assertThat(isValid, new RequestError({ code: 'session.invalid_credentials', status: 422 }));
        break;
      }

Maybe you can tell me the specific file where the error occurred?

@wangsijie
Copy link
Contributor

@fre2d0m I'll try to fix it

fre2d0m and others added 3 commits January 26, 2025 10:28
Add support for legacy password migration with the following changes:

- core: Add legacy password migration implementation and API endpoints
- schemas: Add database schema changes for legacy password encryption
- phrases: Add error messages for legacy password scenarios in all supported languages

This change enables seamless migration of user passwords from legacy systems while
maintaining security and providing appropriate error handling.
@wangsijie wangsijie force-pushed the legacy branch 2 times, most recently from ef3cc81 to b54af24 Compare January 26, 2025 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants