authtest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package authtest tests code protected by the auth package without real tokens, keys or network: the Go side of Powertools Python's mock_claims.

A Verifier answers every token with the same claims. Everything around the verification still runs: Require still extracts the bearer token and checks the scopes, the Authorize functions still build their policies.

verifier := authtest.NewVerifier(map[string]any{"sub": "user-1", "scope": "orders:read"})
protect, _ := auth.Require(verifier, auth.WithScopes("orders:read"))

It bypasses every security check, so use it in tests only, and keep separate tests for the token profiles the application accepts.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Verifier

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

Verifier is an auth.Verifier that trusts every token.

func NewVerifier

func NewVerifier(claims any) *Verifier

NewVerifier returns a Verifier whose Verify returns claims, a map or a struct that marshals to a JSON object. It panics otherwise, as befits a test helper given a constant.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/alessandrolattao/powertools-lambda-go/auth"
	"github.com/alessandrolattao/powertools-lambda-go/auth/authtest"
)

func main() {
	verifier := authtest.NewVerifier(map[string]any{"sub": "test-user", "scope": "orders:read"})
	protect, err := auth.Require(verifier, auth.WithScopes("orders:read"))
	if err != nil {
		panic(err)
	}
	handler := protect(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		claims, _ := auth.ClaimsFromContext(r.Context())
		w.Header().Set("X-Subject", claims.Subject())
	}))

	r := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/orders", http.NoBody)
	r.Header.Set("Authorization", "Bearer any-token")
	w := httptest.NewRecorder()
	handler.ServeHTTP(w, r)
	fmt.Println(w.Code, w.Header().Get("X-Subject"))
}
Output:
200 test-user

func (*Verifier) Prefetch

func (*Verifier) Prefetch(context.Context) error

Prefetch does nothing: there are no keys to fetch.

func (*Verifier) Verify

func (v *Verifier) Verify(context.Context, string) (*auth.Claims, error)

Verify returns a fresh copy of the claims, whatever the token.

Jump to

Keyboard shortcuts

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