mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-07-29 03:15:07 +03:00
Switch to Edition 2024, more clippy lints, and less macro calls (#7200)
* Update to Rust 2024 Edition Updated to the Rust 2024 Edition and added and fixed several lint checks. This is a large change which, because of the extra lints, added some possible fixes for issues. Signed-off-by: BlackDex <black.dex@gmail.com> * Reorder and merge imports Signed-off-by: BlackDex <black.dex@gmail.com> * Remove "db_run!" macro calls where possible Signed-off-by: BlackDex <black.dex@gmail.com> --------- Signed-off-by: BlackDex <black.dex@gmail.com>
This commit is contained in:
+33
-29
@@ -6,15 +6,15 @@ use regex::Regex;
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
CONFIG,
|
||||
api::ApiResult,
|
||||
auth,
|
||||
auth::{AuthMethod, AuthTokens, TokenWrapper, BW_EXPIRATION, DEFAULT_REFRESH_VALIDITY},
|
||||
auth::{AuthMethod, AuthTokens, BW_EXPIRATION, DEFAULT_REFRESH_VALIDITY, TokenWrapper},
|
||||
db::{
|
||||
models::{Device, OIDCAuthenticatedUser, SsoAuth, SsoUser, User},
|
||||
DbConn,
|
||||
models::{Device, OIDCAuthenticatedUser, SsoAuth, SsoUser, User},
|
||||
},
|
||||
sso_client::Client,
|
||||
CONFIG,
|
||||
};
|
||||
|
||||
pub static FAKE_SSO_IDENTIFIER: &str = "00000000-01DC-01DC-01DC-000000000000";
|
||||
@@ -123,7 +123,7 @@ pub fn encode_ssotoken_claims() -> String {
|
||||
nbf: time_now.timestamp(),
|
||||
exp: (time_now + chrono::TimeDelta::try_minutes(2).unwrap()).timestamp(),
|
||||
iss: SSO_JWT_ISSUER.to_string(),
|
||||
sub: "vaultwarden".to_string(),
|
||||
sub: "vaultwarden".to_owned(),
|
||||
};
|
||||
|
||||
auth::encode_jwt(&claims)
|
||||
@@ -171,12 +171,14 @@ fn decode_token_claims(token_name: &str, token: &str) -> ApiResult<BasicTokenCla
|
||||
}
|
||||
|
||||
pub fn decode_state(base64_state: &str) -> ApiResult<OIDCState> {
|
||||
let state = match data_encoding::BASE64.decode(base64_state.as_bytes()) {
|
||||
Ok(vec) => match String::from_utf8(vec) {
|
||||
Ok(valid) => OIDCState(valid),
|
||||
Err(_) => err!(format!("Invalid utf8 chars in {base64_state} after base64 decoding")),
|
||||
},
|
||||
Err(_) => err!(format!("Failed to decode {base64_state} using base64")),
|
||||
let state = if let Ok(vec) = data_encoding::BASE64.decode(base64_state.as_bytes()) {
|
||||
if let Ok(valid) = String::from_utf8(vec) {
|
||||
OIDCState(valid)
|
||||
} else {
|
||||
err!(format!("Invalid utf8 chars in {base64_state} after base64 decoding"))
|
||||
}
|
||||
} else {
|
||||
err!(format!("Failed to decode {base64_state} using base64"))
|
||||
};
|
||||
|
||||
Ok(state)
|
||||
@@ -193,12 +195,15 @@ pub async fn authorize_url(
|
||||
) -> ApiResult<Url> {
|
||||
let redirect_uri = match client_id {
|
||||
"web" | "browser" => format!("{}/sso-connector.html", CONFIG.domain()),
|
||||
"desktop" | "mobile" => "bitwarden://sso-callback".to_string(),
|
||||
"desktop" | "mobile" => "bitwarden://sso-callback".to_owned(),
|
||||
"cli" => {
|
||||
let port_regex = Regex::new(r"^http://localhost:([0-9]{4})$").unwrap();
|
||||
match port_regex.captures(raw_redirect_uri).and_then(|captures| captures.get(1).map(|c| c.as_str())) {
|
||||
Some(port) => format!("http://localhost:{port}"),
|
||||
None => err!("Failed to extract port number"),
|
||||
if let Some(port) =
|
||||
port_regex.captures(raw_redirect_uri).and_then(|captures| captures.get(1).map(|c| c.as_str()))
|
||||
{
|
||||
format!("http://localhost:{port}")
|
||||
} else {
|
||||
err!("Failed to extract port number")
|
||||
}
|
||||
}
|
||||
_ => err!(format!("Unsupported client {client_id}")),
|
||||
@@ -246,9 +251,8 @@ pub async fn exchange_code(
|
||||
) -> ApiResult<(SsoAuth, OIDCAuthenticatedUser)> {
|
||||
use openidconnect::OAuth2TokenResponse;
|
||||
|
||||
let mut sso_auth = match SsoAuth::find_by_code(code, conn).await {
|
||||
None => err!(format!("Invalid code cannot retrieve sso auth")),
|
||||
Some(sso_auth) => sso_auth,
|
||||
let Some(mut sso_auth) = SsoAuth::find_by_code(code, conn).await else {
|
||||
err!("Invalid code cannot retrieve sso auth")
|
||||
};
|
||||
|
||||
if let Some(authenticated_user) = sso_auth.auth_response.clone() {
|
||||
@@ -286,8 +290,8 @@ pub async fn exchange_code(
|
||||
|
||||
let user_name = id_claims.preferred_username().or(user_info.preferred_username()).map(|un| un.to_string());
|
||||
|
||||
let refresh_token = token_response.refresh_token().map(|t| t.secret());
|
||||
if refresh_token.is_none() && CONFIG.sso_scopes_vec().contains(&"offline_access".to_string()) {
|
||||
let refresh_token = token_response.refresh_token().map(openidconnect::RefreshToken::secret);
|
||||
if refresh_token.is_none() && CONFIG.sso_scopes_vec().contains(&"offline_access".to_owned()) {
|
||||
error!("Scope offline_access is present but response contain no refresh_token");
|
||||
}
|
||||
|
||||
@@ -331,7 +335,9 @@ pub async fn redeem(
|
||||
user_sso.save(conn).await?;
|
||||
}
|
||||
|
||||
if !CONFIG.sso_auth_only_not_session() {
|
||||
if CONFIG.sso_auth_only_not_session() {
|
||||
Ok(AuthTokens::new(device, user, AuthMethod::Sso, client_id))
|
||||
} else {
|
||||
let now = Utc::now();
|
||||
|
||||
let (ap_nbf, ap_exp) =
|
||||
@@ -344,9 +350,7 @@ pub async fn redeem(
|
||||
let access_claims =
|
||||
auth::LoginJwtClaims::new(device, user, ap_nbf, ap_exp, AuthMethod::Sso.scope_vec(), client_id, now);
|
||||
|
||||
_create_auth_tokens(device, auth_user.refresh_token, access_claims, auth_user.access_token)
|
||||
} else {
|
||||
Ok(AuthTokens::new(device, user, AuthMethod::Sso, client_id))
|
||||
create_auth_tokens_impl(device, auth_user.refresh_token, access_claims, auth_user.access_token)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,7 +364,9 @@ pub fn create_auth_tokens(
|
||||
access_token: String,
|
||||
expires_in: Option<Duration>,
|
||||
) -> ApiResult<AuthTokens> {
|
||||
if !CONFIG.sso_auth_only_not_session() {
|
||||
if CONFIG.sso_auth_only_not_session() {
|
||||
Ok(AuthTokens::new(device, user, AuthMethod::Sso, client_id))
|
||||
} else {
|
||||
let now = Utc::now();
|
||||
|
||||
let (ap_nbf, ap_exp) = match (decode_token_claims("access_token", &access_token), expires_in) {
|
||||
@@ -372,13 +378,11 @@ pub fn create_auth_tokens(
|
||||
let access_claims =
|
||||
auth::LoginJwtClaims::new(device, user, ap_nbf, ap_exp, AuthMethod::Sso.scope_vec(), client_id, now);
|
||||
|
||||
_create_auth_tokens(device, refresh_token, access_claims, access_token)
|
||||
} else {
|
||||
Ok(AuthTokens::new(device, user, AuthMethod::Sso, client_id))
|
||||
create_auth_tokens_impl(device, refresh_token, access_claims, access_token)
|
||||
}
|
||||
}
|
||||
|
||||
fn _create_auth_tokens(
|
||||
fn create_auth_tokens_impl(
|
||||
device: &Device,
|
||||
refresh_token: Option<String>,
|
||||
access_claims: auth::LoginJwtClaims,
|
||||
@@ -462,7 +466,7 @@ pub async fn exchange_refresh_token(
|
||||
now,
|
||||
);
|
||||
|
||||
_create_auth_tokens(device, None, access_claims, access_token)
|
||||
create_auth_tokens_impl(device, None, access_claims, access_token)
|
||||
}
|
||||
None => err!("No token present while in SSO"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user