use reqsign_core::{Error, Result};
#[derive(Debug, serde::Deserialize)]
pub(super) struct EntraTokenResponse {
pub(super) access_token: String,
pub(super) expires_in: u64,
}
pub(super) fn parse_token_response(
body: &[u8],
response_name: &'static str,
) -> Result<EntraTokenResponse> {
serde_json::from_slice(body).map_err(|err| {
Error::unexpected(format!("failed to parse {response_name}")).with_source(err)
})
}
pub(super) fn token_request_error(request_name: &'static str, status: http::StatusCode) -> Error {
Error::credential_invalid(format!("{request_name} failed"))
.with_context(format!("http_status: {status}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_redacted_real_entra_response() {
let response = include_bytes!("../../tests/fixtures/entra_token_response.json");
let token = parse_token_response(response, "Microsoft Entra token response")
.expect("real response fixture must parse");
assert_eq!(token.access_token, "REDACTED");
assert_eq!(token.expires_in, 3599);
}
}