2023-07-11 19:13:07 -07:00
|
|
|
use super::ids::*;
|
|
|
|
|
use crate::auth::flows::AuthProvider;
|
|
|
|
|
use crate::database::models::DatabaseError;
|
2023-10-06 09:57:33 -07:00
|
|
|
use crate::database::redis::RedisPool;
|
2023-07-14 22:55:00 -07:00
|
|
|
use chrono::Duration;
|
2023-07-11 19:13:07 -07:00
|
|
|
use rand::distributions::Alphanumeric;
|
|
|
|
|
use rand::Rng;
|
|
|
|
|
use rand_chacha::rand_core::SeedableRng;
|
|
|
|
|
use rand_chacha::ChaCha20Rng;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
const FLOWS_NAMESPACE: &str = "flows";
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
|
|
|
pub enum Flow {
|
|
|
|
|
OAuth {
|
|
|
|
|
user_id: Option<UserId>,
|
2023-08-02 14:43:04 -07:00
|
|
|
url: Option<String>,
|
2023-07-11 19:13:07 -07:00
|
|
|
provider: AuthProvider,
|
|
|
|
|
},
|
|
|
|
|
Login2FA {
|
|
|
|
|
user_id: UserId,
|
|
|
|
|
},
|
|
|
|
|
Initialize2FA {
|
|
|
|
|
user_id: UserId,
|
|
|
|
|
secret: String,
|
|
|
|
|
},
|
|
|
|
|
ForgotPassword {
|
|
|
|
|
user_id: UserId,
|
|
|
|
|
},
|
|
|
|
|
ConfirmEmail {
|
|
|
|
|
user_id: UserId,
|
|
|
|
|
confirm_email: String,
|
|
|
|
|
},
|
2023-08-02 14:43:04 -07:00
|
|
|
MinecraftAuth,
|
2023-07-11 19:13:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Flow {
|
|
|
|
|
pub async fn insert(
|
|
|
|
|
&self,
|
2023-07-14 22:55:00 -07:00
|
|
|
expires: Duration,
|
2023-10-06 09:57:33 -07:00
|
|
|
redis: &RedisPool,
|
2023-07-11 19:13:07 -07:00
|
|
|
) -> Result<String, DatabaseError> {
|
|
|
|
|
let flow = ChaCha20Rng::from_entropy()
|
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
|
.take(32)
|
|
|
|
|
.map(char::from)
|
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
redis
|
2023-10-12 17:52:24 -05:00
|
|
|
.set_serialized_to_json(FLOWS_NAMESPACE, &flow, &self, Some(expires.num_seconds()))
|
2023-07-11 19:13:07 -07:00
|
|
|
.await?;
|
|
|
|
|
Ok(flow)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
pub async fn get(id: &str, redis: &RedisPool) -> Result<Option<Flow>, DatabaseError> {
|
2023-10-12 17:52:24 -05:00
|
|
|
redis.get_deserialized_from_json(FLOWS_NAMESPACE, id).await
|
2023-07-11 19:13:07 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-06 09:57:33 -07:00
|
|
|
pub async fn remove(id: &str, redis: &RedisPool) -> Result<Option<()>, DatabaseError> {
|
|
|
|
|
redis.delete(FLOWS_NAMESPACE, id).await?;
|
2023-07-11 19:13:07 -07:00
|
|
|
Ok(Some(()))
|
|
|
|
|
}
|
|
|
|
|
}
|