sts

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sts-go

Authenticate Connect RPC requests with Google ID tokens, Convex JWTs, or existing STS keys verified by https://sts.altipla.consulting. Authenticate Connect clients with Google ID tokens automatically.

Install

go get github.com/altipla-consulting/sts-go

Usage

The module exports package sts. Configure authentication at application startup, then register an interceptor on your server:

import (
	"os"

	"connectrpc.com/connect/v2"
	"github.com/altipla-consulting/sts-go"
)

sts.Configure(sts.Config{
	VerifierToken: os.Getenv("STS_VERIFIER_TOKEN"),
	Audience:      "https://documents.example.com",
	Actors: []sts.ActorRule{
		sts.GoogleSubject("123456789012345678901", "documents.read", "documents.delete"),
		sts.ConvexApp("gentle-fox-123", "documents.read"),
	},
})

server := connect.NewServer(sts.Require("documents.read"))

Register your generated Connect handlers on server. Clients must send Authorization: Bearer <token>. The interceptor authenticates unary and streaming RPCs and requires every permission passed to sts.Require before calling the handler. Use sts.Authenticate() to authenticate requests without requiring specific permissions.

sts.Configure must run before creating sts.Require or sts.Authenticate interceptors. Interceptors share public key caches and retain the configuration they were created with; public keys are fetched on demand. Configuring actor rules without an audience panics, as does passing an empty permission to sts.Require.

Accepted credentials
  • sts.GoogleSubject(subject, permissions...) accepts a Google service account or user by its stable sub claim, not its email address. Google tokens must use the issuer https://accounts.google.com and an RS256 signature.
  • sts.ConvexApp(deployment, permissions...) accepts every subject signed by the named deployment. Pass the deployment name, such as gentle-fox-123, rather than its URL. Tokens must use the issuer https://<deployment>.convex.cloud and an ES256 signature.
  • VerifierToken enables verification of existing opaque STS keys. Their permissions come from the STS service. Leave it empty to disable legacy keys; omit Audience and Actors if you only accept STS keys.

JWT signatures, expiration, issuer, and audience are verified before actor rules grant permissions. Set Audience to the receiving service's identifier and use the same value when obtaining tokens. Permissions from all matching actor rules are additive. A valid JWT whose subject matches no rule returns connect.CodePermissionDenied; invalid JWTs or untrusted issuers return connect.CodeUnauthenticated.

Google clients

Use sts.GoogleIDToken to send Google ID tokens through connect-oauth2 using application default credentials and refresh them as needed. The credentials must support ID token generation, and their Google subject must be allowed by the server. For a Connect v2 client:

import (
	"net/http"

	"connectrpc.com/connect/v2"
	"connectrpc.com/connect/v2/connecthttp"
	"github.com/altipla-consulting/sts-go"
)

func newClient() *connect.Client {
	transport := connecthttp.NewTransport(http.DefaultClient, "https://documents.example.com")
	return connect.NewClient(transport, sts.GoogleIDToken("https://documents.example.com"))
}

Pass the resulting Connect client to your generated service client constructor. The token audience identifies the receiving service independently of the request path; it must match the server's configured Audience.

Permissions inside handlers

Register sts.Authenticate() after configuration to authenticate every method, then use sts.RequirePermissions inside methods that need specific permissions:

server := connect.NewServer(sts.Authenticate())

For example, in a generated Connect handler implementation:

func (s *DocumentsService) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.DeleteResponse, error) {
	if err := sts.RequirePermissions(ctx, "documents.delete"); err != nil {
		return nil, errors.Trace(err)
	}

	// Delete the document.
	return &pb.DeleteResponse{}, nil
}

Use your generated service and protobuf types in place of the example types above. The handler example uses context and github.com/altipla-consulting/errors. Pass the handler's context to the helper; the interceptor stores verified permissions there. Every supplied permission is required. Missing permissions return connect.CodePermissionDenied; calling the helper without the authentication interceptor returns missing authentication interceptor, even when no permissions are supplied.

You can also keep service-wide permissions in sts.Require and require additional permissions inside individual handlers. Both checks return connect.CodePermissionDenied for missing permissions. Locally, a request authenticated with local-token skips both checks.

Verified actors

For requests authenticated with a JWT, retrieve the verified issuer and subject inside a handler with:

