autoendpoint/extractors/
user.rs

1//! User validations
2
3use crate::error::{ApiErrorKind, ApiResult};
4use crate::extractors::routers::RouterType;
5use crate::server::AppState;
6use actix_http::StatusCode;
7use autopush_common::db::{client::DbClient, User};
8use autopush_common::metric_name::MetricName;
9use autopush_common::metrics::StatsdClientExt;
10use cadence::StatsdClient;
11use uuid::Uuid;
12
13/// Perform some validations on the user, including:
14/// - Validate router type
15/// - (WebPush) Check that the subscription/channel exists
16/// - (WebPush) Drop user if inactive
17///
18/// Returns an enum representing the user's router type.
19pub async fn validate_user(
20    user: &User,
21    channel_id: &Uuid,
22    app_state: &AppState,
23) -> ApiResult<RouterType> {
24    let router_type = match user.router_type.parse::<RouterType>() {
25        Ok(router_type) => router_type,
26        Err(_) => {
27            debug!("Unknown router type, dropping user"; "user" => ?user);
28            drop_user(user.uaid, app_state.db.as_ref(), &app_state.metrics).await?;
29            return Err(ApiErrorKind::NoSubscription.into());
30        }
31    };
32
33    // Legacy GCM support was discontinued by Google in Sept 2023.
34    // Since we do not have access to the account that originally created the GCM project
35    // and credentials, we cannot move those users to modern FCM implementations, so we
36    // must drop them.
37    if router_type == RouterType::GCM {
38        debug!("Encountered GCM record, dropping user"; "user" => ?user);
39        // record the bridge error for accounting reasons.
40        app_state
41            .metrics
42            .incr_with_tags(MetricName::NotificationBridgeError)
43            .with_tag("platform", "gcm")
44            .with_tag("reason", "gcm_kill")
45            .with_tag("error", &StatusCode::GONE.to_string())
46            .send();
47        drop_user(user.uaid, app_state.db.as_ref(), &app_state.metrics).await?;
48        return Err(ApiErrorKind::Router(crate::routers::RouterError::NotFound).into());
49    }
50
51    if router_type == RouterType::WebPush {
52        validate_webpush_user(user, channel_id, app_state.db.as_ref()).await?;
53    }
54
55    Ok(router_type)
56}
57
58/// Make sure the user is not inactive and the subscription channel exists
59async fn validate_webpush_user(user: &User, channel_id: &Uuid, db: &dyn DbClient) -> ApiResult<()> {
60    // Make sure the subscription channel exists
61    let channel_ids = db.get_channels(&user.uaid).await?;
62
63    if !channel_ids.contains(channel_id) {
64        return Err(ApiErrorKind::NoSubscription.into());
65    }
66
67    Ok(())
68}
69
70/// Drop a user and increment associated metric
71pub async fn drop_user(uaid: Uuid, db: &dyn DbClient, metrics: &StatsdClient) -> ApiResult<()> {
72    metrics
73        .incr_with_tags(MetricName::UpdatesDropUser)
74        .with_tag("errno", "102")
75        .send();
76
77    db.remove_user(&uaid).await?;
78
79    Ok(())
80}