Skip to main content

Built and signed on GitHub Actions

Works with
This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
•JSR Score100%•
License
Apache-2.0
•
Downloads629/wk
•
Publisheda day ago (0.0.10)

The authentication plugin for NetScript: one install wires a unified auth API, pluggable backends, backend-specific persistence, and durable session streams into your app.

@netscript/plugin-auth

JSR CI Docs

The authentication plugin for NetScript: one install wires a unified auth API, pluggable backends, backend-specific persistence, and durable session streams into your app.

Authentication is the feature every app needs and no team wants to rebuild — providers, sessions, cookies, schema, and the operational glue around all four. @netscript/plugin-auth ships it as one declarative manifest: netscript plugin install auth scaffolds the auth workspace, registers the auth-api service, provisions the selected backend's storage, and adds everything to your Aspire AppHost. Your app talks to one versioned auth API; which identity backend answers is a configuration choice, not a rewrite.

The manifest is plain data. Hosts read it to generate files and wiring; nothing executes until your app boots. The domain schemas, backend port, and v1 contract live in @netscript/plugin-auth-core — this package binds them to a NetScript host.

Why teams use it

  • One auth API, swappable backends — the auth-api service uses an Aspire-allocated endpoint and exposes signin, callback, signout, revokeSession, session, and me over a versioned v1 contract, backed by a single active backend selected via NETSCRIPT_AUTH_BACKEND: kv-oauth (interactive OAuth/OIDC), workos, or better-auth.
  • Stable user subjects — on kv-oauth, NETSCRIPT_AUTH_SUBJECT_SOURCE (id_token | userinfo) and NETSCRIPT_AUTH_SUBJECT_CLAIM choose where the subject comes from. A preset named by NETSCRIPT_AUTH_PROVIDER_ID supplies its default (GitHub: userinfo id, as github:<id>). A provider without a stable identifier refuses sign-in rather than issuing a new subject per sign-in.
  • Capability differences surface, not crash — operations a backend does not support return typed auth-provider errors at the API boundary instead of failing deep in a handler.
  • Backend-specific persistence — kv-oauth uses KV and needs no database or Prisma tables; workos contributes no SQL schema. Only better-auth installs the auth-owned Prisma schema and provisions auth tables alongside the rest of the database.
  • Durable session streams — the browser-safe ./streams subpath builds a durable-stream projection for the authSession entity, with server-side emit helpers on ./streams/server.
  • Provisioning recorded up front — installation resolves NETSCRIPT_AUTH_BACKEND before database planning. The default kv-oauth backend records KV without a SQL requirement; better-auth also records database wiring so netscript db and Aspire can provision it.
  • Typed bearer client opt-in — the manifest advertises a browser/server-safe bearer factory, and install emits auth/sdk-client.ts for explicit placement on the auth-api client only.

Architecture

flowchart LR
    M["authPlugin manifest"] --> H["NetScript host<br/>(plugin install + sync)"]
    H --> A["auth-api<br/>Aspire-allocated endpoint<br/>signin · callback · session · me · signout · revokeSession"]
    A --> B["Active backend<br/>(NETSCRIPT_AUTH_BACKEND)"]
    B --> K["kv-oauth"]
    B --> W["workos"]
    B --> BA["better-auth"]
    A --> S["Durable streams<br/>authSession projection"]
    A --> D["Auth schema<br/>(Postgres) + sessions (KV)"]

Install

From the root of a NetScript project:

netscript plugin install auth --name auth

The plugin owns its setup — the CLI ships no embedded templates. The scaffolder wires the auth API service, the selected backend's persistence, session streams, and Aspire resources into your workspace, then pins the matching @netscript/* versions.

By default Aspire allocates the auth-api host port at every start. An OAuth callback registered with an identity provider must keep its port, so pin it when you use the interactive kv-oauth backend:

netscript plugin install auth --name auth --port 7100

--port writes HostPort on the plugin's appsettings.json entry; netscript plugin update and a forced re-install without --port keep it. A pinned host port is a machine-global reservation, so aspire start --isolated cannot randomise it and two workspaces pinning the same port collide. See Add authentication.

To consume the plugin programmatically (custom hosts, tests, tooling), add it as a library:

deno add jsr:@netscript/plugin-auth@<version>

