Skip to main content

systemprompt_identifiers/
client.rs

1//! OAuth client identifier with classifier helpers.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6crate::define_id!(ClientId, checked, |value| {
7    crate::macros::validate_external_id("ClientId", value)
8});
9
10impl ClientId {
11    pub fn client_type(&self) -> ClientType {
12        if self.0.starts_with("https://") {
13            ClientType::Cimd
14        } else if self.0.starts_with("sp_") {
15            ClientType::FirstParty
16        } else if self.0.starts_with("client_") {
17            ClientType::ThirdParty
18        } else if self.0.starts_with("sys_") {
19            ClientType::System
20        } else {
21            ClientType::Unknown
22        }
23    }
24
25    pub fn is_dcr(&self) -> bool {
26        matches!(
27            self.client_type(),
28            ClientType::FirstParty | ClientType::ThirdParty
29        )
30    }
31
32    pub fn is_cimd(&self) -> bool {
33        self.0.starts_with("https://")
34    }
35
36    pub fn is_system(&self) -> bool {
37        self.0.starts_with("sys_")
38    }
39
40    pub fn web() -> Self {
41        Self("sp_web".to_owned())
42    }
43
44    pub fn cli() -> Self {
45        Self("sp_cli".to_owned())
46    }
47
48    pub fn mobile_ios() -> Self {
49        Self("sp_mobile_ios".to_owned())
50    }
51
52    pub fn mobile_android() -> Self {
53        Self("sp_mobile_android".to_owned())
54    }
55
56    pub fn desktop() -> Self {
57        Self("sp_desktop".to_owned())
58    }
59
60    pub fn bridge() -> Self {
61        Self("sp_bridge".to_owned())
62    }
63
64    pub fn system(service_name: &crate::ServiceName) -> Self {
65        Self(format!("sys_{service_name}"))
66    }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
70#[serde(rename_all = "lowercase")]
71pub enum ClientType {
72    Cimd,
73    FirstParty,
74    ThirdParty,
75    System,
76    Unknown,
77}
78
79impl ClientType {
80    pub const fn as_str(self) -> &'static str {
81        match self {
82            Self::Cimd => "cimd",
83            Self::FirstParty => "firstparty",
84            Self::ThirdParty => "thirdparty",
85            Self::System => "system",
86            Self::Unknown => "unknown",
87        }
88    }
89}
90
91impl std::fmt::Display for ClientType {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        write!(f, "{}", self.as_str())
94    }
95}