atrium_api/
did_doc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Definitions for DID document types.
use http::{uri::Scheme, Uri};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct DidDocument {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@context")]
    pub context: Option<Vec<String>>,
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub also_known_as: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_method: Option<Vec<VerificationMethod>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service: Option<Vec<Service>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct VerificationMethod {
    pub id: String,
    pub r#type: String,
    pub controller: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub public_key_multibase: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Service {
    pub id: String,
    pub r#type: String,
    pub service_endpoint: String,
}

impl DidDocument {
    pub fn get_pds_endpoint(&self) -> Option<String> {
        self.get_service_endpoint("#atproto_pds", "AtprotoPersonalDataServer")
    }
    fn get_service_endpoint(&self, id: &str, r#type: &str) -> Option<String> {
        let full_id = self.id.to_string() + id;
        if let Some(services) = &self.service {
            let service_endpoint = services
                .iter()
                .find(|service| {
                    (service.id == id || service.id == full_id) && service.r#type == r#type
                })
                .map(|service| service.service_endpoint.clone())?;
            return Some(service_endpoint).filter(|s| Self::validate_url(s));
        }
        None
    }
    fn validate_url(url: &str) -> bool {
        url.parse::<Uri>()
            .map(|uri| match uri.scheme() {
                Some(scheme) if (scheme == &Scheme::HTTP || scheme == &Scheme::HTTPS) => {
                    uri.host().is_some()
                }
                _ => false,
            })
            .unwrap_or_default()
    }
}