permission

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package permission provides fixed-size bitmask types, a permission registry, and role composition helpers used by goAuth authorization checks.

Mask sizes

Supported widths: 64, 128, 256, and 512 bits. A mask is selected at registry construction time and is immutable thereafter. Bit positions are assigned by Registry.Register and are stable for the lifetime of the process.

Architecture boundaries

This package is a pure in-memory data structure with no I/O. It provides the codec (Encode/Decode) used by the session binary encoder.

What this package must NOT do

  • Access Redis, databases, or the network.
  • Import goAuth, jwt, or session.
  • Dynamically resize masks after registry construction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeMask

func DecodeMask(data []byte) (interface{}, error)

DecodeMask deserializes a byte slice back into a typed permission mask (Mask64, Mask128, Mask256, or Mask512).

Performance: O(1).
Docs: docs/permission.md, docs/jwt.md

func EncodeMask

func EncodeMask(mask interface{}) ([]byte, error)

EncodeMask serializes a permission bitmask into a byte slice for JWT embedding.

Performance: O(1), single allocation.
Docs: docs/permission.md, docs/jwt.md

Types

type Mask64

type Mask64 uint64

Mask64 is a 64-bit permission bitmask supporting up to 64 permissions.

func (*Mask64) Clear

func (m *Mask64) Clear(bit int)

Clear clears the given bit in the mask.

func (*Mask64) Has

func (m *Mask64) Has(bit int, rootReserved bool) bool

Has reports whether the given bit is set. If rootBitReserved is true and the root bit is set, Has returns true for all bits.

func (*Mask64) Raw

func (m *Mask64) Raw() uint64

Raw returns the underlying uint64 value.

func (*Mask64) Set

func (m *Mask64) Set(bit int)

Set sets the given bit in the mask.

type Mask128

type Mask128 struct {
	A uint64
	B uint64
}

Mask128 is a 128-bit permission bitmask supporting up to 128 permissions.

func (*Mask128) Clear

func (m *Mask128) Clear(bit int)

Clear clears the given bit in the mask.

func (*Mask128) Has

func (m *Mask128) Has(bit int, rootReserved bool) bool

Has reports whether the given bit is set. If rootBitReserved is true and the root bit is set, Has returns true for all bits.

func (*Mask128) Set

func (m *Mask128) Set(bit int)

Set sets the given bit in the mask.

type Mask256

type Mask256 struct {
	A uint64
	B uint64
	C uint64
	D uint64
}

Mask256 is a 256-bit permission bitmask supporting up to 256 permissions.

func (*Mask256) Clear

func (m *Mask256) Clear(bit int)

Clear clears the given bit in the mask.

func (*Mask256) Has

func (m *Mask256) Has(bit int, rootReserved bool) bool

Has reports whether the given bit is set. If rootBitReserved is true and the root bit is set, Has returns true for all bits.

func (*Mask256) Set

func (m *Mask256) Set(bit int)

Set sets the given bit in the mask.

type Mask512

type Mask512 struct {
	A uint64
	B uint64
	C uint64
	D uint64
	E uint64
	F uint64
	G uint64
	H uint64
}

Mask512 is a 512-bit permission bitmask supporting up to 512 permissions.

func (*Mask512) Clear

func (m *Mask512) Clear(bit int)

Clear clears the given bit in the mask.

func (Mask512) Has

func (m Mask512) Has(bit int, rootReserved bool) bool

Has reports whether the given bit is set. If rootBitReserved is true and the root bit is set, Has returns true for all bits.

func (*Mask512) Set

func (m *Mask512) Set(bit int)

Set sets the given bit in the mask.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry maps permission names to bit positions within a bitmask. Supports widths of 64, 128, 256, or 512 bits.

Docs: docs/permission.md

func NewRegistry

func NewRegistry(maxBits int, rootReserved bool) (*Registry, error)

NewRegistry creates a permission Registry that maps permission names to bit positions. maxBits selects the mask width (64/128/256/512); rootBitReserved reserves bit 0 for a super-admin root permission.

Docs: docs/permission.md

func (*Registry) Bit

func (r *Registry) Bit(name string) (int, bool)

Bit returns the bit index for the named permission, or false if not registered.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of registered permissions.

func (*Registry) Freeze

func (r *Registry) Freeze()

Freeze prevents further registrations. Must be called before the registry is used for validation.

func (*Registry) Name

func (r *Registry) Name(bit int) (string, bool)

Name returns the permission name for the given bit index, or false if unassigned.

func (*Registry) Register

func (r *Registry) Register(name string) (int, error)

Register assigns the next available bit to the named permission. Returns the assigned bit index. Must be called before Registry.Freeze.

Docs: docs/permission.md

func (*Registry) RootBit

func (r *Registry) RootBit() (int, bool)

RootBit returns the reserved root permission bit, or false if root-bit reservation is disabled.

type RoleManager

type RoleManager struct {
	// contains filtered or unexported fields
}

RoleManager maps role names to pre-computed permission bitmasks. After RoleManager.Freeze, masks are immutable and safe for concurrent reads.

Docs: docs/permission.md

func NewRoleManager

func NewRoleManager(registry *Registry) *RoleManager

NewRoleManager creates a RoleManager backed by the given Registry.

Docs: docs/permission.md

func (*RoleManager) Count

func (rm *RoleManager) Count() int

Count returns the number of registered roles.

func (*RoleManager) Freeze

func (rm *RoleManager) Freeze()

Freeze prevents further role registrations.

func (*RoleManager) GetMask

func (rm *RoleManager) GetMask(roleName string) (interface{}, bool)

GetMask returns the pre-computed bitmask for the named role, or false if the role is not registered.

func (*RoleManager) RegisterRole

func (rm *RoleManager) RegisterRole(
	roleName string,
	permissionNames []string,
	maxBits int,
	rootReserved bool,
) error

RegisterRole creates a role with the given permissions and registers it. Must be called before RoleManager.Freeze.

Docs: docs/permission.md

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL