Skip to content
Draft
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
37 changes: 26 additions & 11 deletions apps/labrinth/src/database/models/flow_item.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::ids::*;
use crate::auth::AuthProvider;
use crate::auth::oauth::uris::OAuthRedirectUris;
use crate::database::models::DatabaseError;
use crate::database::redis::RedisPool;
use crate::models::pats::Scopes;
use crate::{auth::AuthProvider, routes::internal::flows::TempUser};
use chrono::Duration;
use rand::Rng;
use rand::distributions::Alphanumeric;
Expand All @@ -22,6 +22,11 @@ pub enum DBFlow {
provider: AuthProvider,
existing_user_id: Option<DBUserId>,
},
OAuthPending {
url: String,
provider: AuthProvider,
user: TempUser,
},
Login2FA {
user_id: DBUserId,
},
Expand Down Expand Up @@ -55,28 +60,38 @@ pub enum DBFlow {
}

impl DBFlow {
pub async fn insert(
pub async fn insert_with_state(
&self,
expires: Duration,
redis: &RedisPool,
) -> Result<String, DatabaseError> {
state: &str,
) -> Result<(), DatabaseError> {
let mut redis = redis.connect().await?;

let flow = ChaCha20Rng::from_entropy()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect::<String>();

redis
.set_serialized_to_json(
FLOWS_NAMESPACE,
&flow,
&state,
&self,
Some(expires.num_seconds()),
)
.await?;
Ok(flow)
Ok(())
}

pub async fn insert(
&self,
expires: Duration,
redis: &RedisPool,
) -> Result<String, DatabaseError> {
let state = ChaCha20Rng::from_entropy()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect::<String>();

self.insert_with_state(expires, redis, &state).await?;
Ok(state)
}

pub async fn get(
Expand Down
Loading
Loading