qerexa

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 9 Imported by: ? 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

Qerexa Go SDK

Preview SDK for low-risk direct gate decisions. It is not a certified financial-execution adapter. Review approval never resumes an action: Gate returns ExecutionAuthorizationRequiredError and the caller must stop or use a separate action-bound execution contract. A direct unexpired allow is the only successful Gate result. Metadata is still sent to the API; do not put raw credentials or customer payloads in it. The client no longer duplicates metadata into reasoning.

Local validation

cd apps/sdk/go/qerexa && go test ./...

Quickstart

package main

import (
	"context"
	"log"

	"github.com/rodriguew/qerexa-go"
)

func main() {
	client, err := qerexa.NewClient(&qerexa.ClientConfig{
		APIKey: "sk_live_...", // or set QEREXA_API_KEY
	})
	if err != nil {
		log.Fatal(err)
	}

	// Gate an action — raises GateBlockedError if policy blocks
result, err := client.Gate(context.Background(), "records.read", "public-catalog", "", nil, true)
	if err != nil {
		if gbe, ok := err.(*qerexa.GateBlockedError); ok {
			log.Fatalf("Gate blocked: %v", gbe.Reason)
		}
		log.Fatal(err)
	}

	// Action is allowed, proceed
	log.Printf("Trust score: %v\n", result.TrustScore)
	// Continue with this low-risk read only.
}

Gate a function

Wrap a function with gate enforcement using GatedFunc:

import "github.com/rodriguew/qerexa-go"

client, _ := qerexa.NewClient(&qerexa.ClientConfig{APIKey: "sk_live_..."})

// This only executes if Qerexa allows it
result, err := qerexa.GatedFunc(client, "records.read", func() (interface{}, error) {
	return "public record", nil
})
if err != nil {
	log.Fatal(err)
}

Gate an HTTP handler

Wrap HTTP handlers with middleware for automatic gate enforcement:

import (
	"net/http"
	"github.com/rodriguew/qerexa-go"
)

client, _ := qerexa.NewClient(&qerexa.ClientConfig{APIKey: "sk_live_..."})

mux := http.NewServeMux()

// Wrap the handler with gate enforcement
readHandler := func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("Public record"))
}

middleware := qerexa.HTTPMiddleware(client, &qerexa.GatedOptions{
	Action: "records.read",
})

mux.Handle("/records", middleware(http.HandlerFunc(readHandler)))

Let the handler reason about a block

The middleware can invoke an OnBlock callback to handle rejections gracefully:

middleware := qerexa.HTTPMiddleware(client, &qerexa.GatedOptions{
	Action: "records.read",
	OnBlock: func(err *qerexa.GateBlockedError) interface{} {
		return "Action blocked by policy: " + err.Reason + ". Contact support."
	},
})

Human-in-the-loop approval

When a Qerexa policy marks an action as requiring approval, the gate holds it and notifies the approver. The call waits until a decision is made:

client, _ := qerexa.NewClient(&qerexa.ClientConfig{
	APIKey:          "sk_live_...",
	ApprovalTimeout: 600000, // 10 minutes in milliseconds
	PollInterval:    3000,   // Check every 3 seconds
})

// Review approval returns ExecutionAuthorizationRequiredError, not success.
result, err := client.Gate(context.Background(), "records.read", "public-catalog", "", nil, true)
if err != nil {
	log.Fatal(err)
}

Set waitForApproval: false to fail fast instead:

_, err := client.Gate(ctx, "production.deploy", "service", "", nil, false)
if err != nil {
	if _, ok := err.(*qerexa.ApprovalRequiredError); ok {
		// Approval needed but not waiting
	}
}

Configuration

Setting Env var Default
API key QEREXA_API_KEY required
Base URL QEREXA_BASE_URL https://api.qerexa.com
Timeout n/a 10000ms
Poll interval n/a 2000ms
Approval timeout n/a 300000ms (5 min)
client, _ := qerexa.NewClient(&qerexa.ClientConfig{
	APIKey:          "sk_live_...",
	BaseURL:         "http://your-api.com",
	Timeout:         10000,      // milliseconds
	PollInterval:    2000,       // milliseconds
	ApprovalTimeout: 600000,     // milliseconds
})

