5.4. Enforce a custom federation constraint
Feature guides / 5. Federation policy · Example context
eduGAIN defines a campus-specific constraint understood by its applications. A consumer needs that check to participate in the chain's pass/fail decision.
How to use it
Declare the checker through core's shared ConstraintCheckDefinition interface and install it in the consuming host's options.customConstraintChecks. Its discovery and attached protocol operations enforce the rule. The synchronous callback receives a detached, frozen chain context. This fictional framework requires a boolean approval value. Additional constraints.
import { Leaf } from "@oidfed/leaf";
import { err, ok, federationError, FederationErrorCode,
type ConstraintCheckDefinition, type CryptoContext, type TrustAnchorSet } from "@oidfed/core";
const campusApproval: ConstraintCheckDefinition = {
name: "urn:academic:approved",
check(value) {
return value === true ? ok(undefined) : err(federationError(
FederationErrorCode.InvalidRequest, "Campus approval constraint requires true",
));
},
};
export async function ligoWiki(keys: CryptoContext, trustAnchors: TrustAnchorSet) {
return await Leaf.create({
entityId: "https://wiki.ligo.example", authorityHints: ["https://incommon.example"],
crypto: keys, trustAnchors, metadata: { federation_entity: {} },
options: { customConstraintChecks: [campusApproval] },
});
}
Publish { "urn:academic:approved": true } in the enrolled subordinate's constraints through the Superior's registerSubordinate() or updateSubordinate(). Then call the configured consumer's discoverEntity() or a protocol role operation. A rejecting checker stops validation before the result is accepted.
What your application owns
The boolean rule is a host-defined example, not a standard constraint. Unknown names are ignored by consumers that have no handler, so deployment agreement matters. Built-in names cannot be overridden. Callbacks must not mutate evidence or return promises. They may run again during contextual completion; keep them deterministic and free of side effects. Publish the extension's meaning for other participants.
Share the custom rule with its consumers
The custom constraint callback is local implementation code. eduGAIN and its relying applications must agree on the parameter's meaning and deploy the corresponding checker where it is needed. Peers that do not understand an additional constraint ignore it under the extension rules.
The callback receives a protected snapshot of authenticated chain information. Use it to make a decision and return the prescribed result; do not mutate the evidence or perform asynchronous network lookups inside the synchronous hook. Include both accepting and rejecting paths in your deployment tests, including a process where the extension is intentionally unknown.
Register and run a custom constraint
Federation §6.2 permits additional constraint parameters. Register checks in FederationOptions.customConstraintChecks to enforce the constraints your federation understands. Other extension constraints are ignored.
import {
err,
ok,
InternalErrorCode,
type ConstraintCheckDefinition,
} from "@oidfed/core";
import { Leaf } from "@oidfed/leaf";
import { entityId } from "@oidfed/core";
export async function validateWithCustomConstraint(host: Leaf, statements: string[]) {
const allowedTrustAnchors: ConstraintCheckDefinition = {
name: "allowed_trust_anchors",
check(value, context) {
if (!Array.isArray(value) || !value.every(item => typeof item === "string")) {
return err({
code: InternalErrorCode.ConstraintViolation,
description: "allowed_trust_anchors must be an array of strings",
});
}
return value.includes(context.trustAnchorId)
? ok(undefined)
: err({
code: InternalErrorCode.ConstraintViolation,
description: "Trust Anchor is not permitted by allowed_trust_anchors",
});
},
};
const result = await host.discoverEntityFromChain(entityId("https://op.umu.example"), statements, {
customConstraintChecks: [allowedTrustAnchors],
});
return result;
}
ConstraintCheckContext provides issuerId and subjectId for the carrying Subordinate Statement, trustChainSubjectId, trustAnchorId, its leaf-first position, and detached, deeply frozen statements. The constraint value is also detached and frozen. Inspect the affected subordinate path through this evidence; checking the supplied chain directly does not authenticate it.
Checks run after authentication, once for each carrying statement, in leaf-first order after its standard constraints. A failed check, exception, malformed result, or promise rejects validation with a constraint error. Duplicate registrations and attempts to replace standard constraints are rejected. There is no critical-constraint parameter.
Callbacks must return synchronously. Obtain external information before validation and capture it in the checker. Prepared validation captures the registered names and functions and runs the checks again during completion, so callbacks should be deterministic and safe to repeat. OIDC, wallet, Leaf, and Authority APIs forward these checks through their federation options. resolveTrustChains() only assembles candidates and does not execute them.
Federation-defined constraints
Register customConstraintChecks in the federation options when your deployment understands an additional constraint. An unregistered extension is ignored under Federation §6.2; publishing it does not make other deployments enforce it. Checkers must be synchronous and may run again when prepared validation completes. Use authenticated statement snapshots to enforce the extension’s scope, and obtain any external information before validation. See the host validation example.
Next: 5.5. Complete validation with authenticated protocol context