actor := sts.ActorFromContext(ctx)

actor.Issuer and actor.Subject identify the caller. sts.ActorFromContext panics when no verified JWT actor exists, including requests authenticated with an STS key or local-token. Use it only in handlers that expect JWT authentication.

Local development

Local environments are detected automatically using env.IsProduction(). Outside production, interceptors accept only Authorization: Bearer local-token and skip remote verification and permission checks. sts.GoogleIDToken sends this token automatically without loading Google credentials. Configuration is still required before creating sts.Require or sts.Authenticate interceptors, and actor rules still require an audience.

Migrating from NewInterceptor

sts.NewInterceptor(verifierToken, permissions...) remains available for STS keys but is deprecated. Configure legacy key verification at startup:

sts.Configure(sts.Config{VerifierToken: verifierToken})

Then use sts.Require("documents.read") to require permissions or sts.Authenticate() when only authentication is needed. Add Audience and Actors to accept JWTs alongside existing keys.

Contributing

You can make pull requests or create issues in GitHub. Any code you send should be formatted using make gofmt.

License

MIT License

Documentation

Overview

Package sts authenticates clients and Connect RPC handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authenticate added in v0.2.0

func Authenticate() connect.ServerInterceptor

Authenticate authenticates requests without requiring specific permissions. Call Configure first, and use RequirePermissions in handlers as needed.

func Configure added in v0.2.0

func Configure(config Config)

Configure sets application-wide authentication before interceptors are created. Interceptors share public key caches and retain the configuration they were created with. Invalid configuration panics; public keys are fetched on demand.

func GoogleIDToken added in v0.3.1

func GoogleIDToken(audience string) connect.ClientInterceptor

GoogleIDToken sends Google ID tokens using the application's default credentials and refreshes them as needed. Set audience to the receiving service's identifier, independently of the request path. Outside production, it sends local-token instead.

func NewInterceptor

func NewInterceptor(verifierToken string, permissions ...string) connect.ServerInterceptor

NewInterceptor authenticates existing STS keys without app-wide configuration. Deprecated: use Configure followed by Require or Authenticate instead.

func Require added in v0.2.0

func Require(permission string, permissions ...string) connect.ServerInterceptor

Require authenticates requests and requires every listed permission. Call Configure before creating interceptors. Empty permissions panic.

func RequirePermissions

func RequirePermissions(ctx context.Context, permissions ...string) error

RequirePermissions requires an authenticated context and every listed permission. Permission checks are skipped for requests authenticated with local-token in local environments.

Types

type Actor added in v0.2.0

type Actor struct {
	Issuer  string
	Subject string
}

Actor identifies the verified JWT caller by issuer and subject.

func ActorFromContext added in v0.2.0

func ActorFromContext(ctx context.Context) Actor

ActorFromContext returns the verified JWT actor. It panics if the context has no actor.

type ActorRule added in v0.2.0

type ActorRule struct {
	// contains filtered or unexported fields
}

ActorRule grants permissions to a Google account or a Convex deployment. Construct rules with GoogleSubject, GoogleServiceAccount, or ConvexApp. Matching grants are additive.

func ConvexApp added in v0.2.0

func ConvexApp(issuer string, permissions ...string) ActorRule

ConvexApp accepts every subject signed by the configured Convex app. Pass its full HTTPS issuer URL, including custom domains or regional hosts. Deployment names such as gentle-fox-123 expand to https://gentle-fox-123.convex.cloud.

func GoogleServiceAccount added in v0.2.1

func GoogleServiceAccount(email string, permissions ...string) ActorRule

GoogleServiceAccount accepts a service account by its verified email claim. Tokens must include email and email_verified. A recreated account with the same email is also accepted; use GoogleSubject to bind to a specific account ID.

func GoogleSubject added in v0.2.0

func GoogleSubject(subject string, permissions ...string) ActorRule

GoogleSubject accepts the stable Google sub claim of a service account or user. Pass the account's stable subject ID.

type Config added in v0.2.0

type Config struct {
	VerifierToken string
	Audience      string
	Actors        []ActorRule
}

Config defines the credentials and actors accepted by the application. Audience must match the JWT audience and is required when Actors is nonempty. VerifierToken enables remote STS verification for existing opaque keys. Locally, only local-token is accepted and permission checks are skipped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址