pub fn serialize_flattened_json_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JwsHeaderSet,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JwsHeader) -> Option<&'a dyn JwsSigner>,
    {
        (|| -> anyhow::Result<String> {
            let protected_map = header.claims_set(true);
            let mut b64 = true;
            match protected_map.get("crit") {
                Some(Value::Array(vals)) => {
                    if vals.iter().any(|val| match val {
                        Value::String(val2) => val2 == "b64",
                        _ => false,
                    }) {
                        b64 = match protected_map.get("b64") {
                            Some(Value::Bool(val3)) => *val3,
                            _ => false,
                        };
                    }
                }
                _ => {}
            }

            let merged_map = header.to_map();
            let merged = JwsHeader::from_map(merged_map)?;
            let signer = match selector(&merged) {
                Some(val) => val,
                None => bail!("A signer is not found."),
            };

            let mut protected_map = protected_map.clone();

            match merged.algorithm() {
                Some(val) if val == signer.algorithm().name() => {}
                Some(_) => bail!("A signer is unmatched."),
                None => {
                    protected_map.insert(
                        "alg".to_string(),
                        Value::String(signer.algorithm().name().to_string()),
                    );
                }
            }

            if let None = merged.key_id() {
                if let Some(key_id) = signer.key_id() {
                    protected_map.insert("kid".to_string(), Value::String(key_id.to_string()));
                }
            }

            let protected_json = serde_json::to_string(&protected_map)?;
            let protected_b64 = base64::encode_config(protected_json, base64::URL_SAFE_NO_PAD);

            let payload_b64;
            let payload = if b64 {
                payload_b64 = base64::encode_config(payload, base64::URL_SAFE_NO_PAD);
                &payload_b64
            } else {
                std::str::from_utf8(payload)?
            };

            let message = format!("{}.{}", &protected_b64, payload);
            let signature = signer.sign(message.as_bytes())?;

            let mut json = String::new();
            json.push_str("{\"protected\":\"");
            json.push_str(&protected_b64);
            json.push_str("\"");

            let unprotected = header.claims_set(false);
            if unprotected.len() > 0 {
                let unprotcted_json = serde_json::to_string(unprotected)?;
                json.push_str(",\"header\":");
                json.push_str(&unprotcted_json);
            }

            json.push_str(",\"payload\":\"");
            json.push_str(&payload);
            json.push_str("\"");

            json.push_str(",\"signature\":\"");
            base64::encode_config_buf(&signature, base64::URL_SAFE_NO_PAD, &mut json);
            json.push_str("\"}");

            Ok(json)
        })()
        .map_err(|err| match err.downcast::<JoseError>() {
            Ok(err) => err,
            Err(err) => JoseError::InvalidJwsFormat(err),
        })
    }
/// Return a representation of the data that is formatted by flatted json serialization.
///
/// # Arguments
///
/// * `payload` - The payload data.
/// * `header` - The JWS protected and unprotected header claims.
/// * `selector` - a function for selecting the signing algorithm.
Return a representation of the data that is formatted by flatted json serialization.
Return a representation of the data that is formatted by flatted json serialization .
pub fn serialize_flattened_json_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JwsHeaderSet,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JwsHeader) -> Option<&'a dyn JwsSigner>,
    {
        (|| -> anyhow::Result<String> {
            let protected_map = header.claims_set(true);
            let mut b64 = true;
            match protected_map.get("crit") {
                Some(Value::Array(vals)) => {
                    if vals.iter().any(|val| match val {
                        Value::String(val2) => val2 == "b64",
                        _ => false,
                    }) {
                        b64 = match protected_map.get("b64") {
                            Some(Value::Bool(val3)) => *val3,
                            _ => false,
                        };
                    }
                }
                _ => {}
            }
            let merged_map = header.to_map();
            let merged = JwsHeader::from_map(merged_map)?;
            let signer = match selector(&merged) {
                Some(val) => val,
                None => bail!("A signer is not found."),
            };
            let mut protected_map = protected_map.clone();
            match merged.algorithm() {
                Some(val) if val == signer.algorithm().name() => {}
                Some(_) => bail!("A signer is unmatched."),
                None => {
                    protected_map.insert(
                        "alg".to_string(),
                        Value::String(signer.algorithm().name().to_string()),
                    );
                }
            }
            if let None = merged.key_id() {
                if let Some(key_id) = signer.key_id() {
                    protected_map.insert("kid".to_string(), Value::String(key_id.to_string()));
                }
            }
            let protected_json = serde_json::to_string(&protected_map)?;
            let protected_b64 = base64::encode_config(protected_json, base64::URL_SAFE_NO_PAD);
            let payload_b64;
            let payload = if b64 {
                payload_b64 = base64::encode_config(payload, base64::URL_SAFE_NO_PAD);
                &payload_b64
            } else {
                std::str::from_utf8(payload)?
            };
            let message = format!("{}.{}", &protected_b64, payload);
            let signature = signer.sign(message.as_bytes())?;
            let mut json = String::new();
            json.push_str("{\"protected\":\"");
            json.push_str(&protected_b64);
            json.push_str("\"");
            let unprotected = header.claims_set(false);
            if unprotected.len() > 0 {
                let unprotcted_json = serde_json::to_string(unprotected)?;
                json.push_str(",\"header\":");
                json.push_str(&unprotcted_json);
            }
            json.push_str(",\"payload\":\"");
            json.push_str(&payload);
            json.push_str("\"");
            json.push_str(",\"signature\":\"");
            base64::encode_config_buf(&signature, base64::URL_SAFE_NO_PAD, &mut json);
            json.push_str("\"}");
            Ok(json)
        })()
        .map_err(|err| match err.downcast::<JoseError>() {
            Ok(err) => err,
            Err(err) => JoseError::InvalidJwsFormat(err),
        })
    }
