diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 3d36f20..0000000 --- a/.editorconfig +++ /dev/null @@ -1,11 +0,0 @@ -root = true - -[*] -indent_style = space -indent_size = 2 -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.rs] -indent_size = 4 diff --git a/.gitignore b/.gitignore index 778243e..dba93ae 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,8 @@ Cargo.lock # Text file backups **/*.rs.bk +# Code Editors +.editorconfig + # macOS .DS_Store \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 20d8a9e..aaf64a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neptune-auth" -version = "0.1.0" +version = "0.1.1" authors = [ "Eric Woolsey", "Francisco Inacio", diff --git a/README.md b/README.md index e2ec5a0..a912486 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ pub enum Config { Then you should impl GetPermissionGroup for the Config. ```rust impl GetPermissionGroup for Config { - fn get_permission_group(&self, deps: Deps, _env: &Env) -> Result { + fn get_permission_group(&self, deps: Deps, _env: &Env) -> Result { // How your config accesses storage is up to you // Here we use a map from cw_storage_plus Ok(vec![self.load(deps).unwrap()].into()) @@ -48,7 +48,7 @@ impl NeptuneAuth for ExecuteMsg { And finally you place the authorization check inside the execute entry point (or wherever else you'd like to verify authorization). ```rust #[cfg_attr(not(feature = "library"), entry_point)] -pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> Result { +pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> Result { // This is the line that checks the permissions // It will return an error if the caller does not have the required permissions msg.neptune_authorize(deps.as_ref(), &env, &info.sender)?; diff --git a/src/authorization.rs b/src/authorization.rs index c68d448..d774412 100644 --- a/src/authorization.rs +++ b/src/authorization.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; -use cosmwasm_std::{Addr, Deps, Env}; +use cosmwasm_std::{Addr, CustomQuery, Deps, Empty, Env}; use crate::error::{NeptAuthError, NeptAuthResult}; @@ -17,14 +17,19 @@ impl From> for PermissionGroup { } } -pub type PermissionGroupList<'a> = Vec<&'a dyn GetPermissionGroup>; +pub type PermissionGroupList<'a, C> = Vec<&'a dyn GetPermissionGroup>; /// This trait should be derived for any type that requires authorization. pub trait NeptuneAuth { - fn permissions(&self) -> NeptAuthResult; + fn permissions(&self) -> NeptAuthResult>; /// This function is placed inside the contracts' execute function. - fn neptune_authorize(&self, deps: Deps, env: &Env, address: &Addr) -> NeptAuthResult<()> { + fn neptune_authorize( + &self, + deps: Deps, + env: &Env, + address: &Addr, + ) -> NeptAuthResult<()> { let permissions = self.permissions()?; authorize_permissions(deps, env, address, &permissions) } @@ -32,8 +37,11 @@ pub trait NeptuneAuth { /// This trait determines how a permission group is retrieved. /// It will usually be derived for your config type. -pub trait GetPermissionGroup: Debug { - fn get_permission_group(&self, deps: Deps, env: &Env) -> NeptAuthResult; +pub trait GetPermissionGroup: Debug +where + C: CustomQuery, +{ + fn get_permission_group(&self, deps: Deps, env: &Env) -> NeptAuthResult; } /// These base permission groups are starting points. @@ -45,8 +53,11 @@ pub enum BasePermissionGroups { } /// This is an example of how to implement the GetPermissionGroup trait. -impl GetPermissionGroup for BasePermissionGroups { - fn get_permission_group(&self, _deps: Deps, env: &Env) -> NeptAuthResult { +impl GetPermissionGroup for BasePermissionGroups +where + C: CustomQuery, +{ + fn get_permission_group(&self, _deps: Deps, env: &Env) -> NeptAuthResult { Ok(match self { Self::Internal => PermissionGroup::Restricted(vec![env.contract.address.clone()]), Self::Public => PermissionGroup::Public, @@ -55,11 +66,11 @@ impl GetPermissionGroup for BasePermissionGroups { } /// Verifies that the given address is contained within the given permission group list. -pub fn authorize_permissions( - deps: Deps, +pub fn authorize_permissions( + deps: Deps, env: &Env, addr: &Addr, - permissions: &PermissionGroupList, + permissions: &PermissionGroupList, ) -> NeptAuthResult<()> { let collected_permissions = permissions .iter()