Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Add support for printing api token email on whoami #1212

Merged
merged 5 commits into from
Apr 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/commands/whoami/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::terminal::emoji;
use crate::terminal::{emoji, message};

use cloudflare::endpoints::account::{self, Account};
use cloudflare::endpoints::user::GetUserDetails;
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::response::ApiFailure;

use prettytable::{Cell, Row, Table};

pub fn whoami(user: &GlobalUser) -> Result<(), failure::Error> {
// If using email + API key for auth, simply prints out email from config file.
// Attempt to print email for both GlobalKeyAuth and TokenAuth users
let auth: String = match user {
GlobalUser::GlobalKeyAuth { email, .. } => {
format!("a Global API Key, associated with the email '{}'", email,)
}
GlobalUser::TokenAuth { .. } => "an API Token".to_string(),
GlobalUser::TokenAuth { .. } => {
let token_auth_email: String = fetch_api_token_email(user)?;
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved

if token_auth_email == "" {
"an API Token".to_string()
} else {
format!(
"an API Token, associated with the email '{}'",
token_auth_email,
)
}
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved
}
};

println!("\n{} You are logged in with {}.\n", emoji::WAVING, auth,);
Expand All @@ -23,6 +36,26 @@ pub fn whoami(user: &GlobalUser) -> Result<(), failure::Error> {
Ok(())
}

fn fetch_api_token_email(user: &GlobalUser) -> Result<String, failure::Error> {
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved
let client = http::cf_v4_client(user)?;
let response = client.request(&GetUserDetails {});
match response {
Ok(res) => Ok(res.result.email),
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved
Err(e) => match e {
ApiFailure::Error(_, api_errors) => {
let error = &api_errors.errors[0];
if error.code == 9109 {
message::info("Your token is missing the 'User Details: Read' permission.\n\nPlease generate and auth with a new token that has these perms to be able to identify this token.\n");
Ok("".to_string())
} else {
Ok("".to_string())
}
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved
}
ApiFailure::Invalid(_) => failure::bail!(http::format_error(e, None)),
},
}
}

fn fetch_accounts(user: &GlobalUser) -> Result<Vec<Account>, failure::Error> {
let client = http::cf_v4_client(user)?;
let response = client.request(&account::ListAccounts { params: None });
Expand All @@ -39,7 +72,7 @@ fn format_accounts(user: &GlobalUser, accounts: Vec<Account>) -> Table {

if let GlobalUser::TokenAuth { .. } = user {
if accounts.is_empty() {
println!("Your token is missing the 'Account Settings: Read' permission.\n\nPlease generate and auth with a new token that has these perms to be able to list your accounts.\n");
message::info("Your token is missing the 'Account Settings: Read' permission.\n\nPlease generate and auth with a new token that has these perms to be able to list your accounts.\n");
dhaynespls marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down