pub fn serialize_compact_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JwsHeader,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JwsHeader) -> Option<&'a dyn JwsSigner>,
    {
        (|| -> anyhow::Result<String> {
            let mut b64 = true;
            if let Some(vals) = header.critical() {
                if vals.contains(&"b64") {
                    if let Some(val) = header.base64url_encode_payload() {
                        b64 = val;
                    }
                }
            }

            let signer = match selector(header) {
                Some(val) => val,
                None => bail!("A signer is not found."),
            };

            let mut header = header.claims_set().clone();
            header.insert(
                "alg".to_string(),
                Value::String(signer.algorithm().name().to_string()),
            );
            if let Some(key_id) = signer.key_id() {
                header.insert("kid".to_string(), Value::String(key_id.to_string()));
            }
            let header_bytes = serde_json::to_vec(&header)?;

            let mut capacity = 2;
            capacity += util::ceiling(header_bytes.len() * 4, 3);
            capacity += if b64 {
                util::ceiling(payload.len() * 4, 3)
            } else {
                payload.len()
            };
            capacity += util::ceiling(signer.signature_len() * 4, 3);

            let mut message = String::with_capacity(capacity);
            base64::encode_config_buf(header_bytes, base64::URL_SAFE_NO_PAD, &mut message);
            message.push_str(".");
            if b64 {
                base64::encode_config_buf(payload, base64::URL_SAFE_NO_PAD, &mut message);
            } else {
                let payload = std::str::from_utf8(payload)?;
                if payload.contains(".") {
                    bail!("A JWS payload cannot contain dot.");
                }
                message.push_str(payload);
            }

            let signature = signer.sign(message.as_bytes())?;

            message.push_str(".");
            base64::encode_config_buf(signature, base64::URL_SAFE_NO_PAD, &mut message);

            Ok(message)
        })()
        .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 compact serialization.
///
/// # Arguments
///
/// * `payload` - The payload data.
/// * `header` - The JWS heaser claims.
/// * `selector` - a function for selecting the signing algorithm.
Return a representation of the data that is formatted by compact serialization.
Return a representation of the data that is formatted by compact serialization .
pub fn serialize_compact_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JwsHeader,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JwsHeader) -> Option<&'a dyn JwsSigner>,
    {
        (|| -> anyhow::Result<String> {
            let mut b64 = true;
            if let Some(vals) = header.critical() {
                if vals.contains(&"b64") {
                    if let Some(val) = header.base64url_encode_payload() {
                        b64 = val;
                    }
                }
            }
            let signer = match selector(header) {
                Some(val) => val,
                None => bail!("A signer is not found."),
            };
            let mut header = header.claims_set().clone();
            header.insert(
                "alg".to_string(),
                Value::String(signer.algorithm().name().to_string()),
            );
            if let Some(key_id) = signer.key_id() {
                header.insert("kid".to_string(), Value::String(key_id.to_string()));
            }
            let header_bytes = serde_json::to_vec(&header)?;
            let mut capacity = 2;
            capacity += util::ceiling(header_bytes.len() * 4, 3);
            capacity += if b64 {
                util::ceiling(payload.len() * 4, 3)
            } else {
                payload.len()
            };
            capacity += util::ceiling(signer.signature_len() * 4, 3);
            let mut message = String::with_capacity(capacity);
            base64::encode_config_buf(header_bytes, base64::URL_SAFE_NO_PAD, &mut message);
            message.push_str(".");
            if b64 {
                base64::encode_config_buf(payload, base64::URL_SAFE_NO_PAD, &mut message);
            } else {
                let payload = std::str::from_utf8(payload)?;
                if payload.contains(".") {
                    bail!("A JWS payload cannot contain dot.");
                }
                message.push_str(payload);
            }
            let signature = signer.sign(message.as_bytes())?;
            message.push_str(".");
            base64::encode_config_buf(signature, base64::URL_SAFE_NO_PAD, &mut message);
            Ok(message)
        })()
        .map_err(|err| match err.downcast::<JoseError>() {
            Ok(err) => err,
            Err(err) => JoseError::InvalidJwsFormat(err),
        })
    }
pub fn serialize_compact_with_selector < ' a , F > ( & self , payload : & [ u8 ] , header : & JwsHeader , selector : F , ) -> Result < String , JoseError > where F : Fn ( & JwsHeader ) -> Option < & ' a dyn JwsSigner > , { ( | | -> anyhow :: Result < String > { let mut b64 = true ; if let Some ( vals ) = header . critical ( ) { if vals . contains ( & " " ) { if let Some ( val ) = header . base64url_encode_payload ( ) { b64 = val ; } } } let signer = match selector ( header ) { Some ( val ) => val , None => bail ! ( " " ) , } ; let mut header = header . claims_set ( ) . clone ( ) ; header . insert ( " " . to_string ( ) , Value :: String ( signer . algorithm ( ) . name ( ) . to_string ( ) ) , ) ; if let Some ( key_id ) = signer . key_id ( ) { header . insert ( " " . to_string ( ) , Value :: String ( key_id . to_string ( ) ) ) ; } let header_bytes = serde_json :: to_vec ( & header ) ? ; let mut capacity = 2 ; capacity += util :: ceiling ( header_bytes . len ( ) * 4 , 3 ) ; capacity += if b64 { util :: ceiling ( payload . len ( ) * 4 , 3 ) } else { payload . len ( ) } ; capacity += util :: ceiling ( signer . signature_len ( ) * 4 , 3 ) ; let mut message = String :: with_capacity ( capacity ) ; base64 :: encode_config_buf ( header_bytes , base64 :: URL_SAFE_NO_PAD , & mut message ) ; message . push_str ( " " ) ; if b64 { base64 :: encode_config_buf ( payload , base64 :: URL_SAFE_NO_PAD , & mut message ) ; } else { let payload = std :: str :: from_utf8 ( payload ) ? ; if payload . contains ( " " ) { bail ! ( " " ) ; } message . push_str ( payload ) ; } let signature = signer . sign ( message . as_bytes ( ) ) ? ; message . push_str ( " " ) ; base64 :: encode_config_buf ( signature , base64 :: URL_SAFE_NO_PAD , & mut message ) ; Ok ( message ) } ) ( ) . 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 compact serialization.
Return a representation of the data that is formatted by compact serialization .
&self
payload
&[u8]
header
&JwsHeader
selector
F
&self
payload
&[u8]
header
&JwsHeader
selector
F