-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use super::stringify_rpc_err; | ||
use rpc_client::{auth_new, new_client}; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, StructOpt)] | ||
pub enum AuthCommands { | ||
/// Create a new Authentication token with given permission | ||
#[structopt(about = "<String> Create Authentication token with given permission")] | ||
CreateToken { | ||
#[structopt( | ||
short, | ||
help = "permission to assign to the token, one of: read, write, sign, admin" | ||
)] | ||
perm: String, | ||
}, | ||
} | ||
|
||
impl AuthCommands { | ||
pub async fn run(&self) { | ||
// TODO handle cli config | ||
match self { | ||
Self::CreateToken { perm } => { | ||
let perm: String = perm.parse().unwrap(); | ||
let mut client = new_client(); | ||
|
||
let obj = auth_new(&mut client, perm) | ||
.await | ||
.map_err(stringify_rpc_err) | ||
.unwrap(); | ||
println!("{}", serde_json::to_string_pretty(&obj).unwrap()); | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use super::client::Filecoin; | ||
use jsonrpc_v2::Error as JsonRpcError; | ||
use jsonrpsee::raw::RawClient; | ||
use jsonrpsee::transport::http::HttpTransportClient as HTC; | ||
|
||
lazy_static! { | ||
pub static ref ADMIN: Vec<String> = vec![ | ||
"read".to_string(), | ||
"write".to_string(), | ||
"sign".to_string(), | ||
"admin".to_string() | ||
]; | ||
pub static ref SIGN: Vec<String> = | ||
vec!["read".to_string(), "write".to_string(), "sign".to_string()]; | ||
pub static ref WRITE: Vec<String> = vec!["read".to_string(), "write".to_string()]; | ||
pub static ref READ: Vec<String> = vec!["read".to_string()]; | ||
} | ||
|
||
/// Returns a block with specified CID fom chain via RPC | ||
pub async fn auth_new(client: &mut RawClient<HTC>, perm: String) -> Result<String, JsonRpcError> { | ||
let ret: String = match perm.as_str() { | ||
"admin" => Filecoin::auth_new(client, ADMIN.clone()).await?, | ||
"sign" => Filecoin::auth_new(client, SIGN.clone()).await?, | ||
"write" => Filecoin::auth_new(client, WRITE.clone()).await?, | ||
"read" => Filecoin::auth_new(client, READ.clone()).await?, | ||
_ => return Err(JsonRpcError::INVALID_PARAMS), | ||
}; | ||
Ok(ret) | ||
} |
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
mod auth_ops; | ||
mod chain_ops; | ||
mod client; | ||
|
||
pub use self::auth_ops::*; | ||
pub use self::chain_ops::*; | ||
pub use self::client::*; |
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,69 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use jsonrpc_v2::{Error as JsonRpcError, Params}; | ||
use jsonwebtoken::errors::Result as JWTResult; | ||
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
lazy_static! { | ||
pub static ref ADMIN: Vec<String> = vec![ | ||
"read".to_string(), | ||
"write".to_string(), | ||
"sign".to_string(), | ||
"admin".to_string() | ||
]; | ||
pub static ref SIGN: Vec<String> = | ||
vec!["read".to_string(), "write".to_string(), "sign".to_string()]; | ||
pub static ref WRITE: Vec<String> = vec!["read".to_string(), "write".to_string()]; | ||
pub static ref READ: Vec<String> = vec!["read".to_string()]; | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Claims { | ||
// each string is a permission | ||
#[serde(rename = "Allow")] | ||
allow: Vec<String>, | ||
exp: usize, | ||
} | ||
|
||
/// Create a new JWT Token | ||
pub fn create_token(perms: Vec<String>) -> JWTResult<String> { | ||
let payload = Claims { | ||
allow: perms, | ||
exp: 10000000000, | ||
}; | ||
encode( | ||
&Header::default(), | ||
&payload, | ||
&EncodingKey::from_secret("secret".as_ref()), | ||
) | ||
} | ||
|
||
/// Verify JWT Token and return the allowed permissions from token | ||
pub fn verify_token(token: String) -> JWTResult<Vec<String>> { | ||
let token = decode::<Claims>( | ||
&token, | ||
&DecodingKey::from_secret("secret".as_ref()), | ||
&Validation::default(), | ||
)?; | ||
Ok(token.claims.allow) | ||
} | ||
|
||
/// RPC call to create a new JWT Token | ||
pub(crate) async fn auth_new( | ||
Params(params): Params<(Vec<String>,)>, | ||
) -> Result<String, JsonRpcError> { | ||
let (perms,) = params; | ||
let token = create_token(perms)?; | ||
Ok(token) | ||
} | ||
|
||
/// RPC call to verify JWT Token and return the token's permissions | ||
pub(crate) async fn auth_verify( | ||
Params(params): Params<(String,)>, | ||
) -> Result<Vec<String>, JsonRpcError> { | ||
let (token,) = params; | ||
let perms = verify_token(token)?; | ||
Ok(perms) | ||
} |
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