pub fn create_google_jwt_es256(
    project_name: &str,
    private_key_pem: &str,
    timestamp: usize,
) -> Result<String<JWT_ES256_MAX_LENGTH>, &'static str> {
    if project_name.len() < 6 || project_name.len() > 30 {
        return Err("Project name should be 6-30 characters in length");
    }

    let header = "{\"alg\":\"ES256\",\"typ\":\"JWT\"}";
    let mut claims: String<JWT_CLAIMS_MAX_LENGTH> = String::new();
    uwrite!(
        claims,
        "{{\"aud\":\"{}\",\"iat\":{},\"exp\":{}}}",
        project_name, timestamp, timestamp + JWT_LIFETIME
    ).map_err(|_| "Can not create JSON string for claims")?;

    let mut result: String<JWT_ES256_MAX_LENGTH> = String::new();

    // Encode header
    result.push_str(
        Base64Unpadded::encode(
            header.as_bytes(),
            &mut [0_u8; JWT_HEADER_BASE64_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT header to base64")?
    ).map_err(|_| "Can not push to the JWT string")?;

    result.push('.').map_err(|_|"Can not push to the JWT string")?;

    // Encode claims
    result.push_str(
        Base64Unpadded::encode(
            claims.as_bytes(),
            &mut [0_u8; JWT_CLAIMS_BASE64_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT claims to base64")?
    ).map_err(|_| "Can not push to JWT string")?;

    // Sign concatenated "base64(header).base64(claims)" with ES256 signature
    let signature = sign_es256(result.as_str(), private_key_pem)
        .map_err(|_| "Can not sign JWT")?;

    // Encode signature to the base64 format and join it to the JWT result
    result.push('.').map_err(|_| "Can not push to JWT string")?;
    result.push_str(
        Base64Unpadded::encode(
            signature.as_ref(),
            &mut [0_u8; JWT_ES256_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT signature to base64")?
    ).map_err(|_| "Can not push to JWT string")?;

    Ok(result)
}
/// Creates Google IOT JWT using ES256 signature.
/// Returns heapless string no longer than JWT_ES256_MAX_LENGTH characters.
Creates Google IOT JWT using ES256 signature.
Returns heapless string no longer than JWT_ES256_MAX_LENGTH characters.
Creates Google IOT JWT using ES256 signature . Returns heapless string no longer than JWT_ES256_MAX_LENGTH characters .
pub fn create_google_jwt_es256(
    project_name: &str,
    private_key_pem: &str,
    timestamp: usize,
) -> Result<String<JWT_ES256_MAX_LENGTH>, &'static str> {
    if project_name.len() < 6 || project_name.len() > 30 {
        return Err("Project name should be 6-30 characters in length");
    }
    let header = "{\"alg\":\"ES256\",\"typ\":\"JWT\"}";
    let mut claims: String<JWT_CLAIMS_MAX_LENGTH> = String::new();
    uwrite!(
        claims,
        "{{\"aud\":\"{}\",\"iat\":{},\"exp\":{}}}",
        project_name, timestamp, timestamp + JWT_LIFETIME
    ).map_err(|_| "Can not create JSON string for claims")?;
    let mut result: String<JWT_ES256_MAX_LENGTH> = String::new();
    // Encode header
    result.push_str(
        Base64Unpadded::encode(
            header.as_bytes(),
            &mut [0_u8; JWT_HEADER_BASE64_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT header to base64")?
    ).map_err(|_| "Can not push to the JWT string")?;
    result.push('.').map_err(|_|"Can not push to the JWT string")?;
    // Encode claims
    result.push_str(
        Base64Unpadded::encode(
            claims.as_bytes(),
            &mut [0_u8; JWT_CLAIMS_BASE64_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT claims to base64")?
    ).map_err(|_| "Can not push to JWT string")?;
    // Sign concatenated "base64(header).base64(claims)" with ES256 signature
    let signature = sign_es256(result.as_str(), private_key_pem)
        .map_err(|_| "Can not sign JWT")?;
    // Encode signature to the base64 format and join it to the JWT result
    result.push('.').map_err(|_| "Can not push to JWT string")?;
    result.push_str(
        Base64Unpadded::encode(
            signature.as_ref(),
            &mut [0_u8; JWT_ES256_MAX_LENGTH]
        ).map_err(|_| "Can not encode JWT signature to base64")?
    ).map_err(|_| "Can not push to JWT string")?;
    Ok(result)
}
pub fn create_google_jwt_es256 ( project_name : & str , private_key_pem : & str , timestamp : usize , ) -> Result < String < JWT_ES256_MAX_LENGTH > , & ' static str > { if project_name . len ( ) < 6 || project_name . len ( ) > 30 { return Err ( " " ) ; } let header = " \" \" \" \" \" \" \" \" " ; let mut claims : String < JWT_CLAIMS_MAX_LENGTH > = String :: new ( ) ; uwrite ! ( claims " \" \" \" \" \" \" \" \" " project_name timestamp timestamp JWT_LIFETIME ) . map_err ( | _ | " " ) ? ; let mut result : String < JWT_ES256_MAX_LENGTH > = String :: new ( ) ; // Encode header result . push_str ( Base64Unpadded :: encode ( header . as_bytes ( ) , & mut [ 0_u8 ; JWT_HEADER_BASE64_MAX_LENGTH ] ) . map_err ( | _ | " " ) ? ) . map_err ( | _ | " " ) ? ; result . push ( '.' ) . map_err ( | _ | " " ) ? ; // Encode claims result . push_str ( Base64Unpadded :: encode ( claims . as_bytes ( ) , & mut [ 0_u8 ; JWT_CLAIMS_BASE64_MAX_LENGTH ] ) . map_err ( | _ | " " ) ? ) . map_err ( | _ | " " ) ? ; // Sign concatenated "base64(header).base64(claims)" with ES256 signature let signature = sign_es256 ( result . as_str ( ) , private_key_pem ) . map_err ( | _ | " " ) ? ; // Encode signature to the base64 format and join it to the JWT result result . push ( '.' ) . map_err ( | _ | " " ) ? ; result . push_str ( Base64Unpadded :: encode ( signature . as_ref ( ) , & mut [ 0_u8 ; JWT_ES256_MAX_LENGTH ] ) . map_err ( | _ | " " ) ? ) . map_err ( | _ | " " ) ? ; Ok ( result ) }
Creates Google IOT JWT using ES256 signature.
Creates Google IOT JWT using ES256 signature .
project_name
&str
private_key_pem
&str
timestamp
usize
project_name
&str
private_key_pem
&str
timestamp
usize