Skip to content

Commit 8b38de0

Browse files
authored
feat: add Client::certificate_parts() method (#72)
1 parent d0236c3 commit 8b38de0

File tree

12 files changed

+90
-9
lines changed

12 files changed

+90
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "a2"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
authors = [
55
"Harry Bairstow <[email protected]>",
66
"Julius de Bruijn <[email protected]>",
@@ -14,6 +14,7 @@ repository = "https://github.com/walletconnect/a2.git"
1414
homepage = "https://github.com/walletconnect/a2"
1515
documentation = "https://docs.rs/a2"
1616
edition = "2021"
17+
rust-version = "1.60" # set the minimum rust version we can work with.
1718

1819
[features]
1920
default = ["openssl"]

examples/certificate_client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use a2::{Client, DefaultNotificationBuilder, NotificationBuilder, NotificationOptions};
22
use argparse::{ArgumentParser, Store, StoreOption, StoreTrue};
3-
use tokio;
43

54
// An example client connectiong to APNs with a certificate and key
65
#[tokio::main]

examples/token_client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use argparse::{ArgumentParser, Store, StoreOption, StoreTrue};
22
use std::fs::File;
3-
use tokio;
43

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

src/client.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ impl Client {
8080
Ok(Self::new(connector, None, endpoint))
8181
}
8282

83+
/// Create a connection to APNs using the raw PEM-formatted certificate and
84+
/// key, extracted from the provider client certificate you obtain from your
85+
/// [Apple developer account](https://developer.apple.com/account/)
86+
pub fn certificate_parts(cert_pem: &[u8], key_pem: &[u8], endpoint: Endpoint) -> Result<Client, Error> {
87+
let connector = AlpnConnector::with_client_cert(cert_pem, key_pem)?;
88+
89+
Ok(Self::new(connector, None, endpoint))
90+
}
91+
8392
/// Create a connection to APNs using system certificates, signing every
8493
/// request with a signature using a private key, key id and team id
8594
/// provisioned from your [Apple developer
@@ -181,7 +190,7 @@ mod tests {
181190
use hyper::Method;
182191
use hyper_alpn::AlpnConnector;
183192

184-
const PRIVATE_KEY: &'static str = "-----BEGIN PRIVATE KEY-----
193+
const PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----
185194
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8g/n6j9roKvnUkwu
186195
lCEIvbDqlUhA5FOzcakkG90E8L+hRANCAATKS2ZExEybUvchRDuKBftotMwVEus3
187196
jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ
@@ -458,4 +467,17 @@ jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ
458467

459468
assert_eq!(payload.to_json_string().unwrap(), body_str,);
460469
}
470+
471+
#[tokio::test]
472+
/// Try to create a test client using the unencrypted key & cert provided.
473+
/// These are test values that do not work with Apple, but mimic the sort
474+
/// of values you should get from the Apple Developer Console.
475+
async fn test_cert_parts() -> Result<(), Error> {
476+
let key: Vec<u8> = include_str!("../test_cert/test.key").bytes().collect();
477+
let cert: Vec<u8> = include_str!("../test_cert/test.crt").bytes().collect();
478+
479+
let c = Client::certificate_parts(&cert, &key, Endpoint::Sandbox)?;
480+
assert!(c.signer.is_none());
481+
Ok(())
482+
}
461483
}

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///! Error and result module
1+
/// Error and result module
22
use crate::{response::Response, signer::SignerError};
33
use std::io;
44
use thiserror::Error;

src/request/notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///! The `aps` notification content builders
1+
/// The `aps` notification content builders
22
mod default;
33
mod options;
44
mod web;

src/request/payload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///! Payload with `aps` and custom data
1+
/// Payload with `aps` and custom data
22
use crate::error::Error;
33
use crate::request::notification::{DefaultAlert, NotificationOptions, WebPushAlert};
44
use erased_serde::Serialize;

src/signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn get_time() -> i64 {
248248
mod tests {
249249
use super::*;
250250

251-
const PRIVATE_KEY: &'static str = "-----BEGIN PRIVATE KEY-----
251+
const PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----
252252
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8g/n6j9roKvnUkwu
253253
lCEIvbDqlUhA5FOzcakkG90E8L+hRANCAATKS2ZExEybUvchRDuKBftotMwVEus3
254254
jDwmlD1Gg0yJt1e38djFwsxsfr5q2hv0Rj9fTEqAPr8H7mGm0wKxZ7iQ

test_cert/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
This contains a self signed cert for test purposes. The password is
2+
"test".
3+
4+
These values are not encrypted
5+
6+
Key and Cert generation (unencrypted):
7+
8+
```
9+
$ openssl req -newkey rsa:2048 -nodes \
10+
-keyout test.key -x509 -days 3650 -out test.crt
11+
```

0 commit comments

Comments
 (0)