-
Notifications
You must be signed in to change notification settings - Fork 6
Basic Identity system for Tiles #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e32cc9a
feat: Added create_identity fn in tilekit
madclaws 56c771d
feat: Added basic DID based identity implementation in Tiles
madclaws 72005d9
refactor: Refactored root user represenation
madclaws abbb28f
fix: Moved memory path config to config.toml
madclaws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,38 @@ | ||
| // Handles stuff related to accounts, identity etc.. | ||
| use anyhow::Result; | ||
| use ed25519_dalek::{SigningKey, ed25519::signature::rand_core::OsRng}; | ||
| use keyring::Entry; | ||
| use ucan::did::Ed25519Did; | ||
|
madclaws marked this conversation as resolved.
|
||
|
|
||
| type DID = String; | ||
| type Identity = DID; | ||
|
|
||
| /// Creates an `Identity` | ||
| /// # Parameters | ||
| /// - `app`: The service for which Identity is made (for ex: tiles) | ||
| /// The keypair generated will be stored in OS secure storage | ||
| /// Returns an `Identity` | ||
| pub fn create_identity(app: &str) -> Result<Identity> { | ||
| let mut csprng = OsRng; | ||
| let signing_key = SigningKey::generate(&mut csprng); | ||
| let ed_did = Ed25519Did::from(signing_key.clone()); | ||
| let did = ed_did.to_string(); | ||
| let entry = Entry::new(app, &did)?; | ||
| entry.set_secret(&signing_key.to_keypair_bytes())?; | ||
| Ok(did) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use keyring::{mock, set_default_credential_builder}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_create_success() -> Result<()> { | ||
| set_default_credential_builder(mock::default_credential_builder()); | ||
| let did = create_identity("tiles")?; | ||
| assert!(did.starts_with("did:key")); | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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,2 +1,3 @@ | ||
| pub mod accounts; | ||
| pub mod modelfile; | ||
| pub mod optimize; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,197 @@ | ||
| // Stuff related to account and identity system | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use anyhow::Result; | ||
| use tilekit::accounts::create_identity; | ||
| use toml::Table; | ||
|
|
||
| use crate::utils::config::save_config; | ||
| const ROOT_USER_CONFIG_KEY: &str = "root-user"; | ||
|
|
||
| //TODO: add docs | ||
| pub fn get_root_user_details(config: &Table) -> Result<HashMap<String, String>> { | ||
| Ok(get_root_account(&config)) | ||
| } | ||
|
|
||
| fn get_root_account(config: &Table) -> HashMap<String, String> { | ||
| let root_user = config.get(ROOT_USER_CONFIG_KEY).unwrap(); | ||
| let root_user_table = root_user.as_table().unwrap(); | ||
| let mut root_user_map = HashMap::new(); | ||
| for ele in root_user_table { | ||
| root_user_map.insert(ele.0.to_string(), ele.1.as_str().unwrap().to_owned()); | ||
| } | ||
| root_user_map | ||
| } | ||
|
madclaws marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Lets return main config table only, let the caller do whatever it wants... | ||
| // TODO: Needed docs | ||
| pub fn create_root_account(config: &Table, nickname: Option<String>) -> Result<Table> { | ||
| let root_user = config.get(ROOT_USER_CONFIG_KEY).unwrap(); | ||
| let root_user_table = root_user.as_table().unwrap(); | ||
| let did = root_user_table.get("id").unwrap().as_str().unwrap(); | ||
| if did.is_empty() { | ||
| let root_user_config = create_root_user(root_user_table, nickname)?; | ||
| Ok(root_user_config) | ||
| } else { | ||
| Ok(root_user_table.clone()) | ||
| } | ||
| } | ||
|
|
||
| //TODO: docs | ||
| pub fn save_root_account(mut config: Table, root_user_config: &Table) -> Result<()> { | ||
| config.insert( | ||
| String::from(ROOT_USER_CONFIG_KEY), | ||
| toml::Value::Table(root_user_config.clone()), | ||
| ); | ||
| save_config(&config) | ||
| } | ||
|
|
||
| // TODO: add docs | ||
| pub fn set_nickname(config: &Table, nickname: String) -> Result<Table> { | ||
| let root_user = config.get(ROOT_USER_CONFIG_KEY).unwrap(); | ||
| let mut root_user_table = root_user.as_table().unwrap().clone(); | ||
| let did = root_user_table.get("id").unwrap().as_str().unwrap(); | ||
| if did.is_empty() { | ||
| Err(anyhow::anyhow!("No Root user available")) | ||
| } else { | ||
| root_user_table.insert("id".to_owned(), toml::Value::String(did.to_owned())); | ||
| root_user_table.insert("nickname".to_owned(), toml::Value::String(nickname)); | ||
| Ok(root_user_table) | ||
| } | ||
| } | ||
|
|
||
| // TODO: add docs | ||
| fn create_root_user(root_user_config: &Table, nickname: Option<String>) -> Result<Table> { | ||
| // get root user details | ||
| let mut root_user_table = root_user_config.clone(); | ||
| match create_identity("tiles") { | ||
| Ok(did) => { | ||
| root_user_table.insert("id".to_owned(), toml::Value::String(did)); | ||
| if nickname.is_some() { | ||
| root_user_table.insert( | ||
| "nickname".to_owned(), | ||
| toml::Value::String(nickname.unwrap()), | ||
| ); | ||
| } | ||
| Ok(root_user_table) | ||
| } | ||
| Err(err) => Err(err), | ||
| } | ||
| } | ||
|
madclaws marked this conversation as resolved.
|
||
|
|
||
| #[cfg(test)] | ||
|
|
||
| mod tests { | ||
| use keyring::{mock, set_default_credential_builder}; | ||
| use toml::Table; | ||
|
|
||
| use crate::utils::accounts::{create_root_account, get_root_account}; | ||
|
|
||
| #[test] | ||
| fn test_get_root_user_details_empty_id() { | ||
| let config: Table = toml::from_str( | ||
| r#" | ||
| [root-user] | ||
| id = '' | ||
| nickname = '' | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let acc_details = get_root_account(&config); | ||
| assert!(acc_details.get("id").unwrap().is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_root_user_details_valid_id() { | ||
| let config: Table = toml::from_str( | ||
| r#" | ||
| [root-user] | ||
| id = 'did:key:xyz' | ||
| nickname = '' | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let acc_details = get_root_account(&config); | ||
| assert!(acc_details.get("id").unwrap().contains("did:key")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_create_root_account_but_exists() { | ||
| let config: Table = toml::from_str( | ||
| r#" | ||
| [root-user] | ||
| id = 'did:key:xyz' | ||
| nickname = '' | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let root_user = create_root_account(&config, None).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| root_user.get("id").unwrap().as_str().unwrap(), | ||
| "did:key:xyz" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_create_root_account_new() { | ||
| set_default_credential_builder(mock::default_credential_builder()); | ||
| let config: Table = toml::from_str( | ||
| r#" | ||
| [root-user] | ||
| id = '' | ||
| nickname = '' | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let root_user = create_root_account(&config, None).unwrap(); | ||
|
|
||
| assert_ne!( | ||
| root_user.get("id").unwrap().as_str().unwrap(), | ||
| "did:key:xyz" | ||
| ); | ||
|
|
||
| assert!( | ||
| root_user | ||
| .get("id") | ||
| .unwrap() | ||
| .as_str() | ||
| .unwrap() | ||
| .starts_with("did:key") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_create_root_account_new_w_nickname() { | ||
| set_default_credential_builder(mock::default_credential_builder()); | ||
| let config: Table = toml::from_str( | ||
| r#" | ||
| [root-user] | ||
| id = '' | ||
| nickname = '' | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let root_user = create_root_account(&config, Some(String::from("madclaws"))).unwrap(); | ||
|
|
||
| assert_ne!( | ||
| root_user.get("id").unwrap().as_str().unwrap(), | ||
| "did:key:xyz" | ||
| ); | ||
|
|
||
| assert!( | ||
| root_user | ||
| .get("id") | ||
| .unwrap() | ||
| .as_str() | ||
| .unwrap() | ||
| .starts_with("did:key") | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| root_user.get("nickname").unwrap().as_str().unwrap(), | ||
| "madclaws" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.