Skip to content

Commit 4b03154

Browse files
committed
Added hex deauthenticate command
1 parent a629a6d commit 4b03154

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
- Deprecate `HEXPM_USER` and `HEXPM_PASS` in favour of `HEXPM_API_KEY`.
121121
([Samuel Cristobal](https://github.com/scristobal))
122122

123+
- Added `gleam hex deauthenticate` command.
124+
([Samuel Cristobal](https://github.com/scristobal))
125+
123126
### Language server
124127

125128
- The language server now allows renaming of functions, constants,

compiler-cli/src/hex.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,28 @@ with a new one?";
119119
}
120120
Ok(())
121121
}
122+
123+
pub(crate) fn deauthenticate() -> Result<()> {
124+
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
125+
let config = hexpm::Config::new();
126+
let mut auth = HexAuthentication::new(&runtime, config.clone());
127+
128+
let previous = auth.read_stored_api_key()?;
129+
130+
if previous.is_none() {
131+
println!("You must authentihate first");
132+
return Ok(());
133+
}
134+
135+
let question = "This will delelete and revoke your existing Hex API key. The operation han not be undone. Do you want to proceed?";
136+
137+
if !cli::confirm(question)? {
138+
return Ok(());
139+
};
140+
141+
if let Some(name) = auth.remove_stored_api_key()? {
142+
println!("The Hex API key `{name}` was been removed succesfully.")
143+
}
144+
145+
Ok(())
146+
}

compiler-cli/src/hex/auth.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{cli, fs::ConsoleWarningEmitter, http::HttpClient};
22
use gleam_core::{
3-
Error, Result, Warning, encryption, hex, paths::global_hexpm_credentials_path,
3+
Error, Result, Warning, encryption, error::wrap, hex, paths::global_hexpm_credentials_path,
44
warning::WarningEmitter,
55
};
66
use std::{rc::Rc, time::SystemTime};
@@ -148,6 +148,47 @@ encrypt your Hex API key.
148148
encrypted: encrypted.to_string(),
149149
}))
150150
}
151+
152+
/// Try to remove and revoke existing Hex API key, will return:
153+
///
154+
/// - Error: operation failed
155+
/// - Ok(None): operation successfull but there was no key stored, eg. wrong file format
156+
/// - Ok(Some(String)): operation successfull, plus the name of key
157+
pub fn remove_stored_api_key(&mut self) -> Result<Option<String>> {
158+
let path = global_hexpm_credentials_path();
159+
160+
let Some(EncryptedApiKey { name, .. }) = self.read_stored_api_key()? else {
161+
return Ok(None);
162+
};
163+
164+
let text = wrap(
165+
"
166+
We are going to delete and revoke your existing Hex API key.
167+
In order to do so, we need to use your current local password.",
168+
);
169+
170+
println!("{text}");
171+
172+
let Some(UnencryptedApiKey { unencrypted }) = self.read_and_decrypt_stored_api_key()?
173+
else {
174+
return Ok(None);
175+
};
176+
177+
println!("Deleting local Hex API key from disk...");
178+
crate::fs::delete_file(&path)?;
179+
println!("File {path} deleted successfully.");
180+
181+
println!("Revoking Hex API key from Hex...");
182+
self.runtime.block_on(hex::remove_api_key(
183+
&name,
184+
&self.hex_config,
185+
&unencrypted,
186+
&self.http,
187+
))?;
188+
println!("Key {name} revoked successfully.");
189+
190+
Ok(Some(name.to_string()))
191+
}
151192
}
152193

153194
impl Drop for HexAuthentication<'_> {

compiler-cli/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ enum Hex {
437437

438438
/// Authenticate with Hex
439439
Authenticate,
440+
441+
/// Deauthenticate from Hex
442+
Deauthenticate,
440443
}
441444

442445
#[derive(Subcommand, Debug)]
@@ -558,6 +561,8 @@ fn parse_and_run_command() -> Result<(), Error> {
558561

559562
Command::Hex(Hex::Authenticate) => hex::authenticate(),
560563

564+
Command::Hex(Hex::Deauthenticate) => hex::deauthenticate(),
565+
561566
Command::New(options) => new::create(options, COMPILER_VERSION),
562567

563568
Command::Shell => {

0 commit comments

Comments
 (0)