Skip to content

Commit

Permalink
fix: revert to the base64 encoded p8 key (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 18, 2023
1 parent bbdf178 commit 0339004
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/handlers/update_apns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ pub async fn handler(
body.apns_certificate_password = Some(field.text().await?);
}
"apns_pkcs8_pem" => {
body.apns_pkcs8_pem = Some(field.text().await?);
let data = field.bytes().await?;
let encoded_p8_certificate =
base64::engine::general_purpose::STANDARD.encode(&data);
body.apns_pkcs8_pem = Some(encoded_p8_certificate);
}
"apns_key_id" => {
body.apns_key_id = Some(field.text().await?);
Expand Down Expand Up @@ -210,8 +213,10 @@ pub async fn handler(
apns_key_id,
apns_team_id,
} => {
let decoded = base64::engine::general_purpose::STANDARD
.decode(apns_pkcs8_pem.into_bytes())?;
match a2::Client::token(
&mut std::io::Cursor::new(apns_pkcs8_pem.into_bytes()),
&mut std::io::Cursor::new(decoded),
apns_key_id,
apns_team_id,
a2::Endpoint::Sandbox,
Expand Down
20 changes: 13 additions & 7 deletions src/stores/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use {
chrono::{DateTime, Utc},
serde::{Deserialize, Serialize},
sqlx::{Executor, PgPool},
std::io::BufReader,
tracing::{info, instrument},
};

#[cfg(any(debug_assertions, test))]
Expand Down Expand Up @@ -178,6 +178,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 +197,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 +218,11 @@ 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");
let p8_token =
base64::engine::general_purpose::STANDARD.decode(pkcs8_pem)?;
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 +238,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 0339004

Please sign in to comment.