Skip to content
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

fix(api): use accepted value for "Bitwarden-Client-Name" header #219

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 26 additions & 15 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<'de> serde::Deserialize<'de> for TwoFactorProviderType {
D: serde::Deserializer<'de>,
{
struct TwoFactorProviderTypeVisitor;
impl<'de> serde::de::Visitor<'de> for TwoFactorProviderTypeVisitor {
impl serde::de::Visitor<'_> for TwoFactorProviderTypeVisitor {
type Value = TwoFactorProviderType;

fn expecting(
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'de> serde::Deserialize<'de> for KdfType {
D: serde::Deserializer<'de>,
{
struct KdfTypeVisitor;
impl<'de> serde::de::Visitor<'de> for KdfTypeVisitor {
impl serde::de::Visitor<'_> for KdfTypeVisitor {
type Value = KdfType;

fn expecting(
Expand Down Expand Up @@ -767,6 +767,13 @@ struct FoldersPostReq {
name: String,
}

// Used for the Bitwarden-Client-Name header. Accepted values:
// https://github.com/bitwarden/server/blob/main/src/Core/Enums/BitwardenClient.cs
const BITWARDEN_CLIENT: &str = "cli";

// DeviceType.LinuxDesktop, as per Bitwarden API device types.
const DEVICE_TYPE: u8 = 8;

#[derive(Debug)]
pub struct Client {
base_url: String,
Expand Down Expand Up @@ -796,12 +803,24 @@ impl Client {
let mut default_headers = axum::http::HeaderMap::new();
default_headers.insert(
"Bitwarden-Client-Name",
axum::http::HeaderValue::from_static(env!("CARGO_PKG_NAME")),
axum::http::HeaderValue::from_static(BITWARDEN_CLIENT),
);
default_headers.insert(
"Bitwarden-Client-Version",
axum::http::HeaderValue::from_static(env!("CARGO_PKG_VERSION")),
);
default_headers.append(
"Device-Type",
// unwrap is safe here because DEVICE_TYPE is a number and digits
// are valid ASCII
axum::http::HeaderValue::from_str(&DEVICE_TYPE.to_string())
.unwrap(),
);
let user_agent = format!(
"{}/{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
);
if let Some(client_cert_path) = self.client_cert_path.as_ref() {
let mut buf = Vec::new();
let mut f = tokio::fs::File::open(client_cert_path)
Expand All @@ -819,22 +838,14 @@ impl Client {
let pem = reqwest::Identity::from_pem(&buf)
.map_err(|e| Error::CreateReqwestClient { source: e })?;
Ok(reqwest::Client::builder()
.user_agent(format!(
"{}/{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
))
.user_agent(user_agent)
.identity(pem)
.default_headers(default_headers)
.build()
.map_err(|e| Error::CreateReqwestClient { source: e })?)
} else {
Ok(reqwest::Client::builder()
.user_agent(format!(
"{}/{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
))
.user_agent(user_agent)
.default_headers(default_headers)
.build()
.map_err(|e| Error::CreateReqwestClient { source: e })?)
Expand Down Expand Up @@ -885,7 +896,7 @@ impl Client {
// XXX unwraps here are not necessarily safe
client_id: String::from_utf8(apikey.client_id().to_vec())
.unwrap(),
device_type: 8,
device_type: u32::from(DEVICE_TYPE),
device_identifier: device_id.to_string(),
device_name: "rbw".to_string(),
device_push_token: String::new(),
Expand Down Expand Up @@ -942,7 +953,7 @@ impl Client {
grant_type: "authorization_code".to_string(),
scope: "api offline_access".to_string(),
client_id: "cli".to_string(),
device_type: 8,
device_type: u32::from(DEVICE_TYPE),
device_identifier: device_id.to_string(),
device_name: "rbw".to_string(),
device_push_token: String::new(),
Expand Down
3 changes: 1 addition & 2 deletions src/bin/rbw/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1643,9 +1643,8 @@ fn check_config() -> anyhow::Result<()> {
}

fn version_or_quit() -> anyhow::Result<u32> {
crate::actions::version().map_err(|e| {
crate::actions::version().inspect_err(|_e| {
let _ = crate::actions::quit();
e
})
}

Expand Down
Loading