API

Client
type Client struct {}

// Create a new client
client, err := NewClient(config *ClientConfig) (*Client, error)

// Evaluate without throwing on block
result, err := client.Evaluate(ctx context.Context, action string, target string, actor string, metadata map[string]interface{}) (*GateResult, error)

// Evaluate and throw on block
result, err := client.Gate(ctx context.Context, action string, target string, actor string, metadata map[string]interface{}, waitForApproval bool) (*GateResult, error)
GatedFunc
// Wrap a function with gate enforcement
result, err := GatedFunc(client *Client, action string, fn func() (interface{}, error)) (interface{}, error)

// With context and metadata
result, err := GatedFuncWithContext(
	ctx context.Context,
	client *Client,
	action string,
	target string,
	metadata map[string]interface{},
	fn func() (interface{}, error),
) (interface{}, error)
HTTPMiddleware
// Create HTTP middleware
middleware := HTTPMiddleware(client *Client, opts *GatedOptions) func(http.Handler) http.Handler
Exceptions
  • GateBlockedError — Action was explicitly blocked by policy
  • ApprovalRequiredError — Action needs approval and none was granted in time
  • ExecutionAuthorizationRequiredError — Review approved, but execution is not authorized
  • QerexaError — Network or configuration error
result, err := client.Gate(ctx, action, target, actor, metadata, waitForApproval)
if err != nil {
	switch e := err.(type) {
	case *qerexa.GateBlockedError:
		log.Printf("Blocked: %s", e.Reason)
	case *qerexa.ApprovalRequiredError:
		log.Printf("Approval needed: flow %s", e.FlowID)
	case *qerexa.QerexaError:
		log.Printf("Error: %s", e.Message)
	}
}

Testing

Run unit tests:

go test -v ./...

Why gate actions?

Your IAM knows the service is authenticated. Your infrastructure runs what it's told. Nothing in between enforces whether a specific action, against a specific target, at a specific moment, is authorized — with proof. A service can fire a thousand actions a second. Qerexa is the enforcement layer in that gap.

This preview does not verify receipts or bind approval to execution. Use the documented API proof flow and an independently reviewed execution contract for high-impact workloads.

Releases

This repository receives one snapshot commit per release from Qerexa's development repository; pull requests are not merged here directly. Please report issues in this repository.

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GatedFunc

func GatedFunc(client *Client, action string, fn func() (interface{}, error)) (interface{}, error)

GatedFunc wraps a function with gate enforcement.

Example

ExampleGatedFunc demonstrates the GatedFunc wrapper.

client, err := NewClient(&ClientConfig{
	APIKey: "sk_live_...",
})
if err != nil {
	log.Fatal(err)
}

// Wrap a low-risk function with gate enforcement.
result, err := GatedFunc(client, "records.read", func() (interface{}, error) {
	return "public record", nil
})
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Result: %v\n", result)

func GatedFuncWithContext

func GatedFuncWithContext(ctx context.Context, client *Client, action string, target string, metadata map[string]interface{}, fn func() (interface{}, error)) (interface{}, error)

GatedFuncWithContext wraps a function with gate enforcement using context.

func HTTPMiddleware

func HTTPMiddleware(client *Client, opts *GatedOptions) func(http.Handler) http.Handler

HTTPMiddleware wraps an HTTP handler with gate enforcement.

Types

type ApprovalRequiredError

type ApprovalRequiredError struct {
	Action     string
	FlowID     string
	DecisionID string
}

ApprovalRequiredError is raised when human approval is needed but not obtained.

func (*ApprovalRequiredError) Error

func (e *ApprovalRequiredError) Error() string

type Client

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

Client is the Qerexa gate client.

Example

ExampleClient demonstrates basic gate evaluation.

client, err := NewClient(&ClientConfig{
	APIKey: "sk_live_...",
})
if err != nil {
	log.Fatal(err)
}

