Skip to content

Commit

Permalink
support import and get-all-accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Mart committed Nov 15, 2024
1 parent e9ba83c commit 76f7adc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ impl AppsTable {
Keyvalue::set(&self.prefixed_key(DbKeys::LOGGED_IN), user.as_bytes())
.expect("Failed to set logged-in user");

// Add to connected accounts if not already present
self.connect(user);
}

pub fn connect(&self, user: &str) {
let connected_accounts = Keyvalue::get(&self.prefixed_key(DbKeys::CONNECTED_ACCOUNTS));
let mut connected_accounts = connected_accounts
.map(|c| <ConnectedAccounts>::unpacked(&c).unwrap())
Expand Down
38 changes: 29 additions & 9 deletions services/system/Accounts/plugin/accounts/src/interfaces/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,44 @@ use crate::plugin::AccountsPlugin;
use crate::bindings::accounts::account_tokens::api::*;
use crate::bindings::exports::accounts::plugin::admin::{AppDetails, Guest as Admin};
use crate::bindings::exports::accounts::plugin::api::Guest as API;
use crate::bindings::host::common::client as Client;
use crate::bindings::host::common::client::{self as Client, OriginationData};
use crate::db::apps_table::*;
use crate::db::user_table::*;
use crate::helpers::*;

// Asserts that the caller is the active app, and that it's the `accounts` app.
fn assert_caller_admin() {
fn get_assert_caller_admin(context: &str) -> OriginationData {
let caller = get_assert_top_level_app("admin interface", &vec![]).unwrap();
assert!(
caller.origin == Client::my_service_origin(),
"login_direct only callable by `accounts`"
"{} only callable by `accounts`",
context
);
caller
}

fn assert_caller_admin(context: &str) {
let _ = get_assert_caller_admin(context);
}

fn assert_valid_account(account: &str) {
let account_details =
AccountsPlugin::get_account(account.to_string()).expect("Get account failed");
assert!(account_details.is_some(), "Invalid account name");
}

impl Admin for AccountsPlugin {
fn login_direct(app: AppDetails, user: String) {
assert_caller_admin();
assert_caller_admin("login_direct");

// Verify specified user is valid
let account_details =
AccountsPlugin::get_account(user.to_string()).expect("Get account failed");
assert!(account_details.is_some(), "Invalid account name");
assert_valid_account(&user);

AppsTable::new(&app).login(&user);
UserTable::new(&user).add_connected_app(&app);
}

fn decode_connection_token(token: String) -> Option<AppDetails> {
assert_caller_admin();
assert_caller_admin("decode_connection_token");

if let Some(token) = deserialize_token(&token) {
match token {
Expand All @@ -51,4 +60,15 @@ impl Admin for AccountsPlugin {
);
UserTable::new(&user).get_connected_apps()
}

fn import_account(account: String) {
let caller = get_assert_caller_admin("import_account");
assert_valid_account(&account);
AppsTable::new(&caller).connect(&account);
}

fn get_all_accounts() -> Vec<String> {
let caller = get_assert_caller_admin("get_all_accounts");
AppsTable::new(&caller).get_connected_accounts()
}
}
6 changes: 6 additions & 0 deletions services/system/Accounts/plugin/accounts/wit/impl.wit
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ interface admin {

/// Gets the names of all apps to which the user has connected
get-connected-apps: func(user: string) -> list<string>;

/// Adds an account into the accounts table so that it can be used on this device.
import-account: func(account: string);

/// Gets a list of all accounts that are known by the accounts app on this device.
get-all-accounts: func() -> list<string>;
}

world impl {
Expand Down

0 comments on commit 76f7adc

Please sign in to comment.