Skip to content

Commit

Permalink
Add "no password" variants for setting credentials with a fully-forme…
Browse files Browse the repository at this point in the history
…d certificate and private key
  • Loading branch information
jchambers committed Jul 6, 2024
1 parent c9f92fb commit a17bf4f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ public ApnsClientBuilder setClientCredentials(final InputStream p12InputStream,
return this.setClientCredentials(x509Certificate, privateKey, p12Password);
}

/**
* <p>Sets the TLS credentials for the client under construction. Clients constructed with TLS credentials will use
* TLS-based authentication when sending push notifications. This method assumes that the given private key does
* not require a password.</p>
*
* <p>Clients may not have both TLS credentials and a signing key.</p>
*
* @param clientCertificate the certificate to be used to identify the client to the APNs server
* @param privateKey the private key for the client certificate
*
* @return a reference to this builder
*
* @since 0.16
*/
public ApnsClientBuilder setClientCredentials(final X509Certificate clientCertificate, final PrivateKey privateKey) {
return this.setClientCredentials(clientCertificate, privateKey, null);
}

/**
* <p>Sets the TLS credentials for the client under construction. Clients constructed with TLS credentials will use
* TLS-based authentication when sending push notifications.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ public BaseHttp2ServerBuilder<T> setServerCredentials(final InputStream certific
return this;
}

/**
* <p>Sets the credentials for the server under construction. This method assumes that the given private key does
* not require a password.</p>
*
* @param certificates a certificate chain including the server's own certificate
* @param privateKey the private key for the server's certificate
*
* @return a reference to this builder
*
* @since 0.16
*/
public BaseHttp2ServerBuilder<T> setServerCredentials(final X509Certificate[] certificates, final PrivateKey privateKey) {
return this.setServerCredentials(certificates, privateKey, null);
}

/**
* <p>Sets the credentials for the server under construction.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public BenchmarkApnsServerBuilder setServerCredentials(final InputStream certifi
return this;
}

@Override
public BenchmarkApnsServerBuilder setServerCredentials(final X509Certificate[] certificates, final PrivateKey privateKey) {
super.setServerCredentials(certificates, privateKey);
return this;
}

@Override
public BenchmarkApnsServerBuilder setServerCredentials(final X509Certificate[] certificates, final PrivateKey privateKey, final String privateKeyPassword) {
super.setServerCredentials(certificates, privateKey, privateKeyPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public MockApnsServerBuilder setServerCredentials(final InputStream certificateP
return this;
}

@Override
public MockApnsServerBuilder setServerCredentials(final X509Certificate[] certificates, final PrivateKey privateKey) {
super.setServerCredentials(certificates, privateKey);
return this;
}

@Override
public MockApnsServerBuilder setServerCredentials(final X509Certificate[] certificates, final PrivateKey privateKey, final String privateKeyPassword) {
super.setServerCredentials(certificates, privateKey, privateKeyPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ void testBuildClientWithCertificateAndPasswordProtectedKey() throws Exception {
}
}

@Test
void testBuildClientWithCertificateAndUnprotectedKeyNoPassword() throws Exception {
// We DO need a password to unlock the keystore, but the key itself should be unprotected
try (final InputStream p12InputStream = this.getClass().getResourceAsStream(SINGLE_TOPIC_CLIENT_KEYSTORE_UNPROTECTED_FILENAME)) {

final PrivateKeyEntry privateKeyEntry =
P12Util.getFirstPrivateKeyEntryFromP12InputStream(p12InputStream, KEYSTORE_PASSWORD);

final ApnsClient client = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.PRODUCTION_APNS_HOST)
.setEventLoopGroup(EVENT_LOOP_GROUP)
.setClientCredentials((X509Certificate) privateKeyEntry.getCertificate(), privateKeyEntry.getPrivateKey())
.build();

client.close().get();
}
}

@Test
void testBuildClientWithCertificateAndUnprotectedKey() throws Exception {
// We DO need a password to unlock the keystore, but the key itself should be unprotected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void testSetServerCredentialsX509CertificateArrayPrivateKeyString() throws Excep

// We're happy here as long as nothing explodes
new MockApnsServerBuilder()
.setServerCredentials(new X509Certificate[] { (X509Certificate) privateKeyEntry.getCertificate() }, privateKeyEntry.getPrivateKey(), null)
.setServerCredentials(new X509Certificate[] { (X509Certificate) privateKeyEntry.getCertificate() }, privateKeyEntry.getPrivateKey())
.setHandlerFactory(new AcceptAllPushNotificationHandlerFactory())
.build();
}
Expand Down

0 comments on commit a17bf4f

Please sign in to comment.