|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +// |
| 5 | +// Copyright (c) DUSK NETWORK. All rights reserved. |
| 6 | + |
| 7 | +//! Role-based access control. |
| 8 | +
|
| 9 | +use alloc::collections::{BTreeMap, BTreeSet}; |
| 10 | + |
| 11 | +use crate::auth::{ |
| 12 | + ActionEnvelope, AuthorizationManager, Authorizer, SignedAuthorization, |
| 13 | +}; |
| 14 | +use crate::core::{error, CallContext, Principal}; |
| 15 | + |
| 16 | +/// 32-byte role id. |
| 17 | +pub type Role = [u8; 32]; |
| 18 | + |
| 19 | +/// Default admin role. |
| 20 | +pub const DEFAULT_ADMIN_ROLE: Role = [0u8; 32]; |
| 21 | + |
| 22 | +#[derive(Clone, Debug)] |
| 23 | +struct RoleData { |
| 24 | + members: BTreeSet<Principal>, |
| 25 | + admin_role: Role, |
| 26 | +} |
| 27 | + |
| 28 | +impl RoleData { |
| 29 | + fn new(admin_role: Role) -> Self { |
| 30 | + Self { |
| 31 | + members: BTreeSet::new(), |
| 32 | + admin_role, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Role-based access-control module. |
| 38 | +#[derive(Clone, Debug, Default)] |
| 39 | +pub struct AccessControl { |
| 40 | + roles: BTreeMap<Role, RoleData>, |
| 41 | +} |
| 42 | + |
| 43 | +impl AccessControl { |
| 44 | + /// Creates an empty access-control module. |
| 45 | + pub const fn new() -> Self { |
| 46 | + Self { |
| 47 | + roles: BTreeMap::new(), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Bootstraps the default admin role. |
| 52 | + pub fn init_admin(&mut self, admin: Principal) { |
| 53 | + if admin.is_zero() { |
| 54 | + panic!("{}", error::ZERO_PRINCIPAL); |
| 55 | + } |
| 56 | + if self.roles.contains_key(&DEFAULT_ADMIN_ROLE) { |
| 57 | + panic!("{}", error::ALREADY_INITIALIZED); |
| 58 | + } |
| 59 | + self.roles |
| 60 | + .entry(DEFAULT_ADMIN_ROLE) |
| 61 | + .or_insert_with(|| RoleData::new(DEFAULT_ADMIN_ROLE)) |
| 62 | + .members |
| 63 | + .insert(admin); |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns true when `account` has `role`. |
| 67 | + pub fn has_role(&self, role: Role, account: Principal) -> bool { |
| 68 | + self.roles |
| 69 | + .get(&role) |
| 70 | + .map(|data| data.members.contains(&account)) |
| 71 | + .unwrap_or(false) |
| 72 | + } |
| 73 | + |
| 74 | + /// Panics unless `account` has `role`. |
| 75 | + pub fn assert_role(&self, role: Role, account: Principal) { |
| 76 | + if !self.has_role(role, account) { |
| 77 | + panic!("{}", error::UNAUTHORIZED); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Authorizes any principal with `role` through runtime context or signed |
| 82 | + /// authorization. |
| 83 | + pub fn authorize_role( |
| 84 | + &self, |
| 85 | + role: Role, |
| 86 | + authorizations: &mut AuthorizationManager, |
| 87 | + context: CallContext, |
| 88 | + authorization: Option<&SignedAuthorization>, |
| 89 | + now: u64, |
| 90 | + ) -> Principal { |
| 91 | + let mut authorizer = Authorizer::new(authorizations, context, now); |
| 92 | + self.authorize_role_with(role, &mut authorizer, authorization) |
| 93 | + } |
| 94 | + |
| 95 | + /// Authorizes any principal with `role` using a reusable call authorizer. |
| 96 | + pub fn authorize_role_with( |
| 97 | + &self, |
| 98 | + role: Role, |
| 99 | + authorizer: &mut Authorizer<'_>, |
| 100 | + authorization: Option<&SignedAuthorization>, |
| 101 | + ) -> Principal { |
| 102 | + if let Some(principal) = authorizer.observed_principal() { |
| 103 | + if self.has_role(role, principal) { |
| 104 | + return principal; |
| 105 | + } |
| 106 | + } |
| 107 | + let Some(authorization) = authorization else { |
| 108 | + panic!("{}", error::UNAUTHORIZED); |
| 109 | + }; |
| 110 | + authorizer.require_unbound_signed_if(authorization, |principal| { |
| 111 | + self.has_role(role, principal) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + /// Authorizes any principal with `role` through runtime context or an |
| 116 | + /// action-bound signed authorization. |
| 117 | + pub fn authorize_role_action( |
| 118 | + &self, |
| 119 | + role: Role, |
| 120 | + authorizations: &mut AuthorizationManager, |
| 121 | + context: CallContext, |
| 122 | + authorization: Option<&SignedAuthorization>, |
| 123 | + envelope: ActionEnvelope, |
| 124 | + now: u64, |
| 125 | + ) -> Principal { |
| 126 | + let mut authorizer = Authorizer::new(authorizations, context, now); |
| 127 | + self.authorize_role_action_with( |
| 128 | + role, |
| 129 | + &mut authorizer, |
| 130 | + authorization, |
| 131 | + envelope, |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + /// Authorizes any principal with `role` using a reusable call authorizer |
| 136 | + /// and exact call envelope for signed fallbacks. |
| 137 | + pub fn authorize_role_action_with( |
| 138 | + &self, |
| 139 | + role: Role, |
| 140 | + authorizer: &mut Authorizer<'_>, |
| 141 | + authorization: Option<&SignedAuthorization>, |
| 142 | + envelope: ActionEnvelope, |
| 143 | + ) -> Principal { |
| 144 | + if let Some(principal) = authorizer.observed_principal() { |
| 145 | + if self.has_role(role, principal) { |
| 146 | + return principal; |
| 147 | + } |
| 148 | + } |
| 149 | + let Some(authorization) = authorization else { |
| 150 | + panic!("{}", error::UNAUTHORIZED); |
| 151 | + }; |
| 152 | + authorizer.require_signed_action_if( |
| 153 | + authorization, |
| 154 | + envelope, |
| 155 | + |principal| self.has_role(role, principal), |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + /// Returns a role's admin role. |
| 160 | + pub fn get_role_admin(&self, role: Role) -> Role { |
| 161 | + self.roles |
| 162 | + .get(&role) |
| 163 | + .map(|data| data.admin_role) |
| 164 | + .unwrap_or(DEFAULT_ADMIN_ROLE) |
| 165 | + } |
| 166 | + |
| 167 | + /// Sets a role's admin role. Caller must have the current admin role. |
| 168 | + pub fn set_role_admin( |
| 169 | + &mut self, |
| 170 | + caller: Principal, |
| 171 | + role: Role, |
| 172 | + admin_role: Role, |
| 173 | + ) { |
| 174 | + let current_admin = self.get_role_admin(role); |
| 175 | + self.assert_role(current_admin, caller); |
| 176 | + self.roles |
| 177 | + .entry(role) |
| 178 | + .or_insert_with(|| RoleData::new(DEFAULT_ADMIN_ROLE)) |
| 179 | + .admin_role = admin_role; |
| 180 | + } |
| 181 | + |
| 182 | + /// Grants `role` to `account`. Caller must have the role admin. |
| 183 | + pub fn grant_role( |
| 184 | + &mut self, |
| 185 | + caller: Principal, |
| 186 | + role: Role, |
| 187 | + account: Principal, |
| 188 | + ) { |
| 189 | + if account.is_zero() { |
| 190 | + panic!("{}", error::ZERO_PRINCIPAL); |
| 191 | + } |
| 192 | + let admin = self.get_role_admin(role); |
| 193 | + self.assert_role(admin, caller); |
| 194 | + self.roles |
| 195 | + .entry(role) |
| 196 | + .or_insert_with(|| RoleData::new(DEFAULT_ADMIN_ROLE)) |
| 197 | + .members |
| 198 | + .insert(account); |
| 199 | + } |
| 200 | + |
| 201 | + /// Revokes `role` from `account`. Caller must have the role admin. |
| 202 | + pub fn revoke_role( |
| 203 | + &mut self, |
| 204 | + caller: Principal, |
| 205 | + role: Role, |
| 206 | + account: Principal, |
| 207 | + ) { |
| 208 | + let admin = self.get_role_admin(role); |
| 209 | + self.assert_role(admin, caller); |
| 210 | + if let Some(data) = self.roles.get_mut(&role) { |
| 211 | + data.members.remove(&account); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /// Renounces `role` from `caller`. |
| 216 | + pub fn renounce_role(&mut self, role: Role, caller: Principal) { |
| 217 | + if let Some(data) = self.roles.get_mut(&role) { |
| 218 | + data.members.remove(&caller); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments