{"modulePath":"github.com/authorizerdev/authorizer","version":"v0.0.0-20261204130907-c9f3bd375305","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package engine\n\nPackage engine defines the AuthorizationEngine SPI — the abstraction over a relationship-based access control (ReBAC) backend used by Authorizer's fine-grained authorization (FGA) subsystem.\n\nThe interface is deliberately backend-agnostic. The current implementation (internal/authorization/engine/openfga) embeds OpenFGA in-process — Authorizer IS the engine — and the same contract could front an external OpenFGA service in future without touching callers. The engine speaks the OpenFGA tuple vocabulary: a tuple relates a user (subject) to an object via a relation, e.g. (user:alice, viewer, document:1).\n\nFGA is enabled by configuring a store (--fga-store); when it is empty the engine is not constructed and the fga\\_\\* resolvers fail closed.\n\n## Constants\n\n```go\nconst ContextKeyCurrentTime = \"current_time\"\n```\nContextKeyCurrentTime is the request-context parameter the engine sets on every evaluating call (Check, BatchCheck, ListObjects, ListUsers) to the server's clock, as an RFC 3339 UTC timestamp. Conditions such as a time-limited grant compare against it.\n\nIt is server-owned: callers cannot supply it. A caller-asserted time would turn an expired grant back into an allowed one, and a missing value makes OpenFGA error on any tuple whose condition reads it.\n\n## Variables\n\n```go\nvar ErrNoModel = errors.New(\"no authorization model written yet\")\n```\nErrNoModel is returned (wrapped) by ReadModel when no authorization model has been written to the store yet. Callers can use errors.Is to treat this as an empty state (\"no model yet\") rather than a failure — e.g. a fresh store the admin has not configured. Distinct from a genuine read failure.\n\n## Types\n\n```go\ntype AuthorizationEngine interface {\n\t// Check reports whether user is related to object via relation. Optional\n\t// contextual tuples are evaluated for this call only and are not persisted.\n\t// Returns (false, err) on engine error; callers must fail closed.\n\tCheck(ctx context.Context, user, relation, object string, ctxTuples ...ContextualTuple) (bool, error)\n\n\t// BatchCheck evaluates multiple CheckRequests. The returned slice is\n\t// positionally aligned with the input: result[i] answers requests[i]. An\n\t// error indicates a whole-batch failure; callers must fail closed for every\n\t// request in the batch.\n\tBatchCheck(ctx context.Context, requests []CheckRequest) ([]CheckResult, error)\n\n\t// ListObjects returns the IDs of objects of type objType to which user is\n\t// related via relation. This is the RAG/pre-filter primitive and is an\n\t// expensive enumeration surface — callers must paginate, cap, and\n\t// rate-limit. Returned IDs are fully qualified (\"document:1\").\n\tListObjects(ctx context.Context, user, relation, objType string) ([]string, error)\n\n\t// ListUsers returns the fully qualified user IDs (e.g. \"user:alice\") of type\n\t// userType that have relation on object. It is the inverse of ListObjects:\n\t// \"who can access this object?\". This is a powerful enumeration surface that\n\t// reveals the access graph — callers must admin-gate, cap and audit. Returned\n\t// users are fully qualified (\"user:alice\").\n\tListUsers(ctx context.Context, object, relation, userType string) ([]string, error)\n\n\t// Expand returns the OpenFGA relationship/userset tree for (relation, object)\n\t// rendered as a JSON string. This is the explainability/\"why\" primitive: it\n\t// shows how a relation resolves (direct assignments, usersets, computed\n\t// relations). It reveals the access graph and must be admin-gated.\n\tExpand(ctx context.Context, relation, object string) (string, error)\n\n\t// WriteTuples persists the given relationship tuples. It is additive;\n\t// duplicate writes may error depending on the backend.\n\tWriteTuples(ctx context.Context, tuples []TupleKey) error\n\n\t// DeleteTuples removes the given relationship tuples. Deleting a\n\t// non-existent tuple may error depending on the backend.\n\tDeleteTuples(ctx context.Context, tuples []TupleKey) error\n\n\t// ReadTuples returns a page of persisted tuples matching the filter, plus a\n\t// continuation token. It is an enumeration surface — always paginate.\n\tReadTuples(ctx context.Context, filter ReadTuplesFilter) (*ReadTuplesResult, error)\n\n\t// WriteModel installs a new authorization model from its DSL form and\n\t// returns the backend-assigned model ID. Writing a model is powerful (a\n\t// single edit can re-grant broadly) and must be admin-gated, audited, and\n\t// staged by callers.\n\tWriteModel(ctx context.Context, dsl string) (string, error)\n\n\t// ReadModel returns the currently active authorization model: its\n\t// backend-assigned id and its DSL rendering.\n\tReadModel(ctx context.Context) (id string, dsl string, err error)\n\n\t// TypeRelations returns, for every object type in the active model, the\n\t// relation names defined on it (type -\u003e sorted relation names). Types with\n\t// no relations are omitted. It powers \"list everything this subject can\n\t// access\" enumeration. Returns ErrNoModel (wrapped) when no model has been\n\t// written yet.\n\tTypeRelations(ctx context.Context) (map[string][]string, error)\n\t// TypeNames returns every object type declared in the active model,\n\t// sorted. Unlike TypeRelations it INCLUDES types with no relations, which\n\t// is required to detect a subject-only type such as `agent`.\n\tTypeNames(ctx context.Context) ([]string, error)\n\n\t// ConditionParams returns, for every condition declared in the active\n\t// model, its parameter names (condition name -\u003e sorted parameter names).\n\t// Returns ErrNoModel (wrapped) when no model has been written yet.\n\tConditionParams(ctx context.Context) (map[string][]string, error)\n\n\t// Reset deletes the entire authorization store (the model, all its versions,\n\t// and all tuples) and starts a fresh, empty store. It is destructive and must\n\t// be admin-gated by callers; OpenFGA has no per-version delete, so this is the\n\t// only way to remove a model.\n\tReset(ctx context.Context) error\n}\n```\nAuthorizationEngine is the SPI for a ReBAC authorization backend.\n\nAll decision methods (Check, BatchCheck, ListObjects) are fail-closed at the call site: callers must treat a non-nil error as a deny and never as an allow. Identifiers follow OpenFGA conventions (\"type:id\", relation names, usersets \"type:id#relation\").\n\nImplementations are expected to be safe for concurrent use.\n\n```go\ntype CheckRequest struct {\n\t// User is the subject being checked (e.g. \"user:alice\").\n\tUser string\n\t// Relation is the relation to evaluate (e.g. \"can_view\").\n\tRelation string\n\t// Object is the fully qualified object (e.g. \"document:1\").\n\tObject string\n\t// ContextualTuples are request-scoped tuples not persisted to the store.\n\tContextualTuples []ContextualTuple\n\t// Context supplies request-time condition parameters for this check. It\n\t// must not contain ContextKeyCurrentTime, which the engine always sets.\n\tContext map[string]any\n}\n```\nCheckRequest is a single relationship-check question: \"is User related to Object via Relation?\". ContextualTuples, if any, are evaluated alongside the persisted tuples for this check only.\n\n```go\ntype CheckResult struct {\n\t// Allowed reports whether the relationship holds.\n\tAllowed bool\n}\n```\nCheckResult is the answer to a single CheckRequest.\n\n```go\ntype ContextualTuple struct {\n\t// User is the subject of the relationship.\n\tUser string\n\t// Relation is the relation name.\n\tRelation string\n\t// Object is the fully qualified object.\n\tObject string\n}\n```\nContextualTuple is a tuple supplied only for the duration of a single Check or BatchCheck call. It is not persisted; it lets callers evaluate hypothetical or request-scoped relationships (the OpenFGA contextual-tuples feature) without writing to the store.\n\n```go\ntype ReadTuplesFilter struct {\n\t// User filters by subject (optional).\n\tUser string\n\t// Relation filters by relation (optional).\n\tRelation string\n\t// Object filters by object (optional); may be a type prefix like\n\t// \"document:\" depending on backend support.\n\tObject string\n\t// PageSize caps the number of tuples returned in one page. Zero lets the\n\t// backend choose a default.\n\tPageSize int32\n\t// ContinuationToken resumes a previous ReadTuples call; empty starts from\n\t// the beginning.\n\tContinuationToken string\n}\n```\nReadTuplesFilter narrows a ReadTuples query. Any field left empty acts as a wildcard for that position. An entirely empty filter reads all tuples (use with pagination; this is an enumeration surface).\n\n```go\ntype ReadTuplesResult struct {\n\t// Tuples is the page of matching relationships.\n\tTuples []TupleKey\n\t// ContinuationToken, when non-empty, can be passed back via\n\t// ReadTuplesFilter.ContinuationToken to fetch the next page.\n\tContinuationToken string\n}\n```\nReadTuplesResult is one page of tuples plus a continuation token for the next page (empty when exhausted).\n\n```go\ntype TupleCondition struct {\n\t// Name is the condition name declared in the model.\n\tName string\n\t// Context holds the condition parameters stored with the tuple. May be nil.\n\tContext map[string]any\n}\n```\nTupleCondition attaches an OpenFGA condition to a stored tuple: the tuple only grants access when the named condition (declared in the authorization model) evaluates to true. Context holds the condition parameters bound at write time, e.g. {\"grant\\_time\": \"...\", \"grant\\_duration\": \"10m\"}; stored parameters take precedence over request context at evaluation time.\n\n```go\ntype TupleKey struct {\n\t// User is the subject of the relationship (e.g. \"user:alice\" or a userset\n\t// like \"role:admin#assignee\").\n\tUser string\n\t// Relation is the relation name connecting the user to the object (e.g.\n\t// \"viewer\").\n\tRelation string\n\t// Object is the fully qualified object (e.g. \"document:1\").\n\tObject string\n\t// Condition, when set, makes the relationship conditional. Only meaningful\n\t// on writes and reads; deletes match on (User, Relation, Object) alone.\n\tCondition *TupleCondition\n}\n```\nTupleKey identifies a single relationship: the subject (User) is related to the Object via the Relation. Identifiers are expected to be fully qualified in OpenFGA form, e.g. User=\"user:alice\", Relation=\"viewer\", Object=\"document:1\". The User field may also be a userset reference such as \"role:admin#assignee\".\n\n","path":"github.com/authorizerdev/authorizer/internal/authorization/engine","name":"engine","synopsis":"Package engine defines the AuthorizationEngine SPI — the abstraction over a relationship-based access control (ReBAC) backend used by Authorizer's fine-grained authorization (FGA) subsystem.","isRedistributable":true}
