Role Keys to Short-Lived Sessions and Scoped Access

    1

    A monitoring or operations service often starts with one administrator key in an environment variable, pasted into a console that keeps it in browser storage for the whole visit.

    $39

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    Role Keys to Short-Lived Sessions and Scoped Access

    Example session with this skill installed

    Initialize a new set of access keys and a session secret, then show me how to protect a route so only an operator can update a specific device.

    • Read your context and instructions
    • Compiled the role keys to

    Access keys and session secrets have been provisioned to the secure credential path. The rotation script has generated the initial owner and agent keys.

    Implementation details

    // 1. Provisioning (Host Level)
    // node scripts/rotate-keys.mjs init /etc/ops-api/access-keys.json /etc/ops-api/session-secret
    
    // 2. Route Protection
    import { AccessControl, SessionService } from './lib/auth.mjs';
    
    const ac = new AccessControl();
    const sessions = new SessionService();
    
    export async function updateDeviceHandler(req, res) {
      try {
        // Validate session and extract role/scope
        const session = await sessions.validate(req.headers.authorization);
        
        // Enforce operator role requirement
        ac.assertOperator(session);
        
        // Enforce record-level scope (e.g., for demo/reviewer keys)
        const { deviceId } = req.params;
        ac.assertRecord(session, 'deviceIds', deviceId);
        
        // Logic for updating device...
        res.writeHead(200).end(JSON.stringify({ status: 'updated', deviceId }));
      } catch (err) {
        res.writeHead(403).end(JSON.stringify({ error: err.message }));
      }
    }
    

    Next steps

    • Update your systemd unit file to include LoadCredential paths for the newly created files.
    • Configure the ROLE_KEYS environment variable with the operator IDs and scopes required for your team.
    • Update your frontend login form to call POST /session and store the resulting HMAC token in memory rather than LocalStorage.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    A monitoring or operations service often starts with one administrator key in an environment variable, pasted into a console that keeps it in browser storage for the whole visit. This kit upgrades that in place: the existing key keeps working as the owner credential, least-privilege keys are added by configuration, the console exchanges a key once for a session token, and every revocation path works without editing a file or restarting.

    session-service.mjs: base64url(payload).base64url(hmac-sha256) tokens with a 30-minute default; verify checks signature, expiry, per-session revocation and the revoke-all cutoff (<= so a token issued in the same millisecond as the cutoff also dies); revoke-all records a cutoff instead of rotating the secret, so the secret can live in a read-only credential mount; the secret comes from a file or $CREDENTIALS_DIRECTORY/session-secret and a missing or short secret fails loud.

    access-control.mjs: roles read-only, operator, administrator, owner; the original administrator key stays valid as the owner so existing clients survive the upgrade; a separate agent key for machine ingestion that can read nothing; ROLE_KEYS JSON validated fail-closed (32+ chars, unique ids and keys, no key equal to the owner or agent key, scope only on operators, up to 500 ids per kind); scoped credentials enforced on lists, single records and writes, with an empty list granting nothing; revoke a named role key without restart and its sessions stop on their next request; key listing never returns key material.

    server.mjs: a complete loopback API: session exchange that refuses session tokens, logout, owner-only revoke-all and audit log, administrator-only role-key revocation, scoped record reads and acknowledgements, agent-only ingest. audit-log.mjs: hash-chained JSONL of every authenticated mutation (credential id and role, action, outcome, target, source address) with integrity verification. rate-limit.mjs: per-address fixed windows, tighter for requests presenting a raw key. rotate-keys.mjs: provision and rotate owner key, agent key and session secret into 0600 files on the host; there is deliberately no HTTP route for it.

    Reference: the role table, why sessions, why cutoff beats secret rotation, LoadCredential over environment variables, why a rotation endpoint is a persistence mechanism, scoped reviewer credentials over synthetic records. 8 tests including the full HTTP flow. Node 20+, no packages.

    Static secrets only; no MFA support. Reference record store is in-memory. Intended as a middle ground between env vars and OIDC.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Trust & safety

    Security scan

    Security scan passed 18 days ago

    Requires

    Node.js 20+

    Permissions

    Read Files
    Write Files
    Network Access
    Environment Variables
    Listed18 days ago

    What's inside

    Frequently Asked Questions

    This skill replaces static, long-lived administrator keys with a tiered HMAC session system, preventing permanent credentials from being stored in browsers and reducing the risk of credential theft.

    The skill is designed for Node.js 20+ environments and integrates seamlessly with any AI agent or automation tool capable of executing Node scripts or managing backend API routes.

    You receive the complete Node.js implementation, a CLI tool for secure credential provisioning, a configuration template for role-based scopes, and integration guides for systemd.

    Yes, the 'Scoped Access' feature allows you to generate operator keys restricted to a specific whitelist of record IDs, making it ideal for limited-access demos or third-party audits.

    Setup involves running the provided CLI script to provision local keys, configuring your roles in a JSON file, and wrapping your existing API routes with the provided AccessControl methods.

    The implementation uses a zero-dependency approach relying on native 'node:crypto' to avoid supply-chain vulnerabilities and ensure long-term stability without frequent npm updates.

    New in APIs & Backend