15.7. Add application-specific registration checks
Feature guides / 15. OIDC and OAuth workflows · Example context
Umeå University OP's OP engine needs a profile-specific client setting in its registration record. It can adapt federation-validated metadata before preparing the registration response.
How to use it
Set registrationProtocolAdapter on OidcProviderRole or OAuthAuthorizationServerRole. The shared RegistrationProtocolAdapter contract has a synchronous validation method and a response-enrichment method. The handler first performs federation validation. The adapter can reject unsupported profile metadata and receives optional peer-resolved OP context where available. Registration processing; Response.
import { ok } from "@oidfed/core";
import { OidcProviderRole, type OidcProviderRoleConfig, type RegistrationProtocolAdapter } from "@oidfed/oidc";
const umeaOpAdapter: RegistrationProtocolAdapter = {
validateClientMetadata: () => ok(undefined),
enrichResponseMetadata(metadata, chain, { clientMetadataType }) {
return {
...metadata,
[clientMetadataType]: {
...metadata[clientMetadataType],
client_id: chain.entityId,
custom_registration_profile: "campus",
},
};
},
};
export function umeaOpRole(config: OidcProviderRoleConfig) {
return new OidcProviderRole({ ...config, registrationProtocolAdapter: umeaOpAdapter });
}
What your application owns
The example extension is a fictional host convention. Output must still satisfy federation policy and protocol metadata rules. Keep enrichment free of irreversible acceptance side effects; use beforeRegistration for an accreditation rejection before side effects. The host persists clients and handles secrets through the configured integration, not by expecting an adapter to create an OP database automatically.
With registrationManagement: "read-only" on the provider role, enrichment may also return registration_access_token and registration_client_uri together. They must pass response validation and federation policy. Without that opt-in, both fields are removed. See read-only registration access for the host's endpoint and token responsibilities.
Keep validation separate from persistence
Use the adapter to interpret protocol metadata and enrich the response within the accepted profile. Put an accreditation rejection in the pre-registration gate when it must run before downstream acceptance effects. The declarative requirement runs before the optional callback, so a callback cannot override a failed requirement.
Treat adapter errors as a failed registration and leave the client store consistent. A host callback that throws or rejects must not become a successful partial registration. Test your integration with both a rejected policy and an unavailable persistence layer; these failures happen at different stages and need different recovery.
Implement a protocol adapter
The provider validates the selected protocol profile for every registration. Add a custom adapter when your deployment needs an extra metadata check or custom explicit-response fields.
The validation callback receives the full effective metadata map for explicit registration, automatic registration, and PAR. Use clientMetadataType to read the client block and return ok(undefined) to accept. An optional enrichment callback receives and returns the full explicit response metadata map. Use it to prepare response fields before the library checks policy and signs the response.
import type { RegistrationProtocolAdapter } from "@oidfed/oidc";
import { federationError, FederationErrorCode, ok, err } from "@oidfed/core";
export const customAdapter: RegistrationProtocolAdapter = {
validateClientMetadata(metadata, { clientMetadataType }) {
const contacts = metadata[clientMetadataType]?.contacts;
if (!Array.isArray(contacts) || contacts.length === 0 ||
!contacts.every(contact => typeof contact === "string")) {
return err(federationError(
FederationErrorCode.InvalidMetadata,
"A nonempty contacts list is required by this federation profile",
));
}
return ok(undefined);
},
};
Require accreditation before registration
OidcProviderRole supports trustMarkRequirements and beforeRegistration for automatic and explicit registration. OAuthAuthorizationServerRole exposes the same policy for its automatic and explicit flows. Either option can stand alone. When both are set, requirements run first; the callback runs only if they pass, and both must accept.
import { OidcProviderRole, type OidcProviderRoleConfig } from "@oidfed/oidc";
import { type ValidatedTrustChain, type Result } from "@oidfed/core";
export async function createAccreditedProvider(providerConfig: OidcProviderRoleConfig, applicationAccreditationPolicy: (chain: ValidatedTrustChain, registrationType: "automatic" | "explicit") => Result<void> | Promise<Result<void>>) {
const provider = new OidcProviderRole({
...providerConfig,
trustMarkRequirements: { requiredTypes: ["https://edugain.example/marks/research-service"] },
beforeRegistration: async ({ chain, registrationType }) => {
return applicationAccreditationPolicy(chain, registrationType); // Result<void>
},
});
return provider;
}
The callback receives frozen authenticated chain evidence. Rejection, an exception, or a malformed result stops registration before persistence, response signing, or replay consumption. Requirements recheck mark, delegation, issuer-proof, and historical-evidence deadlines. Use the shared options.trustMarkVerification configuration to establish the required issuer evidence; a valid RP chain alone does not guarantee verified accreditation. A fresh Status request remains a separate application policy choice.