authorizable source pure
Package authorizable is an extension of p/nt/ownable; It allows the user to instantiate an Authorizable struct, which...
View source
v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.
authorizable - Second authorization tier over ownable
Extension of gno.land/p/nt/ownable/v0 that adds a second permission level on top of single-owner ownership: one superuser (the ownable owner) plus a list of authorized addresses. Use it for a moderator tier, an allowlist, or any "owner, plus a set of trusted others" pattern.
Usage
1package myrealm
2
3import (
4 "chain/runtime"
5
6 "gno.land/p/nt/ownable/v0"
7 "gno.land/p/nt/ownable/v0/exts/authorizable"
8)
9
10// The superuser (and first entry on the auth list) is chosen explicitly.
11// Here: the deployer, captured in init.
12var auth *authorizable.Authorizable
13
14func init() {
15 caller := runtime.PreviousRealm()
16 if !caller.IsUserCall() {
17 panic("must be deployed by a user")
18 }
19 auth = authorizable.New(ownable.NewWithAddress(caller.Address()))
20}
21
22// Superuser-only: add a moderator.
23func AddModerator(cur realm, addr address) error {
24 return auth.AddToAuthList(0, cur, addr)
25}
26
27// Gate an action to anyone on the auth list.
28func Moderate(cur realm) {
29 auth.AssertPreviousOnAuthList(0, cur)
30 // ... privileged work ...
31}
API
1type Authorizable struct {
2 *ownable.Ownable // the owner is the superuser; all Ownable methods are inherited
3 // unexported auth list
4}
5
6// New builds an Authorizable from an existing *ownable.Ownable.
7// The owner is automatically added to the auth list.
8func New(o *ownable.Ownable) *Authorizable
9
10// Superuser-only (previous caller must be the owner).
11func (a *Authorizable) AddToAuthList(_ int, rlm realm, addr address) error
12func (a *Authorizable) DeleteFromAuthList(_ int, rlm realm, addr address) error
13
14// Membership checks (return an error; nil means on the list).
15func (a *Authorizable) OnAuthList(_ int, rlm realm) error // is the caller realm itself on the list
16func (a *Authorizable) PreviousOnAuthList(_ int, rlm realm) error // is the realm/user that crossed in on the list
17
18// Assert variants panic instead of returning an error.
19func (a Authorizable) AssertOnAuthList(_ int, rlm realm)
20func (a Authorizable) AssertPreviousOnAuthList(_ int, rlm realm)
21
22// Errors: ErrNotSuperuser, ErrNotInAuthList, ErrAlreadyInList
Notes
- Every method takes the caller's own captured
curasrlmand assertsrlm.IsCurrent(), blocking the designation-forgery read where a non-crossing wrapper makes the realm walk return the wrong address. The first_ intargument is an unused placeholder: pass0. - The superuser is authenticated by
rlm.Previous().Address()matching the underlyingOwnableowner, soAddToAuthList/DeleteFromAuthListsucceed only when the owner is the crossing caller. Ownership transfer, renouncing, etc. come from the embeddedOwnable. PreviousOnAuthList/AssertPreviousOnAuthListare the user-facing gate: they check the address that crossed into your realm.OnAuthListchecks the calling realm itself; use it only when a realm-to-realm caller should be listed directly.- The auth list is backed by a
bptree, keyed by address string.
Package authorizable is an extension of p/nt/ownable; It allows the user to instantiate an Authorizable struct, which extends p/nt/ownable with a list of users that are authorized for something. By using authorizable, you have a superuser (ownable), as well as another authorization level, which can be used for adding moderators or similar to your realm.
1
1
1
type Authorizable
structMethods on Authorizable
func AddToAuthList
method on AuthorizableAddToAuthList adds addr to the auth list. rlm must be the caller's own captured cur; rlm.Previous().Address() must equal the superuser (the underlying Ownable's owner).
func AssertOnAuthList
method on Authorizablefunc AssertPreviousOnAuthList
method on Authorizablefunc DeleteFromAuthList
method on AuthorizableDeleteFromAuthList removes addr from the auth list. rlm must be the caller's own captured cur; rlm.Previous().Address() must equal the superuser (the underlying Ownable's owner).
func OnAuthList
method on AuthorizableOnAuthList reports whether rlm.Address() is on the auth list. rlm must be the caller's own captured cur (asserted via rlm.IsCurrent()). Pre-migration shape used unsafe.CurrentRealm().Address() — vulnerable to the .Title()-class read where a non-crossing wrapper made the walk return the wrong realm. Explicit rlm closes that.
func PreviousOnAuthList
method on AuthorizablePreviousOnAuthList reports whether rlm.Previous().Address() — the realm that crossed into the caller — is on the auth list. Same rlm contract as OnAuthList.