mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 12:05:53 +00:00
ignore TOTP in debug
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::{Datelike, Months, NaiveDate};
|
||||
use chrono::{Months, NaiveDate};
|
||||
use dashmap::DashMap;
|
||||
use eyre::{Result, eyre};
|
||||
use eyre::Result;
|
||||
use rust_decimal::Decimal;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use xredis::RedisPool;
|
||||
@@ -85,6 +85,13 @@ async fn fetch_estimates(
|
||||
let mut map = HashMap::<YearMonth, PeriodEstimate>::new();
|
||||
for response in metrics.responses {
|
||||
for row in response.rows {
|
||||
let Some(raw_estimated_revenue_usd) = row.revenue else {
|
||||
continue;
|
||||
};
|
||||
let Some(impressions) = row.impressions else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let date = row.time.date_naive();
|
||||
let period = YearMonth::from_day1(date);
|
||||
|
||||
@@ -94,15 +101,10 @@ async fn fetch_estimates(
|
||||
});
|
||||
let days = &mut period_estimate.days;
|
||||
|
||||
let day = date.day();
|
||||
days.push(DayEstimate {
|
||||
date,
|
||||
raw_estimated_revenue_usd: row
|
||||
.revenue
|
||||
.wrap_err_with(|| eyre!("no revenue data for day {day}"))?,
|
||||
impressions: row.impressions.wrap_err_with(|| {
|
||||
eyre!("no impressions data for day {day}")
|
||||
})?,
|
||||
raw_estimated_revenue_usd,
|
||||
impressions,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,13 @@ use crate::{
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct StartPayoutRun {
|
||||
pub period: YearMonth,
|
||||
pub two_factor_code: String,
|
||||
pub two_factor_code: Option<String>,
|
||||
/// Skip TOTP verification when testing this route locally.
|
||||
///
|
||||
/// This field does not exist in release builds.
|
||||
#[cfg(debug_assertions)]
|
||||
#[serde(default)]
|
||||
pub ignore_totp: bool,
|
||||
#[serde(with = "rust_decimal::serde::float")]
|
||||
pub raw_actual_revenue_usd: Decimal,
|
||||
pub adjustments: Vec<Adjustment>,
|
||||
@@ -66,26 +72,37 @@ pub async fn start_run(
|
||||
}
|
||||
let user_id = DBUserId::from(user.id);
|
||||
|
||||
let secret = sqlx::query_scalar!(
|
||||
r#"
|
||||
#[cfg(debug_assertions)]
|
||||
let ignore_totp = body.ignore_totp;
|
||||
#[cfg(not(debug_assertions))]
|
||||
let ignore_totp = false;
|
||||
|
||||
if !ignore_totp {
|
||||
let two_factor_code = body
|
||||
.two_factor_code
|
||||
.as_deref()
|
||||
.wrap_auth_err_with(|| AuthenticationError::InvalidCredentials)?;
|
||||
let secret = sqlx::query_scalar!(
|
||||
r#"
|
||||
SELECT totp_secret
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
"#,
|
||||
user_id.0,
|
||||
)
|
||||
.fetch_one(&**pool)
|
||||
.await
|
||||
.wrap_internal_err("fetching user two-factor secret")?
|
||||
.wrap_auth_err_with(|| AuthenticationError::InvalidCredentials)?;
|
||||
let valid_totp =
|
||||
verify_2fa_code(&body.two_factor_code, &secret, user_id, &redis)
|
||||
.await
|
||||
.wrap_auth_err("verifying two-factor code")?;
|
||||
if !valid_totp {
|
||||
return Err(ApiError::Auth(eyre::eyre!(
|
||||
AuthenticationError::InvalidCredentials,
|
||||
)));
|
||||
user_id.0,
|
||||
)
|
||||
.fetch_one(&**pool)
|
||||
.await
|
||||
.wrap_internal_err("fetching user two-factor secret")?
|
||||
.wrap_auth_err_with(|| AuthenticationError::InvalidCredentials)?;
|
||||
let valid_totp =
|
||||
verify_2fa_code(two_factor_code, &secret, user_id, &redis)
|
||||
.await
|
||||
.wrap_auth_err("verifying two-factor code")?;
|
||||
if !valid_totp {
|
||||
return Err(ApiError::Auth(eyre::eyre!(
|
||||
AuthenticationError::InvalidCredentials,
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
if body.raw_actual_revenue_usd.is_sign_negative() {
|
||||
@@ -144,8 +161,7 @@ pub async fn start_run(
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM payout_runs
|
||||
WHERE period = $1
|
||||
AND status IN ('scheduled', 'running')
|
||||
WHERE status IN ('scheduled', 'running')
|
||||
) AS "has_active_run!",
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
|
||||
Reference in New Issue
Block a user