typesec_integrations/jwt/
claims.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct JwtClaims {
8 pub sub: String,
10 pub iss: String,
12 pub aud: Audience,
14 pub exp: usize,
16 #[serde(default)]
18 pub org_id: Option<String>,
19 #[serde(default)]
21 pub organization_membership_id: Option<String>,
22 #[serde(default)]
24 pub role: Option<String>,
25 #[serde(default)]
27 pub permissions: Vec<String>,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(untagged)]
33pub enum Audience {
34 Single(String),
36 Multiple(Vec<String>),
38}
39
40impl Audience {
41 pub(super) fn contains(&self, needle: &str) -> bool {
42 match self {
43 Self::Single(value) => value == needle,
44 Self::Multiple(values) => values.iter().any(|value| value == needle),
45 }
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct VerifiedSubject {
52 pub subject: String,
54 pub org_id: Option<String>,
56 pub organization_membership_id: Option<String>,
58 pub roles: Vec<String>,
60 pub permissions: Vec<String>,
62}
63
64impl VerifiedSubject {
65 pub fn workos_membership_subject(&self) -> &str {
67 self.organization_membership_id
68 .as_deref()
69 .unwrap_or(&self.subject)
70 }
71}
72
73impl From<JwtClaims> for VerifiedSubject {
74 fn from(claims: JwtClaims) -> Self {
75 Self {
76 subject: claims.sub,
77 org_id: claims.org_id,
78 organization_membership_id: claims.organization_membership_id,
79 roles: claims.role.into_iter().collect(),
80 permissions: claims.permissions,
81 }
82 }
83}