verifyv1

import verifyv1 "pocketone.dev/ext/contracts/api-tokens/verify/v1"

Package verifyv1 is the api-tokens.verify/v1 contract: another server checks an app's API token. A host in front of its apps (a PocketHost edge in front of the mothership's accounts) is handed a token by a client and asks the app that issued it who the token acts as and what it may do. Whatever provides api-tokens/v1 provides it too.

A wire contract at /api/_c/api-tokens.verify/v1, for superusers only: it answers for any token.

Source: ext/contracts/api-tokens/verify/v1.

Constants

const (
	Invalid = "invalid"
	Expired = "expired"
)

Reasons a token isn't valid.

Variables

var Contract = extkit.NewContract[Verifier]("api-tokens.verify/v1", extkit.One)

Contract is api-tokens.verify/v1.

var W = extkit.NewWire(Contract, "api_tokens_verify", func(inner Verifier, g *extkit.Guard[Verifier]) Verifier {
	return &guardVerifier{inner, g}
},
	extkit.Op[Verifier]{
		Name:   "verify",
		Desc:   "Checks an API token and says who it acts as and what it may do (its scopes, with their values). A token that isn't valid is an answer (Valid false), not an error, so callers can tell a refused token from an outage.",
		Method: "POST",
		Path:   "",
		In:     reflect.TypeOf(Input{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Verifier, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in Input
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Verify(ctx, a, in)
		},
	},
)

W is api-tokens.verify/v1's wire side: routes at /api/_c/api-tokens.verify/v1/<op>, action hooks, MCP tools and the client.

Functions

OnVerify

func OnVerify(owner any, fn func(e *extkit.CallEvent, in *Input) error)

OnVerify binds fn to verify calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

Types

Client

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

Client is api-tokens.verify/v1 over HTTP, for the CLI and other apps. It implements Verifier; the token it was made with is the credential, so the extkit.Auth argument is ignored.

NewClient

func NewClient(baseURL, token string, hc *http.Client) *Client

NewClient calls api-tokens.verify/v1 on the app at baseURL with token (a PocketBase auth token or an API token).

Client.Verify

func (c *Client) Verify(ctx context.Context, _ extkit.Auth, in Input) (Result, error)

Input

type Input struct {
	Token string `json:"token"` // the secret the client sent ("pbt_...")
	// IP is the client's address, recorded as the token's last use; "" leaves the last use alone.
	IP string `json:"ip,omitempty"`
}

Input is the token to check.

Result

type Result struct {
	Valid bool `json:"valid"`
	// Reason says why a token isn't valid: "invalid" (unknown, malformed or revoked) or "expired".
	Reason string             `json:"reason,omitempty"`
	Grant  *apitokensv1.Grant `json:"grant,omitempty"`
}

Result is whether the token is valid and, when it is, what it grants.

Verifier

type Verifier interface {
	// Verify checks an API token and says who it acts as and what it may do (its scopes, with their values). A token
	// that isn't valid is an answer (Valid false), not an error, so callers can tell a refused token from an outage.
	//
	//op: POST /
	Verify(ctx context.Context, a extkit.Auth, in Input) (Result, error)
}

Verifier checks an app's API tokens for another server.