Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions crates/data-model/src/compat/sso_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ use ulid::Ulid;
use url::Url;

use super::CompatSession;
use crate::InvalidTransitionError;
use crate::{BrowserSession, InvalidTransitionError};

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub enum CompatSsoLoginState {
#[default]
Pending,
Fulfilled {
fulfilled_at: DateTime<Utc>,
session_id: Ulid,
browser_session_id: Ulid,
},
Exchanged {
fulfilled_at: DateTime<Utc>,
exchanged_at: DateTime<Utc>,
session_id: Ulid,
compat_session_id: Ulid,
},
}

Expand Down Expand Up @@ -80,18 +80,21 @@ impl CompatSsoLoginState {
}
}

/// Get the session ID associated with the login.
/// Get the compat session ID associated with the login.
///
/// Returns `None` if the compat SSO login state is [`Pending`].
/// Returns `None` if the compat SSO login state is [`Pending`] or
/// [`Fulfilled`].
///
/// [`Pending`]: CompatSsoLoginState::Pending
/// [`Fulfilled`]: CompatSsoLoginState::Fulfilled
#[must_use]
pub fn session_id(&self) -> Option<Ulid> {
match self {
Self::Pending => None,
Self::Fulfilled { session_id, .. } | Self::Exchanged { session_id, .. } => {
Some(*session_id)
}
Self::Pending | Self::Fulfilled { .. } => None,
Self::Exchanged {
compat_session_id: session_id,
..
} => Some(*session_id),
}
}

Expand All @@ -106,12 +109,12 @@ impl CompatSsoLoginState {
pub fn fulfill(
self,
fulfilled_at: DateTime<Utc>,
session: &CompatSession,
browser_session: &BrowserSession,
) -> Result<Self, InvalidTransitionError> {
match self {
Self::Pending => Ok(Self::Fulfilled {
fulfilled_at,
session_id: session.id,
browser_session_id: browser_session.id,
}),
Self::Fulfilled { .. } | Self::Exchanged { .. } => Err(InvalidTransitionError),
}
Expand All @@ -126,15 +129,19 @@ impl CompatSsoLoginState {
///
/// [`Fulfilled`]: CompatSsoLoginState::Fulfilled
/// [`Exchanged`]: CompatSsoLoginState::Exchanged
pub fn exchange(self, exchanged_at: DateTime<Utc>) -> Result<Self, InvalidTransitionError> {
pub fn exchange(
self,
exchanged_at: DateTime<Utc>,
compat_session: &CompatSession,
) -> Result<Self, InvalidTransitionError> {
match self {
Self::Fulfilled {
fulfilled_at,
session_id,
browser_session_id: _,
} => Ok(Self::Exchanged {
fulfilled_at,
exchanged_at,
session_id,
compat_session_id: compat_session.id,
}),
Self::Pending { .. } | Self::Exchanged { .. } => Err(InvalidTransitionError),
}
Expand Down Expand Up @@ -171,9 +178,9 @@ impl CompatSsoLogin {
pub fn fulfill(
mut self,
fulfilled_at: DateTime<Utc>,
session: &CompatSession,
browser_session: &BrowserSession,
) -> Result<Self, InvalidTransitionError> {
self.state = self.state.fulfill(fulfilled_at, session)?;
self.state = self.state.fulfill(fulfilled_at, browser_session)?;
Ok(self)
}

Expand All @@ -186,8 +193,12 @@ impl CompatSsoLogin {
///
/// [`Fulfilled`]: CompatSsoLoginState::Fulfilled
/// [`Exchanged`]: CompatSsoLoginState::Exchanged
pub fn exchange(mut self, exchanged_at: DateTime<Utc>) -> Result<Self, InvalidTransitionError> {
self.state = self.state.exchange(exchanged_at)?;
pub fn exchange(
mut self,
exchanged_at: DateTime<Utc>,
compat_session: &CompatSession,
) -> Result<Self, InvalidTransitionError> {
self.state = self.state.exchange(exchanged_at, compat_session)?;
Ok(self)
}
}
Loading
Loading