Skip to content

Commit

Permalink
fix: adding jwt verification to the apns and fcm update handler (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 25, 2023
1 parent 7bf555a commit 2c454eb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/handlers/update_apns.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use {
crate::{
error::{Error, Error::InvalidMultipartBody},
handlers::validate_tenant_request,
increment_counter,
request_id::get_req_id,
state::AppState,
stores::tenant::{TenantApnsUpdateAuth, TenantApnsUpdateParams},
},
axum::{
extract::{Multipart, Path, State},
http::HeaderMap,
Json,
},
base64::Engine,
serde::{Deserialize, Serialize},
std::{io::BufReader, sync::Arc},

Check warning on line 17 in src/handlers/update_apns.rs

View workflow job for this annotation

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

unused import: `io::BufReader`

Check warning on line 17 in src/handlers/update_apns.rs

View workflow job for this annotation

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

unused import: `io::BufReader`

Check warning on line 17 in src/handlers/update_apns.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Clippy

unused import: `io::BufReader`

Check warning on line 17 in src/handlers/update_apns.rs

View workflow job for this annotation

GitHub Actions / run-ci / [ubuntu-latest/rust-stable] Clippy

unused import: `io::BufReader`
tracing::warn,
tracing::{error, warn},
};

#[derive(Deserialize)]
Expand Down Expand Up @@ -117,11 +120,37 @@ pub struct UpdateTenantApnsResponse {
pub async fn handler(
State(state): State<Arc<AppState>>,
Path(id): Path<String>,
headers: HeaderMap,
mut form_body: Multipart,
) -> Result<Json<UpdateTenantApnsResponse>, Error> {
// Ensure tenant real
let _existing_tenant = state.tenant_store.get_tenant(&id).await?;

// JWT verification
let req_id = get_req_id(&headers);
#[cfg(feature = "cloud")]
let jwt_verification_result = validate_tenant_request(
&state.registry_client,
&state.gotrue_client,
&headers,
id.clone(),
None,
)
.await;

#[cfg(not(feature = "cloud"))]
let jwt_verification_result = validate_tenant_request(&state.gotrue_client, &headers);

if let Err(e) = jwt_verification_result {
error!(
request_id = %req_id,
tenant_id = %id,
err = ?e,
"JWT verification failed"
);
return Err(e);
}

// ---- retrieve body from form
let mut body = ApnsUpdateBody {
apns_topic: None,
Expand Down
30 changes: 30 additions & 0 deletions src/handlers/update_fcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ use {
Error,
Error::{BadFcmApiKey, InvalidMultipartBody},
},
handlers::validate_tenant_request,
increment_counter,
request_id::get_req_id,
state::AppState,
stores::tenant::TenantFcmUpdateParams,
},
axum::{
extract::{Multipart, Path, State},
http::HeaderMap,
Json,
},
fcm::FcmError,
serde::Serialize,
std::sync::Arc,
tracing::error,
};

pub struct FcmUpdateBody {
Expand All @@ -31,11 +35,37 @@ pub struct UpdateTenantFcmResponse {
pub async fn handler(
State(state): State<Arc<AppState>>,
Path(id): Path<String>,
headers: HeaderMap,
mut form_body: Multipart,
) -> Result<Json<UpdateTenantFcmResponse>, Error> {
// -- check if tenant is real
let _existing_tenant = state.tenant_store.get_tenant(&id).await?;

// JWT token verification
let req_id = get_req_id(&headers);
#[cfg(feature = "cloud")]
let jwt_verification_result = validate_tenant_request(
&state.registry_client,
&state.gotrue_client,
&headers,
id.clone(),
None,
)
.await;

#[cfg(not(feature = "cloud"))]
let jwt_verification_result = validate_tenant_request(&state.gotrue_client, &headers);

if let Err(e) = jwt_verification_result {
error!(
request_id = %req_id,
tenant_id = %id,
err = ?e,
"JWT verification failed"
);
return Err(e);
}

// ---- retrieve body from form
let mut body = FcmUpdateBody {
api_key: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions tests/functional/multitenant/apns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async fn tenant_update_apns_valid_token(ctx: &mut EchoServerContext) {
"http://{}/tenants/{}/apns",
ctx.server.public_addr, &tenant_id
))
.header("AUTHORIZATION", jwt_token.clone())
.multipart(form)
.send()
.await
Expand Down

0 comments on commit 2c454eb

Please sign in to comment.