Signed-off-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
Xuanwo 2023-02-26 15:09:53 +08:00 коммит произвёл GitHub
Родитель 1005ff0031
Коммит 408ae947d7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 67 добавлений и 27 удалений

55
Cargo.lock сгенерированный
Просмотреть файл

@ -152,12 +152,6 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5"
[[package]]
name = "base64"
version = "0.21.0"
@ -2032,7 +2026,7 @@ dependencies = [
"assert_cmd",
"async-trait",
"atty",
"base64 0.20.0",
"base64 0.21.0",
"bincode",
"blake3",
"byteorder",
@ -2199,6 +2193,15 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0efd8caf556a6cebd3b285caf480045fcc1ac04f6bd786b09a6f11af30c4fcf4"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@ -2649,11 +2652,36 @@ dependencies = [
[[package]]
name = "toml"
version = "0.5.11"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
checksum = "f7afcae9e3f0fe2c370fd4657108972cbb2fa9db1b9f84849cefd80741b01cb6"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a1eb0622d28f4b9c90adc4ea4b2b46b47663fde9ac5fafcb14a1369d5508825"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
@ -3144,6 +3172,15 @@ version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
[[package]]
name = "winnow"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf09497b8f8b5ac5d3bb4d05c0a99be20f26fd3d5f2db7b0716e946d5103658"
dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.10.1"

Просмотреть файл

@ -27,7 +27,7 @@ anyhow = "1.0"
ar = "0.9"
async-trait = "0.1"
atty = "0.2.6"
base64 = "0.20"
base64 = "0.21"
bincode = "1"
blake3 = "1"
byteorder = "1.0"
@ -70,7 +70,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "io-util", "time", "net"
tokio-serde = "0.8"
tokio-util = { version = "0.7", features = ["codec", "io"] }
tower = "0.4"
toml = "0.5"
toml = "0.7"
url = { version = "2", optional = true }
uuid = { version = "1.2", features = ["v4"] }
walkdir = "2"

Просмотреть файл

@ -22,6 +22,7 @@ extern crate tar;
extern crate void;
use anyhow::{bail, Context, Error, Result};
use base64::Engine;
use rand::{rngs::OsRng, RngCore};
use sccache::config::{
scheduler as scheduler_config, server as server_config, INSECURE_DIST_CLIENT_TOKEN,
@ -144,7 +145,7 @@ fn run(command: Command) -> Result<i32> {
let mut bytes = vec![0; num_bytes];
OsRng.fill_bytes(&mut bytes);
// As long as it can be copied, it doesn't matter if this is base64 or hex etc
println!("{}", base64::encode_engine(&bytes, &BASE64_URL_SAFE_ENGINE));
println!("{}", BASE64_URL_SAFE_ENGINE.encode(bytes));
Ok(0)
}
Command::Auth(AuthSubcommand::JwtHS256ServerToken {
@ -152,7 +153,7 @@ fn run(command: Command) -> Result<i32> {
server_id,
}) => {
let header = jwt::Header::new(jwt::Algorithm::HS256);
let secret_key = base64::decode_engine(&secret_key, &BASE64_URL_SAFE_ENGINE)?;
let secret_key = BASE64_URL_SAFE_ENGINE.decode(&secret_key)?;
let token = create_jwt_server_token(server_id, &header, &secret_key)
.context("Failed to create server token")?;
println!("{}", token);
@ -197,7 +198,8 @@ fn run(command: Command) -> Result<i32> {
Box::new(move |server_token| check_server_token(server_token, &token))
}
scheduler_config::ServerAuth::JwtHS256 { secret_key } => {
let secret_key = base64::decode_engine(&secret_key, &BASE64_URL_SAFE_ENGINE)
let secret_key = BASE64_URL_SAFE_ENGINE
.decode(&secret_key)
.context("Secret key base64 invalid")?;
if secret_key.len() != 256 / 8 {
bail!("Size of secret key incorrect")

Просмотреть файл

@ -1,5 +1,6 @@
use crate::jwt;
use anyhow::{bail, Context, Result};
use base64::Engine;
use sccache::dist::http::{ClientAuthCheck, ClientVisibleMsg};
use sccache::util::{new_reqwest_blocking_client, BASE64_URL_SAFE_ENGINE};
use std::collections::HashMap;
@ -29,9 +30,11 @@ impl Jwk {
}
// JWK is big-endian, openssl bignum from_slice is big-endian
let n = base64::decode_engine(&self.n, &BASE64_URL_SAFE_ENGINE)
let n = BASE64_URL_SAFE_ENGINE
.decode(&self.n)
.context("Failed to base64 decode n")?;
let e = base64::decode_engine(&self.e, &BASE64_URL_SAFE_ENGINE)
let e = BASE64_URL_SAFE_ENGINE
.decode(&self.e)
.context("Failed to base64 decode e")?;
let n_bn = openssl::bn::BigNum::from_slice(&n)
.context("Failed to create openssl bignum from n")?;

Просмотреть файл

@ -848,7 +848,7 @@ impl CachedConfig {
fn save_file_config(c: &CachedFileConfig) -> Result<()> {
let file_conf_path = &*CACHED_CONFIG_PATH;
let mut file = File::create(file_conf_path).context("Could not open config for writing")?;
file.write_all(&toml::to_vec(c).unwrap())
file.write_all(toml::to_string(c).unwrap().as_bytes())
.map_err(Into::into)
}
}

5
src/dist/client_auth.rs поставляемый
Просмотреть файл

@ -86,6 +86,7 @@ mod code_grant_pkce {
};
use crate::util::new_reqwest_blocking_client;
use crate::util::BASE64_URL_SAFE_ENGINE;
use base64::Engine;
use futures::channel::oneshot;
use hyper::{Body, Method, Request, Response, StatusCode};
use rand::{rngs::OsRng, RngCore};
@ -146,10 +147,10 @@ mod code_grant_pkce {
pub fn generate_verifier_and_challenge() -> Result<(String, String)> {
let mut code_verifier_bytes = vec![0; NUM_CODE_VERIFIER_BYTES];
OsRng.fill_bytes(&mut code_verifier_bytes);
let code_verifier = base64::encode_engine(&code_verifier_bytes, &BASE64_URL_SAFE_ENGINE);
let code_verifier = BASE64_URL_SAFE_ENGINE.encode(&code_verifier_bytes);
let mut hasher = Sha256::new();
hasher.update(&code_verifier);
let code_challenge = base64::encode_engine(hasher.finalize(), &BASE64_URL_SAFE_ENGINE);
let code_challenge = BASE64_URL_SAFE_ENGINE.encode(hasher.finalize());
Ok((code_verifier, code_challenge))
}

Просмотреть файл

@ -31,11 +31,8 @@ use std::time::Duration;
use crate::errors::*;
/// The url safe engine for base64.
pub const BASE64_URL_SAFE_ENGINE: base64::engine::fast_portable::FastPortable =
base64::engine::fast_portable::FastPortable::from(
&base64::alphabet::URL_SAFE,
base64::engine::fast_portable::NO_PAD,
);
pub const BASE64_URL_SAFE_ENGINE: base64::engine::GeneralPurpose =
base64::engine::general_purpose::URL_SAFE_NO_PAD;
#[derive(Clone)]
pub struct Digest {

Просмотреть файл

@ -261,13 +261,13 @@ fn test_auth_with_config(dist_auth: sccache::config::DistAuth) {
}
}
println!("Validating cached config");
let mut cached_config_bytes = vec![];
let mut cached_config_string = String::new();
fs::File::open(sccache_cached_config_path)
.unwrap()
.read_to_end(&mut cached_config_bytes)
.read_to_string(&mut cached_config_string)
.unwrap();
let cached_config: sccache::config::CachedFileConfig =
toml::from_slice(&cached_config_bytes).unwrap();
toml::from_str(&cached_config_string).unwrap();
assert_eq!(cached_config.dist.auth_tokens.len(), 1);
}