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
40 changes: 28 additions & 12 deletions apps/labrinth/src/database/models/flow_item.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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;
use rand_chacha::ChaCha20Rng;
use rand_chacha::rand_core::SeedableRng;
use serde::{Deserialize, Serialize};
use url::Url;

const FLOWS_NAMESPACE: &str = "flows";

Expand All @@ -18,10 +19,15 @@ const FLOWS_NAMESPACE: &str = "flows";
pub enum DBFlow {
OAuth {
user_id: Option<DBUserId>,
url: String,
url: Url,
provider: AuthProvider,
existing_user_id: Option<DBUserId>,
},
OAuthPending {
url: Url,
provider: AuthProvider,
user: TempUser,
},
Login2FA {
user_id: DBUserId,
},
Expand Down Expand Up @@ -55,28 +61,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