Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

authorizable source pure

Package authorizable is an extension of p/nt/ownable; It allows the user to instantiate an Authorizable struct, which...

Readme 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 cur as rlm and asserts rlm.IsCurrent(), blocking the designation-forgery read where a non-crossing wrapper makes the realm walk return the wrong address. The first _ int argument is an unused placeholder: pass 0.
  • The superuser is authenticated by rlm.Previous().Address() matching the underlying Ownable owner, so AddToAuthList / DeleteFromAuthList succeed only when the owner is the crossing caller. Ownership transfer, renouncing, etc. come from the embedded Ownable.
  • PreviousOnAuthList / AssertPreviousOnAuthList are the user-facing gate: they check the address that crossed into your realm. OnAuthList checks 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.

Overview

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.

Variables 1

var ErrNotInAuthList, ErrNotSuperuser, ErrAlreadyInList

1var (
2	ErrNotInAuthList = errors.New("authorizable: caller is not in authorized list")
3	ErrNotSuperuser  = errors.New("authorizable: caller is not superuser")
4	ErrAlreadyInList = errors.New("authorizable: address is already in authorized list")
5)
source

Functions 1

func New

1func New(o *ownable.Ownable) *Authorizable
source

New creates an Authorizable from an existing *ownable.Ownable. The owner is automatically added to the auth list.

Example construction:

Example
1authorizable.New(ownable.NewWithAddress(addr))

Types 1

type Authorizable

struct
1type Authorizable struct {
2	*ownable.Ownable                // owner in ownable is superuser
3	authorized       *bptree.BPTree // chain.Addr > struct{}{}
4}
source

Methods on Authorizable

func AddToAuthList

method on Authorizable
1func (a *Authorizable) AddToAuthList(_ int, rlm realm, addr address) error
source

AddToAuthList 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 Authorizable
1func (a Authorizable) AssertOnAuthList(_ int, rlm realm)
source

func DeleteFromAuthList

method on Authorizable
1func (a *Authorizable) DeleteFromAuthList(_ int, rlm realm, addr address) error
source

DeleteFromAuthList 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 Authorizable
1func (a *Authorizable) OnAuthList(_ int, rlm realm) error
source

OnAuthList 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 Authorizable
1func (a *Authorizable) PreviousOnAuthList(_ int, rlm realm) error
source

PreviousOnAuthList reports whether rlm.Previous().Address() — the realm that crossed into the caller — is on the auth list. Same rlm contract as OnAuthList.

Imports 4

Source Files 4