Skip to content

Commit

Permalink
fix: changing apns key encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 15, 2023
1 parent 80e3bf5 commit 163155f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
13 changes: 4 additions & 9 deletions src/handlers/get_tenant.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
use {
crate::{
error::Error,
handlers::validate_tenant_request,
log::prelude::*,
providers::ProviderKind,
request_id::get_req_id,
state::AppState,
stores::tenant::ApnsType,
error::Error, handlers::validate_tenant_request, log::prelude::*, providers::ProviderKind,
request_id::get_req_id, state::AppState, stores::tenant::ApnsType,
},
axum::{
extract::{Path, State},
http::HeaderMap,
Json,
},
serde::Serialize,
serde::{Deserialize, Serialize},
std::sync::Arc,
};

#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
pub struct GetTenantResponse {
url: String,
enabled_providers: Vec<String>,
Expand Down
30 changes: 19 additions & 11 deletions src/handlers/update_apns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,27 @@ impl ApnsUpdateBody {
}),
})
}
(Some(topic), Some(certificate), None, None, None, None) => Ok(ApnsSqlUpdate {
topic: Some(topic.clone()),
auth: Some(TenantApnsUpdateAuth::Certificate {
apns_certificate: certificate.clone(),
apns_certificate_password: String::new(),
}),
}),
(None, Some(certificate), Some(password), None, None, None) => Ok(ApnsSqlUpdate {
topic: None,
auth: Some(TenantApnsUpdateAuth::Certificate {
apns_certificate: certificate.clone(),
apns_certificate_password: password.clone(),
}),
}),
(None, Some(certificate), None, None, None, None) => Ok(ApnsSqlUpdate {
topic: None,
auth: Some(TenantApnsUpdateAuth::Certificate {
apns_certificate: certificate.clone(),
apns_certificate_password: String::new(),
}),
}),
// Update Token
(Some(topic), None, None, Some(pkcs8_pem), Some(key_id), Some(team_id)) => {
Ok(ApnsSqlUpdate {
Expand Down Expand Up @@ -136,9 +150,7 @@ pub async fn handler(
body.apns_certificate_password = Some(field.text().await?);
}
"apns_pkcs8_pem" => {
let data = field.bytes().await?;
let encoded_pem = base64::engine::general_purpose::STANDARD.encode(&data);
body.apns_pkcs8_pem = Some(encoded_pem);
body.apns_pkcs8_pem = Some(field.text().await?);
}
"apns_key_id" => {
body.apns_key_id = Some(field.text().await?);
Expand Down Expand Up @@ -179,11 +191,10 @@ pub async fn handler(
apns_certificate,
apns_certificate_password,
} => {
let cert_bytes = apns_certificate.into_bytes();
let mut reader = BufReader::new(&*cert_bytes);

let decoded = base64::engine::general_purpose::STANDARD
.decode(apns_certificate.into_bytes())?;
match a2::Client::certificate(
&mut reader,
&mut std::io::Cursor::new(decoded),
&apns_certificate_password,
a2::Endpoint::Sandbox,
) {
Expand All @@ -199,11 +210,8 @@ pub async fn handler(
apns_key_id,
apns_team_id,
} => {
let pem_bytes = apns_pkcs8_pem.into_bytes();
let mut reader = BufReader::new(&*pem_bytes);

match a2::Client::token(
&mut reader,
&mut std::io::Cursor::new(apns_pkcs8_pem.into_bytes()),
apns_key_id,
apns_team_id,
a2::Endpoint::Sandbox,
Expand Down
5 changes: 1 addition & 4 deletions src/stores/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ impl Tenant {
&self.apns_team_id,
) {
(Some(topic), Some(pkcs8_pem), Some(key_id), Some(team_id)) => {
let decoded =
base64::engine::general_purpose::STANDARD.decode(pkcs8_pem)?;
let mut reader = BufReader::new(&*decoded);

let mut reader = BufReader::new(pkcs8_pem.as_bytes());
let apns_client = ApnsProvider::new_token(
&mut reader,
key_id.clone(),
Expand Down

0 comments on commit 163155f

Please sign in to comment.