Skip to main content

haste_repository/types/
authorization_code.rs

1use haste_fhir_model::r4::generated::terminology::IssueType;
2use haste_fhir_operation_error::{OperationOutcomeError, derive::OperationOutcomeError};
3use haste_jwt::{ProjectId, TenantId};
4use sqlx::types::{Json, time::OffsetDateTime};
5use std::time::Duration;
6
7#[derive(Clone, Debug, PartialEq, PartialOrd, sqlx::Type, serde::Deserialize, serde::Serialize)]
8#[sqlx(type_name = "code_kind", rename_all = "lowercase")] // only for PostgreSQL to match a type definition
9pub enum AuthorizationCodeKind {
10    #[sqlx(rename = "password_reset")]
11    PasswordReset,
12    #[sqlx(rename = "oauth2_code_grant")]
13    OAuth2CodeGrant,
14    #[sqlx(rename = "refresh_token")]
15    RefreshToken,
16}
17
18#[derive(Clone, Debug, PartialEq, PartialOrd, sqlx::Type, serde::Deserialize, serde::Serialize)]
19#[sqlx(type_name = "pkce_method")] // only for PostgreSQL to match a type definition
20pub enum PKCECodeChallengeMethod {
21    S256,
22}
23
24impl<'a> TryFrom<&'a str> for PKCECodeChallengeMethod {
25    type Error = OperationOutcomeError;
26
27    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
28        match value {
29            "S256" => Ok(PKCECodeChallengeMethod::S256),
30            _ => Err(OperationOutcomeError::error(
31                IssueType::invalid(),
32                "Invalid PKCE code challenge method.".to_string(),
33            )),
34        }
35    }
36}
37
38impl From<PKCECodeChallengeMethod> for String {
39    fn from(method: PKCECodeChallengeMethod) -> Self {
40        match method {
41            PKCECodeChallengeMethod::S256 => "S256".to_string(),
42        }
43    }
44}
45
46pub struct AuthorizationCodeSearchClaims {
47    pub client_id: Option<String>,
48    pub code: Option<String>,
49    pub kind: Option<AuthorizationCodeKind>,
50    pub user_id: Option<String>,
51    pub user_agent: Option<String>,
52    pub is_expired: Option<bool>,
53}
54
55pub struct CreateAuthorizationCode {
56    pub membership: Option<String>,
57    pub expires_in: Duration,
58    pub kind: AuthorizationCodeKind,
59    pub user_id: String,
60    pub client_id: Option<String>,
61    pub pkce_code_challenge: Option<String>,
62    pub pkce_code_challenge_method: Option<PKCECodeChallengeMethod>,
63    pub redirect_uri: Option<String>,
64    pub meta: Option<Json<serde_json::Value>>,
65}
66
67#[derive(sqlx::FromRow, Debug)]
68pub struct AuthorizationCode {
69    pub membership: Option<String>,
70    pub tenant: TenantId,
71    pub is_expired: Option<bool>,
72    pub kind: AuthorizationCodeKind,
73    pub code: String,
74    pub user_id: String,
75    pub project: Option<ProjectId>,
76    pub client_id: Option<String>,
77    pub pkce_code_challenge: Option<String>,
78    pub pkce_code_challenge_method: Option<PKCECodeChallengeMethod>,
79    pub redirect_uri: Option<String>,
80    pub meta: Option<Json<serde_json::Value>>,
81    pub created_at: Option<OffsetDateTime>,
82}
83
84#[derive(OperationOutcomeError)]
85pub enum CodeErrors {
86    #[error(code = "invalid", diagnostic = "Invalid duration for expires.")]
87    InvalidDuration,
88}