Skip to content

Commit

Permalink
feat: fcm verification
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryET committed Aug 1, 2023
1 parent 219b1ec commit b66c75d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ pub enum Error {

#[error("failed to load geoip database from s3")]
GeoIpS3Failed,

#[error("invalid fcm api key")]
BadFcmApiKey,
}

impl IntoResponse for Error {
Expand Down Expand Up @@ -198,6 +201,18 @@ impl IntoResponse for Error {
message: e.to_string(),
}
], vec![]),
Error::BadFcmApiKey => crate::handlers::Response::new_failure(StatusCode::BAD_REQUEST, vec![
ResponseError {
name: "bad_fcm_api_key".to_string(),
message: "The provided API Key was not valid".to_string(),
}
], vec![
ErrorField {
field: "api_key".to_string(),
description: "The provided API Key was not valid".to_string(),
location: ErrorLocation::Body,
}
]),
Error::Database(e) => crate::handlers::Response::new_failure(StatusCode::INTERNAL_SERVER_ERROR, vec![
ResponseError {
name: "sqlx".to_string(),
Expand Down
22 changes: 21 additions & 1 deletion src/handlers/update_fcm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
crate::{
error::{Error, Error::InvalidMultipartBody},
error::{
Error,
Error::{BadFcmApiKey, InvalidMultipartBody},
},
increment_counter,
state::AppState,
stores::tenant::TenantFcmUpdateParams,
Expand All @@ -9,6 +12,7 @@ use {
extract::{Multipart, Path, State},
Json,
},
fcm::FcmError,
serde::Serialize,
std::sync::Arc,
};
Expand Down Expand Up @@ -50,6 +54,22 @@ pub async fn handler(
return Err(InvalidMultipartBody);
}

// ---- checks
let test_notification = fcm::Client::new()
.send(
fcm::MessageBuilder::new(&body.api_key.clone(), "wc-notification-test")

Check failure on line 60 in src/handlers/update_fcm.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

cannot move out of a mutable reference
.dry_run(true)
.finalize(),
)
.await;
match test_notification {
Err(e) => match e {
FcmError::Unauthorized => Err(BadFcmApiKey),
_ => Ok(()),
},
Ok(_) => Ok(()),
}?;

// ---- handler
let update_body = TenantFcmUpdateParams {
fcm_api_key: body.api_key,
Expand Down

0 comments on commit b66c75d

Please sign in to comment.