Skip to content

Store Hex username in credentials file and improve key revocation error message #4446

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

scristobal
Copy link
Contributor

resolves #4319

Only try to revoke token if new and previous usernames match, otherwise hint the user to delete the token manually.

Summary of changes:

  • Store username in credentials file.
  • Change credentials file format to TOML.
  • Fallback to old format when TOML deserialization fails.

before:

% gleam hex authenticate

You already have a local Hex API token. Would you like to replace it
with a new one? [y/n]: y
https://hex.pm username: B
https://hex.pm password (will not be printed as you type): 

Please enter a new unique password. This will be used to locally
encrypt your Hex API key.

Local password (will not be printed as you type):
Encrypted Hex API key written to .../gleam/hex/hexpm/credentials
Deleting previous key `...` from Hex
error: Hex API failure

There was a problem when using the Hex API.

This was error from the Hex client library:

    an unexpected response was sent by Hex: 404 Not Found: {"message":"Page not found","status":404}

after:

% gleam hex authenticate

You already have a local Hex API key. Would you like to replace it
with a new one? [y/n]: y
https://hex.pm username: B
https://hex.pm password (will not be printed as you type):

Please enter a new unique password. This will be used to locally
encrypt your Hex API key.

Local password (will not be printed as you type):
Encrypted Hex API key written to /Users/samu/Library/Caches/gleam/hex/hexpm/credentials

Your previous Hex API key was created with username `A` which is different from the username
used to create the new Hex API key. You have to delete the key `Mac-1743837627` manually at https://hex.pm

@scristobal scristobal changed the title Store username in credentials file and improve key revocation error message Store Hex username in credentials file and improve key revocation error message Apr 5, 2025
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you. Left some notes inline

println!(
"
Your previous Hex API key was created with username `{}` which is different from the username
used to create the new Hex API key. You have to delete the key `{}` manually at https://hex.pm",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wrap before 80 columns! Could you call the wrap function on this please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

.is_err()
{
println!(
"There was an error deleting key `{}` from Hex. You have to delete the key manually at https://hex.pm",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this also please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!


let encrypted_key = EncryptedApiKey::load(&path);

if let Ok(encrypted_key) = encrypted_key {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This silently discards file system errors!

Let's move all of this loading logic into the load function, and have it read once, return an error if the file isn't readable, and also if the file is in neither format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, than you for your feedback.

I moved the logic to a separate function as suggested, it does also read better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gleam hex authenticate must be run twice to switch accounts.
2 participants