Skip to content

Commit d079732

Browse files
committed
refactor everything to use UnlockedKey
1 parent 9ce04f2 commit d079732

23 files changed

+285
-410
lines changed

src/commands/auth.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
use super::*;
2-
use crate::{
3-
sdk::api_url,
4-
utils::{auth::get_token, config::Config},
5-
};
2+
use crate::{sdk::api_url, utils::config::Config};
63
use anyhow::bail;
74
use reqwest::header;
85

96
/// Test authentication with the server
107
#[derive(Parser)]
118
pub struct Args {
12-
/// Key to sign with
13-
#[clap(short, long)]
14-
key: Option<String>,
15-
169
/// Debug output
1710
#[clap(short, long)]
1811
debug: bool,
1912
}
2013

2114
pub async fn command(args: Args) -> anyhow::Result<()> {
2215
let config = Config::get()?;
23-
let key = config.get_key_or_default(args.key)?;
16+
let key = config.primary_key()?;
17+
let password = config.primary_key_password()?;
2418

25-
let client = reqwest::Client::new();
19+
if key.uuid.is_none() {
20+
bail!("Key does not have a UUID, try `envx upload`");
21+
}
22+
23+
let key = key.unlock(&password);
2624

27-
let uuid = key
28-
.uuid
29-
.clone()
30-
.context("Key does not have a UUID, try `envx upload`")?;
31-
let auth_token = get_token(&key.fingerprint, &uuid)
32-
.await
33-
.context("Failed to get token")?;
25+
let client = reqwest::Client::new();
26+
let auth_token = key.auth_token()?;
3427

3528
println!("auth token:\n{}", auth_token.signature);
3629

src/commands/delete/key.rs

Lines changed: 0 additions & 137 deletions
This file was deleted.

src/commands/delete/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ pub(super) use colored::Colorize;
77
use crate::commands_enum;
88
use clap::Subcommand;
99

10-
pub mod key;
11-
1210
/// Delete a resource. (project, key)
1311
#[derive(Parser)]
1412
pub struct Args {
1513
#[clap(subcommand)]
1614
command: Commands,
1715
}
1816

19-
commands_enum!(key);
17+
commands_enum!();
2018

2119
pub async fn command(args: Args) -> Result<()> {
2220
Commands::exec(args).await?;

src/commands/get/project.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub struct Args {
2222
pub async fn command(args: Args) -> Result<()> {
2323
let config = Config::get()?;
2424
let key = config.get_key_or_default(args.key)?;
25-
let project_id =
26-
Choice::try_project(args.project_id, &key.fingerprint).await?;
27-
let project_info =
28-
SDK::get_project_info(&project_id, &key.fingerprint).await?;
25+
let password = config.primary_key_password()?;
26+
let key = key.unlock(&password);
27+
let project_id = Choice::try_project(args.project_id, &key).await?;
28+
let project_info = SDK::get_project_info(&project_id, &key).await?;
2929
println!("{:?}", project_info);
3030
Ok(())
3131
}

src/commands/get/projects.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ pub struct Args {
1111
pub async fn command(args: Args) -> Result<()> {
1212
let config = Config::get()?;
1313
let key = config.get_key_or_default(None)?;
14+
let password = config.primary_key_password()?;
15+
let key = key.unlock(&password);
1416

1517
let local_projects = config.projects.clone();
16-
let remote_projects = SDK::list_projects(&key.fingerprint)
18+
let remote_projects = SDK::list_projects(&key)
1719
.await
1820
.context("Failed to get projects from server".red())?;
1921

src/commands/link.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ pub async fn command(args: Args) -> Result<()> {
4141
}
4242

4343
let key = config.primary_key()?;
44+
let password = config.primary_key_password()?;
45+
let key = key.unlock(&password);
4446

4547
let project_id = match args.project_id {
4648
Some(p) => p,
47-
None => Choice::choose_project(&key.fingerprint).await?,
49+
None => Choice::choose_project(&config.projects, &key).await?,
4850
};
4951

5052
config.link_project(&project_id)?;

src/commands/project/add_user.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::*;
22
use crate::{
33
sdk::{api_url, SDK},
44
utils::{
5-
auth::get_token,
65
choice::Choice,
76
config::Config,
87
prompt::prompt_text,
@@ -41,41 +40,27 @@ pub async fn command(args: Args) -> Result<()> {
4140

4241
let config = Config::get()?;
4342
let key = config.get_key_or_default(args.key)?;
43+
let password = config.primary_key_password()?;
44+
let key = key.unlock(&password);
4445

45-
let uuid = key
46-
.uuid
47-
.context("Key does not have a UUID, try `envx upload`")?;
48-
let (_, public_key) = SDK::get_user(&key.fingerprint, &user_id)
49-
.await
50-
.context("Failed to get user, is the user ID correct?")?;
46+
let project_id = Choice::try_project(args.project_id, &key).await?;
5147

52-
let project_id =
53-
Choice::try_project(args.project_id, &key.fingerprint).await?;
48+
let project_info = SDK::get_project_info(&project_id, &key).await?;
5449

55-
let project_info =
56-
SDK::get_project_info(&project_id, &key.fingerprint).await?;
57-
58-
let variables = SDK::get_variables(&project_id, &key.fingerprint).await?;
50+
let variables = SDK::get_variables(&project_id, &key).await?;
5951
let kvpairs = variables.to_kvpair();
6052

61-
let mut recipients = project_info
53+
let recipients = project_info
6254
.users
6355
.iter()
6456
.map(|e| e.public_key.clone())
65-
.collect::<Vec<String>>();
66-
67-
recipients.push(public_key);
68-
69-
let recipients = recipients
70-
.into_iter()
71-
.collect::<HashSet<String>>()
72-
.into_iter()
73-
.collect::<Vec<String>>();
57+
.collect::<HashSet<String>>();
7458

75-
let pubkeys = recipients
59+
let mut pubkeys = recipients
7660
.iter()
7761
.map(|k| Ok(SignedPublicKey::from_string(k)?.0))
7862
.collect::<Result<Vec<SignedPublicKey>>>()?;
63+
pubkeys.push(pgp::SignedPublicKey::try_from(&key.key)?);
7964

8065
let messages = kvpairs
8166
.par_iter()
@@ -98,7 +83,7 @@ pub async fn command(args: Args) -> Result<()> {
9883
});
9984

10085
let client = reqwest::Client::new();
101-
let auth_token = get_token(&key.fingerprint, &uuid).await?;
86+
let auth_token = key.auth_token()?.bearer();
10287

10388
let url = api_url().join("/variables/update-many")?;
10489

@@ -114,7 +99,7 @@ pub async fn command(args: Args) -> Result<()> {
11499
println!("Updated {} variables", res.len());
115100
println!("IDs: {:?}", res);
116101

117-
SDK::add_user_to_project(&key.fingerprint, &user_id, &project_id).await?;
102+
SDK::add_user_to_project(&key, &user_id, &project_id).await?;
118103

119104
Ok(())
120105
}

src/commands/project/delete.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ pub struct Args {
1414
pub async fn command(args: Args) -> Result<()> {
1515
let mut config = Config::get()?;
1616
let key = config.primary_key()?;
17+
let password = config.primary_key_password()?;
1718

18-
let project_id =
19-
Choice::try_project(args.project, &key.fingerprint).await?;
19+
let key = key.unlock(&password);
2020

21-
SDK::delete_project(&project_id, &key.fingerprint).await?;
21+
let project_id = Choice::try_project(args.project, &key).await?;
22+
23+
SDK::delete_project(&key, &project_id).await?;
2224
config.delete_project(&project_id)?;
2325
println!("Project {} deleted", &project_id);
2426

src/commands/project/list_users.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ pub struct Args {
2727
pub async fn command(args: Args) -> Result<()> {
2828
let config = Config::get()?;
2929
let key = config.get_key_or_default(args.key)?;
30-
let project_id =
31-
Choice::try_project(args.project_id, &key.fingerprint).await?;
32-
let project_info =
33-
SDK::get_project_info(&project_id, &key.fingerprint).await?;
30+
let password = config.primary_key_password()?;
31+
let key = key.unlock(&password);
32+
let project_id = Choice::try_project(args.project_id, &key).await?;
33+
let project_info = SDK::get_project_info(&project_id, &key).await?;
3434

3535
if args.json && args.all {
3636
println!("{}", serde_json::to_string(&project_info.users)?);

src/commands/project/new.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct Args {
2222
pub async fn command(args: Args) -> Result<()> {
2323
let config = Config::get()?;
2424
let key = config.primary_key()?;
25+
let password = config.primary_key_password()?;
26+
let key = key.unlock(&password);
2527

2628
let name = if args.noname {
2729
"".to_string()
@@ -31,7 +33,7 @@ pub async fn command(args: Args) -> Result<()> {
3133
})
3234
};
3335

34-
let new_project_id = SDK::new_project(&key.fingerprint, &name).await?;
36+
let new_project_id = SDK::new_project(&key, &name).await?;
3537
println!("Created new project with ID: {}", new_project_id);
3638

3739
// early return if nolink flag is set, we are done already

0 commit comments

Comments
 (0)