import { type AuthRole, type AuthRoleSet } from './auth.role'; import { type ArrayOrValue } from '../array/array'; import { type SetIncludesMode } from '../set'; import { type Maybe } from '../value/maybe.type'; /** * Key in the claims. * * @semanticType * @semanticTopic identifier * @semanticTopic string * @semanticTopic dereekb-util:auth */ export type AuthClaimKey = string; /** * Value in claims. */ export type ClearAuthClaimValue = null; /** * Value in claims. */ export type SimpleAuthClaimValue = string | number | boolean; /** * Value in claims. */ export type AuthClaimValue = SimpleAuthClaimValue | object; /** * The template claims object. Only string values are allowed, as JSON values may only be keyed by string. */ export type AuthClaimsObject = { [key: string]: AuthClaimValue; [key: number | symbol]: never; }; /** * An object that contains "claims" in the context of a JSON Web Token (JWT). * * It is keyed by the claims key. */ export type AuthClaims = { [K in keyof T]: T[K]; }; /** * A claims update. All values can be null. */ export type AuthClaimsUpdate = Partial<{ [K in keyof T]: T[K] | ClearAuthClaimValue; }>; /** * Configuration for a claims key. */ export type AuthRoleClaimsFactoryConfigEntry = V extends SimpleAuthClaimValue ? AuthRoleClaimsFactoryConfigEntryEncodeOptions | AuthRoleClaimsFactoryConfigEntrySimpleOptions : AuthRoleClaimsFactoryConfigEntryEncodeOptions; /** * Simple configuration for a claims key that maps a claim value directly to one or more roles. */ export interface AuthRoleClaimsFactoryConfigEntrySimpleOptions { /** * The roles to add when this claims is encountered. */ roles: ArrayOrValue; /** * Describes when to add the value when encoding the roles. * * During encoding back to roles, the value will be set in the claims if ["any"/"all"] roles are not present. * * For "all", set the inverse if "all of the roles are present"/"any of the roles are NOT present" * For "any", set the inverse if "any of the roles are present"/"all of the roles are NOT present" * * True defaults to "any". * * For example, if there is a role that disables/disallowed the "uploads" role, and "uploads" is present during encoding, * then the value will not be set on the inverse claim. */ inverse?: true | SetIncludesMode; /** * (Optional) claim value. Overrides the default claim value. */ value?: V; } /** * A more configurable configuration for a single claims value. */ export interface AuthRoleClaimsFactoryConfigEntryEncodeOptions { /** * (Optional) function of retrieving the value associated with this entry given the input claims. * * If not defined, will defer to role for finding matches and pull from value. */ encodeValueFromRoles: (roles: AuthRoleSet) => Maybe; /** * (Optional) Auth roles associated with this claims. If not defined, the claims key is used. */ decodeRolesFromValue: (value: Maybe) => Maybe; } export type IgnoreAuthRoleClaimsEntry = null; export type AuthRoleClaimsFactoryConfig = { [K in keyof T]: AuthRoleClaimsFactoryConfigEntry | IgnoreAuthRoleClaimsEntry; }; export interface AuthRoleClaimsFactoryDefaults { /** * Default value to use for claims that have no value present. * * If undefined, defaults to DEFAULT_AUTH_ROLE_CLAIMS_CLAIM_VALUE. */ claimValue?: AuthClaimValue; /** * Default value for claims that are not defined. * * If undefined, defaults to DEFAULT_AUTH_ROLE_CLAIMS_EMPTY_VALUE. */ emptyValue?: AuthClaimValue | ClearAuthClaimValue; } export type AuthRoleClaimsToRolesFunction = (roles: AuthRoleSet) => AuthClaimsUpdate; export type AuthRoleRolesToClaimsFunction = (claims: AuthClaims | AuthClaimsUpdate) => AuthRoleSet; /** * Picks the registered claim keys from a source claims object. * * Values that are `undefined` in the source are omitted from the result. `null` values * are preserved so this also works with an {@link AuthClaimsUpdate} (where `null` means * "explicitly clear"). Unknown keys on the source are ignored. */ export type AuthRoleClaimsCopyFunction = (source: AuthClaims | AuthClaimsUpdate) => AuthClaimsUpdate; /** * Service used for converting claims to/from a roles set. */ export interface AuthRoleClaimsService { readonly toClaims: AuthRoleClaimsToRolesFunction; readonly toRoles: AuthRoleRolesToClaimsFunction; /** * Claim keys registered with this service (in declaration order, excluding ignored/null entries). * * Useful for keeping a separate list of "allowed claims" — such as an OIDC scope's claim * allow-list — in sync with the service's configuration. */ readonly claimKeys: ReadonlyArray; /** * Returns a new object containing only the {@link claimKeys} present in the source. * * Intended for use when forwarding a subset of a user's auth claims to another system * (e.g. building OIDC token claims from a Firebase Auth user record's custom claims), * so the set of forwarded keys stays driven by the same registration that defines them. */ readonly copyClaims: AuthRoleClaimsCopyFunction; readonly defaultClaimValue: unknown; readonly defaultEmptyValue: unknown; } export declare const DEFAULT_AUTH_ROLE_CLAIMS_CLAIM_VALUE = 1; export declare const DEFAULT_AUTH_ROLE_CLAIMS_EMPTY_VALUE: null; /** * Creates an {@link AuthRoleClaimsService} that converts between {@link AuthRoleSet} and JWT-style claims objects. * * Each key in the config maps a claim key to role(s). Simple entries map a claim value to one or more roles, * while encode/decode entries allow custom bidirectional conversion logic. * * @param config - Mapping of claim keys to their role configuration entries (or null to ignore) * @param defaults - Optional default values for claim presence and absence. * @returns A service with `toClaims` and `toRoles` conversion functions. * * @dbxUtil * @dbxUtilCategory auth * @dbxUtilKind factory * @dbxUtilTags auth, role, claims, jwt, factory, bidirectional * @dbxUtilRelated auth-role * * @__NO_SIDE_EFFECTS__ */ export declare function authRoleClaimsService(config: AuthRoleClaimsFactoryConfig, defaults?: AuthRoleClaimsFactoryDefaults): AuthRoleClaimsService; /** * Converts an {@link AuthClaimsUpdate} to {@link AuthClaims} by stripping all null-valued keys. * * Useful for cleaning up a claims update before persisting or comparing, since update objects * use `null` to indicate claim removal. * * @param authClaimsUpdate - The claims update object potentially containing null values. * @returns A clean claims object with all null entries removed. */ export declare function authClaims(authClaimsUpdate: AuthClaimsUpdate): AuthClaims;