Foundational identity and permission types for role-based access control — bit-set PermissionMask, immutable Identity value type, and PermissionProvider interface. What's included: - `Identity` value type with NewIdentity / WithTenant constructors and SetInContext / FromContext context helpers - `Permission` (int64 bit position) and `PermissionMask` (int64 bit-set) with O(1) Has and non-mutating Grant - `PermissionProvider` interface for DB-backed ResolveMask(ctx, uid, resource) resolution Tested-via: todo-api POC integration Reviewed-against: docs/adr/
2.6 KiB
2.6 KiB
Changelog
All notable changes to this module will be documented in this file.
The format is based on Keep a Changelog, and this module adheres to Semantic Versioning.
0.9.0 - 2026-03-18
Added
Permission—int64type representing a named bit position (0–62) for a single capability; applications define their own constants using this typePermissionMask—int64type representing a resolved bit-set of capabilities for a user on a resourcePermissionMask.Has(p Permission) bool— O(1) check whether a permission bit is set; returns false for out-of-range values (p < 0 or p >= 63)PermissionMask.Grant(p Permission) PermissionMask— returns a new mask with the given bit set without mutating the receiver; silently ignores out-of-range valuesIdentity— value type (not a pointer) carryingUID,TenantID,DisplayName, andEmailfor an authenticated principalNewIdentity(uid, displayName, email string) Identity— constructs an Identity from token authentication data;TenantIDis intentionally left empty for later enrichmentIdentity.WithTenant(id string) Identity— returns a copy of the Identity withTenantIDset; does not mutate the receiver, safe for concurrent middleware useSetInContext(ctx context.Context, id Identity) context.Context— stores an Identity in a context using a private unexported key type to prevent collisionsFromContext(ctx context.Context) (Identity, bool)— retrieves the Identity stored bySetInContext; returns the zero-value Identity and false if no identity is presentPermissionProviderinterface —ResolveMask(ctx context.Context, uid, resource string) (PermissionMask, error)for DB-backed or in-memory permission resolution
Design Notes
Identityis a value type throughout — every enrichment call (e.g.WithTenant) returns a new copy, eliminating nil-pointer bugs and preventing accidental mutation of a shared context value across concurrent middleware.- Permissions are bit positions (0–62) packed into an
int64mask; applications define their own namedPermissionconstants — none are prescribed by this package — keeping the bit-set model flat and free of role-hierarchy complexity. - This package owns the context key for
Identityvia an unexportedauthContextKey{}struct, so any module that needs to carry an authenticated identity imports onlyrbac; zero micro-lib dependencies (stdlib only).