- Home
- Skills
- APIs & Backend
- Role Keys to Short-Lived Sessions and Scoped Access
Role Keys to Short-Lived Sessions and Scoped Access
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
Works with the AI Agents you already use
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
LoadCredentialpaths for the newly created files. - Configure the
ROLE_KEYSenvironment variable with the operator IDs and scopes required for your team. - Update your frontend login form to call
POST /sessionand 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.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for APIs & Backend.
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
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
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
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
- Software
Node.js 20+