@netscript/plugin-auth-core@0.0.10Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
The reusable auth core for NetScript: domain and session-stream schemas, Zod config, the AuthBackendPort adapter seam, and the versioned auth API contract.
@netscript/plugin-auth-core
The reusable auth core for NetScript: domain and session-stream schemas, Zod config, the
AuthBackendPort adapter seam, and the versioned auth API contract.
Swapping identity providers should be a configuration change, not a rewrite — which means the
contract between "your app's auth API" and "whatever backend answers it" has to live somewhere
neutral. This package is that place. AuthBackendPort composes provider registry, session store,
token crypto, and principal mapping into one seam every backend adapter implements;
AuthConfigSchema normalizes app settings into a defaulted, secure configuration; and
authContract defines the signin, callback, session, me, signout, and revokeSession routes the auth service serves
and typed clients call.
This is the contract surface every auth backend implements and every service host wires; the
deployable @netscript/plugin-auth plugin binds it to a
NetScript host.
Why teams use it
- One port, many backends —
AuthBackendPortcomposes provider registry, session store, token crypto, and principal-mapping sub-ports, so kv-oauth, WorkOS, and better-auth adapters all implement one stable contract. - Single-active-backend registry —
createAuthBackendRegistryandresolveBackendselect one backend per composition root, with typedAuthBackendNotFoundErrorandAuthBackendOperationUnsupportedErrorboundaries for what a backend cannot do. - Secure defaults out of the box —
AuthConfigSchema,AuthSessionPolicySchema, andAuthProviderConfigSchemanormalize settings into a defaultedAuthConfig(secure__Host-cookies, TTL, refresh window). - A versioned API contract —
authContract/authContractV1define the signin, callback, session, me, signout, and revokeSession routes with explicit authentication metadata, so services and clients share one typed source of truth. - Typed client credentials —
createBearerSdkClientContributionresolves credentials from explicit per-call context and contributes only the declaredauthorizationheader. - Observable and streamable —
authStreamSchemaprojectsauth.*session events into durable streams, andcreateAuthTelemetryplusredactAuthPrincipalemit redacted spans, keeping principals out of your telemetry backend.
Endpoint rate limits
AuthConfigSchema accepts optional provider-native endpoint quotas:
import { AuthConfigSchema } from '@netscript/plugin-auth-core/config'; const config = AuthConfigSchema.parse({ backend: 'better-auth', rateLimit: { customRules: { '/sign-in/*': { window: 60, max: 10 } } }, });
window is seconds and max is requests; both must be positive integers. Pattern keys start with
/ and are relative to the provider's auth base path. The auth plugin reads this section from
auth.rateLimit or Auth.RateLimit appsettings and forwards it as better-auth
rateLimit.customRules. Omitting it preserves provider defaults: production sign-in allows three
requests per ten seconds per client IP and path. These endpoint rules take precedence over the
provider's global window/max. Configuration does not enable rate limiting outside production or
change the provider's default in-memory, per-process storage.
Architecture
flowchart LR C["authContract v1<br/>signin · callback · session · me · signout · revokeSession"] --> S["Auth service host"] S --> RG["createAuthBackendRegistry<br/>(single active backend)"] RG --> P["AuthBackendPort<br/>providers · sessions · crypto · principals"] P --> B1["kv-oauth adapter"] P --> B2["workos adapter"] P --> B3["better-auth adapter"] S --> ST["authStreamSchema<br/>auth.* session events"]
Install
deno add jsr:@netscript/plugin-auth-core@<version>
Pin <version> to match your installed CLI; bare jsr:@netscript/* specifiers do not resolve on
the pre-release line.
Quick example
import { AuthConfigSchema, createAuthBackendRegistry } from '@netscript/plugin-auth-core'; import type { AuthBackendPort } from '@netscript/plugin-auth-core'; // A backend adapter (kv-oauth, better-auth, WorkOS, ...) implements AuthBackendPort. declare const kvOAuthBackend: AuthBackendPort; // Parse app settings into a normalized, defaulted auth config. const config = AuthConfigSchema.parse({ backend: 'kv-oauth', session: { cookieName: '__Host-netscript-auth', sameSite: 'lax' }, }); // Register backends and resolve the single active one at the composition root. const registry = createAuthBackendRegistry( new Map([[config.backend, kvOAuthBackend]]), config.backend, ); // Service hosts authenticate requests through the resolved backend port. const backend = registry.resolveBackend(); const session = await backend.sessions.getSession({ token: 'opaque-session-token' });
Public surface
| Entry | What it gives you |
|---|---|
. |
The backend registry, config schemas, error classes, and contract handles |
./ports |
AuthBackendPort and its provider / session / crypto / principal sub-ports |
./domain |
Account, session, and user schemas plus the state vocabularies |
./config |
AuthConfigSchema and the session/provider policy schemas |
./contracts/v1 |
authContract / authContractV1 — the versioned auth API routes |
./sdk |
Typed bearer credential contribution for NetScript service clients |
./streams |
authStreamSchema and the auth.* session-event types |
./telemetry |
createAuthTelemetry, span names, and redactAuthPrincipal |
./presets |
Provider and backend preset registry |
./testing |
Fixtures for exercising backends and contracts in tests |
./authenticator |
Remote session verifier, options, bearer reader and redacted failure vocabulary |
The always-current symbol list is
deno doc jsr:@netscript/plugin-auth-core@<version>
(pin <version> on the pre-release line, as above).
Bearer credentials for service clients
Import createBearerSdkClientContribution from @netscript/plugin-auth-core/sdk, then attach the
returned descriptor explicitly to the intended service client's contributions tuple. The resolver
receives only the contribution's declared context projection. The module never reads environment
variables, cookies, browser storage, or another ambient credential source.
Choose responseCache: { mode: 'direct-only' } when authenticated responses must not enter generated
query caches. For partitioned caching, return a stable, non-secret tenant or account partition. Never
use a bearer token, session identifier, email address, or another reversible credential-derived value
as the partition because cache keys may appear in diagnostics and developer tools.
Credentials are sent over HTTPS and local-development loopback origins by default. Enabling
allowInsecureTransport for another cleartext origin is an explicit security exception and should be
limited to controlled development environments.
Verify sessions in another service
The server-side ./authenticator leaf returns the native service AuthenticatorPort. Supply the
actual Aspire discovery resource name and an explicit timeout at your composition root:
import { createService } from '@netscript/service'; import { createAuthServiceAuthenticator } from '@netscript/plugin-auth-core/authenticator'; declare const authServiceName: string; const app = createService({}, { name: 'protected-api' }) .withAuthn({ authenticator: createAuthServiceAuthenticator({ serviceName: authServiceName, timeoutMs: 10_000, }), }) .build();
The discovery name must match the generated resource reference, which supplies
services__<name>__http__0 (or the HTTPS equivalent). The example timeout is application policy;
the factory requires an integer from 1 through 2,147,483,647 milliseconds. routerName defaults to
auth; protocol follows SDK discovery defaults. No provider secret or backend instance belongs
in the consuming service.
Each request verifies its bearer through the native typed SDK. Missing or malformed bearer is rejected before discovery. Inactive, revoked and expired sessions are rejected. Verification has no principal or response cache, so revocation is checked again on the next request. The principal preserves the auth service's subject, scopes, roles and claims; claims may contain sensitive native session metadata and must not be logged wholesale.
A defined remote UNAUTHORIZED response is credential denial. Discovery, transport, timeout,
provider and malformed-response failures throw RemoteSessionVerificationError with a fixed
message and bounded diagnostics, never the raw response or credential. Native service middleware
maps denial to 401 and verifier failure to a redacted 503. Discovery failures use the transport
code because the SDK exposes no typed discovery discriminator.
The verifier does not forward cookies. On the auth service itself, KV-OAuth resolves explicit
session ID before bearer before cookie; WorkOS resolves bearer before cookie. Better-auth retains
its own request-header behavior, so this factory does not promise bearer support for every provider.
HTTPS and loopback HTTP are accepted by default; other cleartext transport requires explicit
allowInsecureTransport. This API does not add authorization rules; the auth service enforces
signout ownership itself.
Docs
- Auth core reference — ports, schemas, and contract: rickylabs.github.io/netscript/reference/plugin-auth-core/
- Identity & Access — the full authentication story: rickylabs.github.io/netscript/identity-access/
- API docs on JSR: jsr.io/@netscript/plugin-auth-core/doc
Compatibility
Schemas, ports, and the contract are plain TypeScript — importable in any TypeScript environment. Concrete backend adapters and the service host that wires them target Deno 2.9+.
License
Apache-2.0 — see LICENSE. Published to JSR with cryptographically verified provenance.
Examples
Use the contract-only root surface
Use the contract-only root surface
import { AUTH_SESSION_STATES, AuthConfigSchema } from "@netscript/plugin-auth-core"; const config = AuthConfigSchema.parse({ backend: "kv-oauth" }); console.log(config.backend, AUTH_SESSION_STATES.active);
Add Package
deno add jsr:@netscript/plugin-auth-core
Import symbol
import * as plugin_auth_core from "@netscript/plugin-auth-core";
Import directly with a jsr specifier
import * as plugin_auth_core from "jsr:@netscript/plugin-auth-core@^0.0.10";