Skip to content

Commit

Permalink
Handle legacy credentials and fix accounts query
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed May 29, 2024
1 parent 18ec875 commit 1c2e772
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{constant, credential::AccessToken};
pub enum Error {
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error("Invalid token, authenticate with `slot auth login`")]
#[error("Invalid token, please authenticate with `slot auth login`")]
Unauthorized,
}

Expand Down
6 changes: 5 additions & 1 deletion src/command/deployments/accounts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ query KatanaAccounts($project: String!) {
... on KatanaConfig {
seed
genesis
accounts
accounts {
address
publicKey
privateKey
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/deployments/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl AccountsArgs {
}

let total = match config.accounts {
Some(accounts) => accounts as u16,
Some(accounts) => accounts.len() as u16,
None => 10,
};

Expand Down
31 changes: 27 additions & 4 deletions src/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ use crate::command::auth::info::me::MeMe;
const SLOT_DIR: &str = "slot";
const CREDENTIALS_FILE: &str = "credentials.json";

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
IO(#[from] io::Error),
#[error("No credentials found, please authenticate with `slot auth login`")]
Unauthorized,
#[error("Legacy credentials found, please reauthenticate with `slot auth login`")]
LegacyCredentials,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessToken {
pub token: String,
pub r#type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LegacyCredentials {
pub access_token: String,
pub token_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Credentials {
#[serde(flatten)]
Expand All @@ -29,11 +45,18 @@ impl Credentials {
}
}

pub fn load() -> io::Result<Self> {
pub fn load() -> Result<Self, Error> {
let path = get_file_path();
let content = fs::read_to_string(path)?;
let credentials = serde_json::from_str(&content)?;
Ok(credentials)
if !path.exists() {
return Err(Error::Unauthorized);
}

let content = fs::read_to_string(path.clone())?;
let credentials: Result<Credentials, _> = serde_json::from_str(&content);
match credentials {
Ok(creds) => Ok(creds),
Err(_) => Err(Error::LegacyCredentials),
}
}

pub fn write(&self) -> io::Result<()> {
Expand Down

0 comments on commit 1c2e772

Please sign in to comment.