Files
zesdex/src/service/oauth/pkce.rs
T
asepharyanaandClaude Opus 4.8 e29dfadaa7 refactor: surface silent fallbacks with eprintln! logging
Add eprintln! logging before fallback values in 7 files where errors
were previously swallowed without visibility:

- [stream] malformed JSON chunk, missing usage tokens, missing tool call index
- [mcp] missing result fields, client builder failure, response body read error
- [subagent] missing API key in settings, all resolution paths exhausted
- [state] memory_dir no-parent, session_id missing, lock poisoned, store_base_dir
- [input] missing default_model for provider
- [session] corrupt agents.json parse failure
- [pkce] clock-before-epoch on random byte generation

All fallback values are preserved — this adds observability without
changing behavior for callers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-12 10:50:34 +07:00

45 lines
1.1 KiB
Rust

use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use sha2::{Sha256, Digest};
const VERIFIER_LENGTH: usize = 64;
pub struct CodeVerifier(String);
impl CodeVerifier {
pub fn new() -> Self {
let bytes: Vec<u8> = (0..VERIFIER_LENGTH).map(|_| rand_byte()).collect();
CodeVerifier(URL_SAFE_NO_PAD.encode(&bytes))
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn challenge(&self) -> CodeChallenge {
let mut hasher = Sha256::new();
hasher.update(self.0.as_bytes());
let digest = hasher.finalize();
CodeChallenge(URL_SAFE_NO_PAD.encode(digest))
}
}
fn rand_byte() -> u8 {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| {
eprintln!("[pkce] system time before UNIX_EPOCH, using 0 for random byte");
std::time::Duration::default()
})
.subsec_nanos();
(nanos & 0xFF) as u8
}
pub struct CodeChallenge(String);
impl CodeChallenge {
pub fn as_str(&self) -> &str {
&self.0
}
}