Golem add http auth ts

From golemcloud/golem · View interactive version

Golem add http auth ts is an AI agent skill from the GitHub repository golemcloud/golem. Enabling authentication on TypeScript HTTP endpoints. Use when the user asks to add auth, require authentication, use Principal, or protect HTTP endpoints.

How to use this skill

Skill file

Enabling Authentication on TypeScript HTTP Endpoints

Overview

Golem supports authentication on HTTP endpoints via OIDC providers. Authentication is enabled in the agent code and configured via security schemes in golem.yaml. Load the golem-configure-api-domain skill for details on setting up security schemes and domain deployments, including when to use subdomain versus domain.

Enabling Auth on All Endpoints (Mount Level)

Set auth: true in the mount options of http.mount(...) to require authentication for all endpoints:

import { z } from 'zod';
import { defineAgent, method, http } from '@golemcloud/golem-ts-sdk';

export const SecureAgent = defineAgent({
  name: 'SecureAgent',
  id: { name: z.string() },
  http: http.mount('/secure/{name}', { auth: true }),
  methods: {
    // All endpoints require authentication
  },
});

Enabling Auth on Individual Endpoints

Set auth: true in the endpoint options (the second argument to a verb builder):

export const ApiAgent = defineAgent({
  name: 'ApiAgent',
  id: { name: z.string() },
  http: http.mount('/api/{name}'),
  methods: {
    publicData: method({ input: {}, returns: z.string(), http: http.get('/public') }),
    privateData: method({ input: {}, returns: z.string(), http: http.get('/private', { auth: true }) }),
  },
});

Overriding Mount-Level Auth

Per-endpoint auth overrides the mount-level setting:

export const MostlySecureAgent = defineAgent({
  name: 'MostlySecureAgent',
  id: { name: z.string() },
  http: http.mount('/api/{name}', { auth: true }),
  methods: {
    // No auth required — overrides the mount default
    health: method({ input: {}, returns: z.string(), http: http.get('/health', { auth: false }) }),
    // Auth required (inherited from the mount)
    getData: method({ input: {}, returns: Data, http: http.get('/data') }),
  },
});

Receiving the Principal

When auth is enabled, read the authenticated user's Principal either from the handler's this via this.getPrincipal() (the init-time principal), or by declaring a bare s.principal() method parameter — which is auto-injected with the per-call caller principal. Either way the Principal is host-supplied: an auto-injected parameter consumes no wire field and is never mapped to path/query/header variables:

import { z } from 'zod';
import { defineAgent, method, http, s } from '@golemcloud/golem-ts-sdk';

export const ApiAgent = defineAgent({
  name: 'ApiAgent',
  id: { name: z.string() },
  http: http.mount('/api/{name}', { auth: true }),
  methods: {
    whoAmI: method({ input: { caller: s.principal() }, returns: z.object({ kind: z.string() }), http: http.get('/whoami') }),
    getData: method({ input: { id: z.string(), caller: s.principal() }, returns: Data, http: http.get('/data/{id}') }),
  },
});

export const ApiAgentImpl = ApiAgent.implement({
  init: () => ({}),
  methods: {
    whoAmI({ caller }) {
      return { kind: caller.tag };
    },
    getData({ id, caller }) {
      // ... authorize caller and return Data for id ...
    },
  },
});

this.getPrincipal() returns the initialization principal, also available in init via the InitContext argument: init: (ctx) => ({ caller: ctx.principal }). It is not necessarily the caller of a later invocation. To authorize each call, declare a bare s.principal() method input; the host injects the current invocation principal without a caller wire field. See golem-http-params-ts. this.getId(), which returns the agent's AgentId, is also available on every handler's this.

Deployment Configuration

After enabling auth: true in code, you must configure a security scheme in golem.yaml. Load the golem-configure-api-domain skill for the full details, including when to use subdomain versus domain. Quick reference:

httpApi:
  deployments:
    local:
    - subdomain: my-app  # resolves to my-app.localhost:9006 by default
      agents:
        SecureAgent:
          securityScheme: my-oidc            # For production OIDC
        # or for development:
        # SecureAgent:
        #   testSessionHeaderName: X-Test-Auth
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.