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

Show only the last 4 characters of Global API Key & Zone ID in Cloudflare add-on #3376

Closed
GeekPress opened this issue Dec 1, 2020 · 6 comments · Fixed by #6383
Closed

Show only the last 4 characters of Global API Key & Zone ID in Cloudflare add-on #3376

GeekPress opened this issue Dec 1, 2020 · 6 comments · Fixed by #6383
Assignees
Labels
community Issues created by someone outside of our team effort: [S] 1-2 days of estimated development time good first issue Indicates a good issue for first-time contributors module: cloudflare priority: low Issues that can wait type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Milestone

Comments

@GeekPress
Copy link
Contributor

GeekPress commented Dec 1, 2020

Is your feature request related to a problem? Please describe.
WP Rocket settings (accessible by all admins) displays the Cloudflare Global API Key and Zone ID.

Describe the solution you'd like
Once entered and saved, they don't need to be shown on the page.

We could display only the last 4 characters of the Cloudflare Global API Key and Zone ID. The rest will be hidden as if it was an input typed password.

Additional context
https://wp-media.productboard.com/insights/shared-inbox/notes/12491405

@GeekPress GeekPress added type: enhancement Improvements that slightly enhance existing functionality and are fast to implement module: cloudflare priority: low Issues that can wait needs: grooming community Issues created by someone outside of our team labels Dec 1, 2020
@NataliaDrause
Copy link
Contributor

@MathieuLamiot MathieuLamiot added the good first issue Indicates a good issue for first-time contributors label Jan 5, 2024
@jeawhanlee
Copy link
Contributor

jeawhanlee commented Jan 9, 2024

Scope a solution ✅

The solution is quite simple. We will create a new function here to mask the values of any input

function maskField(id_selector){
  var inputField = $('#' + id_selector);

  if (inputField.val().length > 4) {
    // Get the input value
    var inputValue = inputField.val();

    var hiddenPart = '*'.repeat(Math.max(0, inputValue.length - 4));
    var visiblePart = inputValue.slice(-4);

    // Combine the hidden and visible parts
    var maskedValue = hiddenPart + visiblePart;

    inputField.val(maskedValue);
  }
}

Then we can call this function with the ids of the fields we want to mask in this case, it would be cloudflare_api_key and cloudflare_zone_id That should do the trick.
maskField('cloudflare_api_key');
maskField('cloudflare_zone_id');

To solve the issue of saving the masked value, we can update this line here to act as a proxy field by changing the key to cloudflare_api_key_mask

Then we will add the concrete id - cloudflare_api_key to the hidden fields array here only when WP_ROCKET_CF_API_KEY_HIDDEN is false or not defined.

Then we unset the proxy field here

Lastly we bind the 2 fields together by updating the maskField function above to become

function maskField(proxy_selector, concrete_selector){
    var concrete = {
        'val': concrete_selector.val(),
        'length': concrete_selector.val().length
    }

    if (concrete.length > 4) {

        var hiddenPart = '*'.repeat(Math.max(0, concrete.length - 4));
        var visiblePart = concrete.val.slice(-4);

        // Combine the hidden and visible parts
        var maskedValue = hiddenPart + visiblePart;

        proxy_selector.val(maskedValue);
    }
}

proxy_selector = $('#proxy_field');
concrete_selector = $('#concrete_field');

// Update the concrete field when the proxy is updated.
proxy_selector.on('input', function() {
    concrete_selector.val(proxy_selector.val());
});

Before updating the concrete field on input event, we can check if value contains '*' and if not update.

finally we mask the proxy field.
maskField(proxy_selector, concrete_selector);

Estimate the effort ✅

[S]

@jeawhanlee jeawhanlee added the effort: [XS] < 1 day of estimated development time label Jan 9, 2024
@Miraeld
Copy link
Contributor

Miraeld commented Jan 9, 2024

Looks fine to me.

@Tabrisrp
Copy link
Contributor

Tabrisrp commented Jan 9, 2024

Won't that make it that when saving the value, it will contain the * instead of the real value?

@jeawhanlee
Copy link
Contributor

jeawhanlee commented Jan 9, 2024

@Tabrisrp I updated the grooming based on your last point, What do you think?

@jeawhanlee jeawhanlee added effort: [S] 1-2 days of estimated development time and removed effort: [XS] < 1 day of estimated development time labels Jan 10, 2024
@Tabrisrp
Copy link
Contributor

New proposed solution looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Issues created by someone outside of our team effort: [S] 1-2 days of estimated development time good first issue Indicates a good issue for first-time contributors module: cloudflare priority: low Issues that can wait type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants