Skip to content

Commit

Permalink
fix: adding check for the base64 encoded legacy p8 key
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 18, 2023
1 parent 2e6a9fe commit 73c2852
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/stores/tenant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(feature = "multitenant"))]
use {crate::config::Config, std::sync::Arc};
use {
crate::{
config::Config,
error::{
self,
Error::{InvalidTenantId, ProviderNotAvailable},
Expand All @@ -19,7 +19,8 @@ use {
chrono::{DateTime, Utc},
serde::{Deserialize, Serialize},
sqlx::{Executor, PgPool},
std::io::BufReader,
std::sync::Arc,
tracing::{info, instrument},
};

#[cfg(any(debug_assertions, test))]
Expand Down Expand Up @@ -178,6 +179,7 @@ impl Tenant {
}
}

#[instrument(skip_all, fields(tenant_id = %self.id, provider = %provider.as_str()))]
pub fn provider(&self, provider: &ProviderKind) -> Result<Provider> {
if !self.providers().contains(provider) {
return Err(ProviderNotAvailable(provider.into()));
Expand All @@ -196,12 +198,11 @@ impl Tenant {
&self.apns_topic,
) {
(Some(certificate), Some(password), Some(topic)) => {
info!("apns certificate (p12) provider is matched");
let decoded =
base64::engine::general_purpose::STANDARD.decode(certificate)?;
let mut reader = BufReader::new(&*decoded);

let apns_client = ApnsProvider::new_cert(
&mut reader,
&mut &mut std::io::Cursor::new(decoded),
password.clone(),
endpoint,
topic.clone(),
Expand All @@ -218,9 +219,15 @@ impl Tenant {
&self.apns_team_id,
) {
(Some(topic), Some(pkcs8_pem), Some(key_id), Some(team_id)) => {
let mut reader = BufReader::new(pkcs8_pem.as_bytes());
info!("apns token (p8) provider is matched");
// For supporting the legacy format of the p8 key which is double base64 encoded
// we should check if the string is a correct base64 and pass the decoded string
// or pass the string as is if it's not a base64
let p8_token = base64::engine::general_purpose::STANDARD
.decode(pkcs8_pem)
.unwrap_or(pkcs8_pem.as_bytes().to_vec());
let apns_client = ApnsProvider::new_token(
&mut reader,
&mut std::io::Cursor::new(p8_token),
key_id.clone(),
team_id.clone(),
endpoint,
Expand All @@ -236,13 +243,17 @@ impl Tenant {
}
ProviderKind::Fcm => match self.fcm_api_key.clone() {
Some(api_key) => {
info!("fcm provider is matched");
let fcm = FcmProvider::new(api_key);
Ok(Fcm(fcm))
}
None => Err(ProviderNotAvailable(provider.into())),
},
#[cfg(any(debug_assertions, test))]
ProviderKind::Noop => Ok(Noop(NoopProvider::new())),
ProviderKind::Noop => {
info!("noop provider is matched");
Ok(Noop(NoopProvider::new()))
}
}
}
}
Expand Down

0 comments on commit 73c2852

Please sign in to comment.