Skip to content

Secret Field

The browser half of SecretProtector. A credential input that never receives the stored secret.

Decision

When Use
Every API key, password, and token a plugin stores Secret Field.
The stored secret on the page Nothing. Never put it into the page. The server sends only whether one is stored.

Usage

<div class="inputContainer">
    <label for="txtApiKey">API Key</label>
    <input is="emby-input" type="password" id="txtApiKey" />
    <button is="emby-button" type="button" id="btnClearApiKey" class="raised stolon-button stolon-secondary stolon-small stolon-hidden"><span>Clear</span></button>
</div>
import { bindSecretField, readSecretField, SECRET_KEPT } from './configurationpage?name=myplugin_stolon.js';

// After every load, since a save may change whether a secret is stored.
bindSecretField(view.querySelector('#txtApiKey'), config.HasApiKey, { clearButton: view.querySelector('#btnClearApiKey') });

// On save: SECRET_KEPT when untouched, '' after a clear, otherwise the typed value.
config.ApiKey = readSecretField(view.querySelector('#txtApiKey'));

The same three functions are on the bag and take an id or an element. clearSecretField(input) empties the field and marks the stored secret to clear on the next save.

States and Modifiers

  • A field shows one of three things. With nothing stored it shows its own placeholder, the help text. With a value typed in, it shows the browser's regular dots. With a stored secret the field shows eight dots in secondary gray, so it reads as filled though the browser never has the value. Its title says "Saved, leave blank to keep", which opts.placeholder replaces. Typing replaces the dots.
  • opts.allowExpose: true adds an eye inside the box that shows and hides what was typed. It is off by default.
  • opts.clearButton is shown while a secret is stored. Its click clears the secret, hides the button, and fires an input event.
  • readSecretField returns the typed value when there is one, '' after a clear, SECRET_KEPT for an empty field with a stored secret, and '' otherwise.
  • SECRET_KEPT is __JPK_SECRET_KEPT__ and mirrors SecretProtector.KeptSentinel. Feed the posted value through SecretProtector.ResolveIncoming on the server.
  • SECRET_DOTS is the eight dots a stored secret shows.
  • A stored secret counts as filled for a required rule of the validation core, until it is cleared.

Gotchas

  • Call bindSecretField after every load.
  • The input must be a real input with a dataset.
  • No kit CSS styles the clear button, so give it button classes yourself.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

SECRET_KEPT

var SECRET_KEPT: string

Sentinel a config page posts back in a secret field to keep the stored value, so the real secret never travels to the browser. It mirrors SecretProtector.KeptSentinel, and the C# side reads the posted value through SecretProtector.ResolveIncoming.

SECRET_DOTS

var SECRET_DOTS: string

The dots a field shows while a secret is stored, so it reads as filled though the browser never gets the value.

bindSecretField

function bindSecretField(input: HTMLInputElement | null, hasStored: boolean, opts?: SecretFieldOptions): void

Prepares a secret input so the stored secret never reaches the browser. While a secret is stored the field is empty and shows eight dots, so it reads as filled, with a hint in its title. Call it after every load, since a save may change whether a secret is stored.

clearSecretField

function clearSecretField(input: HTMLInputElement | null): void

Marks a bound secret field to clear the stored secret on the next save, since an empty field otherwise keeps it.

readSecretField

function readSecretField(input: HTMLInputElement | null): string

Returns the value to post for a bound secret field. That is SECRET_KEPT when it is empty and a secret is stored, '' after clearSecretField, and otherwise the typed replacement.

Class What it looks like
stolon-secret-stored A secret field with a stored secret, whose eight placeholder dots show in secondary gray, since the value is not in the page

Example

Example Code

<!-- title: Secret Field -->
<div class="inputContainer">
    <label for="txtApiKey">API Key</label>
    <input is="emby-input" type="password" id="txtApiKey" />
    <button is="emby-button" type="button" id="btnClearApiKey" class="raised stolon-button stolon-secondary stolon-small stolon-hidden"><span>Clear</span></button>
</div>
<div style="display: flex; gap: 10px; align-items: center;">
    <button type="button" id="btnRead" class="raised stolon-button stolon-small"><span>Read</span></button>
    <span id="readOut" class="stolon-mono"></span>
</div>
<script type="module">
const base = await window.__baseReady;
const input = document.getElementById('txtApiKey');
base.bindSecretField(input, true, { clearButton: document.getElementById('btnClearApiKey') });
document.getElementById('btnRead').addEventListener('click', () => {
    document.getElementById('readOut').textContent = JSON.stringify(base.readSecretField(input));
});
</script>