lockbook_server_lib/
lib.rs

1use billing::app_store_client::AppStoreClient;
2use billing::google_play_client::GooglePlayClient;
3use billing::stripe_client::StripeClient;
4use document_service::DocumentService;
5use std::env;
6use std::fmt::Debug;
7use std::sync::{Arc, Mutex};
8
9use libsecp256k1::PublicKey;
10use lockbook_shared::api::{ErrorWrapper, Request, RequestWrapper};
11use lockbook_shared::{clock, pubkey, SharedError};
12use semver::Version;
13use serde::{Deserialize, Serialize};
14
15use crate::account_service::GetUsageHelperError;
16use crate::billing::billing_service::StripeWebhookError;
17use crate::billing::stripe_error::SimplifiedStripeError;
18use crate::schema::ServerV4;
19use crate::ServerError::ClientError;
20pub use stripe;
21use tracing::log::warn;
22
23static CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
24
25#[derive(Clone)]
26pub struct ServerState<S, A, G, D>
27where
28    S: StripeClient,
29    A: AppStoreClient,
30    G: GooglePlayClient,
31    D: DocumentService,
32{
33    pub config: config::Config,
34    pub index_db: Arc<Mutex<ServerV4>>,
35    pub stripe_client: S,
36    pub google_play_client: G,
37    pub app_store_client: A,
38    pub document_service: D,
39}
40
41#[derive(Clone)]
42pub struct RequestContext<TRequest> {
43    pub request: TRequest,
44    pub public_key: PublicKey,
45}
46
47#[derive(Clone, Debug, Serialize, Deserialize)]
48pub enum ServerError<U: Debug> {
49    ClientError(U),
50    InternalError(String),
51}
52
53#[macro_export]
54macro_rules! internal {
55    ($($arg:tt)*) => {{
56        let msg = format!($($arg)*);
57        tracing::error!("{}", msg);
58        $crate::ServerError::InternalError(msg)
59    }};
60}
61
62pub fn handle_version_header<Req: Request>(
63    config: &config::Config, version: &Option<String>,
64) -> Result<(), ErrorWrapper<Req::Error>> {
65    let v = &version.clone().unwrap_or("0.0.0".to_string());
66    let Ok(v) = Version::parse(v) else {
67        warn!("version not parsable, request rejected: {v}");
68        return Err(ErrorWrapper::BadRequest);
69    };
70    router_service::CORE_VERSION_COUNTER
71        .with_label_values(&[&(v.to_string())])
72        .inc();
73    if !config.server.min_core_version.matches(&v) {
74        return Err(ErrorWrapper::<Req::Error>::ClientUpdateRequired);
75    }
76    Ok(())
77}
78
79pub fn verify_auth<TRequest>(
80    config: &config::Config, request: &RequestWrapper<TRequest>,
81) -> Result<(), SharedError>
82where
83    TRequest: Request + Serialize,
84{
85    pubkey::verify(
86        &request.signed_request.public_key,
87        &request.signed_request,
88        config.server.max_auth_delay as u64,
89        config.server.max_auth_delay as u64,
90        clock::get_time,
91    )
92}
93
94pub mod account_service;
95pub mod billing;
96pub mod config;
97pub mod document_service;
98pub mod error_handler;
99pub mod file_service;
100pub mod loggers;
101pub mod metrics;
102pub mod router_service;
103pub mod schema;
104pub mod utils;