-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move logout functionality into auth crate
- Loading branch information
Showing
9 changed files
with
161 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,121 @@ | ||
use turborepo_ui::{cprintln, GREY, UI}; | ||
use tracing::error; | ||
use turborepo_api_client::Client; | ||
use turborepo_ui::{cprintln, GREY}; | ||
|
||
pub fn logout(ui: &UI) { | ||
cprintln!(ui, GREY, ">>> Logged out"); | ||
use crate::{Error, LogoutOptions}; | ||
|
||
pub fn logout<T: Client>(options: &LogoutOptions<T>) -> Result<(), Error> { | ||
if let Err(err) = remove_token(options) { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
cprintln!(options.ui, GREY, ">>> Logged out"); | ||
Ok(()) | ||
} | ||
|
||
fn remove_token<T: Client>(options: &LogoutOptions<T>) -> Result<(), Error> { | ||
let global_config_path = options.path; | ||
let content = global_config_path.read_to_string()?; | ||
|
||
let mut data: serde_json::Value = serde_json::from_str(&content)?; | ||
if data.is_object() && data.get("token").is_some() { | ||
// Since we do the `is_object` check above, we can safely unwrap here. | ||
data.as_object_mut().unwrap().remove("token"); | ||
} | ||
|
||
let new_content = serde_json::to_string_pretty(&data)?; | ||
global_config_path.create_with_contents(new_content)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use async_trait::async_trait; | ||
use reqwest::{RequestBuilder, Response}; | ||
use tempfile::TempDir; | ||
use turbopath::AbsoluteSystemPathBuf; | ||
use turborepo_ui::UI; | ||
use turborepo_vercel_api::{ | ||
SpacesResponse, Team, TeamsResponse, UserResponse, VerifiedSsoUser, | ||
}; | ||
use url::Url; | ||
|
||
use super::*; | ||
|
||
struct MockApiClient {} | ||
|
||
impl MockApiClient { | ||
fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Client for MockApiClient { | ||
async fn get_user(&self, _token: &str) -> turborepo_api_client::Result<UserResponse> { | ||
unimplemented!("get_user") | ||
} | ||
async fn get_teams(&self, _token: &str) -> turborepo_api_client::Result<TeamsResponse> { | ||
unimplemented!("get_teams") | ||
} | ||
async fn get_team( | ||
&self, | ||
_token: &str, | ||
_team_id: &str, | ||
) -> turborepo_api_client::Result<Option<Team>> { | ||
unimplemented!("get_team") | ||
} | ||
fn add_ci_header(_request_builder: RequestBuilder) -> RequestBuilder { | ||
unimplemented!("add_ci_header") | ||
} | ||
async fn get_spaces( | ||
&self, | ||
_token: &str, | ||
_team_id: Option<&str>, | ||
) -> turborepo_api_client::Result<SpacesResponse> { | ||
unimplemented!("get_spaces") | ||
} | ||
async fn verify_sso_token( | ||
&self, | ||
token: &str, | ||
_: &str, | ||
) -> turborepo_api_client::Result<VerifiedSsoUser> { | ||
Ok(VerifiedSsoUser { | ||
token: token.to_string(), | ||
team_id: Some("team_id".to_string()), | ||
}) | ||
} | ||
async fn handle_403(_response: Response) -> turborepo_api_client::Error { | ||
unimplemented!("handle_403") | ||
} | ||
fn make_url(&self, _endpoint: &str) -> turborepo_api_client::Result<Url> { | ||
unimplemented!("make_url") | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_remove_token() { | ||
let path = turborepo_dirs::config_dir() | ||
.unwrap() | ||
.join("turborepo") | ||
.join("config.json"); | ||
let abs_path = AbsoluteSystemPathBuf::try_from(path).expect("could not create path"); | ||
let content = r#"{"token":"some-token"}"#; | ||
abs_path | ||
.create_with_contents(content) | ||
.expect("could not create file"); | ||
|
||
let options = LogoutOptions { | ||
ui: &UI::new(false), | ||
api_client: &MockApiClient::new(), | ||
path: &abs_path, | ||
}; | ||
|
||
remove_token(&options).unwrap(); | ||
|
||
let new_content = abs_path.read_to_string().unwrap(); | ||
assert_eq!(new_content, "{}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,13 @@ | ||
use tracing::error; | ||
use turborepo_auth::logout as auth_logout; | ||
use turborepo_auth::{logout as auth_logout, LogoutOptions}; | ||
use turborepo_telemetry::events::command::CommandEventBuilder; | ||
|
||
use crate::{cli::Error, commands::CommandBase, config, rewrite_json::unset_path}; | ||
use crate::{cli::Error, commands::CommandBase}; | ||
|
||
pub fn logout(base: &mut CommandBase, _telemetry: CommandEventBuilder) -> Result<(), Error> { | ||
if let Err(err) = remove_token(base) { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
auth_logout(&base.ui); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn remove_token(base: &mut CommandBase) -> Result<(), Error> { | ||
let global_config_path = base.global_config_path()?; | ||
let before = global_config_path | ||
.read_existing_to_string_or(Ok("{}")) | ||
.map_err(|e| { | ||
Error::Config(config::Error::FailedToReadConfig { | ||
config_path: global_config_path.clone(), | ||
error: e, | ||
}) | ||
})?; | ||
|
||
if let Some(after) = unset_path(&before, &["token"], true)? { | ||
global_config_path.create_with_contents(after).map_err(|e| { | ||
Error::Config(config::Error::FailedToSetConfig { | ||
config_path: global_config_path.clone(), | ||
error: e, | ||
}) | ||
}) | ||
} else { | ||
Ok(()) | ||
} | ||
auth_logout(&LogoutOptions { | ||
ui: &base.ui, | ||
api_client: &base.api_client()?, | ||
path: &base.global_config_path()?, | ||
}) | ||
.map_err(Error::from) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters