Casbin-style authorization engine in MoonBit: model-driven policy
configuration, matcher and effect evaluation, RBAC role hierarchy, and an
in-memory policy store.
moonbit-casbin is a Casbin-style authorization engine implemented in
MoonBit. It loads the Casbin model configuration format (request, policy,
and role definitions, policy effect, matchers), ships its own matcher
expression engine (lexer, Pratt parser, tree-walking evaluator with
short-circuit logic, in lists, field access, and an extensible function
registry), enforces requests end to end (accessor preprocessing, an
in-memory policy store, a CSV policy adapter, the five Casbin policy
effects, and Enforcer::enforce with per-row matching and early
termination), implements RBAC (role hierarchy with a configurable depth
limit, role domains, g / g2 function injection, management APIs for
policies, roles, implicit roles, implicit permissions, and cascading
deletes), and provides the full built-in operator set: keyMatch /
keyMatch2..keyMatch5, keyGet / keyGet2 / keyGet3, regexMatch,
globMatch, and ipMatch for IPv4/IPv6 with CIDR. It is not a port
of the Casbin Go source code; the model format and semantics are
reimplemented from the public documentation. The library depends only on
the MoonBit standard library and the official moonbitlang/regexp
package, and passes check, build, and test on wasm, wasm-gc, js,
and native.
moonbit-casbin
Casbin-style authorization engine in MoonBit: model-driven policy configuration, matcher and effect evaluation, RBAC role hierarchy, and an in-memory policy store.
中文项目介绍
moonbit-casbin是使用 MoonBit 实现的 Casbin 风格授权引擎。它按照与 Casbin 相同的模型配置格式([request_definition]、[policy_definition]、[role_definition]、[policy_effect]、[matchers])加载授权模型,并自带 matcher 表达式求值引擎;后续将提供访问决策 API(enforce)、策略存储与 管理接口。当前已实现:
#/;注释、空行、重复节合并、重复键按声明顺序保留、CRLF 换行;&&/||(短路)、in列表、字段访问、算术与字符串拼接; 函数注册表可通过FunctionRegistry::add扩展;语法错误携带字符偏移;r.sub→r_sub等预处理、内存策略存储(保序 + 去重)、 CSV 策略解析(RFC 4180 引号子集)、5 种 effect 聚合 (allow-override / deny-override / allow-and-deny / priority / subjectPriority)、Enforcer::enforce逐行匹配与提前终止;maxHierarchyLevel限制)、角色域隔离 (g = _, _, _)、g/g2函数自动注入、分组策略变更后角色链接自动 重建(可关闭);delete_user/delete_role级联删除;keyMatch/keyMatch2..keyMatch5、keyGet/keyGet2/keyGet3、regexMatch、globMatch、ipMatch(IPv4/IPv6 + CIDR),用例表对齐 Casbin 官方测试;ConfigSyntax、ModelValidation、PolicySyntax、MatcherSyntax、MatcherEval、Enforcement),语法错误携带行号或字符偏移;moonbitlang/regexp(Apache-2.0),wasm/wasm-gc/js/native四目标通过检查、 构建与测试;--deny-warn检查、 四目标构建与测试、打包清单)。English Summary
moonbit-casbinis a Casbin-style authorization engine implemented in MoonBit. It loads the Casbin model configuration format (request, policy, and role definitions, policy effect, matchers), ships its own matcher expression engine (lexer, Pratt parser, tree-walking evaluator with short-circuit logic,inlists, field access, and an extensible function registry), enforces requests end to end (accessor preprocessing, an in-memory policy store, a CSV policy adapter, the five Casbin policy effects, andEnforcer::enforcewith per-row matching and early termination), implements RBAC (role hierarchy with a configurable depth limit, role domains,g/g2function injection, management APIs for policies, roles, implicit roles, implicit permissions, and cascading deletes), and provides the full built-in operator set:keyMatch/keyMatch2..keyMatch5,keyGet/keyGet2/keyGet3,regexMatch,globMatch, andipMatchfor IPv4/IPv6 with CIDR. It is not a port of the Casbin Go source code; the model format and semantics are reimplemented from the public documentation. The library depends only on the MoonBit standard library and the officialmoonbitlang/regexppackage, and passes check, build, and test onwasm,wasm-gc,js, andnative.Casbin 简介
Casbin 是一个广泛使用的授权库,把访问控制策略从业务代码中抽离为模型 (model)与策略(policy)两部分:
r = sub, obj, act)、策略参数 (p = sub, obj, act)、角色定义(g = _, _)、匹配表达式 (m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act)以及 聚合方式(e = some(where (p.eft == allow)));p, alice, data1, read、g, alice, admin。同一个模型可以支撑 ACL、RBAC、ABAC 等不同授权风格,切换风格通常只需换 模型文件,不改业务代码。参考实现见 casbin/casbin。
项目价值
wasm/wasm-gc/js/native,适合服务端与边缘场景。功能支持矩阵
r/p/g/e/m)g/g2、domain、层级上限)keyMatch/keyMatch2..keyMatch5、keyGet/keyGet2/keyGet3regexMatch、globMatch、ipMatch(IPv4/IPv6 + CIDR)AddMatchingFunc/ 域匹配函数)不支持内容
当前代码(v0.4)不包含:
AddMatchingFunc/AddDomainMatchingFunc):has_link按名字精确比较;eval()内置函数、条件角色(temporal roles)与EnforceContext;上表”计划中”的能力按
Roadmap逐步实现;在实现之前,README 与发布说明 不会声称支持。本地使用方式
环境要求:MoonBit 工具链(含
wasm/wasm-gc/js/native目标)。四目标严格验证(与 CI 相同):
快速开始
加载一个 RBAC 模型(
rbac_model.conf的内容与 Casbin 官方示例一致):错误处理:
Config::parse只返回ConfigSyntax,Model::from_config只返回ModelValidation,错误携带可读描述与行号(如适用)。Matcher 表达式引擎可以独立使用。标识符是 Casbin 预处理后的名字 (
r.sub写作r_sub),求值时的取值方式由调用方提供:端到端判定:模型 + 策略文本 →
enforce。RBAC:
g规则定义角色,判定期自动解析角色层级,管理 API 可在线增删。开发与验证
可运行示例:
测试结果
keyMatch/keyGet、keyMatch2..keyMatch5/keyGet2/keyGet3/regexMatch/globMatch/ipMatch用例表、EscapeAssertion/RemoveComments用例表、RBAC 模型与角色 API 链路均移植自 Casbin 官方 测试与示例,逐项来源见THIRD_PARTY_NOTICES.md;wasm/wasm-gc/js/native四目标:check/build/test均通过,0 errors,0 warnings。目录结构
Roadmap
enforce)。g/g2注入、策略与角色管理 API、 隐式角色与权限、级联删除。keyMatch2..keyMatch5、keyGet2/keyGet3、regexMatch、globMatch、ipMatch)与两个可运行示例。后续计划(尚未开始):CLI 工具、持久化适配器、角色名模式匹配、
eval()与EnforceContext。移植说明
casbin/casbin);keyMatch/keyGet的用例表已移植自 Casbin 官方测试 (util/builtin_operators_test.go),逐项来源与范围见THIRD_PARTY_NOTICES.md。发布状态
截至项目立项时对 MoonBit 生态的公开检索(GitHub
language:moonbit仓库与 mooncakes.io 注册表),未发现 Casbin 风格授权引擎的完整实现。这不是绝对 保证,仅代表立项时检索到的公开信息。License
Apache-2.0,见 LICENSE。