Pin <version> to match your installed CLI; bare jsr:@netscript/* specifiers do not resolve on the pre-release line.

Quick example

Install the plugin:

$ netscript plugin install auth --name auth
Installed auth plugin "auth". View its endpoint in the Aspire dashboard.
Created 1 plugin files.
Regenerated 12 Aspire helper files.

As a library, the manifest is inspectable data — verify the contributions it brings before the app boots its services:

import { inspectPlugin } from '@netscript/plugin';
import { authPlugin } from '@netscript/plugin-auth';

const inspection = inspectPlugin(authPlugin);

if (inspection.details.contributionGroups === 0) {
  throw new Error('auth service contribution is required');
}

console.log(
  inspection.target,
  inspection.details.version,
  inspection.details.contributionGroups,
);

The manifest reference makes the bearer factory discoverable; it does not activate credentials. The install-owned auth/sdk-client.ts exports authSdkClientContribution. Add it explicitly to the named auth service and supply its required context on authenticated calls:

import { createServiceClient } from '@netscript/sdk/client';
import { authContract } from '@netscript/plugin-auth-core/contracts/v1';
import { authSdkClientContribution } from './auth/sdk-client.ts';

declare const applicationSession: { accessToken: string; accountId: string };

const auth = createServiceClient({
  contract: authContract,
  serviceName: 'auth-api',
  routerName: 'auth',
  contributions: [authSdkClientContribution] as const,
});

await auth.session(undefined, {
  context: {
    auth: { getAccessToken: () => applicationSession.accessToken },
    // Stable and non-secret; never put a token, session id, or email in a cache key.
    authCachePartition: applicationSession.accountId,
  },
});

The generated module reads no environment variable, cookie, or browser storage. Credential lookup remains application-owned and per call; bearer headers default to HTTPS plus local loopback origins.

Public surface

Entry What it gives you
. authPlugin plus the AUTH_* identity and service constants
./services The auth API service composition (auth-api, Aspire-allocated port)
./streams Browser-safe durable-stream projection for the authSession entity
./streams/server Server-side session-stream emit helpers
./contracts The versioned auth API contract generated registries bind against
./scaffold The plugin-owned scaffolder netscript plugin install auth executes
./authenticator Remote session verifier, options, bearer reader and redacted failure vocabulary

The always-current symbol list is deno doc jsr:@netscript/plugin-auth@<version> (pin <version> on the pre-release line, as above).

Remote verification

@netscript/plugin-auth/authenticator re-exports the auth-core remote verifier without another implementation. Use createAuthServiceAuthenticator({ serviceName, timeoutMs }) with native createService(...).withAuthn(...). The consuming service needs discovery wiring to the auth service, not its provider secrets or backend storage. See the auth-core verification guide for denial versus unavailability, transport policy and backend limits.

Transport configuration

For kv-oauth behind a TLS-terminating proxy, set NETSCRIPT_AUTH_TRUST_PROXY_HEADERS=true. Default is false. The proxy must replace client-supplied X-Forwarded-Proto / Forwarded headers and block direct service access. This configures one HTTPS policy for OAuth flow checks and Secure cookies; outbound provider endpoints still require HTTPS. Trusted hop/CIDR verification is tracked in #2191.

For direct TLS at the auth service, set NETSCRIPT_AUTH_COOKIE_SECURE=true. Sign-in and callback can inspect their HTTPS request URL, but session refresh receives a URL-less AuthnRequest; the explicit setting ensures refreshed cookies remain Secure. Unset preserves protocol derivation; false explicitly disables Secure (and is refused for __Host- cookies unless local development is enabled). This cookie override does not bypass the inbound HTTPS flow gate. Automatic host TLS metadata and trusted-hop verification remain tracked in #2191.

For explicit local HTTP development, set NETSCRIPT_AUTH_ALLOW_INSECURE_HTTP_REQUESTS=true. It opens the inbound flow and development cookie gates. The separate NETSCRIPT_AUTH_ALLOW_INSECURE_REQUESTS=true only allows outbound HTTP OAuth discovery/token endpoints. All three switches default to false. Missing or partial provider configuration never silently enables insecure transport, including the unconfigured local placeholder.

Migration: replace the old outbound switch with the new inbound switch for HTTP development; use proxy trust for production TLS termination. Transport refusals return AUTH_TRANSPORT_ERROR (400), and backend configuration refusals return AUTH_CONFIGURATION_ERROR (400), with the gate named in the reason. Missing service request capture is an internal wiring failure (INTERNAL, 500). Actual upstream failures remain AUTH_PROVIDER_ERROR (502).

Docs

Compatibility

The auth API service requires Deno 2.9+ and Deno KV for session-related state. Only the better-auth backend also needs a SQL database and its auth schema, provisioned through netscript db and Aspire. The manifest, typed SDK contribution reference, generated bearer starter, and browser-safe ./streams subpath are importable in any TypeScript environment.

License

Apache-2.0 — see LICENSE. Published to JSR with cryptographically verified provenance.

Built and signed on
GitHub Actions

Add Package

deno add jsr:@netscript/plugin-auth@0.0.10

Import symbol

import * as plugin_auth from "@netscript/plugin-auth";
or

Import directly with a jsr specifier

import * as plugin_auth from "jsr:@netscript/plugin-auth@0.0.10";