Skip to content

Commit

Permalink
feat: add Client::certificate_parts() method (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrconlin committed Jun 5, 2023
1 parent d0236c3 commit 8b38de0
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "a2"
version = "0.7.0"
version = "0.7.1"
authors = [
"Harry Bairstow <harry@walletconnect.com>",
"Julius de Bruijn <julius@nauk.io>",
Expand All @@ -14,6 +14,7 @@ repository = "https://github.com/walletconnect/a2.git"
homepage = "https://github.com/walletconnect/a2"
documentation = "https://docs.rs/a2"
edition = "2021"
rust-version = "1.60" # set the minimum rust version we can work with.

[features]
default = ["openssl"]
Expand Down
1 change: 0 additions & 1 deletion examples/certificate_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use a2::{Client, DefaultNotificationBuilder, NotificationBuilder, NotificationOptions};
use argparse::{ArgumentParser, Store, StoreOption, StoreTrue};
use tokio;

// An example client connectiong to APNs with a certificate and key
#[tokio::main]
Expand Down
1 change: 0 additions & 1 deletion examples/token_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use argparse::{ArgumentParser, Store, StoreOption, StoreTrue};
use std::fs::File;
use tokio;

use a2::{Client, DefaultNotificationBuilder, Endpoint, NotificationBuilder, NotificationOptions};

Expand Down
24 changes: 23 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ impl Client {
Ok(Self::new(connector, None, endpoint))
}

/// Create a connection to APNs using the raw PEM-formatted certificate and
/// key, extracted from the provider client certificate you obtain from your
/// [Apple developer account](https://developer.apple.com/account/)
pub fn certificate_parts(cert_pem: &[u8], key_pem: &[u8], endpoint: Endpoint) -> Result<Client, Error> {
let connector = AlpnConnector::with_client_cert(cert_pem, key_pem)?;

Ok(Self::new(connector, None, endpoint))
}

/// Create a connection to APNs using system certificates, signing every
/// request with a signature using a private key, key id and team id
/// provisioned from your [Apple developer
Expand Down Expand Up @@ -181,7 +190,7 @@ mod tests {
use hyper::Method;
use hyper_alpn::AlpnConnector;

const PRIVATE_KEY: &'static str = "-----BEGIN PRIVATE KEY-----
const PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8g/n6j9roKvnUkwu
lCEIvbDqlUhA5FOzcakkG90E8L+hRANCAATKS2ZExEybUvchRDuKBftotMwVEus3
jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ
Expand Down Expand Up @@ -458,4 +467,17 @@ jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ

assert_eq!(payload.to_json_string().unwrap(), body_str,);
}

#[tokio::test]
/// Try to create a test client using the unencrypted key & cert provided.
/// These are test values that do not work with Apple, but mimic the sort
/// of values you should get from the Apple Developer Console.
async fn test_cert_parts() -> Result<(), Error> {
let key: Vec<u8> = include_str!("../test_cert/test.key").bytes().collect();
let cert: Vec<u8> = include_str!("../test_cert/test.crt").bytes().collect();

let c = Client::certificate_parts(&cert, &key, Endpoint::Sandbox)?;
assert!(c.signer.is_none());
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Error and result module
/// Error and result module
use crate::{response::Response, signer::SignerError};
use std::io;
use thiserror::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/request/notification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! The `aps` notification content builders
/// The `aps` notification content builders
mod default;
mod options;
mod web;
Expand Down
2 changes: 1 addition & 1 deletion src/request/payload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Payload with `aps` and custom data
/// Payload with `aps` and custom data
use crate::error::Error;
use crate::request::notification::{DefaultAlert, NotificationOptions, WebPushAlert};
use erased_serde::Serialize;
Expand Down
2 changes: 1 addition & 1 deletion src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ fn get_time() -> i64 {
mod tests {
use super::*;

const PRIVATE_KEY: &'static str = "-----BEGIN PRIVATE KEY-----
const PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8g/n6j9roKvnUkwu
lCEIvbDqlUhA5FOzcakkG90E8L+hRANCAATKS2ZExEybUvchRDuKBftotMwVEus3
jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ
Expand Down
11 changes: 11 additions & 0 deletions test_cert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This contains a self signed cert for test purposes. The password is
"test".

These values are not encrypted

Key and Cert generation (unencrypted):

```
$ openssl req -newkey rsa:2048 -nodes \
-keyout test.key -x509 -days 3650 -out test.crt
```
21 changes: 21 additions & 0 deletions test_cert/test.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDhTCCAm2gAwIBAgIUJgWPx+sbai7lWxPHuIS6aFXM9jwwDQYJKoZIhvcNAQEL
BQAwUjELMAkGA1UEBhMCVVMxDTALBgNVBAgMBFRlc3QxDTALBgNVBAcMBFRlc3Qx
FjAUBgNVBAoMDVRlc3R5IFRlc3RpbmcxDTALBgNVBAMMBFRlc3QwHhcNMjMwNTIy
MjIwOTUxWhcNMzMwNTE5MjIwOTUxWjBSMQswCQYDVQQGEwJVUzENMAsGA1UECAwE
VGVzdDENMAsGA1UEBwwEVGVzdDEWMBQGA1UECgwNVGVzdHkgVGVzdGluZzENMAsG
A1UEAwwEVGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOUGcm/w
hdvnCJIxFgR3Quj/DLmeush7lHh5J77bMXUWTJy/CltQZdQtJVYg35bmVTDMtjUw
1crkhFjgsF90uYPWUf5JgIHpg+5WXMkP686GvPC8sscWjMFVnqs1ot2gdMQkTSPL
0Kaxfch02TXJjK1PI6bnbJGX6qrFLR2/7a6IfUtAn2eFwHDHhZOKwNichEnlCxc+
TwjHnbIDGtupa5OMF4ILAH60/YfRop9uE7Ueekib1zWFhrFrVKcUvjbIXlrXEybp
ojSktmesgKg78Nr7ZiBrMR/wuZAdcnWUWHZwrOKyJlYO3VmRNhbAazahtHzXy1pZ
D7aIWMjdmT0pCAUCAwEAAaNTMFEwHQYDVR0OBBYEFGiyrdtyFdw0USMw47NVcZZx
+jzyMB8GA1UdIwQYMBaAFGiyrdtyFdw0USMw47NVcZZx+jzyMA8GA1UdEwEB/wQF
MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGAagQG+vrBTtaUCEogWBY93+gvTEzB7
kIE3vP/BX8cNwRbnNM4O4AKB85/oh5RWsEc6aiKfF5bIUf8ixXBmGwet/sb7OIAU
VGZY5IyUGCNiQ5Q3elqqROFHz4Vkx6oJkly1vjYJFsZgjm5JIFD1IMqHMdGPdpZb
Bez9Asr6xFuikF2V5X1Q1QD25tshZqHquS7kZ5WLqv1OJf8z+hcyRQKfKbvOgPsd
9Pntn/5ftTN+Kp73Lgyfsf5G4JgBVyPNPOgIwMYkfE0o6QoKtIkxCVBGw1QlNcyC
j4hNPXkc4nde8MWlIOCFSg+ri7qehR10dcMQyb7RQG9Nq3Qq+4KI15g=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions test_cert/test.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDlBnJv8IXb5wiS
MRYEd0Lo/wy5nrrIe5R4eSe+2zF1FkycvwpbUGXULSVWIN+W5lUwzLY1MNXK5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