Skip to main content

haste_jwt/
claims.rs

1use crate::{AuthorId, AuthorKind, ProjectId, TenantId, UserRole, VersionId, scopes::Scopes};
2use derivative::Derivative;
3use haste_fhir_model::r4::generated::terminology::IssueType;
4use haste_fhir_operation_error::OperationOutcomeError;
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
8pub enum SubscriptionTier {
9    #[serde(rename = "free")]
10    Free,
11    #[serde(rename = "professional")]
12    Professional,
13    #[serde(rename = "team")]
14    Team,
15    #[serde(rename = "unlimited")]
16    Unlimited,
17}
18
19impl From<SubscriptionTier> for String {
20    fn from(tier: SubscriptionTier) -> Self {
21        match tier {
22            SubscriptionTier::Free => "free".to_string(),
23            SubscriptionTier::Professional => "professional".to_string(),
24            SubscriptionTier::Team => "team".to_string(),
25            SubscriptionTier::Unlimited => "unlimited".to_string(),
26        }
27    }
28}
29
30impl TryFrom<String> for SubscriptionTier {
31    type Error = OperationOutcomeError;
32    fn try_from(value: String) -> Result<Self, Self::Error> {
33        match value.as_str() {
34            "free" => Ok(SubscriptionTier::Free),
35            "professional" => Ok(SubscriptionTier::Professional),
36            "team" => Ok(SubscriptionTier::Team),
37            "unlimited" => Ok(SubscriptionTier::Unlimited),
38            _ => Err(OperationOutcomeError::error(
39                IssueType::Invalid(None),
40                format!("Invalid subscription tier: '{}'", value),
41            )),
42        }
43    }
44}
45
46#[derive(Derivative, Serialize, Deserialize, Clone)]
47#[derivative(Debug = "transparent")]
48pub struct UserTokenClaims {
49    pub sub: AuthorId,
50    #[derivative(Debug = "ignore")]
51    pub exp: usize,
52    #[derivative(Debug = "ignore")]
53    pub aud: String,
54    #[derivative(Debug = "ignore")]
55    pub scope: Scopes,
56
57    #[serde(rename = "https://haste.health/tenant")]
58    #[derivative(Debug = "ignore")]
59    pub tenant: TenantId,
60    #[serde(rename = "https://haste.health/subscription_tier")]
61    #[derivative(Debug = "ignore")]
62    pub subscription_tier: SubscriptionTier,
63    #[serde(rename = "https://haste.health/project")]
64    #[derivative(Debug = "ignore")]
65    pub project: Option<ProjectId>,
66    #[serde(rename = "https://haste.health/user_role")]
67    #[derivative(Debug = "ignore")]
68    pub user_role: UserRole,
69    #[serde(rename = "https://haste.health/user_id")]
70    #[derivative(Debug = "ignore")]
71    pub user_id: AuthorId,
72    #[serde(rename = "https://haste.health/resource_type")]
73    #[derivative(Debug = "ignore")]
74    pub resource_type: AuthorKind,
75    #[serde(rename = "https://haste.health/access_policies")]
76    #[derivative(Debug = "ignore")]
77    pub access_policy_version_ids: Vec<VersionId>,
78    #[serde(rename = "https://haste.health/membership")]
79    #[derivative(Debug = "ignore")]
80    pub membership: Option<String>,
81}