Skip to content

Commit

Permalink
Merge pull request #41 from CoLearn-Dev/policy-module-forward
Browse files Browse the repository at this point in the history
update proto; add generate_token_with_signature; add test_user_management
  • Loading branch information
stneng authored Jan 13, 2023
2 parents 54623cb + afe5440 commit c2ed6c6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "colink"
version = "0.2.7"
version = "0.2.8"
edition = "2021"
description = "CoLink Rust SDK"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CoLink SDK helps both application adnd protocol developers access the functional
Add this to your Cargo.toml:
```toml
[dependencies]
colink = "0.2.7"
colink = "0.2.8"
```

## Getting Started
Expand Down
7 changes: 5 additions & 2 deletions examples/user_policy_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use colink::extensions::policy_module::{Rule, TaskFilter};
use colink::extensions::policy_module::{Action, Rule, TaskFilter};
use colink::CoLink;
use std::env;

Expand All @@ -18,7 +18,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
protocol_name: "greetings".to_string(),
..Default::default()
}),
action: "approve".to_string(),
action: Some(Action {
r#type: "approve".to_string(),
..Default::default()
}),
priority: 1,
..Default::default()
})
Expand Down
2 changes: 1 addition & 1 deletion proto
29 changes: 29 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ impl CoLink {
GenerateTokenRequest {
expiration_time,
privilege: privilege.to_string(),
..Default::default()
},
))
.await?;
debug!("RESPONSE={:?}", response);
Ok(response.get_ref().jwt.clone())
}

pub async fn generate_token_with_signature(
&self,
public_key: &secp256k1::PublicKey,
signature_timestamp: i64,
expiration_timestamp: i64,
signature: &[u8],
) -> Result<String, Error> {
let public_key_vec = public_key.serialize().to_vec();
let mut client = self._grpc_connect(&self.core_addr).await?;
let response = client
.generate_token(generate_request(
&self.jwt,
GenerateTokenRequest {
expiration_time: expiration_timestamp,
privilege: "user".to_string(),
user_consent: Some(UserConsent {
public_key: public_key_vec,
signature_timestamp,
expiration_timestamp,
signature: signature.to_vec(),
}),
},
))
.await?;
Expand Down
52 changes: 52 additions & 0 deletions tests/test_user_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use colink::{
decode_jwt_without_validation,
extensions::instant_server::{InstantRegistry, InstantServer},
generate_user, prepare_import_user_signature, CoLink,
};

#[tokio::test]
async fn test_user_management() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let _ir = InstantRegistry::new();
let is = InstantServer::new();
let cl = is.get_colink();
let core_addr = cl.get_core_addr()?;

let expiration_timestamp = chrono::Utc::now().timestamp() + 86400 * 31;
let (pk, sk) = generate_user();
let core_pub_key = cl.request_info().await?.core_public_key;
let (signature_timestamp, sig) =
prepare_import_user_signature(&pk, &sk, &core_pub_key, expiration_timestamp);

let user_jwt = cl
.import_user(&pk, signature_timestamp, expiration_timestamp, &sig)
.await?;
let user_id = decode_jwt_without_validation(&user_jwt)?.user_id;

let cl = CoLink::new(&core_addr, &user_jwt);
let new_expiration_timestamp = chrono::Utc::now().timestamp() + 86400 * 60;
let guest_jwt = cl
.generate_token_with_expiration_time(new_expiration_timestamp, "guest")
.await?;
let guest_auth_content = decode_jwt_without_validation(&guest_jwt)?;
assert!(guest_auth_content.user_id == user_id);
assert!(guest_auth_content.privilege == "guest");
assert!(guest_auth_content.exp == new_expiration_timestamp);

let cl = CoLink::new(&core_addr, "");
let (new_signature_timestamp, new_sig) =
prepare_import_user_signature(&pk, &sk, &core_pub_key, new_expiration_timestamp);
let new_user_jwt = cl
.generate_token_with_signature(
&pk,
new_signature_timestamp,
new_expiration_timestamp,
&new_sig,
)
.await?;
let user_auth_content = decode_jwt_without_validation(&new_user_jwt)?;
assert!(user_auth_content.user_id == user_id);
assert!(user_auth_content.privilege == "user");
assert!(user_auth_content.exp == new_expiration_timestamp);

Ok(())
}

0 comments on commit c2ed6c6

Please sign in to comment.