-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial bitwise RPC authorization-permission system
- Loading branch information
Showing
10 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn, Path}; | ||
|
||
pub fn auth(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let api_op = parse_macro_input!(attr as Path); | ||
let mut func = parse_macro_input!(item as ItemFn); | ||
|
||
let check = syn::parse2(quote! { | ||
if !self.flags.has_enabled(#api_op) { | ||
// As macro processing happens after async_trait processing its wrapped with async_trait return type | ||
return std::boxed::Box::pin(std::future::ready(Err(RpcError::UnauthorizedMethod))); | ||
} | ||
}) | ||
.unwrap(); | ||
|
||
func.block.stmts.insert(0, check); | ||
quote!(#func).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use kaspa_rpc_core::api::ops::RpcApiOps; | ||
|
||
// Struct to manage flags as a combined bitmask | ||
#[derive(Debug)] | ||
pub struct Flags { | ||
bitmask: u128, | ||
} | ||
|
||
impl Flags { | ||
// Create an empty flag set | ||
pub fn new() -> Self { | ||
Flags { bitmask: 0 } | ||
} | ||
|
||
// Adds a flag | ||
pub fn add(&mut self, op: RpcApiOps) { | ||
self.bitmask |= op.bitmask(); | ||
} | ||
|
||
// Removes a flag | ||
pub fn remove(&mut self, op: RpcApiOps) { | ||
self.bitmask &= !op.bitmask(); | ||
} | ||
|
||
// Check if a flag is enabled | ||
pub fn has_enabled(&self, op: RpcApiOps) -> bool { | ||
(self.bitmask & op.bitmask()) != 0 | ||
} | ||
|
||
// Create a flag set from a slice of operations | ||
pub fn from_ops(ops: &[RpcApiOps]) -> Self { | ||
let mut permissions = Flags::new(); | ||
for &op in ops { | ||
permissions.add(op); | ||
} | ||
permissions | ||
} | ||
} | ||
|
||
impl From<u128> for Flags { | ||
fn from(bitmask: u128) -> Self { | ||
Flags { bitmask } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod collector; | ||
pub mod converter; | ||
pub mod flags; | ||
pub mod service; |
Oops, something went wrong.