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: password migration hook #3978

Merged
merged 10 commits into from
Jul 3, 2024
Merged

feat: password migration hook #3978

merged 10 commits into from
Jul 3, 2024

Conversation

hperl
Copy link
Contributor

@hperl hperl commented Jul 1, 2024

This PR adds a password migration hook to easily migrate passwords for which we do not have the hash.

  • For each user that needs to be migrated to Ory Network, a new identity is created with a credential of type password with a config of {"use_password_migration_hook": true} .
  • When a user logs in, the credential identifier and password will be sent to the password_migration web hook if all of these are true:
    • The user’s identity’s password credential is {"use_password_migration_hook": true}
    • The password_migration hook is configured
  • After calling the password_migration hook, the HTTP status code will be inspected:
    • On 200, we parse the response as JSON and look for {"status": "password_match"}. The password credential config will be replaced with the hash of the actual password.
    • On any other status code, we assume that the password is not valid.

Related issue(s)

Checklist

  • I have read the contributing guidelines.
  • I have referenced an issue containing the design document if my change
    introduces a new feature.
  • I am following the
    contributing code guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got the approval (please contact
    [email protected]) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further Comments

@hperl hperl self-assigned this Jul 1, 2024
@hperl hperl removed the request for review from zepatrik July 1, 2024 12:20
@hperl hperl force-pushed the hperl/password-migration-hook branch from 4182bec to bbadd81 Compare July 1, 2024 12:24
selfservice/strategy/password/login.go Outdated Show resolved Hide resolved
selfservice/hook/password_migration_hook.go Outdated Show resolved Hide resolved
@hperl hperl requested a review from jonas-jonas July 2, 2024 07:01
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

Attention: Patch coverage is 87.01299% with 10 lines in your changes missing coverage. Please review.

Project coverage is 78.23%. Comparing base (c508980) to head (6e6b9d5).
Report is 1 commits behind head on master.

Files Patch % Lines
selfservice/hook/password_migration_hook.go 84.90% 4 Missing and 4 partials ⚠️
selfservice/strategy/password/login.go 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3978      +/-   ##
==========================================
+ Coverage   78.21%   78.23%   +0.02%     
==========================================
  Files         363      365       +2     
  Lines       25516    25586      +70     
==========================================
+ Hits        19958    20018      +60     
- Misses       4034     4039       +5     
- Partials     1524     1529       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

jonas-jonas
jonas-jonas previously approved these changes Jul 2, 2024
@hperl hperl force-pushed the hperl/password-migration-hook branch from e1ebd30 to 6c8f8a1 Compare July 2, 2024 09:48
Copy link
Member

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks very good, I just have some improvement suggestions for the test.

selfservice/hook/password_migration_hook.go Show resolved Hide resolved
selfservice/strategy/password/login.go Show resolved Hide resolved
selfservice/strategy/password/login_test.go Show resolved Hide resolved
selfservice/strategy/password/login_test.go Show resolved Hide resolved
selfservice/strategy/password/login_test.go Outdated Show resolved Hide resolved
selfservice/strategy/password/login_test.go Outdated Show resolved Hide resolved
selfservice/strategy/password/login_test.go Outdated Show resolved Hide resolved
selfservice/strategy/password/login_test.go Show resolved Hide resolved
@hperl hperl merged commit c9d5573 into master Jul 3, 2024
29 checks passed
@hperl hperl deleted the hperl/password-migration-hook branch July 3, 2024 07:28
dec := json.NewDecoder(io.LimitReader(resp.Body, 1024)) // limit the response body to 1KB
var response PasswordMigrationResponse
if err := dec.Decode(&response); err != nil || response.Status != "password_match" {
return errors.WithStack(schema.NewInvalidCredentialsError())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs some tracing or something, because it will be difficult to debug this hook otherwise

if err := hash.Compare(r.Context(), []byte(p.Password), []byte(o.HashedPassword)); err != nil {
return nil, s.handleLoginError(w, r, f, &p, errors.WithStack(schema.NewInvalidCredentialsError()))
}
if o.ShouldUsePasswordMigrationHook() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who sets this to false once this is executed successfully? Is it s.migratePasswordHash?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if the password hash is not empty, this returns false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants