app_store_server_library/
utils.rs

1use base64::engine::general_purpose::STANDARD;
2use base64::{DecodeError, Engine};
3
4/// Converts a base64URL-encoded string to a standard base64-encoded string.
5///
6/// Replaces '/' with '+' and '_' with '-', and adds padding if needed.
7///
8/// # Examples
9///
10/// ```ignore
11/// let encoded_string = "aGVsbG8gd29ybGQh";
12/// let result = base64_url_to_base64(encoded_string);
13/// assert_eq!(result, "aGVsbG8gd29ybGQh==");
14/// ```
15pub(crate) fn base64_url_to_base64(encoded_string: &str) -> String {
16    let replaced_string = encoded_string.replace('/', "+").replace('_', "-");
17
18    if replaced_string.len() % 4 != 0 {
19        return replaced_string.clone() + &"=".repeat(4 - replaced_string.len() % 4);
20    }
21
22    replaced_string
23}
24
25/// A trait for extending the functionality of Rust strings.
26pub trait StringExt {
27    /// Converts the string into a DER-encoded byte vector.
28    ///
29    /// This method attempts to parse the string as a DER-encoded byte sequence
30    /// and returns the result as a `Vec<u8>`. If the parsing fails, it returns
31    /// a `DecodeError`.
32    ///
33    /// # Errors
34    ///
35    /// If the string cannot be successfully parsed as DER-encoded bytes, this
36    /// method returns a `DecodeError` indicating the reason for the failure.
37    ///
38    fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError>;
39}
40
41impl StringExt for String {
42    fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError> {
43        STANDARD.decode(self)
44    }
45}
46
47impl StringExt for &str {
48    fn as_der_bytes(&self) -> Result<Vec<u8>, DecodeError> {
49        STANDARD.decode(self)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_base64_url_to_base64() {
59        // Test with a base64URL-encoded string
60        let encoded_string = "aGVsbG8gd29ybGQh";
61        let result = base64_url_to_base64(encoded_string);
62        assert_eq!(result, "aGVsbG8gd29ybGQh");
63
64        // Test with a base64URL-encoded string requiring padding
65        let encoded_string_padding = "aGVsbG8gd29ybz";
66        let result_padding = base64_url_to_base64(encoded_string_padding);
67        assert_eq!(result_padding, "aGVsbG8gd29ybz==");
68    }
69}