| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
pub(crate) mod apps; |
| 18 |
pub(crate) mod auth; |
| 19 |
pub(crate) mod billing; |
| 20 |
pub(crate) mod blobs; |
| 21 |
pub(crate) mod groups; |
| 22 |
pub(crate) mod keys; |
| 23 |
mod subscribe; |
| 24 |
pub(crate) mod sync; |
| 25 |
|
| 26 |
use axum::routing::get; |
| 27 |
use chrono::{DateTime, Utc}; |
| 28 |
use serde::{Deserialize, Serialize}; |
| 29 |
use tower_governor::GovernorLayer; |
| 30 |
|
| 31 |
use crate::{ |
| 32 |
AppState, constants, |
| 33 |
csrf::{ |
| 34 |
CsrfRouter, delete_csrf, delete_csrf_skip, patch_csrf, post_csrf, post_csrf_skip, put_csrf, |
| 35 |
put_csrf_skip, |
| 36 |
}, |
| 37 |
db::{ |
| 38 |
self, SyncAppId, SyncDeviceId, SyncGroupId, SyncGroupInvitationId, SyncOperation, |
| 39 |
SyncPlatform, UserId, |
| 40 |
}, |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
const SYNCKIT_API_KEY_SKIP: &str = "synckit server-to-server: api_key auth, no session"; |
| 48 |
const SYNCKIT_APP_SECRET_SKIP: &str = |
| 49 |
"synckit server-to-server: keys-endpoint app_secret auth, no session"; |
| 50 |
const SYNCKIT_JWT_SKIP: &str = "synckit JWT bearer auth (SyncUser), no session"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
const CLIENT_VERSION_MAX_LENGTH: usize = 32; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
pub(crate) fn client_version(headers: &axum::http::HeaderMap) -> Option<String> { |
| 67 |
let version = headers |
| 68 |
.get(axum::http::header::USER_AGENT)? |
| 69 |
.to_str() |
| 70 |
.ok()? |
| 71 |
.split_whitespace() |
| 72 |
.next()? |
| 73 |
.strip_prefix("synckit-client/")?; |
| 74 |
let ok = !version.is_empty() |
| 75 |
&& version.len() <= CLIENT_VERSION_MAX_LENGTH |
| 76 |
&& version.starts_with(|c: char| c.is_ascii_digit()) |
| 77 |
&& version |
| 78 |
.chars() |
| 79 |
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '+' | '_')); |
| 80 |
ok.then(|| version.to_string()) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 86 |
pub(crate) struct SyncAuthRequest { |
| 87 |
pub email: String, |
| 88 |
pub password: String, |
| 89 |
pub api_key: String, |
| 90 |
|
| 91 |
|
| 92 |
pub key: String, |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Serialize, utoipa::ToSchema)] |
| 96 |
pub(crate) struct SyncAuthResponse { |
| 97 |
token: String, |
| 98 |
#[schema(value_type = String)] |
| 99 |
user_id: UserId, |
| 100 |
#[schema(value_type = String)] |
| 101 |
app_id: SyncAppId, |
| 102 |
} |
| 103 |
|
| 104 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 105 |
pub(crate) struct ValidateAppQuery { |
| 106 |
pub(crate) api_key: String, |
| 107 |
} |
| 108 |
|
| 109 |
#[derive(Serialize, utoipa::ToSchema)] |
| 110 |
pub(crate) struct ValidateAppResponse { |
| 111 |
app_name: String, |
| 112 |
} |
| 113 |
|
| 114 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 115 |
pub(crate) struct PushRequest { |
| 116 |
#[schema(value_type = String)] |
| 117 |
pub device_id: SyncDeviceId, |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub batch_id: uuid::Uuid, |
| 122 |
pub changes: Vec<ChangeEntry>, |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 126 |
pub(crate) struct ChangeEntry { |
| 127 |
pub table: String, |
| 128 |
#[schema(value_type = String)] |
| 129 |
pub op: SyncOperation, |
| 130 |
pub row_id: String, |
| 131 |
#[schema(value_type = String)] |
| 132 |
pub timestamp: DateTime<Utc>, |
| 133 |
pub data: Option<serde_json::Value>, |
| 134 |
} |
| 135 |
|
| 136 |
#[derive(Serialize, utoipa::ToSchema)] |
| 137 |
pub(crate) struct PushResponse { |
| 138 |
cursor: i64, |
| 139 |
} |
| 140 |
|
| 141 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 142 |
pub(crate) struct PullRequest { |
| 143 |
#[schema(value_type = String)] |
| 144 |
pub device_id: SyncDeviceId, |
| 145 |
pub cursor: i64, |
| 146 |
|
| 147 |
#[serde(default)] |
| 148 |
pub tables: Option<Vec<String>>, |
| 149 |
|
| 150 |
#[serde(default)] |
| 151 |
#[schema(value_type = Option<String>)] |
| 152 |
pub since: Option<DateTime<Utc>>, |
| 153 |
} |
| 154 |
|
| 155 |
#[derive(Serialize, utoipa::ToSchema)] |
| 156 |
pub(crate) struct PullResponse { |
| 157 |
changes: Vec<PullChangeEntry>, |
| 158 |
cursor: i64, |
| 159 |
has_more: bool, |
| 160 |
} |
| 161 |
|
| 162 |
#[derive(Serialize, utoipa::ToSchema)] |
| 163 |
pub(crate) struct PullChangeEntry { |
| 164 |
seq: i64, |
| 165 |
#[schema(value_type = String)] |
| 166 |
device_id: SyncDeviceId, |
| 167 |
table: String, |
| 168 |
op: String, |
| 169 |
row_id: String, |
| 170 |
#[schema(value_type = String)] |
| 171 |
timestamp: DateTime<Utc>, |
| 172 |
data: Option<serde_json::Value>, |
| 173 |
|
| 174 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 175 |
key_id: Option<i32>, |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 181 |
gck_version: Option<i32>, |
| 182 |
} |
| 183 |
|
| 184 |
#[derive(Serialize, utoipa::ToSchema)] |
| 185 |
pub(crate) struct SyncDeviceResponse { |
| 186 |
#[schema(value_type = String)] |
| 187 |
id: SyncDeviceId, |
| 188 |
#[schema(value_type = String)] |
| 189 |
app_id: SyncAppId, |
| 190 |
#[schema(value_type = String)] |
| 191 |
user_id: UserId, |
| 192 |
device_name: String, |
| 193 |
platform: String, |
| 194 |
#[schema(value_type = String)] |
| 195 |
last_seen_at: DateTime<Utc>, |
| 196 |
#[schema(value_type = String)] |
| 197 |
created_at: DateTime<Utc>, |
| 198 |
} |
| 199 |
|
| 200 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 201 |
pub(crate) struct RegisterDeviceRequest { |
| 202 |
pub device_name: String, |
| 203 |
#[schema(value_type = String)] |
| 204 |
pub platform: SyncPlatform, |
| 205 |
} |
| 206 |
|
| 207 |
#[derive(Deserialize)] |
| 208 |
pub struct CreateAppRequest { |
| 209 |
pub name: String, |
| 210 |
pub project_id: Option<String>, |
| 211 |
pub item_id: Option<String>, |
| 212 |
} |
| 213 |
|
| 214 |
#[derive(Deserialize)] |
| 215 |
pub struct UpdateAppLinkRequest { |
| 216 |
pub project_id: Option<String>, |
| 217 |
pub item_id: Option<String>, |
| 218 |
} |
| 219 |
|
| 220 |
#[derive(Deserialize)] |
| 221 |
pub struct UpdateAppSlugRequest { |
| 222 |
pub slug: String, |
| 223 |
} |
| 224 |
|
| 225 |
#[derive(Serialize, utoipa::ToSchema)] |
| 226 |
pub(crate) struct SyncStatusResponse { |
| 227 |
total_changes: i64, |
| 228 |
latest_cursor: Option<i64>, |
| 229 |
} |
| 230 |
|
| 231 |
#[derive(Serialize, utoipa::ToSchema)] |
| 232 |
pub(crate) struct SyncAccountResponse { |
| 233 |
pub email: String, |
| 234 |
pub username: String, |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 240 |
pub(crate) struct CreateGroupRequest { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#[schema(value_type = String)] |
| 246 |
pub id: SyncGroupId, |
| 247 |
pub name: String, |
| 248 |
|
| 249 |
|
| 250 |
pub admin_sealed_gck: String, |
| 251 |
|
| 252 |
|
| 253 |
pub admin_pubkey: String, |
| 254 |
} |
| 255 |
|
| 256 |
#[derive(Serialize, utoipa::ToSchema)] |
| 257 |
pub(crate) struct GroupResponse { |
| 258 |
#[schema(value_type = String)] |
| 259 |
id: SyncGroupId, |
| 260 |
#[schema(value_type = String)] |
| 261 |
app_id: SyncAppId, |
| 262 |
#[schema(value_type = String)] |
| 263 |
admin_user_id: UserId, |
| 264 |
name: String, |
| 265 |
gck_version: i32, |
| 266 |
#[schema(value_type = String)] |
| 267 |
created_at: DateTime<Utc>, |
| 268 |
} |
| 269 |
|
| 270 |
impl From<db::DbSyncGroup> for GroupResponse { |
| 271 |
fn from(g: db::DbSyncGroup) -> Self { |
| 272 |
Self { |
| 273 |
id: g.id, |
| 274 |
app_id: g.app_id, |
| 275 |
admin_user_id: g.admin_user_id, |
| 276 |
name: g.name, |
| 277 |
gck_version: g.gck_version, |
| 278 |
created_at: g.created_at, |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 284 |
pub(crate) struct AddMemberRequest { |
| 285 |
|
| 286 |
pub member_email: String, |
| 287 |
|
| 288 |
|
| 289 |
pub sealed_gck: String, |
| 290 |
|
| 291 |
|
| 292 |
pub member_pubkey: String, |
| 293 |
|
| 294 |
#[serde(default)] |
| 295 |
pub role: Option<String>, |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 300 |
pub(crate) struct CreateInvitationRequest { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#[serde(default)] |
| 305 |
pub expires_in_hours: Option<i64>, |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
#[derive(Serialize, utoipa::ToSchema)] |
| 312 |
pub(crate) struct CreateInvitationResponse { |
| 313 |
#[schema(value_type = String)] |
| 314 |
pub id: SyncGroupInvitationId, |
| 315 |
|
| 316 |
pub token: String, |
| 317 |
#[schema(value_type = String)] |
| 318 |
pub expires_at: DateTime<Utc>, |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 326 |
pub(crate) struct AcceptInvitationRequest { |
| 327 |
|
| 328 |
pub token: String, |
| 329 |
|
| 330 |
|
| 331 |
pub invitee_pubkey: String, |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
#[derive(Serialize, utoipa::ToSchema)] |
| 340 |
pub(crate) struct InvitationPreviewResponse { |
| 341 |
pub group_name: String, |
| 342 |
|
| 343 |
|
| 344 |
pub inviter_email: String, |
| 345 |
|
| 346 |
|
| 347 |
pub redeemable: bool, |
| 348 |
|
| 349 |
pub state: String, |
| 350 |
#[schema(value_type = String)] |
| 351 |
pub expires_at: DateTime<Utc>, |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 362 |
pub(crate) struct ConfirmInvitationRequest { |
| 363 |
|
| 364 |
|
| 365 |
pub sealed_gck: String, |
| 366 |
|
| 367 |
#[serde(default)] |
| 368 |
pub role: Option<String>, |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
#[derive(Serialize, utoipa::ToSchema)] |
| 377 |
pub(crate) struct InvitationResponse { |
| 378 |
#[schema(value_type = String)] |
| 379 |
pub id: SyncGroupInvitationId, |
| 380 |
|
| 381 |
pub state: String, |
| 382 |
|
| 383 |
pub invitee_email: Option<String>, |
| 384 |
|
| 385 |
|
| 386 |
pub invitee_pubkey: Option<String>, |
| 387 |
#[schema(value_type = String)] |
| 388 |
pub expires_at: DateTime<Utc>, |
| 389 |
#[schema(value_type = String)] |
| 390 |
pub created_at: DateTime<Utc>, |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
pub(crate) fn invitation_state(inv: &db::DbSyncGroupInvitation) -> &'static str { |
| 400 |
if inv.redeemed_at.is_some() { |
| 401 |
"redeemed" |
| 402 |
} else if inv.revoked_at.is_some() { |
| 403 |
"revoked" |
| 404 |
} else if inv.accepted_at.is_some() { |
| 405 |
"accepted" |
| 406 |
} else if inv.expires_at <= Utc::now() { |
| 407 |
"expired" |
| 408 |
} else { |
| 409 |
"pending" |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
impl From<db::DbSyncGroupInvitation> for InvitationResponse { |
| 414 |
fn from(inv: db::DbSyncGroupInvitation) -> Self { |
| 415 |
let state = invitation_state(&inv); |
| 416 |
Self { |
| 417 |
id: inv.id, |
| 418 |
state: state.to_string(), |
| 419 |
invitee_email: inv.invitee_email, |
| 420 |
invitee_pubkey: inv.invitee_pubkey, |
| 421 |
expires_at: inv.expires_at, |
| 422 |
created_at: inv.created_at, |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
#[derive(Serialize, utoipa::ToSchema)] |
| 430 |
pub(crate) struct GroupMemberPubkey { |
| 431 |
#[schema(value_type = String)] |
| 432 |
user_id: UserId, |
| 433 |
pubkey: String, |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 438 |
pub(crate) struct GrantQuery { |
| 439 |
|
| 440 |
#[serde(default)] |
| 441 |
pub version: Option<i32>, |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 446 |
pub(crate) struct RotateGrant { |
| 447 |
#[schema(value_type = String)] |
| 448 |
pub user_id: UserId, |
| 449 |
|
| 450 |
|
| 451 |
pub sealed_gck: String, |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 460 |
pub(crate) struct RotateGroupKeyRequest { |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
pub gck_version: i32, |
| 465 |
|
| 466 |
pub grants: Vec<RotateGrant>, |
| 467 |
} |
| 468 |
|
| 469 |
#[derive(Serialize, utoipa::ToSchema)] |
| 470 |
pub(crate) struct GroupMemberResponse { |
| 471 |
#[schema(value_type = String)] |
| 472 |
user_id: UserId, |
| 473 |
|
| 474 |
email: String, |
| 475 |
role: String, |
| 476 |
#[schema(value_type = String)] |
| 477 |
added_at: DateTime<Utc>, |
| 478 |
} |
| 479 |
|
| 480 |
#[derive(Serialize, utoipa::ToSchema)] |
| 481 |
pub(crate) struct GroupGrantResponse { |
| 482 |
|
| 483 |
|
| 484 |
sealed_gck: String, |
| 485 |
|
| 486 |
gck_version: i32, |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
#[derive(Serialize, utoipa::ToSchema)] |
| 492 |
pub(crate) struct SyncSubscriptionStatusResponse { |
| 493 |
pub active: bool, |
| 494 |
|
| 495 |
|
| 496 |
pub tier: Option<String>, |
| 497 |
pub status: Option<String>, |
| 498 |
pub storage_limit_bytes: Option<i64>, |
| 499 |
|
| 500 |
|
| 501 |
pub pending_storage_limit_bytes: Option<i64>, |
| 502 |
pub storage_used_bytes: Option<i64>, |
| 503 |
pub current_period_end: Option<String>, |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 509 |
pub(crate) struct AppPricingRequest { |
| 510 |
pub api_key: String, |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
#[derive(Serialize, utoipa::ToSchema)] |
| 517 |
pub(crate) struct AppPricingResponse { |
| 518 |
pub app_name: String, |
| 519 |
|
| 520 |
pub min_charge_cents: i64, |
| 521 |
|
| 522 |
pub per_gb_tenths_of_cent_per_month: i64, |
| 523 |
|
| 524 |
pub annual_multiplier: i64, |
| 525 |
pub min_cap_bytes: i64, |
| 526 |
pub max_cap_bytes: i64, |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 531 |
pub(crate) struct SyncQuoteRequest { |
| 532 |
pub cap_bytes: i64, |
| 533 |
pub interval: String, |
| 534 |
} |
| 535 |
|
| 536 |
#[derive(Serialize, utoipa::ToSchema)] |
| 537 |
pub(crate) struct SyncQuoteResponse { |
| 538 |
pub cap_bytes: i64, |
| 539 |
pub interval: String, |
| 540 |
pub price_cents: i64, |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 545 |
pub(crate) struct SyncSubscribeRequest { |
| 546 |
pub cap_bytes: i64, |
| 547 |
|
| 548 |
pub interval: String, |
| 549 |
} |
| 550 |
|
| 551 |
#[derive(Serialize, utoipa::ToSchema)] |
| 552 |
pub(crate) struct SyncCheckoutResponse { |
| 553 |
pub checkout_url: String, |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 559 |
pub(crate) struct SyncCapChangeRequest { |
| 560 |
pub cap_bytes: i64, |
| 561 |
} |
| 562 |
|
| 563 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 564 |
pub(crate) struct PutKeyRequest { |
| 565 |
pub encrypted_key: String, |
| 566 |
|
| 567 |
|
| 568 |
pub expected_version: i32, |
| 569 |
} |
| 570 |
|
| 571 |
#[derive(Serialize, utoipa::ToSchema)] |
| 572 |
pub(crate) struct GetKeyResponse { |
| 573 |
encrypted_key: String, |
| 574 |
key_version: i32, |
| 575 |
|
| 576 |
key_id: i32, |
| 577 |
|
| 578 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 579 |
pending_key: Option<PendingKeyInfo>, |
| 580 |
} |
| 581 |
|
| 582 |
#[derive(Serialize, utoipa::ToSchema)] |
| 583 |
pub(crate) struct PendingKeyInfo { |
| 584 |
encrypted_key: String, |
| 585 |
key_id: i32, |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 591 |
pub(crate) struct BeginRotationRequest { |
| 592 |
#[schema(value_type = String)] |
| 593 |
pub device_id: SyncDeviceId, |
| 594 |
pub new_encrypted_key: String, |
| 595 |
pub expected_key_version: i32, |
| 596 |
} |
| 597 |
|
| 598 |
#[derive(Serialize, utoipa::ToSchema)] |
| 599 |
pub(crate) struct BeginRotationResponse { |
| 600 |
rotation_id: uuid::Uuid, |
| 601 |
target_seq: i64, |
| 602 |
new_key_id: i32, |
| 603 |
} |
| 604 |
|
| 605 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 606 |
pub(crate) struct RotationEntriesRequest { |
| 607 |
pub rotation_id: uuid::Uuid, |
| 608 |
pub after_seq: i64, |
| 609 |
} |
| 610 |
|
| 611 |
#[derive(Serialize, utoipa::ToSchema)] |
| 612 |
pub(crate) struct RotationEntriesResponse { |
| 613 |
entries: Vec<RotationEntry>, |
| 614 |
has_more: bool, |
| 615 |
} |
| 616 |
|
| 617 |
#[derive(Serialize, utoipa::ToSchema)] |
| 618 |
pub(crate) struct RotationEntry { |
| 619 |
seq: i64, |
| 620 |
|
| 621 |
|
| 622 |
table: String, |
| 623 |
row_id: String, |
| 624 |
data: Option<serde_json::Value>, |
| 625 |
} |
| 626 |
|
| 627 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 628 |
pub(crate) struct RotationBatchRequest { |
| 629 |
pub rotation_id: uuid::Uuid, |
| 630 |
pub entries: Vec<RotationBatchEntry>, |
| 631 |
} |
| 632 |
|
| 633 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 634 |
pub(crate) struct RotationBatchEntry { |
| 635 |
pub seq: i64, |
| 636 |
pub data: Option<serde_json::Value>, |
| 637 |
} |
| 638 |
|
| 639 |
#[derive(Serialize, utoipa::ToSchema)] |
| 640 |
pub(crate) struct RotationBatchResponse { |
| 641 |
updated_count: u64, |
| 642 |
} |
| 643 |
|
| 644 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 645 |
pub(crate) struct CompleteRotationRequest { |
| 646 |
pub rotation_id: uuid::Uuid, |
| 647 |
} |
| 648 |
|
| 649 |
#[derive(Serialize, utoipa::ToSchema)] |
| 650 |
pub(crate) struct CompleteRotationErrorResponse { |
| 651 |
remaining: i64, |
| 652 |
} |
| 653 |
|
| 654 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 655 |
pub(crate) struct BlobUploadUrlRequest { |
| 656 |
pub hash: String, |
| 657 |
pub size_bytes: i64, |
| 658 |
} |
| 659 |
|
| 660 |
#[derive(Serialize, utoipa::ToSchema)] |
| 661 |
pub(crate) struct BlobUploadUrlResponse { |
| 662 |
upload_url: String, |
| 663 |
already_exists: bool, |
| 664 |
} |
| 665 |
|
| 666 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 667 |
pub(crate) struct BlobConfirmRequest { |
| 668 |
pub hash: String, |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 678 |
pub(crate) struct BlobMultipartStartRequest { |
| 679 |
pub hash: String, |
| 680 |
pub size_bytes: i64, |
| 681 |
} |
| 682 |
|
| 683 |
#[derive(Serialize, utoipa::ToSchema)] |
| 684 |
pub(crate) struct BlobMultipartStartResponse { |
| 685 |
upload_id: String, |
| 686 |
part_size: usize, |
| 687 |
part_count: u32, |
| 688 |
|
| 689 |
|
| 690 |
already_exists: bool, |
| 691 |
} |
| 692 |
|
| 693 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 694 |
pub(crate) struct BlobMultipartPartsRequest { |
| 695 |
pub hash: String, |
| 696 |
pub upload_id: String, |
| 697 |
|
| 698 |
|
| 699 |
pub size_bytes: i64, |
| 700 |
pub first_part: u32, |
| 701 |
pub count: u32, |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
#[serde(default)] |
| 709 |
pub checksums: Option<Vec<String>>, |
| 710 |
} |
| 711 |
|
| 712 |
#[derive(Serialize, utoipa::ToSchema)] |
| 713 |
pub(crate) struct BlobMultipartPartUrl { |
| 714 |
part_number: i32, |
| 715 |
content_length: u64, |
| 716 |
url: String, |
| 717 |
} |
| 718 |
|
| 719 |
#[derive(Serialize, utoipa::ToSchema)] |
| 720 |
pub(crate) struct BlobMultipartPartsResponse { |
| 721 |
parts: Vec<BlobMultipartPartUrl>, |
| 722 |
expires_in: u64, |
| 723 |
} |
| 724 |
|
| 725 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 726 |
pub(crate) struct BlobMultipartCompletedPart { |
| 727 |
pub part_number: i32, |
| 728 |
pub etag: String, |
| 729 |
} |
| 730 |
|
| 731 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 732 |
pub(crate) struct BlobMultipartCompleteRequest { |
| 733 |
pub hash: String, |
| 734 |
pub upload_id: String, |
| 735 |
pub parts: Vec<BlobMultipartCompletedPart>, |
| 736 |
} |
| 737 |
|
| 738 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 739 |
pub(crate) struct BlobMultipartAbortRequest { |
| 740 |
pub hash: String, |
| 741 |
pub upload_id: String, |
| 742 |
} |
| 743 |
|
| 744 |
#[derive(Deserialize, utoipa::ToSchema)] |
| 745 |
pub(crate) struct BlobDownloadUrlRequest { |
| 746 |
pub hash: String, |
| 747 |
} |
| 748 |
|
| 749 |
#[derive(Serialize, utoipa::ToSchema)] |
| 750 |
pub(crate) struct BlobDownloadUrlResponse { |
| 751 |
download_url: String, |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
#[derive(Deserialize)] |
| 765 |
pub(crate) struct BillingActivateRequest { |
| 766 |
pub enforcement_mode: String, |
| 767 |
pub storage_gb_cap: Option<u32>, |
| 768 |
pub key_cap: Option<u32>, |
| 769 |
pub gb_per_key: Option<u32>, |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
pub(crate) type BillingPatchRequest = BillingActivateRequest; |
| 775 |
|
| 776 |
|
| 777 |
#[derive(Serialize)] |
| 778 |
pub(crate) struct BillingSetupResponse { |
| 779 |
pub stripe_customer_id: String, |
| 780 |
pub billing_portal_url: String, |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
#[derive(Serialize)] |
| 786 |
pub(crate) struct BillingUpdatedResponse { |
| 787 |
pub monthly_price_cents: i64, |
| 788 |
pub billing_status: String, |
| 789 |
pub stripe_subscription_id: Option<String>, |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
#[derive(Serialize)] |
| 794 |
pub(crate) struct BillingStatusResponse { |
| 795 |
pub app_id: SyncAppId, |
| 796 |
pub billing_status: String, |
| 797 |
pub is_internal: bool, |
| 798 |
pub enforcement_mode: String, |
| 799 |
pub storage_gb_cap: Option<u32>, |
| 800 |
pub key_cap: Option<u32>, |
| 801 |
pub gb_per_key: Option<u32>, |
| 802 |
pub bytes_stored: i64, |
| 803 |
|
| 804 |
|
| 805 |
pub bytes_egress_period: i64, |
| 806 |
pub keys_claimed: u32, |
| 807 |
pub last_warning_pct: u8, |
| 808 |
pub current_period_start: Option<DateTime<Utc>>, |
| 809 |
pub current_period_end: Option<DateTime<Utc>>, |
| 810 |
|
| 811 |
|
| 812 |
pub monthly_price_cents: Option<i64>, |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
#[derive(Deserialize)] |
| 824 |
pub(crate) struct ClaimKeyRequest { |
| 825 |
pub app_secret: String, |
| 826 |
pub key: String, |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
#[derive(Serialize)] |
| 831 |
pub(crate) struct ClaimKeyResponse { |
| 832 |
pub newly_claimed: bool, |
| 833 |
pub total_claimed: i32, |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
#[derive(Deserialize)] |
| 838 |
pub(crate) struct ReleaseKeyRequest { |
| 839 |
pub app_secret: String, |
| 840 |
pub key: String, |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
#[derive(Serialize)] |
| 845 |
pub(crate) struct ReleaseKeyResponse { |
| 846 |
pub newly_released: bool, |
| 847 |
pub total_claimed: i32, |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
#[derive(Deserialize)] |
| 853 |
pub(crate) struct ListKeysRequest { |
| 854 |
pub app_secret: String, |
| 855 |
pub limit: Option<u32>, |
| 856 |
pub offset: Option<u32>, |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
#[derive(Serialize)] |
| 861 |
pub(crate) struct KeyInfo { |
| 862 |
pub id: uuid::Uuid, |
| 863 |
pub key: String, |
| 864 |
pub claimed_at: DateTime<Utc>, |
| 865 |
|
| 866 |
|
| 867 |
pub bytes_stored: i64, |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
#[derive(Serialize)] |
| 872 |
pub(crate) struct ListKeysResponse { |
| 873 |
pub keys: Vec<KeyInfo>, |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
#[derive(Serialize)] |
| 878 |
pub(super) struct AppWithKey { |
| 879 |
#[serde(flatten)] |
| 880 |
pub app: db::DbSyncApp, |
| 881 |
|
| 882 |
pub api_key: String, |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
#[derive(Serialize)] |
| 888 |
pub(super) struct AppKeysSecret { |
| 889 |
#[serde(flatten)] |
| 890 |
pub app: db::DbSyncApp, |
| 891 |
|
| 892 |
|
| 893 |
pub app_secret: String, |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
pub(super) fn generate_api_key() -> String { |
| 899 |
use rand::Rng; |
| 900 |
let mut bytes = [0u8; constants::SYNCKIT_API_KEY_LENGTH]; |
| 901 |
rand::rng().fill_bytes(&mut bytes); |
| 902 |
hex::encode(bytes) |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
pub(super) fn generate_app_secret() -> String { |
| 909 |
generate_api_key() |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
pub fn synckit_routes(synckit_jwt_secret: Option<std::sync::Arc<String>>) -> CsrfRouter<AppState> { |
| 932 |
let auth_rate_limit = crate::helpers::rate_limiter_per_sec( |
| 933 |
constants::SYNCKIT_AUTH_RATE_LIMIT_PER_SEC, |
| 934 |
constants::SYNCKIT_AUTH_RATE_LIMIT_BURST, |
| 935 |
); |
| 936 |
|
| 937 |
let auth_routes = CsrfRouter::new() |
| 938 |
.route( |
| 939 |
"/api/sync/auth", |
| 940 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, auth::sync_auth), |
| 941 |
) |
| 942 |
.route( |
| 943 |
"/api/v1/sync/auth", |
| 944 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, auth::sync_auth), |
| 945 |
) |
| 946 |
.route( |
| 947 |
"/api/sync/validate-app", |
| 948 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, auth::validate_app), |
| 949 |
) |
| 950 |
.route( |
| 951 |
"/api/v1/sync/validate-app", |
| 952 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, auth::validate_app), |
| 953 |
) |
| 954 |
|
| 955 |
.route( |
| 956 |
"/api/sync/keys/claim", |
| 957 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::claim), |
| 958 |
) |
| 959 |
.route( |
| 960 |
"/api/v1/sync/keys/claim", |
| 961 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::claim), |
| 962 |
) |
| 963 |
.route( |
| 964 |
"/api/sync/keys/release", |
| 965 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::release), |
| 966 |
) |
| 967 |
.route( |
| 968 |
"/api/v1/sync/keys/release", |
| 969 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::release), |
| 970 |
) |
| 971 |
.route( |
| 972 |
"/api/sync/keys/list", |
| 973 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::list), |
| 974 |
) |
| 975 |
.route( |
| 976 |
"/api/v1/sync/keys/list", |
| 977 |
post_csrf_skip(SYNCKIT_APP_SECRET_SKIP, keys::list), |
| 978 |
) |
| 979 |
.route( |
| 980 |
"/api/sync/app/pricing", |
| 981 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, sync::get_app_pricing), |
| 982 |
) |
| 983 |
.route( |
| 984 |
"/api/v1/sync/app/pricing", |
| 985 |
post_csrf_skip(SYNCKIT_API_KEY_SKIP, sync::get_app_pricing), |
| 986 |
) |
| 987 |
.route_layer(GovernorLayer::new(auth_rate_limit)); |
| 988 |
|
| 989 |
let sync_ip_rate_limit = crate::helpers::rate_limiter_ms( |
| 990 |
constants::SYNCKIT_SYNC_RATE_LIMIT_MS, |
| 991 |
constants::SYNCKIT_SYNC_RATE_LIMIT_BURST, |
| 992 |
); |
| 993 |
let sync_app_rate_limit = crate::helpers::synckit_app_rate_limiter_ms( |
| 994 |
synckit_jwt_secret, |
| 995 |
constants::SYNCKIT_APP_RATE_LIMIT_MS, |
| 996 |
constants::SYNCKIT_APP_RATE_LIMIT_BURST, |
| 997 |
); |
| 998 |
|
| 999 |
let sync_routes = CsrfRouter::new() |
| 1000 |
.route( |
| 1001 |
"/api/sync/push", |
| 1002 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::sync_push), |
| 1003 |
) |
| 1004 |
.route( |
| 1005 |
"/api/v1/sync/push", |
| 1006 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::sync_push), |
| 1007 |
) |
| 1008 |
.route( |
| 1009 |
"/api/sync/pull", |
| 1010 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::sync_pull), |
| 1011 |
) |
| 1012 |
.route( |
| 1013 |
"/api/v1/sync/pull", |
| 1014 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::sync_pull), |
| 1015 |
) |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
.route( |
| 1020 |
"/api/sync/groups", |
| 1021 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::create_group), |
| 1022 |
) |
| 1023 |
.route( |
| 1024 |
"/api/v1/sync/groups", |
| 1025 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::create_group), |
| 1026 |
) |
| 1027 |
.route_get("/api/sync/groups", get(groups::list_groups)) |
| 1028 |
.route_get("/api/v1/sync/groups", get(groups::list_groups)) |
| 1029 |
.route( |
| 1030 |
"/api/sync/groups/{id}/members", |
| 1031 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::add_member), |
| 1032 |
) |
| 1033 |
.route( |
| 1034 |
"/api/v1/sync/groups/{id}/members", |
| 1035 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::add_member), |
| 1036 |
) |
| 1037 |
.route_get("/api/sync/groups/{id}/members", get(groups::list_members)) |
| 1038 |
.route_get( |
| 1039 |
"/api/v1/sync/groups/{id}/members", |
| 1040 |
get(groups::list_members), |
| 1041 |
) |
| 1042 |
.route( |
| 1043 |
"/api/sync/groups/{id}/members/{user_id}", |
| 1044 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, groups::remove_member), |
| 1045 |
) |
| 1046 |
.route( |
| 1047 |
"/api/v1/sync/groups/{id}/members/{user_id}", |
| 1048 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, groups::remove_member), |
| 1049 |
) |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
.route( |
| 1054 |
"/api/sync/groups/{id}/invitations", |
| 1055 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::create_invitation), |
| 1056 |
) |
| 1057 |
.route( |
| 1058 |
"/api/v1/sync/groups/{id}/invitations", |
| 1059 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::create_invitation), |
| 1060 |
) |
| 1061 |
.route_get( |
| 1062 |
"/api/sync/groups/{id}/invitations", |
| 1063 |
get(groups::list_invitations), |
| 1064 |
) |
| 1065 |
.route_get( |
| 1066 |
"/api/v1/sync/groups/{id}/invitations", |
| 1067 |
get(groups::list_invitations), |
| 1068 |
) |
| 1069 |
.route( |
| 1070 |
"/api/sync/groups/{id}/invitations/{invitation_id}/confirm", |
| 1071 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::confirm_invitation), |
| 1072 |
) |
| 1073 |
.route( |
| 1074 |
"/api/v1/sync/groups/{id}/invitations/{invitation_id}/confirm", |
| 1075 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::confirm_invitation), |
| 1076 |
) |
| 1077 |
.route( |
| 1078 |
"/api/sync/groups/{id}/invitations/{invitation_id}", |
| 1079 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, groups::revoke_invitation), |
| 1080 |
) |
| 1081 |
.route( |
| 1082 |
"/api/v1/sync/groups/{id}/invitations/{invitation_id}", |
| 1083 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, groups::revoke_invitation), |
| 1084 |
) |
| 1085 |
.route_get( |
| 1086 |
"/api/sync/invitations/{token}", |
| 1087 |
get(groups::preview_invitation), |
| 1088 |
) |
| 1089 |
.route_get( |
| 1090 |
"/api/v1/sync/invitations/{token}", |
| 1091 |
get(groups::preview_invitation), |
| 1092 |
) |
| 1093 |
.route( |
| 1094 |
"/api/sync/invitations/accept", |
| 1095 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::accept_invitation), |
| 1096 |
) |
| 1097 |
.route( |
| 1098 |
"/api/v1/sync/invitations/accept", |
| 1099 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::accept_invitation), |
| 1100 |
) |
| 1101 |
.route_get("/api/sync/groups/{id}/grant", get(groups::get_grant)) |
| 1102 |
.route_get("/api/v1/sync/groups/{id}/grant", get(groups::get_grant)) |
| 1103 |
.route_get("/api/sync/groups/{id}/pubkeys", get(groups::list_pubkeys)) |
| 1104 |
.route_get( |
| 1105 |
"/api/v1/sync/groups/{id}/pubkeys", |
| 1106 |
get(groups::list_pubkeys), |
| 1107 |
) |
| 1108 |
.route( |
| 1109 |
"/api/sync/groups/{id}/rotate", |
| 1110 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::rotate_key), |
| 1111 |
) |
| 1112 |
.route( |
| 1113 |
"/api/v1/sync/groups/{id}/rotate", |
| 1114 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::rotate_key), |
| 1115 |
) |
| 1116 |
.route( |
| 1117 |
"/api/sync/groups/{id}/push", |
| 1118 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::group_push), |
| 1119 |
) |
| 1120 |
.route( |
| 1121 |
"/api/v1/sync/groups/{id}/push", |
| 1122 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::group_push), |
| 1123 |
) |
| 1124 |
.route( |
| 1125 |
"/api/sync/groups/{id}/pull", |
| 1126 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::group_pull), |
| 1127 |
) |
| 1128 |
.route( |
| 1129 |
"/api/v1/sync/groups/{id}/pull", |
| 1130 |
post_csrf_skip(SYNCKIT_JWT_SKIP, groups::group_pull), |
| 1131 |
) |
| 1132 |
.route_get("/api/sync/subscribe", get(subscribe::sync_subscribe)) |
| 1133 |
.route_get("/api/v1/sync/subscribe", get(subscribe::sync_subscribe)) |
| 1134 |
.route_get("/api/sync/status", get(sync::sync_status)) |
| 1135 |
.route_get("/api/v1/sync/status", get(sync::sync_status)) |
| 1136 |
.route_get("/api/sync/account", get(sync::sync_account)) |
| 1137 |
.route_get("/api/v1/sync/account", get(sync::sync_account)) |
| 1138 |
.route_get( |
| 1139 |
"/api/sync/subscription", |
| 1140 |
get(sync::sync_subscription_status), |
| 1141 |
) |
| 1142 |
.route_get( |
| 1143 |
"/api/v1/sync/subscription", |
| 1144 |
get(sync::sync_subscription_status), |
| 1145 |
) |
| 1146 |
.route( |
| 1147 |
"/api/sync/subscription/quote", |
| 1148 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::quote_subscription_price), |
| 1149 |
) |
| 1150 |
.route( |
| 1151 |
"/api/v1/sync/subscription/quote", |
| 1152 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::quote_subscription_price), |
| 1153 |
) |
| 1154 |
.route( |
| 1155 |
"/api/sync/subscription/checkout", |
| 1156 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::create_subscription_checkout), |
| 1157 |
) |
| 1158 |
.route( |
| 1159 |
"/api/v1/sync/subscription/checkout", |
| 1160 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::create_subscription_checkout), |
| 1161 |
) |
| 1162 |
.route( |
| 1163 |
"/api/sync/subscription/storage-cap", |
| 1164 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::queue_storage_cap_change), |
| 1165 |
) |
| 1166 |
.route( |
| 1167 |
"/api/v1/sync/subscription/storage-cap", |
| 1168 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::queue_storage_cap_change), |
| 1169 |
) |
| 1170 |
.route( |
| 1171 |
"/api/sync/devices", |
| 1172 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::register_device), |
| 1173 |
) |
| 1174 |
.route( |
| 1175 |
"/api/v1/sync/devices", |
| 1176 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::register_device), |
| 1177 |
) |
| 1178 |
.route_get("/api/sync/devices", get(sync::list_devices)) |
| 1179 |
.route_get("/api/v1/sync/devices", get(sync::list_devices)) |
| 1180 |
.route( |
| 1181 |
"/api/sync/devices/{id}", |
| 1182 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, sync::delete_device), |
| 1183 |
) |
| 1184 |
.route( |
| 1185 |
"/api/v1/sync/devices/{id}", |
| 1186 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, sync::delete_device), |
| 1187 |
) |
| 1188 |
.route( |
| 1189 |
"/api/sync/keys", |
| 1190 |
put_csrf_skip(SYNCKIT_JWT_SKIP, sync::put_sync_key), |
| 1191 |
) |
| 1192 |
.route( |
| 1193 |
"/api/v1/sync/keys", |
| 1194 |
put_csrf_skip(SYNCKIT_JWT_SKIP, sync::put_sync_key), |
| 1195 |
) |
| 1196 |
.route_get("/api/sync/keys", get(sync::get_sync_key)) |
| 1197 |
.route_get("/api/v1/sync/keys", get(sync::get_sync_key)) |
| 1198 |
.route( |
| 1199 |
"/api/sync/keys/rotate", |
| 1200 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::begin_rotation), |
| 1201 |
) |
| 1202 |
.route( |
| 1203 |
"/api/v1/sync/keys/rotate", |
| 1204 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::begin_rotation), |
| 1205 |
) |
| 1206 |
.route( |
| 1207 |
"/api/sync/keys/rotate", |
| 1208 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, sync::cancel_rotation), |
| 1209 |
) |
| 1210 |
.route( |
| 1211 |
"/api/v1/sync/keys/rotate", |
| 1212 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, sync::cancel_rotation), |
| 1213 |
) |
| 1214 |
.route( |
| 1215 |
"/api/sync/keys/rotate/entries", |
| 1216 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::rotation_entries), |
| 1217 |
) |
| 1218 |
.route( |
| 1219 |
"/api/v1/sync/keys/rotate/entries", |
| 1220 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::rotation_entries), |
| 1221 |
) |
| 1222 |
.route( |
| 1223 |
"/api/sync/keys/rotate/batch", |
| 1224 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::rotation_batch), |
| 1225 |
) |
| 1226 |
.route( |
| 1227 |
"/api/v1/sync/keys/rotate/batch", |
| 1228 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::rotation_batch), |
| 1229 |
) |
| 1230 |
.route( |
| 1231 |
"/api/sync/keys/rotate/complete", |
| 1232 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::complete_rotation), |
| 1233 |
) |
| 1234 |
.route( |
| 1235 |
"/api/v1/sync/keys/rotate/complete", |
| 1236 |
post_csrf_skip(SYNCKIT_JWT_SKIP, sync::complete_rotation), |
| 1237 |
) |
| 1238 |
.route( |
| 1239 |
"/api/sync/blobs/upload", |
| 1240 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_upload_url), |
| 1241 |
) |
| 1242 |
.route( |
| 1243 |
"/api/v1/sync/blobs/upload", |
| 1244 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_upload_url), |
| 1245 |
) |
| 1246 |
.route( |
| 1247 |
"/api/sync/blobs/multipart/start", |
| 1248 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_start), |
| 1249 |
) |
| 1250 |
.route( |
| 1251 |
"/api/v1/sync/blobs/multipart/start", |
| 1252 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_start), |
| 1253 |
) |
| 1254 |
.route( |
| 1255 |
"/api/sync/blobs/multipart/parts", |
| 1256 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_parts), |
| 1257 |
) |
| 1258 |
.route( |
| 1259 |
"/api/v1/sync/blobs/multipart/parts", |
| 1260 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_parts), |
| 1261 |
) |
| 1262 |
.route( |
| 1263 |
"/api/sync/blobs/multipart/complete", |
| 1264 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_complete), |
| 1265 |
) |
| 1266 |
.route( |
| 1267 |
"/api/v1/sync/blobs/multipart/complete", |
| 1268 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_complete), |
| 1269 |
) |
| 1270 |
.route( |
| 1271 |
"/api/sync/blobs/multipart/abort", |
| 1272 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_abort), |
| 1273 |
) |
| 1274 |
.route( |
| 1275 |
"/api/v1/sync/blobs/multipart/abort", |
| 1276 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_multipart_abort), |
| 1277 |
) |
| 1278 |
.route( |
| 1279 |
"/api/sync/blobs/confirm", |
| 1280 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_confirm_upload), |
| 1281 |
) |
| 1282 |
.route( |
| 1283 |
"/api/v1/sync/blobs/confirm", |
| 1284 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_confirm_upload), |
| 1285 |
) |
| 1286 |
.route( |
| 1287 |
"/api/sync/blobs/download", |
| 1288 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_download_url), |
| 1289 |
) |
| 1290 |
.route( |
| 1291 |
"/api/v1/sync/blobs/download", |
| 1292 |
post_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_download_url), |
| 1293 |
) |
| 1294 |
.route( |
| 1295 |
"/api/sync/blobs/{hash}", |
| 1296 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_delete), |
| 1297 |
) |
| 1298 |
.route( |
| 1299 |
"/api/v1/sync/blobs/{hash}", |
| 1300 |
delete_csrf_skip(SYNCKIT_JWT_SKIP, blobs::blob_delete), |
| 1301 |
) |
| 1302 |
|
| 1303 |
|
| 1304 |
.route_layer(GovernorLayer::new(sync_app_rate_limit)) |
| 1305 |
|
| 1306 |
|
| 1307 |
.route_layer(GovernorLayer::new(sync_ip_rate_limit)); |
| 1308 |
|
| 1309 |
|
| 1310 |
let app_routes = CsrfRouter::new() |
| 1311 |
.route("/api/sync/apps", post_csrf(apps::create_app)) |
| 1312 |
.route("/api/v1/sync/apps", post_csrf(apps::create_app)) |
| 1313 |
.route_get("/api/sync/apps", get(apps::list_apps)) |
| 1314 |
.route_get("/api/v1/sync/apps", get(apps::list_apps)) |
| 1315 |
.route( |
| 1316 |
"/api/sync/apps/{id}/regenerate-key", |
| 1317 |
post_csrf(apps::regenerate_app_key), |
| 1318 |
) |
| 1319 |
.route( |
| 1320 |
"/api/v1/sync/apps/{id}/regenerate-key", |
| 1321 |
post_csrf(apps::regenerate_app_key), |
| 1322 |
) |
| 1323 |
.route( |
| 1324 |
"/api/sync/apps/{id}/keys-secret", |
| 1325 |
post_csrf(apps::regenerate_app_keys_secret), |
| 1326 |
) |
| 1327 |
.route( |
| 1328 |
"/api/v1/sync/apps/{id}/keys-secret", |
| 1329 |
post_csrf(apps::regenerate_app_keys_secret), |
| 1330 |
) |
| 1331 |
.route("/api/sync/apps/{id}/link", put_csrf(apps::update_app_link)) |
| 1332 |
.route( |
| 1333 |
"/api/v1/sync/apps/{id}/link", |
| 1334 |
put_csrf(apps::update_app_link), |
| 1335 |
) |
| 1336 |
.route("/api/sync/apps/{id}/slug", put_csrf(apps::update_app_slug)) |
| 1337 |
.route( |
| 1338 |
"/api/v1/sync/apps/{id}/slug", |
| 1339 |
put_csrf(apps::update_app_slug), |
| 1340 |
) |
| 1341 |
.route("/api/sync/apps/{id}", delete_csrf(apps::delete_app)) |
| 1342 |
.route("/api/v1/sync/apps/{id}", delete_csrf(apps::delete_app)) |
| 1343 |
|
| 1344 |
.route( |
| 1345 |
"/api/sync/apps/{id}/billing/setup", |
| 1346 |
post_csrf(billing::setup), |
| 1347 |
) |
| 1348 |
.route( |
| 1349 |
"/api/v1/sync/apps/{id}/billing/setup", |
| 1350 |
post_csrf(billing::setup), |
| 1351 |
) |
| 1352 |
.route( |
| 1353 |
"/api/sync/apps/{id}/billing/activate", |
| 1354 |
post_csrf(billing::activate), |
| 1355 |
) |
| 1356 |
.route( |
| 1357 |
"/api/v1/sync/apps/{id}/billing/activate", |
| 1358 |
post_csrf(billing::activate), |
| 1359 |
) |
| 1360 |
.route("/api/sync/apps/{id}/billing", patch_csrf(billing::patch)) |
| 1361 |
.route("/api/v1/sync/apps/{id}/billing", patch_csrf(billing::patch)) |
| 1362 |
.route("/api/sync/apps/{id}/billing", delete_csrf(billing::cancel)) |
| 1363 |
.route( |
| 1364 |
"/api/v1/sync/apps/{id}/billing", |
| 1365 |
delete_csrf(billing::cancel), |
| 1366 |
) |
| 1367 |
.route_get("/api/sync/apps/{id}/billing", get(billing::get)) |
| 1368 |
.route_get("/api/v1/sync/apps/{id}/billing", get(billing::get)) |
| 1369 |
.route_get("/api/sync/apps/{id}/billing/portal", get(billing::portal)) |
| 1370 |
.route_get( |
| 1371 |
"/api/v1/sync/apps/{id}/billing/portal", |
| 1372 |
get(billing::portal), |
| 1373 |
); |
| 1374 |
|
| 1375 |
auth_routes.merge(sync_routes).merge(app_routes) |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[cfg(test)] |
| 1379 |
mod tests { |
| 1380 |
use super::client_version; |
| 1381 |
use axum::http::{HeaderMap, HeaderValue, header::USER_AGENT}; |
| 1382 |
|
| 1383 |
fn ua(value: &str) -> HeaderMap { |
| 1384 |
let mut headers = HeaderMap::new(); |
| 1385 |
headers.insert(USER_AGENT, HeaderValue::from_str(value).unwrap()); |
| 1386 |
headers |
| 1387 |
} |
| 1388 |
|
| 1389 |
#[test] |
| 1390 |
fn reads_the_sdk_version() { |
| 1391 |
assert_eq!( |
| 1392 |
client_version(&ua("synckit-client/0.6.0")).as_deref(), |
| 1393 |
Some("0.6.0") |
| 1394 |
); |
| 1395 |
|
| 1396 |
assert_eq!( |
| 1397 |
client_version(&ua("synckit-client/1.0.0-rc.2")).as_deref(), |
| 1398 |
Some("1.0.0-rc.2") |
| 1399 |
); |
| 1400 |
|
| 1401 |
assert_eq!( |
| 1402 |
client_version(&ua("synckit-client/0.6.0 audiofiles/0.9.1")).as_deref(), |
| 1403 |
Some("0.6.0") |
| 1404 |
); |
| 1405 |
} |
| 1406 |
|
| 1407 |
#[test] |
| 1408 |
fn ignores_anything_that_is_not_the_sdk() { |
| 1409 |
assert_eq!(client_version(&HeaderMap::new()), None); |
| 1410 |
assert_eq!(client_version(&ua("curl/8.5.0")), None); |
| 1411 |
assert_eq!(client_version(&ua("Mozilla/5.0 (X11; Linux x86_64)")), None); |
| 1412 |
|
| 1413 |
assert_eq!(client_version(&ua("synckit-client/")), None); |
| 1414 |
|
| 1415 |
assert_eq!(client_version(&ua("evil-synckit-client/9.9.9")), None); |
| 1416 |
} |
| 1417 |
|
| 1418 |
#[test] |
| 1419 |
fn rejects_junk_rather_than_storing_it() { |
| 1420 |
|
| 1421 |
|
| 1422 |
let long = format!("synckit-client/{}", "9".repeat(64)); |
| 1423 |
assert_eq!(client_version(&ua(&long)), None); |
| 1424 |
|
| 1425 |
|
| 1426 |
assert_eq!(client_version(&ua("synckit-client/not-a-version")), None); |
| 1427 |
assert_eq!(client_version(&ua("synckit-client/../../etc/passwd")), None); |
| 1428 |
|
| 1429 |
let at_max = format!("synckit-client/1{}", "0".repeat(31)); |
| 1430 |
assert!(client_version(&ua(&at_max)).is_some()); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|