medic / cht-core

The CHT Core Framework makes it faster to build responsive, offline-first digital health apps that equip health workers to provide better care in their communities. It is a central resource of the Community Health Toolkit.
https://communityhealthtoolkit.org
GNU Affero General Public License v3.0
469 stars 217 forks source link

feat(#9547): add password change feature on first time login #9581

Open Benmuiruri opened 4 weeks ago

Benmuiruri commented 4 weeks ago

Description

Password reset for offline user https://github.com/user-attachments/assets/90694549-98d3-4ad5-bf6b-37b9f6992849

Closes #9547

Code review checklist

Compose URLs

If Build CI hasn't passed, these may 404:

License

The software is provided under AGPL-3.0. Contributions to this project are accepted under the same license.

Benmuiruri commented 4 weeks ago

Hi @garethbowen I worked on the first part to understand the API side routing and display the password page. Now I'm updating the form to meet the Figma design and checking out the update password logic to use it in the password change form. As I do that, I'd like your input of my approach / understanding of this feature.

My Thoughts

garethbowen commented 4 weeks ago

The approach I would like to take for this is to edit the add user form in the admin to add a checkbox option, which is unchecked by default (hence false / null). When checked it sets require_password_change to true in the user object.

Unfortunately this does not solve the requirement. The requirement is that every time a password is set by anyone other than the user themselves (ie: admin), the user is required to set a new password on login. This should be enforced in the API somewhere to ensure this happens regardless of whether the password is set via the admin app, the user management app, or any other method. Therefore there must be no checkbox in the admin app to turn this off. You'll have to check how the user management app is written to make sure it applies there as well.

Because this may be disruptive we will include a feature flag to turn it off, just like we do for the new UX features like the action bar -> floating action button. In some time if/when this has been proven to work we will remove the feature flag and make this the only option for all user password setting.

All of this must default to true, otherwise we cannot claim the CHT is secure by default.

I believe this would work with CHT User Management tool to add the checkbox option when creating single or multiple users.

It's also possible to set up the user with scripts and so on, so I prefer the API approach so we can catch all cases where the password is set.

I believe this would work if a user loses their phone because the admin can select the require_password_change checkbox in the admin.

If the phone is lost then the administrator MUST change the password, not set the checkbox. You have to change the password in order to invalidate the session so anyone finding the phone is kicked out of the app. If they're changing the password then the process above (mandatory password reset) will mean you don't need the checkbox at all.

I believe this would not be a breaking change for existing users because require_password_change would be null for them.

Thanks for thinking about this! That's an absolute requirement.

Benmuiruri commented 3 weeks ago

Hi @latin-panda this is ready for an early review even though it is not 100% done. A couple of things:

With those three points, I think the solution design is ready for an early review.

Benmuiruri commented 1 week ago

Hi @garethbowen and @latin-panda I believe this is ready for a review.

The password reset logic , unit tests and integration tests are ready for review.

Unfortunately the e2e are a bit flaky with the password reset. Sometimes a test hangs on the password reset page causing the test to fail. I want to reach out to QA to get possibly a more robust way of doing the password reset.

While I work on the e2e I think the review can go ahead.

garethbowen commented 1 week ago

@Benmuiruri Another thing to think about is what happens if the user connects with basic auth, either through curl or directly in the browser. Can they bypass the password reset this way?

Benmuiruri commented 4 days ago

Hi @garethbowen Thanks for your review.

This video shows the progress so far https://github.com/user-attachments/assets/16a4fbcf-d003-421e-b13a-388db8f9d072

I'm currently updating failing e2e tests (almost done). As I continues, could I get your review of my implementation of your requested changes. Thanks