wagtail / wagtail

A Django content management system focused on flexibility and user experience
https://wagtail.org
BSD 3-Clause "New" or "Revised" License
18.45k stars 3.91k forks source link

Add ability to auto-generate a random/unique value for Private access shared passwords #11537

Open lb- opened 10 months ago

lb- commented 10 months ago

Is your proposal related to a problem?

This is part of a set of improvements to help improve the way that the Private access (shared password method) works. See https://github.com/wagtail/wagtail/issues/10588

We want to make it easier for users to choose more secure values for this shared password, making it harder to guess and also gain access to easily with over the shoulder viewing of editors.

Describe the solution you'd like

Add the ability to generate a randomized string and also copy the value to the clipboard easily. Example screenshot below.

Mock (example) screenshot

This would involve;

  1. Enhancing client/src/controllers/ActionController.ts (ActionController) with the ability to create a random value.
  2. Enhancing ActionController to support it's actions targeting a specific element with a custom selector value.
  3. Adopting the ClipboardController for this field to support easy copy to clipboard behaviour. You can see how this is used in https://github.com/wagtail/wagtail/pull/11317 & https://github.com/wagtail/wagtail/issues/3683 & the code snippet below.

https://github.com/wagtail/wagtail/blob/54802abcc4a264fc62f1ddaa846f679521850be2/wagtail/images/templates/wagtailimages/images/url_generator.html#L34-L48


Important: It may make more sense to add this behaviour to the SlugController and rename that controller to something like CleanController (for cleaning/modifying field input values). Generally the change will be the same, have a read and provide thoughts in the comments.

ActionController - adding a new value

This controller is a bit of a toolkit controller, it let's us perform some kind of simple action on a button or input with Stimulus easily.

We will want to abstract the ability to get the most suitable target element. Read up on Stimulus values and actions if you are not sure what's happening here.

  static values = {
    target: { type: String, default: '' }, // add this
  };

  // add this method
  getTarget(event) {
    const { target = '' } = { ...event.detail, ...event.params };
    return (document.querySelector(target) || this.element) as HTMLElement;
  }

From there, we should really update all other methods to use this. e.g.

  click() {
    this.element.click();
  }
// would be replaced with.
  click(event) {
    this.getTarget(event).click();
  }

The other methods will be a bit more complex but not too much more.

ActionController - Add a unique method.

This would be very similar to reset on the ActionController but use a way to generate a set of unique values and output them as a string.

We would want to dispatch a change event also.

  unique(event) {
    const count = 5
    const array = new Uint32Array(5);
    // get the count from params/detail if possible.
    const newValue = [...window.crypto.getRandomValues(array)].map(val => val.toString(36)).join('-');
    // update the this.getTarget().value to the new value
  }

Describe alternatives you've considered

Multiple discussions have been had on https://github.com/wagtail/wagtail/issues/10588

Additional context

Working on this

RealOrangeOne commented 10 months ago

If we're going to include a password generator, we should make sure it works and is secure. window.crypto tends to only work in secure contexts (eg HTTPS and localhost), which might be an issue for some. Also, it would need to interact with Django's password validator framework, which is going to be quite complex.

Alternatively, we could link out to a password generator: https://bitwarden.com/password-generator/, at least in the first instance.

Or, final option, we do "Leave blank to generate a secure password", and defer the generation to the backend instead.

lb- commented 10 months ago

We could make a server side call if needed.

However I suggested the getRandomValues intentionally as it doesn't require secure context.

https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues

As for the interaction with the password validation, I think we would just have to disable the generator if that feature is used.