pub fn serialize_flattened_json_with_selector < ' a , F > ( & self , payload : & [ u8 ] , header : & JwsHeaderSet , selector : F , ) -> Result < String , JoseError > where F : Fn ( & JwsHeader ) -> Option < & ' a dyn JwsSigner > , { ( | | -> anyhow :: Result < String > { let protected_map = header . claims_set ( true ) ; let mut b64 = true ; match protected_map . get ( " " ) { Some ( Value :: Array ( vals ) ) => { if vals . iter ( ) . any ( | val | match val { Value :: String ( val2 ) => val2 == " " , _ => false , } ) { b64 = match protected_map . get ( " " ) { Some ( Value :: Bool ( val3 ) ) => * val3 , _ => false , } ; } } _ => { } } let merged_map = header . to_map ( ) ; let merged = JwsHeader :: from_map ( merged_map ) ? ; let signer = match selector ( & merged ) { Some ( val ) => val , None => bail ! ( " " ) , } ; let mut protected_map = protected_map . clone ( ) ; match merged . algorithm ( ) { Some ( val ) if val == signer . algorithm ( ) . name ( ) => { } Some ( _ ) => bail ! ( " " ) , None => { protected_map . insert ( " " . to_string ( ) , Value :: String ( signer . algorithm ( ) . name ( ) . to_string ( ) ) , ) ; } } if let None = merged . key_id ( ) { if let Some ( key_id ) = signer . key_id ( ) { protected_map . insert ( " " . to_string ( ) , Value :: String ( key_id . to_string ( ) ) ) ; } } let protected_json = serde_json :: to_string ( & protected_map ) ? ; let protected_b64 = base64 :: encode_config ( protected_json , base64 :: URL_SAFE_NO_PAD ) ; let payload_b64 ; let payload = if b64 { payload_b64 = base64 :: encode_config ( payload , base64 :: URL_SAFE_NO_PAD ) ; & payload_b64 } else { std :: str :: from_utf8 ( payload ) ? } ; let message = format ! ( " " protected_b64 payload ) ; let signature = signer . sign ( message . as_bytes ( ) ) ? ; let mut json = String :: new ( ) ; json . push_str ( " \" \" \" " ) ; json . push_str ( & protected_b64 ) ; json . push_str ( " \" " ) ; let unprotected = header . claims_set ( false ) ; if unprotected . len ( ) > 0 { let unprotcted_json = serde_json :: to_string ( unprotected ) ? ; json . push_str ( " \" \" " ) ; json . push_str ( & unprotcted_json ) ; } json . push_str ( " \" \" \" " ) ; json . push_str ( & payload ) ; json . push_str ( " \" " ) ; json . push_str ( " \" \" \" " ) ; base64 :: encode_config_buf ( & signature , base64 :: URL_SAFE_NO_PAD , & mut json ) ; json . push_str ( " \" " ) ; Ok ( json ) } ) ( ) . map_err ( | err | match err . downcast :: < JoseError > ( ) { Ok ( err ) => err , Err ( err ) => JoseError :: InvalidJwsFormat ( err ) , } ) }
Return a representation of the data that is formatted by flatted json serialization.
Return a representation of the data that is formatted by flatted json serialization .
&self
payload
&[u8]
header
&JwsHeaderSet
selector
F
&self
payload
&[u8]
header
&JwsHeaderSet
selector
F