| 1 |
|
| 2 |
|
| 3 |
use axum::{Json, extract::State, response::IntoResponse}; |
| 4 |
|
| 5 |
use sqlx::PgPool; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
auth::verify_password_async, |
| 9 |
config::Config, |
| 10 |
db, |
| 11 |
error::{AppError, Result}, |
| 12 |
synckit_auth, validation, |
| 13 |
}; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
static DUMMY_HASH: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| { |
| 18 |
crate::auth::hash_password("anti-timing-dummy").expect("dummy hash") |
| 19 |
}); |
| 20 |
|
| 21 |
use super::{SyncAuthRequest, SyncAuthResponse, ValidateAppQuery, ValidateAppResponse}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#[utoipa::path( |
| 29 |
post, |
| 30 |
path = "/api/v1/sync/auth", |
| 31 |
tag = "SyncKit", |
| 32 |
request_body = SyncAuthRequest, |
| 33 |
responses( |
| 34 |
(status = 200, description = "JWT token for sync API access", body = SyncAuthResponse), |
| 35 |
(status = 401, description = "Invalid credentials or API key"), |
| 36 |
), |
| 37 |
)] |
| 38 |
#[tracing::instrument(skip_all, name = "synckit::sync_auth")] |
| 39 |
pub(super) async fn sync_auth( |
| 40 |
State(db): State<PgPool>, |
| 41 |
State(config): State<Config>, |
| 42 |
headers: axum::http::HeaderMap, |
| 43 |
Json(req): Json<SyncAuthRequest>, |
| 44 |
) -> Result<impl IntoResponse> { |
| 45 |
let secret = config |
| 46 |
.synckit_jwt_secret |
| 47 |
.as_deref() |
| 48 |
.ok_or_else(|| AppError::ServiceUnavailable("SyncKit is not configured".to_string()))?; |
| 49 |
|
| 50 |
validation::validate_synckit_key(&req.key)?; |
| 51 |
|
| 52 |
|
| 53 |
let app = db::synckit::get_sync_app_by_api_key(&db, &req.api_key) |
| 54 |
.await? |
| 55 |
.ok_or(AppError::Unauthorized)?; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if crate::validation::password_too_long(&req.password) { |
| 61 |
let _ = verify_password_async("dummy".to_string(), DUMMY_HASH.clone()).await; |
| 62 |
return Err(AppError::Unauthorized); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
let Ok(email) = db::Email::new(&req.email) else { |
| 68 |
|
| 69 |
let _ = verify_password_async("dummy".to_string(), DUMMY_HASH.clone()).await; |
| 70 |
return Err(AppError::Unauthorized); |
| 71 |
}; |
| 72 |
let Some(user) = db::users::get_user_by_email(&db, &email).await? else { |
| 73 |
|
| 74 |
let _ = verify_password_async("dummy".to_string(), DUMMY_HASH.clone()).await; |
| 75 |
return Err(AppError::Unauthorized); |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
match crate::auth::relying_party_login_gate(&db, &user, &req.password).await? { |
| 89 |
crate::auth::LoginGate::Deny { .. } => { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
let ip = crate::helpers::extract_client_ip(&headers); |
| 95 |
if let Err(e) = db::synckit::record_security_event( |
| 96 |
&db, |
| 97 |
app.id, |
| 98 |
Some(user.id), |
| 99 |
db::synckit::sync_security_event::AUTH_FAILURE, |
| 100 |
None, |
| 101 |
ip.as_deref(), |
| 102 |
) |
| 103 |
.await |
| 104 |
{ |
| 105 |
tracing::error!(error = ?e, "failed to record auth_failure security event"); |
| 106 |
} |
| 107 |
return Err(AppError::Unauthorized); |
| 108 |
} |
| 109 |
crate::auth::LoginGate::Allow => {} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
let billing = db::synckit_billing::get_app_with_billing(&db, app.id) |
| 122 |
.await? |
| 123 |
.ok_or(AppError::Unauthorized)?; |
| 124 |
if !billing.is_internal && billing.enforcement_mode == "per_key" { |
| 125 |
let key_cap = billing.key_cap.unwrap_or(0); |
| 126 |
let claim = db::synckit_billing::claim_key(&db, app.id, &req.key, Some(key_cap)).await?; |
| 127 |
if claim.cap_reached { |
| 128 |
return Err(AppError::PaymentRequired(format!( |
| 129 |
"key limit reached ({} of {key_cap} keys claimed); release an unused key or raise the cap", |
| 130 |
claim.total_claimed |
| 131 |
))); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
let token = synckit_auth::create_sync_token(secret, user.id, app.id, &req.key)?; |
| 136 |
|
| 137 |
Ok(Json(SyncAuthResponse { |
| 138 |
token, |
| 139 |
user_id: user.id, |
| 140 |
app_id: app.id, |
| 141 |
})) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[utoipa::path( |
| 148 |
post, |
| 149 |
path = "/api/v1/sync/validate-app", |
| 150 |
tag = "SyncKit", |
| 151 |
request_body = ValidateAppQuery, |
| 152 |
responses( |
| 153 |
(status = 200, description = "App name", body = ValidateAppResponse), |
| 154 |
(status = 401, description = "Invalid API key"), |
| 155 |
), |
| 156 |
)] |
| 157 |
#[tracing::instrument(skip_all, name = "synckit::validate_app")] |
| 158 |
pub(super) async fn validate_app( |
| 159 |
State(db): State<PgPool>, |
| 160 |
Json(params): Json<ValidateAppQuery>, |
| 161 |
) -> Result<impl IntoResponse> { |
| 162 |
let app = db::synckit::get_sync_app_by_api_key(&db, ¶ms.api_key) |
| 163 |
.await? |
| 164 |
.ok_or(AppError::Unauthorized)?; |
| 165 |
Ok(Json(ValidateAppResponse { app_name: app.name })) |
| 166 |
} |
| 167 |
|