diff options
author | BlackDex <[email protected]> | 2023-03-09 16:31:28 +0100 |
---|---|---|
committer | BlackDex <[email protected]> | 2023-03-11 16:58:32 +0100 |
commit | 9e5b94924f5fea4ef405fa1f8aeb836b52f85a73 (patch) | |
tree | 83dcde146ea7557e0ef0a3209d681128a6dbc803 /src/api/admin.rs | |
parent | f21089900e86274c8a89a15a6ff79dfb9c433ca2 (diff) | |
download | vaultwarden-9e5b94924f5fea4ef405fa1f8aeb836b52f85a73.tar.gz vaultwarden-9e5b94924f5fea4ef405fa1f8aeb836b52f85a73.zip |
Merge ClientIp with Headers.
Since we now use the `ClientIp` Guard on a lot more places, it also
increases the size of binary, and the macro generated code because of
this extra Guard. By merging the `ClientIp` Guard with the several
`Header` guards we have it reduces the amount of code generated
(including LLVM IR), but also a small speedup in build time.
I also spotted some small `json!()` optimizations which also reduced the
amount of code generated.
Diffstat (limited to 'src/api/admin.rs')
-rw-r--r-- | src/api/admin.rs | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/api/admin.rs b/src/api/admin.rs index 651e51b1..0fd7c2cf 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -369,7 +369,7 @@ async fn get_user_json(uuid: String, _token: AdminToken, mut conn: DbConn) -> Js } #[post("/users/<uuid>/delete")] -async fn delete_user(uuid: String, _token: AdminToken, mut conn: DbConn, ip: ClientIp) -> EmptyResult { +async fn delete_user(uuid: String, token: AdminToken, mut conn: DbConn) -> EmptyResult { let user = get_user_or_404(&uuid, &mut conn).await?; // Get the user_org records before deleting the actual user @@ -383,7 +383,7 @@ async fn delete_user(uuid: String, _token: AdminToken, mut conn: DbConn, ip: Cli user_org.org_uuid, String::from(ACTING_ADMIN_USER), 14, // Use UnknownBrowser type - &ip.ip, + &token.ip.ip, &mut conn, ) .await; @@ -443,12 +443,7 @@ struct UserOrgTypeData { } #[post("/users/org_type", data = "<data>")] -async fn update_user_org_type( - data: Json<UserOrgTypeData>, - _token: AdminToken, - mut conn: DbConn, - ip: ClientIp, -) -> EmptyResult { +async fn update_user_org_type(data: Json<UserOrgTypeData>, token: AdminToken, mut conn: DbConn) -> EmptyResult { let data: UserOrgTypeData = data.into_inner(); let mut user_to_edit = @@ -489,7 +484,7 @@ async fn update_user_org_type( data.org_uuid, String::from(ACTING_ADMIN_USER), 14, // Use UnknownBrowser type - &ip.ip, + &token.ip.ip, &mut conn, ) .await; @@ -724,15 +719,24 @@ async fn backup_db(_token: AdminToken, mut conn: DbConn) -> EmptyResult { } } -pub struct AdminToken {} +pub struct AdminToken { + ip: ClientIp, +} #[rocket::async_trait] impl<'r> FromRequest<'r> for AdminToken { type Error = &'static str; async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { + let ip = match ClientIp::from_request(request).await { + Outcome::Success(ip) => ip, + _ => err_handler!("Error getting Client IP"), + }; + if CONFIG.disable_admin_token() { - Outcome::Success(Self {}) + Outcome::Success(Self { + ip, + }) } else { let cookies = request.cookies(); @@ -741,19 +745,16 @@ impl<'r> FromRequest<'r> for AdminToken { None => return Outcome::Failure((Status::Unauthorized, "Unauthorized")), }; - let ip = match ClientIp::from_request(request).await { - Outcome::Success(ip) => ip.ip, - _ => err_handler!("Error getting Client IP"), - }; - if decode_admin(access_token).is_err() { // Remove admin cookie cookies.remove(Cookie::build(COOKIE_NAME, "").path(admin_path()).finish()); - error!("Invalid or expired admin JWT. IP: {}.", ip); + error!("Invalid or expired admin JWT. IP: {}.", &ip.ip); return Outcome::Failure((Status::Unauthorized, "Session expired")); } - Outcome::Success(Self {}) + Outcome::Success(Self { + ip, + }) } } } |