Skip to content

Commit eb8562c

Browse files
committed
Added hex deauthenticate command
1 parent 4513aca commit eb8562c

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@
188188
deprecated environment variables.
189189
([Surya Rose](https://github.com/GearsDatapacks))
190190

191+
- Added `gleam hex deauthenticate` command.
192+
([Samuel Cristobal](https://github.com/scristobal))
193+
191194
### Language server
192195

193196
- The code action to add missing labels to function now also works in patterns:

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

154197
impl Drop for HexAuthentication<'_> {

compiler-cli/src/lib.rs

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

450450
/// Authenticate with Hex
451451
Authenticate,
452+
453+
/// Deauthenticate from Hex
454+
Deauthenticate,
452455
}
453456

454457
#[derive(Subcommand, Debug)]
@@ -570,6 +573,8 @@ fn parse_and_run_command() -> Result<(), Error> {
570573

571574
Command::Hex(Hex::Authenticate) => hex::authenticate(),
572575

576+
Command::Hex(Hex::Deauthenticate) => hex::deauthenticate(),
577+
573578
Command::New(options) => new::create(options, COMPILER_VERSION),
574579

575580
Command::Shell => {

0 commit comments

Comments
 (0)