// Evaluate without throwing on block
result, err := client.Evaluate(context.Background(), "database.write", "users", "", nil)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Decision: %v\n", result.Decision)
fmt.Printf("Reason: %s\n", result.Reason)

func NewClient

func NewClient(config *ClientConfig) (*Client, error)

NewClient creates a new Qerexa client.

func (*Client) Evaluate

func (c *Client) Evaluate(ctx context.Context, action string, target string, actor string, metadata map[string]interface{}) (*GateResult, error)

Evaluate makes a gate evaluation request without throwing on block.

func (*Client) Gate

func (c *Client) Gate(ctx context.Context, action string, target string, actor string, metadata map[string]interface{}, waitForApproval bool) (*GateResult, error)

Gate evaluates and enforces a gate, throwing on block.

func (*Client) WaitForApproval

func (c *Client) WaitForApproval(ctx context.Context, flowID string) (string, error)

WaitForApproval reconnects to a durable flow and waits until it reaches a terminal state.

type ClientConfig

type ClientConfig struct {
	APIKey          string
	BaseURL         string
	Timeout         int // milliseconds
	PollInterval    int // milliseconds
	ApprovalTimeout int // milliseconds
}

ClientConfig holds configuration for QerexaClient.

type ExecutionAuthorizationRequiredError

type ExecutionAuthorizationRequiredError struct {
	Action string
	FlowID string
}

ExecutionAuthorizationRequiredError prevents a review approval from being mistaken for permission to execute the protected action.

func (*ExecutionAuthorizationRequiredError) Error

type GateBlockedError

type GateBlockedError struct {
	Action     string
	Reason     string
	DecisionID string
}

GateBlockedError is raised when a gate blocks an action.

func (*GateBlockedError) Error

func (e *GateBlockedError) Error() string

type GateDecision

type GateDecision string

GateDecision represents the outcome of a gate evaluation.

const (
	Allowed          GateDecision = "allowed"
	Blocked          GateDecision = "blocked"
	ApprovalRequired GateDecision = "approval_required"
)

type GateHandler

type GateHandler func(ctx context.Context) (interface{}, error)

GateHandler is a function type that can be gated.

func Gate

func Gate(client *Client, opts *GatedOptions, handler GateHandler) GateHandler

Gate wraps a handler function with gate enforcement.

Example

ExampleGate demonstrates gate enforcement that throws on block.

client, err := NewClient(&ClientConfig{
	APIKey: "sk_live_...",
})
if err != nil {
	log.Fatal(err)
}

// Gate will raise GateBlockedError if policy denies
result, err := client.Gate(context.Background(), "records.read", "public-catalog", "", nil, true)
if err != nil {
	if gbe, ok := err.(*GateBlockedError); ok {
		fmt.Printf("Gate blocked: %v\n", gbe.Reason)
	} else if are, ok := err.(*ApprovalRequiredError); ok {
		fmt.Printf("Approval required: flow %s\n", are.FlowID)
	} else {
		log.Fatal(err)
	}
	return
}

fmt.Printf("Action allowed, trust score: %v\n", result.TrustScore)

type GateResult

type GateResult struct {
	Decision       GateDecision           `json:"decision"`
	DecisionID     string                 `json:"decision_id,omitempty"`
	Reason         string                 `json:"reason,omitempty"`
	FlowID         string                 `json:"flow_id,omitempty"`
	ReviewDeadline string                 `json:"review_deadline,omitempty"`
	RiskScore      *float64               `json:"risk_score,omitempty"`
	TrustScore     *float64               `json:"trust_score,omitempty"`
	Raw            map[string]interface{} `json:"raw"`
}

GateResult contains the result of a gate evaluation.

type GatedOptions

type GatedOptions struct {
	Action          string
	Client          *Client
	Target          string
	WaitForApproval bool
	OnBlock         func(error *GateBlockedError) interface{}
}

GatedOptions configures gate behavior for a function or handler.

type QerexaError

type QerexaError struct {
	Message string
}

QerexaError is the base error type for all Qerexa errors.

func (*QerexaError) Error

func (e *QerexaError) Error() string

Jump to

Keyboard shortcuts

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