ruma_common/
third_party_invite.rs
1use std::ops::Deref;
6
7use serde::{Deserialize, Serialize};
8
9use crate::serde::{
10 base64::{Standard, UrlSafe},
11 Base64, Base64DecodeError,
12};
13
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
26#[allow(clippy::exhaustive_structs)]
27pub struct IdentityServerBase64PublicKey(pub String);
28
29impl IdentityServerBase64PublicKey {
30 pub fn new(bytes: &[u8]) -> Self {
33 Self(Base64::<Standard, &[u8]>::new(bytes).encode())
34 }
35
36 pub fn decode(&self) -> Result<Vec<u8>, Base64DecodeError> {
41 let is_url_safe_alphabet = self.0.contains(['-', '_']);
42
43 if is_url_safe_alphabet {
44 Ok(Base64::<UrlSafe>::parse(&self.0)?.into_inner())
45 } else {
46 Ok(Base64::<Standard>::parse(&self.0)?.into_inner())
47 }
48 }
49}
50
51impl From<String> for IdentityServerBase64PublicKey {
52 fn from(value: String) -> Self {
53 Self(value)
54 }
55}
56
57impl AsRef<str> for IdentityServerBase64PublicKey {
58 fn as_ref(&self) -> &str {
59 &self.0
60 }
61}
62
63impl Deref for IdentityServerBase64PublicKey {
64 type Target = str;
65
66 fn deref(&self) -> &Self::Target {
67 self.as_ref()
68 }
69}
70
71impl PartialEq<String> for IdentityServerBase64PublicKey {
72 fn eq(&self, other: &String) -> bool {
73 self.0.eq(other)
74 }
75}
76
77impl<'a> PartialEq<&'a str> for IdentityServerBase64PublicKey {
78 fn eq(&self, other: &&'a str) -> bool {
79 self.0.eq(other)
80 }
81}
82
83impl PartialEq<str> for IdentityServerBase64PublicKey {
84 fn eq(&self, other: &str) -> bool {
85 self.0.eq(other)
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::IdentityServerBase64PublicKey;
92
93 #[test]
94 fn identity_server_base64_public_key_encode_then_decode() {
95 let original = b"foobar";
96 let encoded = IdentityServerBase64PublicKey::new(original);
97 assert_eq!(encoded, "Zm9vYmFy");
98 assert_eq!(encoded.decode().unwrap(), original);
99 }
100
101 #[test]
102 fn identity_server_base64_public_key_decode_standard_and_url_safe() {
103 let original = &[60, 98, 62, 77, 68, 78, 60, 47, 98, 62];
104 let standard_base64 = IdentityServerBase64PublicKey("PGI+TUROPC9iPg".to_owned());
105 assert_eq!(standard_base64.decode().unwrap(), original);
106 let urlsafe_base64 = IdentityServerBase64PublicKey("PGI-TUROPC9iPg".to_owned());
107 assert_eq!(urlsafe_base64.decode().unwrap(), original);
108 }
109}