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",
23            SubscriptionTier::Professional => "professional",
24            SubscriptionTier::Team => "team",
25            SubscriptionTier::Unlimited => "unlimited",
26        }
27        .to_string()
28    }
29}
30
31impl TryFrom<String> for SubscriptionTier {
32    type Error = OperationOutcomeError;
33    fn try_from(value: String) -> Result<Self, Self::Error> {
34        match value.as_str() {
35            "free" => Ok(SubscriptionTier::Free),
36            "professional" => Ok(SubscriptionTier::Professional),
37            "team" => Ok(SubscriptionTier::Team),
38            "unlimited" => Ok(SubscriptionTier::Unlimited),
39            _ => Err(OperationOutcomeError::error(
40                IssueType::invalid(),
41                format!("Invalid subscription tier: '{value}'"),
42            )),
43        }
44    }
45}
46
47#[derive(Derivative, Serialize, Deserialize, Clone)]
48#[derivative(Debug = "transparent")]
49pub struct UserTokenClaims {
50    pub sub: AuthorId,
51    #[derivative(Debug = "ignore")]
52    pub exp: usize,
53    #[derivative(Debug = "ignore")]
54    pub aud: String,
55    #[derivative(Debug = "ignore")]
56    pub scope: Scopes,
57
58    #[serde(rename = "https://haste.health/tenant")]
59    #[derivative(Debug = "ignore")]
60    pub tenant: TenantId,
61    #[serde(rename = "https://haste.health/subscription_tier")]
62    #[derivative(Debug = "ignore")]
63    pub subscription_tier: SubscriptionTier,
64    #[serde(rename = "https://haste.health/project")]
65    #[derivative(Debug = "ignore")]
66    pub project: Option<ProjectId>,
67    #[serde(rename = "https://haste.health/user_role")]
68    #[derivative(Debug = "ignore")]
69    pub user_role: UserRole,
70    #[serde(rename = "https://haste.health/user_id")]
71    #[derivative(Debug = "ignore")]
72    pub user_id: AuthorId,
73    #[serde(rename = "https://haste.health/resource_type")]
74    #[derivative(Debug = "ignore")]
75    pub resource_type: AuthorKind,
76    #[serde(rename = "https://haste.health/access_policies")]
77    #[derivative(Debug = "ignore")]
78    pub access_policy_version_ids: Vec<VersionId>,
79    #[serde(rename = "https://haste.health/membership")]
80    #[derivative(Debug = "ignore")]
81    pub membership: Option<String>,
82}