Stacking add-on licenses on one install
A customer buys the base product, then an add-on pack delivered as a second signed key, and now one install holds several independently-signed grants. This post shows how to fold N verified Keyright licenses into one effective entitlement set with merge operators that form commutative monoids — so the answer never depends on key order — and how to model revocation as an absorbing Deny that no other grant can out-vote.
A customer buys the base edition of your product. Six months later they buy an add-on pack that unlocks three more modules, and your fulfilment pipeline delivers it as a second signed key — not a reissue of the first. Now the installation holds two Keyright licenses at once. Next quarter an enterprise account stacks a site license with a per-seat top-up, and that is three. Your IsEnabled("analytics") was written against a single LicenseInfo. What does it even mean to ask whether a feature is enabled when the answer is spread across several independently-signed grants with different expiries — and one of them has just been revoked?
The instinct is to pick one license as “primary” and treat the rest as patches. That instinct is wrong, and it gets worse every time you add a rule. The right model is algebraic: the effective license is a fold of every valid grant, under merge operators chosen so that the result cannot depend on the order the grants were processed in.
The real question
Given N independently-signed grants on one machine, each with its own entitlements, quotas, limits, and expiry, produce one effective entitlement set such that: a forged or expired grant can only ever remove entitlements, never add them; a revocation can subtract a feature that another grant still enables; and the answer is byte-for-byte identical no matter what order the keys sit in on disk. Get the last property for free, by construction, and you never have to think about key ordering again.
Each grant is verified independently
Nothing about stacking weakens the trust model. Every grant is a canonical payload signed by your issuing key, and every copy of your app embeds the matching public key and verifies each grant offline — exactly as it does for a single license. A grant that fails its signature check, or whose NotBeforeUtc is in the future, simply does not contribute to the fold. One bad key does not poison the others.
public enum Grant { Unset = 0, Allow = 1, Deny = 2 } // Deny is the top / absorbing state
// The strongest state across grants wins. max over {Unset < Allow < Deny}
// is commutative, associative, and idempotent — a bounded join-semilattice.
static Grant Merge(Grant a, Grant b) => (Grant)Math.Max((int)a, (int)b);
That three-valued enum is the whole trick for capability flags. Unset is the identity (a grant silent about a feature changes nothing), Allow turns a feature on, and Deny turns it off and keeps it off. Because Deny is the maximum, once any grant in the stack denies a feature, no later Allow can raise it back — Deny is an absorbing element. That is precisely what a revocation needs to be.
The fold
Everything else is the same shape: pick the right monoid for each kind of entitlement and fold. Additive credit pools sum (identity 0). Scalar limits such as seat counts take the maximum — a higher-tier grant raises the ceiling and a lower one never lowers it. A per-entitlement expiry takes the latest among the grants that Allow it, so an add-on genuinely extends the life of a feature.
public sealed record EffectiveLicense(
IReadOnlyDictionary<string, Grant> Flags,
IReadOnlyDictionary<string, DateTime> FlagExpiry,
IReadOnlyDictionary<string, long> Pools,
IReadOnlyDictionary<string, long> Limits)
{
public bool IsEnabled(string entitlement, DateTime now) =>
Flags.TryGetValue(entitlement, out var g) && g == Grant.Allow &&
FlagExpiry.TryGetValue(entitlement, out var exp) && exp > now;
}
public static EffectiveLicense Combine(IEnumerable<LicenseInfo> grants, DateTime now)
{
var flags = new Dictionary<string, Grant>();
var expiry = new Dictionary<string, DateTime>();
var pools = new Dictionary<string, long>();
var limits = new Dictionary<string, long>();
foreach (LicenseInfo g in grants) // order is irrelevant — see below
{
if (!g.SignatureValid || g.NotBeforeUtc > now) continue; // drop forged / not-yet-valid
foreach (var (name, state) in g.Flags)
{
flags[name] = Merge(flags.GetValueOrDefault(name), state); // Deny absorbs Allow
if (state == Grant.Allow && g.ExpiresUtc > expiry.GetValueOrDefault(name))
expiry[name] = g.ExpiresUtc; // latest Allow wins
}
foreach (var (pool, credits) in g.Pools)
pools[pool] = pools.GetValueOrDefault(pool) + credits; // additive monoid
foreach (var (cap, value) in g.Limits)
limits[cap] = Math.Max(limits.GetValueOrDefault(cap), value);
}
return new EffectiveLicense(flags, expiry, pools, limits);
}
Notice what IsEnabled does not trust: it never reads an overall license expiry. Each Allow carries its own expiry into FlagExpiry, and the check enforces that at read time. A Deny, by contrast, carries no expiry — once a revocation grant is present and signature-valid, the feature stays off regardless of clocks. That asymmetry is deliberate and fail-safe: expiry can only take features away as time passes, and revocation can never time out.
Why order cannot matter
Run Combine over [base, addOn, revoke] or over [revoke, addOn, base] and you get the same EffectiveLicense, down to the byte. That is not luck and it is not something to test for — it falls out of the operators. Math.Max, integer +, and the Grant merge are each commutative (a ∘ b == b ∘ a) and associative ((a ∘ b) ∘ c == a ∘ (b ∘ c)), and each has an identity element (Unset, 0, long.MinValue for the clamp). A fold of a commutative monoid is invariant under permutation of its inputs. So you never designate a primary key, never sort the grants, and never worry that reordering how keys are loaded from disk could quietly change a customer’s entitlements.
The discipline this imposes is simple: do not introduce a merge rule that is not a commutative monoid. The moment you write “whichever grant was issued most recently wins this field,” you have a non-commutative operator, and order matters again. If you genuinely need recency to win — say, the most recent grant sets the support tier — then make the tie-break explicit and total: fold a (IssuedUtc, licenseId) pair with a max that compares both, so two grants issued in the same millisecond still resolve deterministically by id.
Caveats and the honest limits
Summing pools is a billing decision, not a default. Two paid grants that each carry credits should sum; two trial grants must not, or a customer doubles their free evaluation by activating a second trial. Filter before you fold — keep at most one grant of a given trial kind — rather than teaching the fold about trials.
A deleted Deny is still a risk. Signature verification stops an attacker from forging a grant that re-enables a revoked feature, and from forging away your Deny. It does not stop them deleting the file that holds your revocation grant. That is why a Deny issued to kill a leaked key belongs in the online blocklist the issuing service propagates, with the local grant as a fast-path copy, not the sole line of defence.
Expiry union can outlive a base. Because expiry takes the latest Allow, an add-on that re-grants a base entitlement will keep that feature alive after the base license lapses. For some products that is the intended “you keep what you paid for” behaviour; for others it is a licensing hole. Decide it on purpose, and if you want strict containment, scope the add-on’s entitlements so it never re-grants anything the base owns.
Cache the merged snapshot, not the fold. Combine is cheap, but it still allocates. Run it once when the set of keys changes, hold the resulting EffectiveLicense, and re-evaluate only the time-dependent IsEnabled on the hot path — the same two-tier approach as caching the signature check.
Takeaway
Stacked licensing stops being a tangle of special cases the moment you model the effective license as a fold with commutative-monoid operators. Flags join under a lattice where Deny absorbs, pools sum, limits max, and expiries take the latest enabling grant — and because every one of those is commutative and associative, the answer is independent of key order by construction. Make revocation the absorbing Deny and you get a subtractive lever no grant can out-vote, without ever reissuing a key. See Keyright and the entitlement docs for the full model.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.